From f1b3e0fc6d1fe87ebe74631fe9407bae10802a00 Mon Sep 17 00:00:00 2001 From: Ilya Sakharchuk Date: Sun, 29 Sep 2024 00:00:13 +0300 Subject: [PATCH] Add WarriorMedals support --- info.toml | 4 ++-- src/consts.as | 3 ++- src/main.as | 60 ++++++++++++++++++++++++++++++++++----------------- 3 files changed, 44 insertions(+), 23 deletions(-) diff --git a/info.toml b/info.toml index 0cf5a50..8baae80 100755 --- a/info.toml +++ b/info.toml @@ -2,8 +2,8 @@ name = "Run History" author = "Vanawy" category = "Overlay" -version = "0.5.3" +version = "0.6.0" [script] dependencies = ["MLHook", "MLFeedRaceData"] -optional_dependencies = ["ChampionMedals"] \ No newline at end of file +optional_dependencies = ["ChampionMedals", "WarriorMedals"] \ No newline at end of file diff --git a/src/consts.as b/src/consts.as index f0479a2..af18fb3 100644 --- a/src/consts.as +++ b/src/consts.as @@ -33,7 +33,8 @@ const string COLOR_SILVER = "\\$899"; const string COLOR_GOLD = "\\$db4"; const string COLOR_AUTHOR = "\\$071"; const string COLOR_CHAMPION = "\\$f69"; +const string COLOR_WARRIOR = "\\$3cf"; const string COLOR_NO_MEDAL = "\\$555"; // other -const uint MAX_CM_UPDATE_ATTEMPTS_PER_MAP = 3; \ No newline at end of file +const uint MAX_CUSTOM_MEDAL_UPDATE_ATTEMPTS_PER_MAP = 3; \ No newline at end of file diff --git a/src/main.as b/src/main.as index 0e30b32..ef3b7a3 100755 --- a/src/main.as +++ b/src/main.as @@ -3,6 +3,9 @@ enum DefaultTargetMedalOptions { pb, #if DEPENDENCY_CHAMPIONMEDALS champion, +#endif +#if DEPENDENCY_WARRIORMEDALS + warrior, #endif author, gold, @@ -22,6 +25,7 @@ Target@ silver = Target(COLOR_SILVER, ICON_MEDAL); Target@ gold = Target(COLOR_GOLD, ICON_MEDAL); Target@ author = Target(COLOR_AUTHOR, ICON_MEDAL); Target@ champion = Target(COLOR_CHAMPION, ICON_MEDAL); +Target@ warrior = Target(COLOR_WARRIOR, ICON_MEDAL); Target@ no_medal = Target(COLOR_NO_MEDAL, ICON_NO_MEDAL); array targets = { @@ -43,11 +47,6 @@ uint championMedalUpdateAttempts = 0; void Main() { -#if DEPENDENCY_CHAMPIONMEDALS - print("ChampionMedals detected"); -#else - warn("ChampionMedals not installed"); -#endif string lastMapId = ""; string lastGhostId = ""; @@ -56,8 +55,18 @@ void Main() thresholdsTable.FromString(settingDeltasSerialized); +#if DEPENDENCY_WARRIORMEDALS + print("WarriorMedals detected"); + targets.InsertAt(1, warrior); +#else + warn("WarriorMedals not installed"); +#endif + #if DEPENDENCY_CHAMPIONMEDALS + print("ChampionMedals detected"); targets.InsertAt(1, champion); +#else + warn("ChampionMedals not installed"); #endif @@ -341,6 +350,12 @@ Target@ GetDefaultTarget() if (champion.hasTime()) { return @champion; } +#endif +#if DEPENDENCY_WARRIORMEDALS + case DefaultTargetMedalOptions::warrior: + if (warrior.hasTime()) { + return @warrior; + } #endif case DefaultTargetMedalOptions::pb: return @pb; @@ -362,6 +377,7 @@ Target@ GetHardestMedalBeaten(int time) silver, gold, author, + warrior, champion, }; @@ -434,9 +450,9 @@ void OnMapChange(CGameCtnChallenge@ map) silver.time = map.TMObjective_SilverTime; gold.time = map.TMObjective_GoldTime; champion.time = 0; + warrior.time = 0; custom.time = 0; pb.time = 0; - championMedalUpdateAttempts = 0; auto trackmania = cast(GetApp()); auto network = trackmania.Network; @@ -459,29 +475,33 @@ void OnMapChange(CGameCtnChallenge@ map) UpdateCurrentTarget(); #if DEPENDENCY_CHAMPIONMEDALS - UpdateChampionTime(); + // UpdateChampionTime(); + UpdateCustomMedalTime(champion, function() { return ChampionMedals::GetCMTime(); }); +#endif +#if DEPENDENCY_WARRIORMEDALS + // UpdateWarriorTime(); + UpdateCustomMedalTime(warrior, function() { return WarriorMedals::GetWMTimeAsync(); }); #endif } -#if DEPENDENCY_CHAMPIONMEDALS -void UpdateChampionTime() { - - while (champion.time <= 0 - && championMedalUpdateAttempts < MAX_CM_UPDATE_ATTEMPTS_PER_MAP +funcdef int MedalTimeCB(); + +void UpdateCustomMedalTime(Target @medal, MedalTimeCB @GetTime) { + int attempts = 0; + while (!medal.hasTime() + && attempts < MAX_CUSTOM_MEDAL_UPDATE_ATTEMPTS_PER_MAP ) { - championMedalUpdateAttempts += 1; + attempts += 1; - auto newTime = ChampionMedals::GetCMTime(); - if (champion.time != int(newTime)) { - champion.time = newTime; - print(champion.icon + Time::Format(champion.time) + " try#" + championMedalUpdateAttempts); + int newTime = GetTime(); + if (medal.time != int(newTime)) { + medal.time = newTime; + print(medal.icon + Time::Format(medal.time) + " attempt#" + attempts); UpdateCurrentTarget(); } - sleep(1000 * championMedalUpdateAttempts); + sleep(1000 * attempts); } - } -#endif void OnThresholdsTableChange()