A team game with an emphasis on movement (with no shooting), inspired by Overwatch and Zineth
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.2 KiB

  1. extends Node
  2. var SERVER_TO_SERVER_PORT = 54671
  3. var MATCHMAKING_PORT = 54672
  4. var GAME_SIZE = 6
  5. # Number of games we can make without blowing up the computer
  6. var MAX_GAMES = 50 # Based on how many ports I decided to forward
  7. var next_port = 54673
  8. # Filled with queue info which contains
  9. # { "netid" }
  10. var skirmishing_players = []
  11. var skirmish
  12. # To avoid the confusion of the gameSERVERS being CLIENTS,
  13. # we just call them games whenever possible
  14. # var games = []
  15. var game_connections = []
  16. var game_streams = []
  17. # Matchmaker to game servers
  18. var matchmaker_to_games
  19. enum messages {
  20. ready_to_connect,
  21. }
  22. onready var lobby = get_node("..")
  23. func _ready():
  24. # By default, having this node doesn't do naything
  25. # You must call start_matchmaker to enable it
  26. # If not called, don't call _process (= don't matchmake)
  27. set_process(false)
  28. func start_matchmaker():
  29. # Actually run the matchmaker
  30. set_process(true)
  31. # Setup skirmish server
  32. skirmish = spawn_server(true)
  33. # Set up communication between GAMESERVERS
  34. # This is necessary for eg, when a player leaves to backfill
  35. matchmaker_to_games = TCP_Server.new()
  36. if matchmaker_to_games.listen(SERVER_TO_SERVER_PORT):
  37. print("Error, could not listen")
  38. # Use ENet for matchmaker because we can (makes client code cleaner)
  39. var matchmaker_to_players = NetworkedMultiplayerENet.new()
  40. print("Starting matchmaker on port " + str(MATCHMAKING_PORT))
  41. matchmaker_to_players.create_server(MATCHMAKING_PORT, MAX_GAMES)
  42. get_tree().set_network_peer(matchmaker_to_players)
  43. matchmaker_to_players.connect("peer_connected", self, "queue")
  44. func _process(delta):
  45. # Manage connection to GAMESERVERS (not clients)
  46. if matchmaker_to_games.is_connection_available(): # check if a gameserver's trying to connect
  47. var game = matchmaker_to_games.take_connection() # accept connection
  48. game_connections.append(game) # store the connection
  49. var stream = PacketPeerStream.new()
  50. stream.set_stream_peer(game) # bind peerstream to new client
  51. game_streams.append(stream) # make new data transfer object for game
  52. for stream in game_streams:
  53. if stream.get_available_packet_count():
  54. var message = stream.get_var()
  55. if message == messages.ready_to_connect:
  56. var port = stream.get_var()
  57. print("Server " + str(port) + " has requested connection")
  58. skirmish_to_game(port, GAME_SIZE)
  59. func queue(netid):
  60. print("Player " + str(netid) + " connected.")
  61. add_to_game(netid, skirmish)
  62. skirmishing_players.append(netid)
  63. check_queue()
  64. func add_to_game(netid, port):
  65. networking.rpc_id(netid, "reconnect", networking.global_server_ip, port)
  66. func skirmish_to_game(port, count=1):
  67. for i in range(count):
  68. if not skirmishing_players.size():
  69. return false
  70. print(skirmishing_players[0])
  71. print("to")
  72. print(port)
  73. add_to_game(skirmishing_players[0], port)
  74. return true
  75. func check_queue():
  76. # Prefer making a full game to backfilling
  77. if skirmishing_players.size() >= GAME_SIZE:
  78. spawn_server()
  79. # games.append(port)
  80. func spawn_server(skirmish=false):
  81. var args = ['-port='+str(next_port)]
  82. if skirmish:
  83. # Begin skirmish immediately, so players "join" instead of "ready"
  84. args.append("-start-game")
  85. OS.execute("util/server.sh", args, false)
  86. next_port += 1
  87. return (next_port - 1) # Return original port