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.

156 lines
4.7 KiB

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