Skip to content

Commit

Permalink
Add WarriorMedals support
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanawy committed Sep 28, 2024
1 parent 660e076 commit f1b3e0f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 23 deletions.
4 changes: 2 additions & 2 deletions info.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
optional_dependencies = ["ChampionMedals", "WarriorMedals"]
3 changes: 2 additions & 1 deletion src/consts.as
Original file line number Diff line number Diff line change
Expand Up @@ -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;
const uint MAX_CUSTOM_MEDAL_UPDATE_ATTEMPTS_PER_MAP = 3;
60 changes: 40 additions & 20 deletions src/main.as
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ enum DefaultTargetMedalOptions {
pb,
#if DEPENDENCY_CHAMPIONMEDALS
champion,
#endif
#if DEPENDENCY_WARRIORMEDALS
warrior,
#endif
author,
gold,
Expand All @@ -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<Target@> targets = {
Expand All @@ -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 = "";
Expand All @@ -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


Expand Down Expand Up @@ -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;
Expand All @@ -362,6 +377,7 @@ Target@ GetHardestMedalBeaten(int time)
silver,
gold,
author,
warrior,
champion,
};

Expand Down Expand Up @@ -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<CTrackMania@>(GetApp());
auto network = trackmania.Network;
Expand All @@ -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()
Expand Down

0 comments on commit f1b3e0f

Please sign in to comment.