summaryrefslogtreecommitdiff
path: root/Network/Lobby.gd
blob: 67a1f348425607bf25b2fc98f6be0938d9696751 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
extends Control


func _ready():
	Network.connect("connection_succeeded", Callable(self, "_on_connection_succeeded"))
	Network.connect("connection_failed", Callable(self, "_on_connection_failed"))
	Network.connect("player_list_changed", Callable(self, "refresh_lobby"))
	Network.connect("game_error", Callable(self, "_on_game_error"))
	Network.connect("game_ended", Callable(self, "_on_game_ended"))
	$HTTPRequest.connect("request_completed", Callable(self, '_set_remote_ip'))
	
	if OS.has_environment("USERNAME"):
		$Connect/Name.text = OS.get_environment("USERNAME")
	elif OS.has_environment("USER"):
		$Connect/Name.text = OS.get_environment("USER")
	else:
		var desktop_path = OS.get_system_dir(0).replace("\\", "/").split("/")
		$Connect/Name.text = desktop_path[desktop_path.size() - 2]
	
	# show lobby if we're already connected somewhere
	if Network.peer != null:
		self._on_connection_succeeded()
		self.set_ip()
		self.refresh_lobby()


func disable_connect_buttons(is_disabled = true):
	$Connect/Host.disabled = is_disabled
	$Connect/Join.disabled = is_disabled


func refresh_lobby():
	var players = Network.players.values()
	var player = Network.player
	
	$Players/List.clear()
	$Players/List.add_item(player.name + " (You)")
	for p in players:
		$Players/List.add_item(p.name)
	
	$Players/Start.disabled = not multiplayer.is_server()


func set_ip():
	self.set_local_ip()
	self.set_remote_ip()

func set_local_ip():
	for ip in IP.get_local_addresses():
		if ip.begins_with("192.168"):
			$Players/LocalIP.text = ip
			break

func set_remote_ip():
	$Players/RemoteIP.text = ""
	$HTTPRequest.request("https://ipv4.icanhazip.com/")

func _set_remote_ip(_result, response_code, _headers, body):
	if response_code == 200:
		$Players/RemoteIP.text = body.get_string_from_utf8()
	else:
		$Players/RemoteIP.text = "Remote IP request failed!"
		self._on_game_error("Remote IP request failed!")


func _on_connection_succeeded():
	$Connect.hide()
	$Players.show()


func _on_connection_failed():
	self.disable_connect_buttons()
	$Connect/Error.set_text("Connection failed!")


func _on_game_error(error):
	$ErrorDialog.dialog_text = error
	$ErrorDialog.popup_centered_clamped()
	self.disable_connect_buttons()


func _on_game_ended():
	self.show()
	$Connect.show()
	$Players.hide()
	self.disable_connect_buttons()


func _on_Host_pressed():
	$Connect.hide()
	$Players.show()
	$Connect/Error.text = ""
	
	self.set_ip()
	
	var player_name = $Connect/Name.text
	Network.host_game(player_name)
	self.refresh_lobby()


func _on_Join_pressed():
	var ip = $Connect/IP.text
	if not ip.is_valid_ip_address():
		$Connect/Error.text = "Invalid IP address!"
		return
	
	$Connect/Error.text = ""
	self.disable_connect_buttons(false)
	
	self.set_ip()
	var player_name = $Connect/Name.text
	Network.join_game(ip, player_name)


func _on_Start_pressed():
	Global.start_game()


func _on_Back_pressed():
	$Connect.show()
	$Players.hide()
	Network.leave_game()