-
Notifications
You must be signed in to change notification settings - Fork 0
/
Global.gd
107 lines (88 loc) · 2.37 KB
/
Global.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
extends Node
@export var equippedPackages: int = 0
@export var canInteract: bool = 0 : set = setCanInteract
@export var score: int = 0
@export var username: String = ""
@export var authToken: String = ""
@export var userId: int = -1
func _ready():
pass # Replace with function body.
func equipPackage():
if equippedPackages < 5:
equippedPackages += 1
return true
return false
func dropPackage():
if equippedPackages > 0:
equippedPackages -= 1
func setCanInteract(v):
canInteract = v
func gotPacket():
score += 1000
func set_user(username, password):
if username == "":
username = "Anonymous"
if password == null:
self.username = username
if self.authToken != "":
_logout()
return {"success": true}
else:
if self.username == username and self.authToken != "":
return {"success": true}
if self.authToken != "":
_logout()
return await _login(username, password)
func _logout():
# TODO: invalidate token on server
self.authToken = ""
self.userId = -1
func _login(username, password):
self.username = username
var res = await _make_post_request("/login", {
"username": username,
"password": password
});
_process_auth(res)
return res
func register(username, password):
self.username = username
var res = await _make_post_request("/register", {
"username": username,
"password": password
});
_process_auth(res)
return res
func _make_post_request(endpoint, data_to_send):
# Convert data to json string:
var http = HTTPRequest.new()
add_child(http)
var query = JSON.stringify(data_to_send)
var headers = ["Content-Type: application/json"]
http.request("https://scorejam2022.webfreak.org" + endpoint, headers, true, HTTPClient.METHOD_POST, query)
var res = await http.request_completed;
var result: int = res[0]
var response_code: int = res[1]
var retHeaders: PackedStringArray = res[2]
var body: Variant = res[3].get_string_from_utf8()
if body != "" and body[0] == '{':
body = JSON.parse_string(body)
else:
body = {"raw": body}
return {
"success": response_code == 200,
"error": null \
if response_code < 400 else \
body["statusMessage"],
"data": body
}
func _process_auth(res):
if res["success"]:
self.authToken = res["data"]["token"]
self.userId = res["data"]["id"]
else:
self.authToken = ""
self.userId = -1
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass