summaryrefslogtreecommitdiff
path: root/UI/Start.gd
blob: b078d655698339ec586b0e7f4369906fc907345b (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
extends Control


func _ready():
	multiplayer.peer_connected.connect(_on_peer_connected)
	multiplayer.peer_disconnected.connect(_on_peer_disconnected)
	
	if OS.has_environment("USER"):
		%Username.text = OS.get_environment("USER")
	elif OS.has_environment("USERNAME"):
		%Username.text = OS.get_environment("USERNAME")
	
	%GameName.text = "%s's Game" % [%Username.text]


func get_ip() -> String:
	var ip := "127.0.0.1"
	if %IP.text:
		ip = %IP.text
	
	return ip


func get_port() -> int:
	var port := 1234
	if %Port.text:
		port = int(%Port.text)
	
	return port


func _on_peer_connected(id):
	Network._on_peer_connected(id)
func _on_peer_disconnected(id):
	Network._on_peer_disconnected(id)


func _on_host_pressed() -> void:
	Client.player.username = %Username.text
	
	var response = await Client.request(
		"%s" % [Client.game_lobby_url],
		[],
		HTTPClient.METHOD_POST,
		JSON.stringify({"host": true, "port": get_port(), "name" : %GameName.text})
	)
	
	if response.result_code == HTTPRequest.RESULT_SUCCESS:
		var result = response.parse_json()
		if result["success"]:
			Client.current_game_id = result["data"]
	
	Network.host_game(get_port())
	get_tree().change_scene_to_file("res://UI/PlayerLobby/Lobby.tscn")


func _on_join_pressed() -> void:
	Client.player.username = %Username.text
	Network.join_game(get_ip(), get_port())
	get_tree().change_scene_to_file("res://UI/PlayerLobby/Lobby.tscn")


func _on_search_pressed() -> void:
	Client.player.username = %Username.text
	get_tree().change_scene_to_file("res://UI/GameLobby/game_lobby.tscn")