111 lines
2.9 KiB
Haxe
111 lines
2.9 KiB
Haxe
package funkin.savedata;
|
|
|
|
import openfl.Lib;
|
|
import flixel.util.FlxSave;
|
|
|
|
/**
|
|
* Class used for saves WITHOUT going through the struggle of type checks
|
|
* Just add your save variables the way you would do in the Options.hx file.
|
|
* The macro will automatically generate the `flush` and `load` functions.
|
|
*/
|
|
@:build(funkin.system.macros.FunkinSaveMacro.build("save", "flush", "load"))
|
|
class FunkinSave {
|
|
public static var highscores:Map<HighscoreEntry, SongScore> = [];
|
|
|
|
|
|
/**
|
|
* ONLY OPEN IF YOU WANT TO EDIT FUNCTIONS RELATED TO SAVING, LOADING OR HIGHSCORES.
|
|
*/
|
|
#if REGION
|
|
@:dox(hide) @:doNotSave
|
|
private static var __eventAdded = false;
|
|
@:doNotSave
|
|
public static var save:FlxSave;
|
|
|
|
public static function init() {
|
|
save = new FlxSave();
|
|
save.bind('save-default', #if sys 'YoshiCrafter29/CodenameEngine' #else 'CodenameEngine' #end);
|
|
load();
|
|
|
|
if (!__eventAdded) {
|
|
Lib.application.onExit.add(function(i:Int) {
|
|
trace("Saving savedata...");
|
|
flush();
|
|
});
|
|
__eventAdded = true;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Returns the high-score for a song.
|
|
* @param name Song name
|
|
* @param diff Song difficulty
|
|
* @param changes Changes made to that song in freeplay.
|
|
*/
|
|
public static inline function getSongHighscore(name:String, diff:String, ?changes:Array<HighscoreChange>) {
|
|
if (changes == null) changes = [];
|
|
return safeGetHighscore(HSongEntry(name.toLowerCase(), diff.toLowerCase(), changes));
|
|
}
|
|
|
|
public static inline function setSongHighscore(name:String, diff:String, highscore:SongScore, ?changes:Array<HighscoreChange>) {
|
|
if (changes == null) changes = [];
|
|
if (safeRegisterHighscore(HSongEntry(name.toLowerCase(), diff.toLowerCase(), changes), highscore)) {
|
|
flush();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static inline function getWeekHighscore(name:String, diff:String)
|
|
return safeGetHighscore(HWeekEntry(name.toLowerCase(), diff.toLowerCase()));
|
|
|
|
public static inline function setWeekHighscore(name:String, diff:String, highscore:SongScore) {
|
|
if (safeRegisterHighscore(HWeekEntry(name.toLowerCase(), diff.toLowerCase()), highscore)) {
|
|
flush();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private static function safeGetHighscore(entry:HighscoreEntry):SongScore {
|
|
if (!highscores.exists(entry)) {
|
|
return {
|
|
score: 0,
|
|
accuracy: 0,
|
|
misses: 0,
|
|
hits: [],
|
|
date: null
|
|
};
|
|
}
|
|
return highscores.get(entry);
|
|
}
|
|
|
|
private static function safeRegisterHighscore(entry:HighscoreEntry, highscore:SongScore) {
|
|
var oldHigh = safeGetHighscore(entry);
|
|
if (oldHigh.date == null || oldHigh.score < highscore.score) {
|
|
highscores.set(entry, highscore);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
#end
|
|
|
|
}
|
|
|
|
enum HighscoreEntry {
|
|
HWeekEntry(weekName:String, difficulty:String);
|
|
HSongEntry(songName:String, difficulty:String, changes:Array<HighscoreChange>);
|
|
}
|
|
|
|
enum HighscoreChange {
|
|
CCoopMode;
|
|
COpponentMode;
|
|
}
|
|
|
|
typedef SongScore = {
|
|
var score:Int;
|
|
var accuracy:Float;
|
|
var misses:Int;
|
|
var hits:Map<String, Int>;
|
|
var date:String;
|
|
} |