teststrecke-game/scripts/highscoreHandler.gd

41 lines
1.2 KiB
GDScript

extends Node
const HIGHSCORE_FILE_PATH = "highscores.json"
func updateHighscore(mapname:String,rounds:int,preset:int,time:float):
var highscores=loadHighscoreDict()
if highscores.has(mapname):
var lasthighscore=loadHighscore(mapname)
if time<lasthighscore: #better time
highscores[mapname].append({"created":Time.get_unix_time_from_system(),"time":time})
storeHighscoreDict(highscores)
return time-lasthighscore
else: #first entry
highscores[mapname]=[{"created":Time.get_unix_time_from_system(),"time":time}]
storeHighscoreDict(highscores)
return 0
func loadHighscore(mapname:String):
var highscores=loadHighscoreDict()
if highscores.has(mapname):
if highscores[mapname].size()>0:
return highscores[mapname][highscores[mapname].size()-1]["time"] #return last entry
else:
return null
func loadHighscoreDict():
var file = FileAccess.open(HIGHSCORE_FILE_PATH, FileAccess.READ)
if file == null:
return {}
var content = file.get_as_text()
return JSON.parse_string(content)
func storeHighscoreDict(content:Dictionary):
print("Saved Highscorefile:"+str(content))
var file = FileAccess.open(HIGHSCORE_FILE_PATH, FileAccess.WRITE)
file.store_string(JSON.stringify(content, "\t"))