ape-ame/Leaderboard.gd

73 lines
2.4 KiB
GDScript3
Raw Normal View History

2024-04-19 17:59:19 -04:00
extends RichTextLabel
2024-04-16 20:29:45 -04:00
@export var stopwatch: Stopwatch
2024-04-17 17:22:29 -04:00
var config = ConfigFile.new()
2024-04-16 20:29:45 -04:00
2024-04-19 17:59:19 -04:00
var API_BASE = "https://flask-hello-world-nine-psi.vercel.app"
2024-04-17 17:22:29 -04:00
func make_urlsafe(data: String):
return data.replace("+", "-").replace("/", "_")
2024-04-17 15:00:20 -04:00
2024-04-16 20:29:45 -04:00
func get_scores():
2024-04-17 15:00:20 -04:00
$HTTPRequest.request(API_BASE + "/leaderboard")
2024-04-16 20:29:45 -04:00
func submit_score():
2024-04-17 17:22:29 -04:00
var err = config.load("user://settings.cfg")
if not (err == OK and config.has_section_key("config", "username")):
text = "Error: No Username!\nSet a username in settings to submit scores"
var encrypted_score = "EGH" + make_urlsafe(Marshalls.utf8_to_base64(str(stopwatch.time_elapsed))) + "eHa" + make_urlsafe(Marshalls.utf8_to_base64(config.get_value("config", "username")))
2024-04-16 20:29:45 -04:00
print(encrypted_score)
$HTTPRequest.request_completed.connect(_on_request_completed)
2024-04-17 17:22:29 -04:00
$HTTPRequest.request(API_BASE + "/leaderboard?score=%s" % encrypted_score, [], HTTPClient.METHOD_POST)
2024-04-16 20:29:45 -04:00
func _on_request_completed(result, response_code, headers, body):
if body.get_string_from_utf8() == "OK":
get_scores()
return true
var json = JSON.parse_string(body.get_string_from_utf8())
#print(json)
if json and "scores" in json:
2024-04-17 17:22:29 -04:00
var scoredata = json["scores"]
var scores = []
for score in scoredata:
scores.append(float(score[1]))
2024-04-19 17:59:19 -04:00
#print(scores)
2024-04-16 20:29:45 -04:00
var score = stopwatch.time_elapsed
2024-04-19 17:59:19 -04:00
#print(score)
2024-04-17 19:14:46 -04:00
var placement = closest(score, scores)
2024-04-19 17:59:19 -04:00
#print(placement)
2024-04-16 20:29:45 -04:00
if placement < 50:
2024-04-19 17:59:19 -04:00
text = "[center]You placed #%d\n[/center]" % [placement]
2024-04-16 20:29:45 -04:00
else:
2024-04-19 17:59:19 -04:00
text = "[center]You placed top %d%%\n[/center]" % [(float(placement) / len(scores)) * 100]
2024-04-16 20:29:45 -04:00
var n = 1
2024-04-19 17:59:19 -04:00
while n <= 50 and n <= len(scores):
text += "%d.\t\t%s:\t\t%s\n" % [ n, scoredata[n-1][0], seconds2mmss(scores[n-1]) ]
2024-04-16 20:29:45 -04:00
n += 1
func closest(my_number:int, my_array:Array)->int:
# Initialize
var closest_num:int
var closest_delta:int = 0
var closest_idx = -1
var temp_delta:int = 0
# Loop through entire array
for i in range(my_array.size()):
2024-04-17 17:22:29 -04:00
if my_array[i] == my_number:
print("Exact Match!")
return my_array[i] # exact match found!
2024-04-16 20:29:45 -04:00
temp_delta = int(abs(my_array[i]-my_number))
if closest_delta == 0 or temp_delta < closest_delta:
closest_num = my_array[i]
closest_idx = i
closest_delta = temp_delta
# Return closest number found
return closest_idx
func seconds2mmss(total_seconds: float) -> String:
var seconds: float = fmod(total_seconds , 60.0)
var minutes: int = int(total_seconds / 60.0) % 60
var hhmmss_string: String = "%02d:%05.2f" % [minutes, seconds]
return hhmmss_string