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.1 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. 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 run_matchmaker to enable it
  26. # If not called, don't call _process (= don't matchmake)
  27. set_process(false)
  28. func run_matchmaker():
  29. # Actually run the matchmaker
  30. set_process(true)
  31. # Setup skirmish server
  32. skirmish = spawn_server()
  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. # # This is only for clients, but it's in here so we can rpc it easily
  65. # slave func join_game(port):
  66. # #
  67. func add_to_game(netid, port):
  68. lobby.rpc_id(netid, "_client_init", port)
  69. func skirmish_to_game(port, count=1):
  70. for i in range(count):
  71. if not skirmishing_players.size():
  72. return false
  73. print(skirmishing_players[0])
  74. print("to")
  75. print(port)
  76. add_to_game(skirmishing_players[0], port)
  77. return true
  78. func check_queue():
  79. # Prefer making a full game to backfilling
  80. if skirmishing_players.size() >= GAME_SIZE:
  81. spawn_server()
  82. # games.append(port)
  83. func spawn_server():
  84. OS.execute("util/server.sh", ['-port='+str(next_port)], false)
  85. next_port += 1
  86. return (next_port - 1) # Return original port