Skip to content

Commit dcf34cb

Browse files
[WebAPIService] - Fixes Aurora leaderboard handling.
The leaderboard code was wonky, daily based despite no daily stuff. Also fixes the persistence.
1 parent 57c97ae commit dcf34cb

File tree

2 files changed

+61
-5
lines changed

2 files changed

+61
-5
lines changed

AuxiliaryServices/WebAPIService/NDREAMS/Aurora/AuroraDBManager.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,9 @@ public static string ProcessOrbrunnerScores(DateTime CurrentDate, byte[] PostDat
272272

273273
if (int.TryParse(score, out int resscore))
274274
{
275+
OrbrunnerScoreBoardData.CheckForInitiatedleaderboard(apipath);
275276
OrbrunnerScoreBoardData.UpdateScoreBoard(name, resscore);
276-
OrbrunnerScoreBoardData.UpdateTodayScoreboardXml(apipath, DateTime.Now.ToString("yyyy_MM_dd"));
277+
OrbrunnerScoreBoardData.UpdateScoreboardXml(apipath);
277278
HighestScore = OrbrunnerScoreBoardData.GetHighestScore();
278279
if (HighestScore != null && !string.IsNullOrEmpty(HighestScore.Value.Item1))
279280
{
@@ -293,6 +294,7 @@ public static string ProcessOrbrunnerScores(DateTime CurrentDate, byte[] PostDat
293294
return $"<xml><success>false</success><error>Signature Mismatch</error><extra>{errMsg}</extra><function>ProcessOrbrunnerScores</function></xml>";
294295
}
295296
case "high":
297+
OrbrunnerScoreBoardData.CheckForInitiatedleaderboard(apipath);
296298
HighestScore = OrbrunnerScoreBoardData.GetHighestScore();
297299
if (HighestScore != null && !string.IsNullOrEmpty(HighestScore.Value.Item1))
298300
high = $"{HighestScore.Value.Item1},{HighestScore.Value.Item2}";

AuxiliaryServices/WebAPIService/NDREAMS/Aurora/OrbrunnerScoreBoardData.cs

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.IO;
2-
using System.Globalization;
32
using System.Text;
43
using System.Collections.Generic;
54
using System;
@@ -9,6 +8,8 @@ namespace WebAPIService.NDREAMS.Aurora
98
{
109
public class OrbrunnerScoreBoardData
1110
{
11+
private static bool _initiated = false;
12+
1213
private static object _Lock = new object();
1314

1415
public class ScoreboardEntry
@@ -19,6 +20,41 @@ public class ScoreboardEntry
1920

2021
private static List<ScoreboardEntry> scoreboard = new List<ScoreboardEntry>();
2122

23+
private static void LoadScoreBoardFromText(string text)
24+
{
25+
if (string.IsNullOrWhiteSpace(text))
26+
{
27+
_initiated = true;
28+
return;
29+
}
30+
31+
// Split the input text by commas into pairs
32+
string[] parts = text.Split(',');
33+
34+
if (parts.Length % 2 != 0)
35+
{
36+
CustomLogger.LoggerAccessor.LogError("[ndreams] - Orbrunner - Invalid scoreboard input format.");
37+
_initiated = true;
38+
return;
39+
}
40+
41+
// Iterate over the pairs (psnid, score)
42+
for (int i = 0; i < parts.Length; i += 2)
43+
{
44+
string psnid = parts[i];
45+
if (!int.TryParse(parts[i + 1], out int score)) // Ensure valid score format
46+
{
47+
CustomLogger.LoggerAccessor.LogWarn("[ndreams] - Orbrunner - Invalid score for player {psnid}. Skipping this entry.");
48+
continue;
49+
}
50+
51+
// Update or add the score entry to the scoreboard
52+
UpdateScoreBoard(psnid, score);
53+
}
54+
55+
_initiated = true;
56+
}
57+
2258
public static void UpdateScoreBoard(string psnid, int newScore)
2359
{
2460
// Check if the player already exists in the scoreboard
@@ -45,6 +81,23 @@ public static void UpdateScoreBoard(string psnid, int newScore)
4581
scoreboard.RemoveRange(10, scoreboard.Count - 10);
4682
}
4783

84+
public static void CheckForInitiatedleaderboard(string apiPath)
85+
{
86+
if (!_initiated)
87+
{
88+
lock (_Lock)
89+
{
90+
string filePath = $"{apiPath}/NDREAMS/Aurora/Orbrunner/leaderboard.txt";
91+
Directory.CreateDirectory($"{apiPath}/NDREAMS/Aurora/Orbrunner");
92+
if (File.Exists(filePath))
93+
LoadScoreBoardFromText(File.ReadAllText(filePath));
94+
else
95+
_initiated = true;
96+
CustomLogger.LoggerAccessor.LogDebug($"[ndreams] - Orbrunner - scoreboard initiated.");
97+
}
98+
}
99+
}
100+
48101
public static (string, int)? GetHighestScore()
49102
{
50103
if (scoreboard.Count == 0)
@@ -72,13 +125,14 @@ public static string ConvertScoreboardToText()
72125
return sb.ToString();
73126
}
74127

75-
public static void UpdateTodayScoreboardXml(string apiPath, string date)
128+
public static void UpdateScoreboardXml(string apiPath)
76129
{
77130
lock (_Lock)
78131
{
132+
string filePath = $"{apiPath}/NDREAMS/Aurora/Orbrunner/leaderboard.txt";
79133
Directory.CreateDirectory($"{apiPath}/NDREAMS/Aurora/Orbrunner");
80-
File.WriteAllText($"{apiPath}/NDREAMS/Aurora/Orbrunner/leaderboard_{date}.txt", ConvertScoreboardToText());
81-
CustomLogger.LoggerAccessor.LogDebug($"[ndreams] - Orbrunner - scoreboard {date} TEXT updated.");
134+
File.WriteAllText(filePath, ConvertScoreboardToText());
135+
CustomLogger.LoggerAccessor.LogDebug($"[ndreams] - Orbrunner - scoreboard updated.");
82136
}
83137
}
84138
}

0 commit comments

Comments
 (0)