-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPlayerPanel.gd
250 lines (188 loc) · 6.81 KB
/
PlayerPanel.gd
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
extends Control
class_name PlayerPanel
@onready var username: LineEdit = %Username
@onready var password: LineEdit = %Password
@onready var colorPicker: ColorPickerButton = %ColorPicker
@onready var guestLabel: Label = %GuestLabel
@onready var guestTickBox: CheckBox = %GuestTickBox
@onready var loginButton: Button = %LoginButton
@onready var asGuestButton: Button = %AsGuestButton
@onready var logoutButton: Button = %LogoutButton
@onready var randomColorButton: Button = %RandomColorButton
@onready var randomUsernameButton: Button = %RandomUsernameButton
@onready var profilePicture: TextureRect = %ProfilePicture
var isMainPlayer: bool = false
var defaultPicture: Texture
var sessionToken: String = ""
var userId: String = ""
func _ready():
defaultPicture = profilePicture.texture
connectSignals()
func connectSignals():
username.text_changed.connect(onUsername_textChanged)
password.text_changed.connect(onPassword_textChanged)
username.text_submitted.connect(onLoginButton_pressed)
password.text_submitted.connect(onLoginButton_pressed)
colorPicker.color_changed.connect(onColorPicker_colorChanged)
guestTickBox.toggled.connect(onGuestTickBox_toggled)
loginButton.pressed.connect(onLoginButton_pressed)
asGuestButton.pressed.connect(asGuestButton_pressed)
logoutButton.pressed.connect(onLogoutButton_pressed)
randomColorButton.pressed.connect(onRandomColorButton_pressed)
randomUsernameButton.pressed.connect(onRandomUsernameButton_pressed)
func onUsername_textChanged(_newText: String):
pass
func onPassword_textChanged(_newText: String):
pass
func onColorPicker_colorChanged(_newColor: Color):
pass
func onGuestTickBox_toggled(pressed: bool):
password.visible = !pressed
loginButton.visible = !pressed
asGuestButton.visible = pressed
func onLoginButton_pressed(_sink = null):
if VersionCheck.versionCheckComplete == false:
await VersionCheck.fetchedNewestVersion
if VersionCheck.offline:
AlertManager.showAlert(self, "Offline mode", "You are in offline mode. Please update the game to play online.")
return
setButtonsLoggingIn()
print("Logging in...")
var loginData = getLoginData()
var loginRequest = HTTPRequest.new()
Network.add_child(loginRequest)
loginRequest.timeout = 10
loginRequest.request_completed.connect(onLoginRequestCompleted)
var httpError = loginRequest.request(
Backend.BACKEND_IP_ADRESS + "/api/auth/login",
["Content-Type: application/json"],
HTTPClient.METHOD_POST,
JSON.stringify(loginData)
)
if httpError != OK:
print("Error: " + error_string(httpError))
setButtonsLoggedOut()
func asGuestButton_pressed():
if VersionCheck.offline:
AlertManager.showAlert(self, "Offline mode", "You are in offline mode. Please update the game to play online.")
return
setButtonsLoggingIn()
var loginData = getLoginData()
var loginRequest = HTTPRequest.new()
Network.add_child(loginRequest)
loginRequest.timeout = 10
loginRequest.request_completed.connect(onLoginRequestCompleted)
var httpError = loginRequest.request(
Backend.BACKEND_IP_ADRESS + "/api/auth/guest",
["Content-Type: application/json"],
HTTPClient.METHOD_POST,
JSON.stringify(loginData)
)
if httpError != OK:
print("Error: " + error_string(httpError))
func onLogoutButton_pressed():
setButtonsLoggedOut()
setRandomPlayerData()
pass
func onLoginRequestCompleted(_result: int, responseCode: int, _headers: PackedStringArray, body: PackedByteArray):
if responseCode != 200:
setButtonsLoggedOut()
print("Error after login request: ", body.get_string_from_utf8())
if GlobalProperties.playerSelectorNode.visible:
AlertManager.showAlert(self, "Error logging in", body.get_string_from_utf8())
print("Result: ", responseCode)
return
setButtonsLoggedIn()
var json = JSON.parse_string(body.get_string_from_utf8())
password.text = ""
sessionToken = json.sessionToken
userId = json.userId
if isMainPlayer:
GlobalProperties.USER_ID = userId
GlobalProperties.SESSION_TOKEN = sessionToken
var profilePictureUrl = json.profilePictureUrl
# loadProfilePicture(profilePictureUrl)
TextureLoader.loadOnlineTexture(profilePictureUrl, func(image):
profilePicture.texture = image
)
func onRandomColorButton_pressed():
colorPicker.color = Color(randf(), randf(), randf(), 1.0)
# colorPicker.color_changed.emit(colorPicker.color)
if isMainPlayer:
GlobalProperties.PLAYER_COLOR = colorPicker.color
func onRandomUsernameButton_pressed():
if !username.editable:
return
username.text = UsernameGenerator.getUsername()
if isMainPlayer:
GlobalProperties.PLAYER_NAME = username.text
func setMainPlayerData():
# while !is_node_ready():
# await get_tree().create_timer(1.0).timeout
isMainPlayer = true
username.text = GlobalProperties.PLAYER_NAME
username.text_changed.connect(onTextChanged)
password.text = GlobalProperties.PLAYER_PASSWORD
password.text_changed.connect(onPasswordTextChanged)
colorPicker.color = GlobalProperties.PLAYER_COLOR
colorPicker.color_changed.connect(onColorChanged)
onLoginButton_pressed()
func setRandomPlayerData():
username.text = UsernameGenerator.getUsername()
colorPicker.color = Color(randf(), randf(), randf(), 1.0)
sessionToken = ""
userId = ""
profilePicture.texture = defaultPicture
if isMainPlayer:
GlobalProperties.PLAYER_NAME = username.text
GlobalProperties.PLAYER_COLOR = colorPicker.color
GlobalProperties.PLAYER_PASSWORD = password.text
GlobalProperties.USER_ID = ""
GlobalProperties.SESSION_TOKEN = ""
func onTextChanged(newText: String) -> void:
if newText != "":
GlobalProperties.PLAYER_NAME = newText
func onPasswordTextChanged(newText: String) -> void:
if newText != "":
GlobalProperties.PLAYER_PASSWORD = newText
func onColorChanged(new_color):
GlobalProperties.PLAYER_COLOR = new_color
func getPlayerData() -> PlayerData:
return PlayerData.new(userId, username.text, colorPicker.color, sessionToken)
func getLoginData() -> Dictionary:
return {
"username": username.text,
"password": password.text,
}
func setButtonsLoggingIn():
loginButton.disabled = true
loginButton.text = "Loading"
asGuestButton.disabled = true
asGuestButton.text = "Loading"
username.editable = false
password.editable = false
randomUsernameButton.visible = false
# guestLabel.visible = false
# guestTickBox.visible = false
func setButtonsLoggedIn():
loginButton.visible = false
asGuestButton.visible = false
logoutButton.visible = true
username.editable = false
password.editable = false
password.visible = false
randomUsernameButton.visible = false
func setButtonsLoggedOut():
loginButton.disabled = false
loginButton.visible = true
loginButton.text = "Log In"
asGuestButton.disabled = false
asGuestButton.visible = true
asGuestButton.text = "As Guest"
logoutButton.visible = false
# guestLabel.visible = true
# guestTickBox.visible = true
username.editable = true
password.editable = true
password.visible = true
randomUsernameButton.visible = true