new highscore system

This commit is contained in:
Yoshman29
2022-11-03 04:03:26 +01:00
parent daa35cbdad
commit 46d3d956b0
13 changed files with 215 additions and 217 deletions
+60 -43
View File
@@ -4,84 +4,101 @@ import flixel.FlxG;
class Highscore
{
#if (haxe >= "4.0.0")
public static var songScores:Map<String, Int> = new Map();
#else
public static var songScores:Map<String, Int> = new Map<String, Int>();
#end
public static var songScores:Map<String, SongScore> = new Map();
public static var weekScores:Map<String, SongScore> = new Map();
public static function saveScore(song:String, score:Int = 0, ?diff:Int = 0):Void
public static function saveScore(song:String, score:SongScore, ?diff:String = "normal"):Void
{
prepareScore(score);
var daSong:String = formatSong(song, diff);
if (songScores.exists(daSong))
{
if (songScores.get(daSong) < score)
setScore(daSong, score);
}
else
if (!songScores.exists(daSong) || songScores.get(daSong).score < score.score)
setScore(daSong, score);
}
public static function saveWeekScore(week:Int = 1, score:Int = 0, ?diff:Int = 0):Void
public static function saveWeekScore(weekName:String, score:SongScore, ?diff:String = "normal"):Void
{
prepareScore(score);
var daWeek:String = formatSong(weekName, diff);
var daWeek:String = formatSong('week' + week, diff);
if (songScores.exists(daWeek))
{
if (songScores.get(daWeek) < score)
setScore(daWeek, score);
}
else
setScore(daWeek, score);
if (!weekScores.exists(daWeek) || weekScores.get(daWeek).score < score.score)
setWeekScore(daWeek, score);
}
/**
* YOU SHOULD FORMAT SONG WITH formatSong() BEFORE TOSSING IN SONG VARIABLE
*/
static function setScore(song:String, score:Int):Void
static function setScore(song:String, score:SongScore):Void
{
// Reminder that I don't need to format this song, it should come formatted!
if (score == null) return;
songScores.set(song, score);
save();
}
static function setWeekScore(song:String, score:SongScore):Void
{
if (score == null) return;
weekScores.set(song, score);
save();
}
public static function save() {
FlxG.save.data.songScores = songScores;
FlxG.save.data.weekScores = weekScores;
FlxG.save.flush();
}
public static function formatSong(song:String, diff:Int):String
public static function formatSong(song:String, diff:String):String
{
var daSong:String = song;
if (diff == 0)
daSong += '-easy';
else if (diff == 2)
daSong += '-hard';
diff = diff.toLowerCase();
if (diff != "normal")
daSong += '-$diff';
return daSong;
}
public static function getScore(song:String, diff:Int):Int
public static function getScore(song:String, diff:String):SongScore
{
if (!songScores.exists(formatSong(song, diff)))
setScore(formatSong(song, diff), 0);
var daSong = formatSong(song, diff);
if (!songScores.exists(daSong))
setScore(daSong, {});
return songScores.get(formatSong(song, diff));
return prepareScore(songScores.get(formatSong(song, diff)));
}
public static function getWeekScore(week:Int, diff:Int):Int
public static function getWeekScore(week:String, diff:String):SongScore
{
if (!songScores.exists(formatSong('week' + week, diff)))
setScore(formatSong('week' + week, diff), 0);
var daWeek = formatSong(week, diff);
if (!weekScores.exists(daWeek))
setScore(daWeek, {});
return songScores.get(formatSong('week' + week, diff));
return prepareScore(weekScores.get(daWeek));
}
public static function prepareScore(score:SongScore):SongScore {
if (score == null) score = {};
score.setFieldDefault('score', 0);
score.setFieldDefault('accuracy', 0);
score.setFieldDefault('misses', 0);
score.setFieldDefault('hits', new Map<String, Int>());
return score;
}
public static function load():Void
{
if (FlxG.save.data.songScores != null)
{
songScores = FlxG.save.data.songScores;
weekScores = FlxG.save.data.weekScores;
}
for(k=>e in songScores)
if (e is Int)
songScores.remove(k);
}
}
typedef SongScore = {
@:optional var score:Int;
@:optional var accuracy:Float;
@:optional var misses:Int;
@:optional var hits:Map<String, Int>;
}
+23 -18
View File
@@ -1,5 +1,6 @@
package funkin.game;
import funkin.menus.StoryMenuState.WeekData;
import funkin.ui.FunkinText;
import flixel.group.FlxSpriteGroup;
import funkin.options.Options;
@@ -67,9 +68,9 @@ class PlayState extends MusicBeatState
*/
public static var SONG:SwagSong;
public static var isStoryMode:Bool = false;
public static var storyWeek:Int = 0;
public static var storyWeek:WeekData = null;
public static var storyPlaylist:Array<String> = [];
public static var storyDifficulty:Int = 1;
public static var difficulty:String = "normal";
public static var fromMods:Bool = false;
public var scripts:ScriptPack;
@@ -142,7 +143,7 @@ class PlayState extends MusicBeatState
#if desktop
// Discord RPC variables
public var storyDifficultyText:String = "";
public var difficultyText:String = "";
public var iconRPC:String = "";
public var songLength:Float = 0;
public var detailsText:String = "";
@@ -186,7 +187,7 @@ class PlayState extends MusicBeatState
// String that contains the mode defined here so it isn't necessary to call changePresence for each mode
if (isStoryMode)
{
detailsText = "Story Mode: Week " + storyWeek;
detailsText = "Story Mode: " + storyWeek.name;
}
else
{
@@ -197,7 +198,7 @@ class PlayState extends MusicBeatState
detailsPausedText = "Paused - " + detailsText;
// Updating Discord Rich Presence.
DiscordClient.changePresence(detailsText, SONG.song + " (" + storyDifficultyText + ")", iconRPC);
DiscordClient.changePresence(detailsText, SONG.song + " (" + difficultyText + ")", iconRPC);
#end
dad = new Character(100, 100, SONG.player2);
@@ -625,11 +626,11 @@ class PlayState extends MusicBeatState
#if desktop
if (startTimer.finished)
{
DiscordClient.changePresence(detailsText, SONG.song + " (" + storyDifficultyText + ")", iconRPC, true, songLength - Conductor.songPosition);
DiscordClient.changePresence(detailsText, SONG.song + " (" + difficultyText + ")", iconRPC, true, songLength - Conductor.songPosition);
}
else
{
DiscordClient.changePresence(detailsText, SONG.song + " (" + storyDifficultyText + ")", iconRPC);
DiscordClient.changePresence(detailsText, SONG.song + " (" + difficultyText + ")", iconRPC);
}
#end
}
@@ -645,11 +646,11 @@ class PlayState extends MusicBeatState
{
if (Conductor.songPosition > 0.0)
{
DiscordClient.changePresence(detailsText, SONG.song + " (" + storyDifficultyText + ")", iconRPC, true, songLength - Conductor.songPosition);
DiscordClient.changePresence(detailsText, SONG.song + " (" + difficultyText + ")", iconRPC, true, songLength - Conductor.songPosition);
}
else
{
DiscordClient.changePresence(detailsText, SONG.song + " (" + storyDifficultyText + ")", iconRPC);
DiscordClient.changePresence(detailsText, SONG.song + " (" + difficultyText + ")", iconRPC);
}
}
#end
@@ -663,7 +664,7 @@ class PlayState extends MusicBeatState
#if desktop
if (health > 0 && !paused)
{
DiscordClient.changePresence(detailsPausedText, SONG.song + " (" + storyDifficultyText + ")", iconRPC);
DiscordClient.changePresence(detailsPausedText, SONG.song + " (" + difficultyText + ")", iconRPC);
}
#end
@@ -713,7 +714,7 @@ class PlayState extends MusicBeatState
songLength = FlxG.sound.music.length;
// Updating Discord Rich Presence (with Time Left)
DiscordClient.changePresence(detailsText, SONG.song + " (" + storyDifficultyText + ")", iconRPC, true, songLength);
DiscordClient.changePresence(detailsText, SONG.song + " (" + difficultyText + ")", iconRPC, true, songLength);
#end
scripts.call("onDiscordPresenceUpdate");
}
@@ -915,7 +916,7 @@ class PlayState extends MusicBeatState
#if desktop
// Game Over doesn't get his own variable because it's only used here
DiscordClient.changePresence("Game Over - " + detailsText, SONG.song + " (" + storyDifficultyText + ")", iconRPC);
DiscordClient.changePresence("Game Over - " + detailsText, SONG.song + " (" + difficultyText + ")", iconRPC);
#end
}
@@ -995,7 +996,11 @@ class PlayState extends MusicBeatState
if (SONG.validScore)
{
#if !switch
Highscore.saveScore(SONG.song, songScore, storyDifficulty);
// TODO: Accuracy stuff
Highscore.saveScore(SONG.song, {
score: songScore,
misses: misses
}, difficulty);
#end
}
@@ -1015,17 +1020,17 @@ class PlayState extends MusicBeatState
if (SONG.validScore)
{
Highscore.saveWeekScore(storyWeek, campaignScore, storyDifficulty);
// TODO: more week info saving
Highscore.saveWeekScore(storyWeek.name, {
score: campaignScore
}, difficulty);
}
FlxG.save.flush();
}
else
{
// TODO: NOT INT DIFFICULTIES
var difficulty:String = CoolUtil.difficultyString();
trace('LOADING NEXT SONG');
trace(PlayState.storyPlaylist[0].toLowerCase() + difficulty);
trace(PlayState.storyPlaylist[0].toLowerCase(), difficulty);
if (SONG.song.toLowerCase() == 'eggnog')
{