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.

84 lines
2.6 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 # Totally random guess
  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. onready var lobby = get_node("..")
  20. func _ready():
  21. # By default, having this node doesn't do naything
  22. # You must call run_matchmaker to enable it
  23. # If not called, don't call _process (= don't matchmake)
  24. set_process(false)
  25. func run_matchmaker():
  26. # Actually run the matchmaker
  27. set_process(true)
  28. # Setup skirmish server
  29. skirmish = spawn_server()
  30. # Set up communication between GAMESERVERS
  31. # This is necessary for eg, when a player leaves to backfill
  32. matchmaker_to_games = TCP_Server.new()
  33. if matchmaker_to_games.listen(SERVER_TO_SERVER_PORT):
  34. print("Error, could not listen")
  35. # Use ENet for matchmaker because we can (makes client code cleaner)
  36. var matchmaker_to_players = NetworkedMultiplayerENet.new()
  37. print("Starting matchmaker on port " + str(MATCHMAKING_PORT))
  38. matchmaker_to_players.create_server(MATCHMAKING_PORT, MAX_GAMES)
  39. get_tree().set_network_peer(matchmaker_to_players)
  40. matchmaker_to_players.connect("peer_connected", self, "queue")
  41. func _process(delta):
  42. # Manage connection to GAMESERVERS (not clients)
  43. if matchmaker_to_games.is_connection_available(): # check if a gameserver's trying to connect
  44. var game = matchmaker_to_games.take_connection() # accept connection
  45. game_connections.append(game) # store the connection
  46. var stream = PacketPeerStream.new()
  47. stream.set_stream_peer(game) # bind peerstream to new client
  48. game_streams.append(stream) # make new data transfer object for game
  49. print("Server has requested connection")
  50. func queue(netid):
  51. print("Player " + str(netid) + " connected.")
  52. $"..".rpc_id(netid, "_client_init", skirmish)
  53. skirmishing_players.append(netid)
  54. check_queue()
  55. # # This is only for clients, but it's in here so we can rpc it easily
  56. # slave func join_game(port):
  57. # #
  58. func check_queue():
  59. if skirmishing_players.size() >= GAME_SIZE:
  60. var port = spawn_server()
  61. games.push(port)
  62. for i in range(GAME_SIZE):
  63. var p = skirmishing_players[i]
  64. rpc_id(p.netid, "join_game", port)
  65. func spawn_server():
  66. OS.execute("util/server.sh", ['-port='+str(next_port)], false)
  67. next_port += 1
  68. return (next_port - 1) # Return original port