teststrecke-game/scripts/highscoreHandler.gd

53 lines
1.7 KiB
GDScript

extends Node
const HIGHSCORE_FILE_PATH = "highscores.json"
func updateHighscore(mapname:String,preset:String,rounds:String,time:float):
var highscores=loadHighscoreDict()
var lasthighscore=loadHighscore(mapname,preset,rounds)
if lasthighscore !=null:
if time<lasthighscore: #better time
highscores[mapname][preset][rounds].append({"created":Time.get_unix_time_from_system(),"time":time})
storeHighscoreDict(highscores)
return time-lasthighscore
else: #first entry for this map and config
print("first entry for this config")
if !highscores.has(mapname):
print("mapname not known")
highscores[mapname]={}
if !highscores[mapname].has(preset):
print("preset not known")
highscores[mapname][preset]={}
if !highscores[mapname][preset].has(rounds):
print("rounds not known")
highscores[mapname][preset][rounds]=[{"created":Time.get_unix_time_from_system(),"time":time}]
storeHighscoreDict(highscores)
return 0
func loadHighscore(mapname:String,preset:String,rounds:String):
var highscores=loadHighscoreDict()
if highscores.has(mapname):
if highscores[mapname].has(preset):
if highscores[mapname][preset].has(rounds):
if highscores[mapname][preset][rounds].size()>0:
return highscores[mapname][preset][rounds][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"))