Skip to content

Commit ccb155f

Browse files
committed
Implement record lookup in StatsCommand
1 parent aaa7cf6 commit ccb155f

2 files changed

Lines changed: 57 additions & 18 deletions

File tree

src/Atlasd/Battlenet/Product.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,28 @@ public static ProductCode FromBytes(byte[] product, bool validityCheck)
3737

3838
if (validityCheck)
3939
{
40-
var codeStr = Encoding.ASCII.GetString(product);
40+
var codeStr = Encoding.UTF8.GetString(product);
4141
if (!IsValid(code)) code = (ProductCode)BitConverter.ToUInt32(product.Reverse().ToArray());
42-
if (!IsValid(code)) code = (ProductCode)BitConverter.ToUInt32(Encoding.ASCII.GetBytes(codeStr.ToUpperInvariant()));
43-
if (!IsValid(code)) code = (ProductCode)BitConverter.ToUInt32(Encoding.ASCII.GetBytes(codeStr.ToUpperInvariant().Reverse().ToArray()));
42+
if (!IsValid(code)) code = (ProductCode)BitConverter.ToUInt32(Encoding.UTF8.GetBytes(codeStr.ToUpperInvariant()));
43+
if (!IsValid(code)) code = (ProductCode)BitConverter.ToUInt32(Encoding.UTF8.GetBytes(codeStr.ToUpperInvariant().Reverse().ToArray()));
44+
if (!IsValid(code)) code = ProductCode.None;
45+
}
46+
47+
return code;
48+
}
49+
50+
public static ProductCode FromString(string product, bool validityCheck)
51+
{
52+
if (product.Length != 4)
53+
throw new ArgumentException($"Cannot convert string to product, expected 4 characters, got {product.Length}");
54+
55+
ProductCode code = (ProductCode)BitConverter.ToUInt32(Encoding.UTF8.GetBytes(product)[0..4]);
56+
57+
if (validityCheck)
58+
{
59+
if (!IsValid(code)) code = (ProductCode)BitConverter.ToUInt32(Encoding.UTF8.GetBytes(product.Reverse().ToString())[0..4]);
60+
if (!IsValid(code)) code = (ProductCode)BitConverter.ToUInt32(Encoding.UTF8.GetBytes(product.ToUpperInvariant())[0..4]);
61+
if (!IsValid(code)) code = (ProductCode)BitConverter.ToUInt32(Encoding.UTF8.GetBytes(product.ToUpperInvariant().Reverse().ToString())[0..4]);
4462
if (!IsValid(code)) code = ProductCode.None;
4563
}
4664

src/Atlasd/Battlenet/Protocols/Game/ChatCommands/StatsCommand.cs

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using System.Collections.Generic;
44
using System.Globalization;
5+
using System.Linq;
56
using System.Text;
67

78
namespace Atlasd.Battlenet.Protocols.Game.ChatCommands
@@ -21,7 +22,8 @@ public override void Invoke(ChatCommandContext context)
2122

2223
if (Arguments.Count < 1)
2324
{
24-
new ChatEvent(ChatEvent.EventIds.EID_ERROR, gs.ChannelFlags, gs.Client.RemoteIPAddress, gs.Ping, gs.OnlineName, Resources.StatsCommandInvalid).WriteTo(gs.Client);
25+
foreach (var line in Resources.StatsCommandInvalid.Split(Battlenet.Common.NewLine))
26+
new ChatEvent(ChatEvent.EventIds.EID_ERROR, gs.ChannelFlags, gs.Client.RemoteIPAddress, gs.Ping, gs.OnlineName, line).WriteTo(gs.Client);
2527
return;
2628
}
2729

@@ -31,21 +33,25 @@ public override void Invoke(ChatCommandContext context)
3133
RawBuffer = RawBuffer[(Encoding.UTF8.GetByteCount(targetStr) + (Arguments.Count > 0 ? 1 : 0))..];
3234

3335
Product.ProductCode code = Product.ProductCode.None;
36+
string codeStr = string.Empty;
37+
3438
if (Arguments.Count == 0)
3539
{
3640
code = gs.Product;
3741
}
38-
else if (Arguments.Count > 0)
42+
else
3943
{
40-
var codeStr = Arguments[0];
44+
codeStr = Arguments[0];
4145
Arguments.RemoveAt(0);
4246
// Calculates and removes (codeStr+' ') from (raw) which prints into (newRaw):
4347
RawBuffer = RawBuffer[(Encoding.UTF8.GetByteCount(codeStr) + (Arguments.Count > 0 ? 1 : 0))..];
4448

45-
while (codeStr.Length < 4) codeStr += 0x00;
49+
while (codeStr.Length < 4) codeStr += (char)0x00;
4650
code = Product.FromBytes(Encoding.UTF8.GetBytes(codeStr[0..Math.Min(4, codeStr.Length)]), true);
4751
}
52+
codeStr = Encoding.UTF8.GetString(BitConverter.GetBytes((uint)code).Reverse().ToArray());
4853

54+
// Verify product code is a record-keeping type
4955
bool valid = code switch
5056
{
5157
Product.ProductCode.StarcraftOriginal => true,
@@ -55,10 +61,10 @@ public override void Invoke(ChatCommandContext context)
5561
Product.ProductCode.WarcraftIIIFrozenThrone => true,
5662
_ => false,
5763
};
58-
5964
if (!valid)
6065
{
61-
new ChatEvent(ChatEvent.EventIds.EID_ERROR, gs.ChannelFlags, gs.Client.RemoteIPAddress, gs.Ping, gs.OnlineName, Resources.StatsCommandInvalid).WriteTo(gs.Client);
66+
foreach (var line in Resources.StatsCommandInvalid.Split(Battlenet.Common.NewLine))
67+
new ChatEvent(ChatEvent.EventIds.EID_ERROR, gs.ChannelFlags, gs.Client.RemoteIPAddress, gs.Ping, gs.OnlineName, line).WriteTo(gs.Client);
6268
return;
6369
}
6470

@@ -67,16 +73,31 @@ public override void Invoke(ChatCommandContext context)
6773
buffer += Battlenet.Common.NewLine + Resources.StatsCommandLadder;
6874
if (code == Product.ProductCode.WarcraftIIBNE) buffer += Battlenet.Common.NewLine + Resources.StatsCommandIronMan;
6975

70-
buffer = buffer.Replace("{ironManDraws}", "0", true, CultureInfo.InvariantCulture);
71-
buffer = buffer.Replace("{ironManLosses}", "0", true, CultureInfo.InvariantCulture);
72-
buffer = buffer.Replace("{ironManWins}", "0", true, CultureInfo.InvariantCulture);
73-
buffer = buffer.Replace("{ladderDraws}", "0", true, CultureInfo.InvariantCulture);
74-
buffer = buffer.Replace("{ladderLosses}", "0", true, CultureInfo.InvariantCulture);
75-
buffer = buffer.Replace("{ladderWins}", "0", true, CultureInfo.InvariantCulture);
76-
buffer = buffer.Replace("{normalDraws}", "0", true, CultureInfo.InvariantCulture);
77-
buffer = buffer.Replace("{normalLosses}", "0", true, CultureInfo.InvariantCulture);
78-
buffer = buffer.Replace("{normalWins}", "0", true, CultureInfo.InvariantCulture);
76+
int normal = 0;
77+
int ladder = 1;
78+
int ironMan = 3;
79+
80+
int normalWins = context.GameState.ActiveAccount.Get($"record\\{codeStr}\\{normal}\\wins", 0);
81+
int normalLosses = context.GameState.ActiveAccount.Get($"record\\{codeStr}\\{normal}\\losses", 0);
82+
int normalDraws = context.GameState.ActiveAccount.Get($"record\\{codeStr}\\{normal}\\disconnects", 0);
83+
int ladderWins = context.GameState.ActiveAccount.Get($"record\\{codeStr}\\{ladder}\\wins", 0);
84+
int ladderLosses = context.GameState.ActiveAccount.Get($"record\\{codeStr}\\{ladder}\\losses", 0);
85+
int ladderDraws = context.GameState.ActiveAccount.Get($"record\\{codeStr}\\{ladder}\\disconnects", 0);
86+
int ironManWins = context.GameState.ActiveAccount.Get($"record\\{codeStr}\\{ironMan}\\wins", 0);
87+
int ironManLosses = context.GameState.ActiveAccount.Get($"record\\{codeStr}\\{ironMan}\\losses", 0);
88+
int ironManDraws = context.GameState.ActiveAccount.Get($"record\\{codeStr}\\{ironMan}\\disconnects", 0);
89+
90+
buffer = buffer.Replace("{ironManDraws}", $"{ironManDraws}", true, CultureInfo.InvariantCulture);
91+
buffer = buffer.Replace("{ironManLosses}", $"{ironManLosses}", true, CultureInfo.InvariantCulture);
92+
buffer = buffer.Replace("{ironManWins}", $"{ironManWins}", true, CultureInfo.InvariantCulture);
93+
buffer = buffer.Replace("{ladderDraws}", $"{ladderDraws}", true, CultureInfo.InvariantCulture);
94+
buffer = buffer.Replace("{ladderLosses}", $"{ladderLosses}", true, CultureInfo.InvariantCulture);
95+
buffer = buffer.Replace("{ladderWins}", $"{ladderWins}", true, CultureInfo.InvariantCulture);
96+
buffer = buffer.Replace("{normalDraws}", $"{normalDraws}", true, CultureInfo.InvariantCulture);
97+
buffer = buffer.Replace("{normalLosses}", $"{normalLosses}", true, CultureInfo.InvariantCulture);
98+
buffer = buffer.Replace("{normalWins}", $"{normalWins}", true, CultureInfo.InvariantCulture);
7999
buffer = buffer.Replace("{user}", targetStr, true, CultureInfo.InvariantCulture);
100+
buffer = buffer.Replace("{userName}", targetStr, true, CultureInfo.InvariantCulture);
80101

81102
foreach (var line in buffer.Split(Battlenet.Common.NewLine))
82103
new ChatEvent(ChatEvent.EventIds.EID_INFO, gs.ChannelFlags, gs.Client.RemoteIPAddress, gs.Ping, gs.OnlineName, line).WriteTo(gs.Client);

0 commit comments

Comments
 (0)