Skip to content

Commit 3fb124c

Browse files
[CastleLibrary/WebAPIService/ApacheNet] - Adjusts the Nethasher CRC namespace, fixup the Cutteridge default chance XML response, fixup the Firing Range input validation routine, and implements the Athletic Home games scoreboard system.
1 parent dcf34cb commit 3fb124c

File tree

19 files changed

+222
-20
lines changed

19 files changed

+222
-20
lines changed

AuxiliaryServices/HomeTools/ChannelID/SIDKeyGenerator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using EndianTools;
22
using System;
33
using System.Collections;
4-
using CastleLibrary.NetHasher.CRC;
4+
using NetHasher.CRC;
55

66
namespace HomeTools.ChannelID
77
{
@@ -178,8 +178,8 @@ public SceneKey GenerateNewerType(ushort SceneID)
178178
byte[] numArray = new byte[16];
179179
new BitArray(bytes3).Xor(new BitArray(new SceneKey(new byte[] { 0xB9, 0x20, 0x86, 0xBC, 0x3E, 0x8B, 0x4A, 0xDF, 0xA3, 0x01, 0x4D, 0xEE, 0x2F, 0xA3, 0xAB, 0x69 }).GetBytes())).CopyTo(numArray, 0);
180180
ushort crc = EndianUtils.ReverseUshort(CRC16.Create(numArray, 0, 14));
181-
numArray[14] = (byte)(crc >> 8); // Get the higher 8 bits
182-
numArray[15] = (byte)crc; // Get the lower 8 bits
181+
numArray[14] = (byte)(crc >> 8);
182+
numArray[15] = (byte)crc;
183183
return new SceneKey(numArray);
184184
}
185185

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using CustomLogger;
2+
using HttpMultipartParser;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Globalization;
6+
using System.IO;
7+
8+
namespace WebAPIService.HOMELEADERBOARDS
9+
{
10+
public static class HOMELEADERBOARDSClass
11+
{
12+
private static Dictionary<string, HomeLeaderboards> _leaderboards = new Dictionary<string, HomeLeaderboards>();
13+
14+
public static string ProcessEntryBare(byte[] postdata, string boundary, string apiPath)
15+
{
16+
if (postdata != null && !string.IsNullOrEmpty(boundary))
17+
{
18+
try
19+
{
20+
using (MemoryStream copyStream = new MemoryStream(postdata))
21+
{
22+
var data = MultipartFormDataParser.Parse(copyStream, boundary);
23+
24+
string postType = data.GetParameterValue("postType");
25+
string game = data.GetParameterValue("game");
26+
27+
switch (postType)
28+
{
29+
case "getHighScore":
30+
if (!string.IsNullOrEmpty(game))
31+
{
32+
lock (_leaderboards)
33+
{
34+
if (!_leaderboards.ContainsKey(game))
35+
_leaderboards.Add(game, new HomeLeaderboards());
36+
return $"<MsRoot>{_leaderboards[game].UpdateScoreboardXml(apiPath, game)}</MsRoot>";
37+
}
38+
}
39+
break;
40+
case "postScore":
41+
double score = double.Parse(data.GetParameterValue("score"), CultureInfo.InvariantCulture);
42+
string player = data.GetParameterValue("player");
43+
44+
lock (_leaderboards)
45+
{
46+
if (!string.IsNullOrEmpty(game) && _leaderboards.ContainsKey(game))
47+
{
48+
_leaderboards[game].UpdateScoreBoard(player, score);
49+
return $"<MsRoot>{_leaderboards[game].UpdateScoreboardXml(apiPath, game)}</MsRoot>";
50+
}
51+
}
52+
break;
53+
}
54+
}
55+
}
56+
catch (Exception ex)
57+
{
58+
LoggerAccessor.LogError($"[HOMELEADERBOARDSClass] - entryBare request thrown an assertion. (Exception: {ex})");
59+
}
60+
}
61+
62+
return null;
63+
}
64+
}
65+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Xml.Linq;
5+
6+
namespace WebAPIService.HOMELEADERBOARDS
7+
{
8+
internal class HomeLeaderboards
9+
{
10+
private static bool _initiated = false;
11+
12+
private object _Lock = new object();
13+
14+
public class HomeScoreboardEntry
15+
{
16+
public string psnid { get; set; }
17+
public double score { get; set; }
18+
}
19+
20+
private List<HomeScoreboardEntry> scoreboard = new List<HomeScoreboardEntry>();
21+
22+
public void LoadScoreboardFromXml(string path)
23+
{
24+
if (!File.Exists(path))
25+
{
26+
_initiated = true;
27+
return;
28+
}
29+
30+
scoreboard.Clear();
31+
32+
foreach (var playerElement in XDocument.Parse(File.ReadAllText(path)).Descendants("ENTRY"))
33+
{
34+
string psnid = playerElement.Attribute("player")?.Value;
35+
string scoreStr = playerElement.Attribute("score")?.Value;
36+
37+
double.TryParse(scoreStr, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out double score);
38+
39+
scoreboard.Add(new HomeScoreboardEntry
40+
{
41+
psnid = psnid,
42+
score = score,
43+
});
44+
}
45+
46+
scoreboard.Sort((a, b) => b.score.CompareTo(a.score));
47+
48+
if (scoreboard.Count > 8)
49+
scoreboard.RemoveRange(8, scoreboard.Count - 8);
50+
51+
_initiated = true;
52+
}
53+
54+
public void UpdateScoreBoard(string psnid, double newScore)
55+
{
56+
// Check if the player already exists in the scoreboard
57+
var existingEntry = scoreboard.Find(e => e.psnid != null && e.psnid.Equals(psnid, StringComparison.OrdinalIgnoreCase));
58+
59+
if (existingEntry != null)
60+
{
61+
// If the new score is higher, update the existing entry
62+
if (newScore > existingEntry.score)
63+
existingEntry.score = newScore;
64+
}
65+
else
66+
{
67+
// If the player is not in the scoreboard, add a new entry
68+
if (scoreboard.Count < 8)
69+
scoreboard.Add(new HomeScoreboardEntry { psnid = psnid, score = newScore });
70+
}
71+
72+
// Sort the scoreboard by score in descending order
73+
scoreboard.Sort((a, b) => b.score.CompareTo(a.score));
74+
75+
// Trim the scoreboard to the top 8 entries
76+
if (scoreboard.Count > 8)
77+
scoreboard.RemoveRange(8, scoreboard.Count - 8);
78+
}
79+
80+
private string ConvertScoreboardToXml(string path)
81+
{
82+
if (!_initiated)
83+
LoadScoreboardFromXml(path);
84+
85+
XElement xmlScoreboard = new XElement("PAGE");
86+
87+
foreach (var entry in scoreboard)
88+
{
89+
XElement xmlEntry = new XElement("ENTRY",
90+
new XAttribute("player", entry.psnid ?? "Voodooperson05"),
91+
new XAttribute("score", ((float)entry.score).ToString().Replace(",",".")));
92+
93+
xmlScoreboard.Add(xmlEntry);
94+
}
95+
96+
return xmlScoreboard.ToString();
97+
}
98+
99+
public string UpdateScoreboardXml(string apiPath, string gameName)
100+
{
101+
string directoryPath = $"{apiPath}/HOME_LEADERBOARDS/{gameName}";
102+
string filePath = $"{apiPath}/HOME_LEADERBOARDS/{gameName}/leaderboard.xml";
103+
104+
lock (_Lock)
105+
{
106+
Directory.CreateDirectory(directoryPath);
107+
string xmlData = ConvertScoreboardToXml(filePath);
108+
File.WriteAllText(filePath, xmlData);
109+
CustomLogger.LoggerAccessor.LogDebug($"[HOMELEADERBOARDS] - {gameName} - scoreboard XML updated.");
110+
return xmlData;
111+
}
112+
}
113+
}
114+
}

AuxiliaryServices/WebAPIService/JUGGERNAUT/cutteridge/effects2012chances.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public static string ProcessChances(string apiPath)
88
if (File.Exists($"{apiPath}/juggernaut/cutteridge/effects2012chances.xml"))
99
return File.ReadAllText($"{apiPath}/juggernaut/cutteridge/effects2012chances.xml");
1010

11-
return null;
11+
return "<scarecrow>500</scarecrow><girlChance>350</girlChance><doorChance>650</doorChance><kitchenChance>900</kitchenChance>";
1212
}
1313
}
1414
}

AuxiliaryServices/WebAPIService/RCHOME/RCHOMEClass.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using NetworkLibrary.HTTP;
55
using System;
66
using System.Collections.Generic;
7+
using System.Globalization;
78
using System.IO;
89
using WebAPIService.LOOT;
910

@@ -83,9 +84,9 @@ public string ProcessRequest(byte[] PostData = null, string ContentType = null)
8384
{
8485
lock (_leaderboards)
8586
{
86-
if (!string.IsNullOrEmpty(gameName) && _leaderboards.ContainsKey(gameName) && int.TryParse(score, out int intScore))
87+
if (!string.IsNullOrEmpty(gameName) && _leaderboards.ContainsKey(gameName))
8788
{
88-
_leaderboards[gameName].UpdateScoreBoard(player, intScore);
89+
_leaderboards[gameName].UpdateScoreBoard(player, (int)double.Parse(score, CultureInfo.InvariantCulture));
8990
return _leaderboards[gameName].UpdateScoreboardXml(workpath, gameName);
9091
}
9192
}

AuxiliaryServices/WebAPIService/Utils/WebAPIsUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.IO.Compression;
33
using System.IO;
44
using System;
5-
using CastleLibrary.NetHasher.CRC;
5+
using NetHasher.CRC;
66
using System.Text;
77

88
namespace WebAPIService.Utils

AuxiliaryServices/WebAPIService/VEEMEE/gofish/GFScoreBoardData.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ public static void LoadScoreboardFromXml(string path)
4444
string biggestfishweight = playerElement.Element("biggestfishweight")?.Value;
4545
string totalfishweight = playerElement.Element("totalfishweight")?.Value;
4646

47-
float score = 0;
48-
float.TryParse(scoreStr, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out score);
47+
float.TryParse(scoreStr, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out float score);
4948

5049
scoreboard.Add(new ScoreboardEntry
5150
{

AuxiliaryServices/WebAPIService/VEEMEE/olm/olmScoreBoardData.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ public static void LoadScoreboardFromXml(string path)
4141
string scoreStr = playerElement.Element("score")?.Value;
4242
string throws = playerElement.Element("throws")?.Value;
4343

44-
float score = 0;
45-
float.TryParse(scoreStr, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out score);
44+
float.TryParse(scoreStr, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out float score);
4645

4746
scoreboard.Add(new ScoreboardEntry
4847
{

BackendServices/CastleLibrary/NetHasher/CRC/CRC16.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22

3-
namespace CastleLibrary.NetHasher.CRC
3+
namespace NetHasher.CRC
44
{
55
public static class CRC16
66
{

BackendServices/CastleLibrary/NetHasher/CRC/CRC32.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#endif
1010
using EndianTools;
1111

12-
namespace CastleLibrary.NetHasher.CRC
12+
namespace NetHasher.CRC
1313
{
1414
public static class CRC32
1515
{

0 commit comments

Comments
 (0)