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.

140 lines
3.9 KiB

  1. extends Control
  2. var ServerCreateGameMenu = preload("res://scenes/menus/ServerCreateGameMenu.tscn")
  3. var LobbyMenu = preload("res://scenes/menus/LobbyMenu.tscn")
  4. var DirectHostMenu = preload("res://scenes/menus/DirectHostMenu.tscn")
  5. var DirectJoinMenu = preload("res://scenes/menus/DirectJoinMenu.tscn")
  6. onready var games_list = get_node("menu/games")
  7. var games = []
  8. var selected_game = {}
  9. func _ready():
  10. get_node("menu/back").connect("pressed",self,"_on_back_pressed")
  11. client.connect("connection_succeeded", self, "_connection_succeeded")
  12. client.connect("connection_failed", self, "_connection_failed")
  13. client.connect("server_disconnected", self, "_server_disconnected")
  14. func _draw():
  15. find_node("refresh").grab_focus()
  16. func _on_multiplayer_menu_draw():
  17. get_games(find_node("open").is_pressed())
  18. get_node("refresh_timer").start()
  19. func _on_multiplayer_menu_hide():
  20. get_node("refresh_timer").stop()
  21. func _on_back_pressed():
  22. queue_free()
  23. get_tree().get_root().get_node("main_menu").show()
  24. func _on_direct_host_pressed():
  25. var direct_host_menu = DirectHostMenu.instance()
  26. get_tree().get_root().add_child(direct_host_menu)
  27. get_tree().get_root().get_node("main_menu").hide()
  28. func _on_direct_join_pressed():
  29. var direct_join_menu = DirectJoinMenu.instance()
  30. get_tree().get_root().add_child(direct_join_menu)
  31. get_tree().get_root().get_node("main_menu").hide()
  32. func _on_open_toggled(button_pressed):
  33. get_games(button_pressed)
  34. func _on_refresh_pressed():
  35. if get_node("refresh_timer").is_stopped():
  36. get_node("refresh_timer").start()
  37. get_games(find_node("open").is_pressed())
  38. func _on_refresh_timer_timeout():
  39. get_games(find_node("open").is_pressed())
  40. func _server_disconnected():
  41. show_error_dialog(tr("SERVER_DISCONNECTED"))
  42. func show_error_dialog(text:String):
  43. var dialog = get_node("error_dialog")
  44. dialog.set_text(text)
  45. dialog.popup_centered()
  46. func get_games(only_open:bool):
  47. var req = game_server.http()
  48. req.connect_http(self,"_on_get_games")
  49. if only_open:
  50. req.get_request('client/games?open')
  51. else:
  52. req.get_request('client/games')
  53. func _on_get_games(result, response_code, headers, body):
  54. if result == OK && response_code == HTTPClient.RESPONSE_OK:
  55. find_node("create").set_disabled(false)
  56. find_node("open").set_disabled(false)
  57. var json = JSON.parse(body.get_string_from_utf8())
  58. games_list.clear()
  59. games = json.result
  60. for game in games:
  61. var name = game.name
  62. name += " ("
  63. name += str(game.player_count)
  64. name += "/4)"
  65. if game.bots:
  66. name += " [with Bots]"
  67. games_list.add_item(name)
  68. if game.running:
  69. games_list.set_item_icon(games_list.get_item_count()-1, games_list.get_node("running_icon").get_button_icon())
  70. elif game.has('private') && game.private:
  71. games_list.set_item_icon(games_list.get_item_count()-1, games_list.get_node("private_icon").get_button_icon())
  72. else:
  73. games_list.set_item_icon(games_list.get_item_count()-1, games_list.get_node("open_icon").get_button_icon())
  74. else:
  75. find_node("create").set_disabled(true)
  76. find_node("join").set_disabled(true)
  77. find_node("open").set_disabled(true)
  78. show_error_dialog(tr("SERVER_NO_CONNECTION"))
  79. get_node("refresh_timer").stop()
  80. func _on_create_pressed():
  81. var server_create_game_menu = ServerCreateGameMenu.instance()
  82. get_tree().get_root().add_child(server_create_game_menu)
  83. get_tree().get_root().get_node("main_menu").hide()
  84. func _on_join_pressed():
  85. client.join_game(game_server.get_server_addr(), int(selected_game['port']))
  86. func _connection_succeeded():
  87. var lobby_menu = LobbyMenu.instance()
  88. if selected_game && selected_game.has('name'):
  89. gamestate.game_name = selected_game.name
  90. get_tree().get_root().add_child(lobby_menu)
  91. get_node("refresh_timer").stop()
  92. hide()
  93. func _connection_failed():
  94. show_error_dialog(tr("CONNECTION_FAILED"))
  95. func _on_games_item_selected(index):
  96. selected_game = games[index]
  97. find_node("join").set_disabled(false)
  98. func _on_games_nothing_selected():
  99. find_node("join").set_disabled(true)