Kade-Engine/source/Highscore.hx
2021-06-21 01:22:54 -07:00

160 lines
3.5 KiB
Haxe

package;
import flixel.FlxG;
using StringTools;
class Highscore
{
#if (haxe >= "4.0.0")
public static var songScores:Map<String, Int> = new Map();
public static var songCombos:Map<String, String> = new Map();
#else
public static var songScores:Map<String, Int> = new Map<String, Int>();
public static var songCombos:Map<String, String> = new Map<String, String>();
#end
public static function saveScore(song:String, score:Int = 0, ?diff:Int = 0):Void
{
var daSong:String = formatSong(song, diff);
#if !switch
NGio.postScore(score, song);
#end
if(!FlxG.save.data.botplay)
{
if (songScores.exists(daSong))
{
if (songScores.get(daSong) < score)
setScore(daSong, score);
}
else
setScore(daSong, score);
}else trace('BotPlay detected. Score saving is disabled.');
}
public static function saveCombo(song:String, combo:String, ?diff:Int = 0):Void
{
var daSong:String = formatSong(song, diff);
var finalCombo:String = combo.split(')')[0].replace('(', '');
if(!FlxG.save.data.botplay)
{
if (songCombos.exists(daSong))
{
if (getComboInt(songCombos.get(daSong)) < getComboInt(finalCombo))
setCombo(daSong, finalCombo);
}
else
setCombo(daSong, finalCombo);
}
}
public static function saveWeekScore(week:Int = 1, score:Int = 0, ?diff:Int = 0):Void
{
#if !switch
NGio.postScore(score, "Week " + week);
#end
if(!FlxG.save.data.botplay)
{
var daWeek:String = formatSong('week' + week, diff);
if (songScores.exists(daWeek))
{
if (songScores.get(daWeek) < score)
setScore(daWeek, score);
}
else
setScore(daWeek, score);
}else trace('BotPlay detected. Score saving is disabled.');
}
/**
* YOU SHOULD FORMAT SONG WITH formatSong() BEFORE TOSSING IN SONG VARIABLE
*/
static function setScore(song:String, score:Int):Void
{
// Reminder that I don't need to format this song, it should come formatted!
songScores.set(song, score);
FlxG.save.data.songScores = songScores;
FlxG.save.flush();
}
static function setCombo(song:String, combo:String):Void
{
// Reminder that I don't need to format this song, it should come formatted!
songCombos.set(song, combo);
FlxG.save.data.songCombos = songCombos;
FlxG.save.flush();
}
public static function formatSong(song:String, diff:Int):String
{
var daSong:String = song;
if (diff == 0)
daSong += '-easy';
else if (diff == 2)
daSong += '-hard';
return daSong;
}
static function getComboInt(combo:String):Int
{
switch(combo)
{
case 'SDCB':
return 1;
case 'FC':
return 2;
case 'GFC':
return 3;
case 'MFC':
return 4;
default:
return 0;
}
}
public static function getScore(song:String, diff:Int):Int
{
if (!songScores.exists(formatSong(song, diff)))
setScore(formatSong(song, diff), 0);
return songScores.get(formatSong(song, diff));
}
public static function getCombo(song:String, diff:Int):String
{
if (!songCombos.exists(formatSong(song, diff)))
setCombo(formatSong(song, diff), '');
return songCombos.get(formatSong(song, diff));
}
public static function getWeekScore(week:Int, diff:Int):Int
{
if (!songScores.exists(formatSong('week' + week, diff)))
setScore(formatSong('week' + week, diff), 0);
return songScores.get(formatSong('week' + week, diff));
}
public static function load():Void
{
if (FlxG.save.data.songScores != null)
{
songScores = FlxG.save.data.songScores;
}
if (FlxG.save.data.songCombos != null)
{
songCombos = FlxG.save.data.songCombos;
}
}
}