Public repository for MUR pre alpha
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.

169 lines
5.4 KiB

  1. extends Node
  2. const MAX_PEERS = 4
  3. const LOBBY_READY_WAIT_TIME = 3
  4. const EMPTY_WAIT_TIME = 30
  5. const EMPTY_CLOSING_TIME = 60
  6. var port:int = -1
  7. var dedicated_server:bool = false
  8. var server_secret:String = ""
  9. var server_id:String = "[GAME:-1] "
  10. var timer:Timer
  11. signal server_error(type, data)
  12. func _ready():
  13. for argument in OS.get_cmdline_args():
  14. if argument.split("=")[0] == "--port":
  15. dedicated_server = true
  16. port = int(argument.split("=")[1])
  17. if argument.split("=")[0] == "--server-id":
  18. server_id = "[GAME:" + str(argument.split("=")[1]) + "] "
  19. if argument.split("=")[0] == "--secret":
  20. server_secret=argument.split("=")[1]
  21. if argument.split("=")[0] == "--bots":
  22. gamestate.set_bots(bool(int(argument.split("=")[1])))
  23. if argument.split("=")[0] == "--server-addr":
  24. game_server.set_server_addr(str(argument.split("=")[1]))
  25. if argument.split("=")[0] == "--api-addr":
  26. game_server.set_api_addr(str(argument.split("=")[1]))
  27. if dedicated_server:
  28. var err = host_game(port)
  29. if err == OK:
  30. print(server_id + "New game hosted: port=" + str(port) + " secret=" + str(server_secret) + " bots=" + str(gamestate.bots) + " server-addr=" + game_server.get_server_addr() + " api-addr=" + game_server.get_api_addr())
  31. print(server_id + "Waiting for connection, closing server in " + str(EMPTY_CLOSING_TIME) + " seconds")
  32. timer.set_wait_time(EMPTY_CLOSING_TIME)
  33. timer.connect("timeout",self,"quit_server",[true])
  34. timer.start()
  35. else:
  36. push_error(server_id + "Could not create Server! (port=" + str(port) + " secret=" + str(server_secret) + " server-addr=" + game_server.get_server_addr() + " api-addr=" + game_server.get_api_addr() + ")")
  37. quit_server()
  38. func host_game(port:int, set_bots:bool = false):
  39. var host = NetworkedMultiplayerENet.new()
  40. var err = host.create_server(port, MAX_PEERS)
  41. get_tree().set_network_peer(host)
  42. get_tree().connect("network_peer_connected", self, "_client_connected")
  43. get_tree().connect("network_peer_disconnected", self,"_client_disconnected")
  44. gamestate.connect("players_changed",self,"_players_changed")
  45. gamestate.connect("game_ready",self,"_game_ready")
  46. gamestate.connect("game_ended",self,"_game_ended")
  47. if set_bots:
  48. gamestate.bots = true
  49. if timer == null:
  50. timer = Timer.new()
  51. timer.set_name("timer")
  52. timer.set_timer_process_mode(Timer.TIMER_PROCESS_PHYSICS)
  53. timer.set_one_shot(true)
  54. timer.set_autostart(false)
  55. add_child(timer)
  56. if err != OK:
  57. emit_signal("server_error", "HOST", err)
  58. return err
  59. func local_game(name:String, color:Color, bots:bool):
  60. var localhost = NetworkedMultiplayerENet.new()
  61. localhost.create_server(0, 0)
  62. get_tree().set_network_peer(localhost)
  63. gamestate.bots = bots
  64. gamestate.set_player(get_tree().get_network_unique_id(), name, color)
  65. gamestate.prepare_game()
  66. gamestate.post_start_game()
  67. func update_server_data():
  68. var req = game_server.http()
  69. var player_count = 0
  70. if gamestate.players != null:
  71. player_count = gamestate.players.size()
  72. req.put_request("game",to_json({'secret' : server_secret, 'player_count' : player_count, 'running' : gamestate.game_running}))
  73. func _client_connected(id):
  74. if get_tree().is_network_server():
  75. if dedicated_server:
  76. print(server_id + "peer connected: " + str(id))
  77. gamestate.rpc_id(id, "set_bots" ,gamestate.bots)
  78. if not timer.is_stopped():
  79. timer.disconnect("timeout",self,"quit_server")
  80. timer.stop()
  81. func _client_disconnected(id):
  82. if get_tree().is_network_server():
  83. if dedicated_server:
  84. print(server_id + "peer disconnected: " + str(id))
  85. if gamestate.game_running:
  86. emit_signal("server_error", "DISCONNECTED", [id])
  87. gamestate.rpc("remove_player" ,id)
  88. else:
  89. gamestate.rpc("remove_player" ,id)
  90. func _players_changed(players):
  91. if get_tree().is_network_server():
  92. if dedicated_server:
  93. update_server_data()
  94. if players.empty():
  95. if gamestate.game_running:
  96. print(server_id + "All players disconnected, close server.")
  97. quit_server()
  98. else:
  99. print(server_id + "All players disconnected, wait for closing...")
  100. timer.set_wait_time(EMPTY_WAIT_TIME)
  101. timer.connect("timeout",self,"quit_server",[true])
  102. timer.start()
  103. func quit_server(triggered:bool = false):
  104. if dedicated_server && triggered:
  105. print(server_id + "Closing time over...quit")
  106. var req = game_server.http()
  107. req.connect_http(self,"_on_delete_server")
  108. req.delete_request("game",to_json({'secret' : server_secret}))
  109. func _on_delete_server(result, response_code, headers, body):
  110. if result != OK || response_code != HTTPClient.RESPONSE_OK:
  111. print(server_id + "ERROR: _on_delete_server, server might be in undefined state!")
  112. get_tree().quit()
  113. func _start_game():
  114. if get_tree().is_network_server():
  115. get_tree().set_refuse_new_network_connections(true)
  116. gamestate.rpc("prepare_game")
  117. func _game_ready(all_ready):
  118. if get_tree().is_network_server():
  119. if all_ready:
  120. timer.set_wait_time(LOBBY_READY_WAIT_TIME)
  121. timer.connect("timeout",self,"_start_game")
  122. timer.start()
  123. else:
  124. timer.disconnect("timeout",self,"_start_game")
  125. timer.stop()
  126. func _game_ended():
  127. get_tree().disconnect("network_peer_connected", self, "_client_connected")
  128. get_tree().disconnect("network_peer_disconnected", self,"_client_disconnected")
  129. gamestate.disconnect("players_changed",self,"_players_changed")
  130. gamestate.disconnect("game_ready",self,"_game_ready")
  131. gamestate.disconnect("game_ended",self,"_game_ended")
  132. get_tree().set_refuse_new_network_connections(false)
  133. get_tree().set_network_peer(null)