-
Notifications
You must be signed in to change notification settings - Fork 0
/
Global.gd
247 lines (230 loc) · 9.52 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
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
extends Node
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
var checkpoints_left = 0
var total_checkpoints = 0
var player = {"finishing": false, "physics": false, "timer": 0}
var num_tracks = 5
var track_has_finish = true
var sec_playing = "Beginner"
var track_playing = -1
var tracks = {"Beginner": [], "Intermediate": [], "Accomplished": [], "Advanced": [], "Professional": []}
var official_times = {"Beginner": [], "Intermediate": [], "Accomplished": [], "Advanced": [], "Professional": []}
var world_times = {"Beginner": [], "Intermediate": [], "Accomplished": [], "Advanced": [], "Professional": []}
var pb_times = {"Beginner": [], "Intermediate": [], "Accomplished": [], "Advanced": [], "Professional": []}
var best_run = {"time": 0}
var opp_type = "-"
var opp_run
var URL_OFFICIAL = "https://pro-racer-2d-default-rtdb.firebaseio.com/Official.json"
var URL_WORLD = "https://pro-racer-2d-default-rtdb.firebaseio.com/World"
var sec_has = 1
var can_play_world = false
var live_splits = {"split_on": 0, "splits": [], "time" : 0}
var live_wins = 0
var live_races = 0
#var intro_track = preload("res://Tracks/Intro Track.tscn")
var Msound = 0
var FXsound = 0
var colR = 0
var colG = 130
var colB = 255
var had_reset = true
var can_start = false
func save_to_file(content, filename):
# var file = File.new()
# file.open("user://" + filename +".dat", File.WRITE)
# file.store_var(content)
# file.close()
JavaScript.eval("localStorage.setItem(\"" + filename + "\", JSON.stringify(" + content as String + "));")
#JavaScript.eval("console.log(localStorage.setItem)")
print("saving to " + filename)
print(content if not "_times" in filename else "")
#load_from_file(filename)
func load_from_file(filename):
# var file = File.new()
# file.open("user://" + filename + ".dat", File.READ)
# var content = file.get_var()
# file.close()
var content = JavaScript.eval("localStorage.getItem(\"" + filename + "\")")#.eval("Window.localStorage.getItem(" + filename + ");"
content = JSON.parse(content).result
print(filename + " loading")
#print(content if not "_times" in filename else "")
JavaScript.eval("console.log(" + (content as String if not "_times" in filename else "") + ")")
return content
func gen_time(time):
return (floor(time/60) if time >= 0 else ceil(time/60)) as String + ":" + (abs(fmod(time, 60.0)) as String if fmod(time, 60.0) > 10 else ("0" + abs(fmod(time, 60.0)) as String))
# Called when the node enters the scene tree for the first time.
func vec2_to_xy(vec2):
return {"x": vec2.x, "y": vec2.y}
func xy_to_vec2(obj):
return Vector2(obj["x"], obj["y"])
func num_to_name(num):
if(num == 1):
return "Race"
elif(num == 2):
return "Road"
elif(num == 3):
return "Dirt"
elif(num == 4):
return "Precision"
elif(num == 5):
return "Lap"
else:
return "Houston, we have a problem"
func num_to_name_2(num):
if(num == 1):
return "Beginner"
elif(num == 2):
return "Intermediate"
elif(num == 3):
return "Accomplished"
elif(num == 4):
return "Advanced"
elif(num == 5):
return "Professional"
else:
return "Houston, we have a problem"
func name_to_num(name):
if name == "Race" or name == "Beginner":
return 1
elif name == "Road" or name == "Intermediate":
return 2
elif name == "Dirt" or name == "Accomplished":
return 3
elif name == "Precision" or name == "Advanced":
return 4
elif name == "Lap" or name == "Professional":
return 5
else:
return NAN
func _ready():
#NetworkManager.connect("split", self, "_on_split")
for i in num_tracks:
official_times["Beginner"].append({"time":0})
official_times["Intermediate"].append({"time":0})
official_times["Accomplished"].append({"time":0})
official_times["Advanced"].append({"time":0})
official_times["Professional"].append({"time":0})
if load_from_file("pb_times"):
pb_times = load_from_file("pb_times")
else:
for i in num_tracks:
pb_times["Beginner"].append({"time":0})
pb_times["Intermediate"].append({"time":0})
pb_times["Accomplished"].append({"time":0})
pb_times["Advanced"].append({"time":0})
pb_times["Professional"].append({"time":0})
save_to_file(pb_times, "pb_times")
if load_from_file("Msound") != null and load_from_file("FXsound") != null:
Msound = load_from_file("Msound")
FXsound = load_from_file("FXsound")
else:
save_to_file(Msound, "Msound")
save_to_file(FXsound, "FXsound")
if load_from_file("color"):
var cols = load_from_file("color")
colR = cols["r"]
colG = cols["g"]
colB = cols["b"]
else:
save_to_file({"r": colR, "g": colG, "b": colB}, "color")
if load_from_file("live_stats"):
live_wins = load_from_file("live_stats")["live_wins"]
live_races = load_from_file("live_stats")["live_races"]
else:
save_to_file({"live_races": Global.live_races, "live_wins": Global.live_wins}, "live_stats")
for i in num_tracks:
tracks["Beginner"].append(load("res://Tracks/Beginner/Track " + (i + 1) as String + ".tscn"))
tracks["Intermediate"].append(load("res://Tracks/Intermediate/Track " + (i + 1) as String + ".tscn"))
tracks["Accomplished"].append(load("res://Tracks/Accomplished/Track " + (i + 1) as String + ".tscn"))
tracks["Advanced"].append(load("res://Tracks/Advanced/Track " + (i + 1) as String + ".tscn"))
tracks["Professional"].append(load("res://Tracks/Professional/Track " + (i + 1) as String + ".tscn"))
$HTTPRequest.request(URL_WORLD + ".json")
#print(pb_times)
func _make_post_request(url, data_to_send, use_ssl = false):
# Convert data to json string:
var query = JSON.print(data_to_send)
# Add 'Content-Type' header:
var headers = ["Content-Type: application/json"]
$HTTPRequest.request(url, headers, use_ssl, HTTPClient.METHOD_PUT, query)
var got_stuff = "world"
var data
var last_opp_type = "-"
func _process(delta):
$Menuing.volume_db = Msound
$Racing.volume_db = Msound + 5
if opp_type == "official":
opp_run = official_times[sec_playing][track_playing]
elif opp_type == "world":
opp_run = world_times[sec_playing][track_playing]
if opp_type == "official" or opp_type == "world":
#print(pb_times[sec_playing][track_playing]["time"])
live_splits = {"split_on": 0, "splits": [], "time" : 0}
best_run = pb_times[sec_playing][track_playing] if (pb_times[sec_playing][track_playing]["time"] != 0 and pb_times[sec_playing][track_playing]["time"] <= opp_run["time"]) else opp_run
elif opp_type == "live":
#print(opp_type)
best_run = {"time":0}
#best_run["time"] = live_splits["time"]
else:
live_splits = {"split_on": 0, "splits": [], "time" : 0}
best_run = pb_times[sec_playing][track_playing]
if opp_type == "-" and last_opp_type != opp_type:
$Menuing.play()
$Racing.stop()
elif opp_type != "-" and last_opp_type != opp_type:
$Racing.play()
$Menuing.stop()
if $Racing.get_playback_position() >= 51.19:
$Racing.stop()
$Racing.play()
print($Racing.get_playback_position())
last_opp_type = opp_type
# print(sec_has)
# if pb_times["Advanced"][4]["time"] != 0:
# #print(pb_times["Advanced"][1]["time"])
# print("posting")
# _make_post_request("https://pro-racer-2d-default-rtdb.firebaseio.com/Official/Advanced/4.json", pb_times["Advanced"][4])
# pb_times["Accomplished"][1] = {"time"
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass
func _on_HTTPRequest_request_completed(result, response_code, headers, body):
if got_stuff == "world":
world_times = JSON.parse(body.get_string_from_utf8()).result
if had_reset:
got_stuff = "official"
$HTTPRequest.request(URL_OFFICIAL)
else:
got_stuff = ""
#_make_post_request(URL_OFFICIAL, world_times)
print("done with getting data")
elif got_stuff == "official":
official_times = JSON.parse(body.get_string_from_utf8()).result
#save_to_file(official_times, "official_times")
for i in range(0, num_tracks):
if pb_times["Beginner"][i]["time"] < official_times["Beginner"][i]["time"] and pb_times["Beginner"][i]["time"] != 0:
sec_has += 0.2
# if pb_times["Beginner"][i]["time"] < world_times["Beginner"][i]["time"]:
# _make_post_request(URL_WORLD + "/Beginner/" + i as String + ".json", pb_times["Beginner"][i])
#print(sec_has)
if pb_times["Intermediate"][i]["time"] < official_times["Intermediate"][i]["time"] and pb_times["Intermediate"][i]["time"] != 0:
sec_has += 0.2
# if pb_times["Intermediate"][i]["time"] < world_times["Intermediate"][i]["time"]:
# _make_post_request(URL_WORLD + "/Intermediate/" + i as String + ".json", pb_times["Intermediate"][i])
if pb_times["Accomplished"][i]["time"] < official_times["Accomplished"][i]["time"] and pb_times["Accomplished"][i]["time"] != 0:
sec_has += 0.2
# if pb_times["Accomplished"][i]["time"] < world_times["Accomplished"][i]["time"]:
# _make_post_request(URL_WORLD + "/Accomplished/" + i as String + ".json", pb_times["Accomplished"][i])
if pb_times["Advanced"][i]["time"] < official_times["Advanced"][i]["time"] and pb_times["Advanced"][i]["time"] != 0:
sec_has += 0.2
# if pb_times["Advanced"][i]["time"] < world_times["Advanced"][i]["time"]:
# _make_post_request(URL_WORLD + "/Advanced/" + i as String + ".json", pb_times["Advanced"][i])
if pb_times["Professional"][i]["time"] < official_times["Professional"][i]["time"] and pb_times["Professional"][i]["time"] != 0:
sec_has += 0.2
# if pb_times["Professional"][i]["time"] < world_times["Professional"][i]["time"]:
# _make_post_request(URL_WORLD + "/Professional/" + i as String + ".json", pb_times["Professional"][i])
print("done with getting data")
got_stuff = ""
#_make_post_request("https://pro-racer-2d-default-rtdb.firebaseio.com/Official/Advanced/4.json", pb_times["Advanced"][4])
#print("why")