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.

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