Skip to content

Commit aaa7cf6

Browse files
committed
Add StatsCommand
1 parent 8891338 commit aaa7cf6

File tree

4 files changed

+161
-0
lines changed

4 files changed

+161
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ private static ChatCommand Parse(string text, byte[] raw)
8686
case "rejoin":
8787
case "rj":
8888
return new ReJoinCommand(_raw, args);
89+
case "stats":
90+
return new StatsCommand(_raw, args);
8991
case "time":
9092
return new TimeCommand(_raw, args);
9193
case "unban":
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using Atlasd.Localization;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Globalization;
5+
using System.Text;
6+
7+
namespace Atlasd.Battlenet.Protocols.Game.ChatCommands
8+
{
9+
class StatsCommand : ChatCommand
10+
{
11+
public StatsCommand(byte[] rawBuffer, List<string> arguments) : base(rawBuffer, arguments) { }
12+
13+
public override bool CanInvoke(ChatCommandContext context)
14+
{
15+
return context != null && context.GameState != null && context.GameState.ActiveAccount != null;
16+
}
17+
18+
public override void Invoke(ChatCommandContext context)
19+
{
20+
var gs = context.GameState;
21+
22+
if (Arguments.Count < 1)
23+
{
24+
new ChatEvent(ChatEvent.EventIds.EID_ERROR, gs.ChannelFlags, gs.Client.RemoteIPAddress, gs.Ping, gs.OnlineName, Resources.StatsCommandInvalid).WriteTo(gs.Client);
25+
return;
26+
}
27+
28+
var targetStr = Arguments[0];
29+
Arguments.RemoveAt(0);
30+
// Calculates and removes (targetStr+' ') from (raw) which prints into (newRaw):
31+
RawBuffer = RawBuffer[(Encoding.UTF8.GetByteCount(targetStr) + (Arguments.Count > 0 ? 1 : 0))..];
32+
33+
Product.ProductCode code = Product.ProductCode.None;
34+
if (Arguments.Count == 0)
35+
{
36+
code = gs.Product;
37+
}
38+
else if (Arguments.Count > 0)
39+
{
40+
var codeStr = Arguments[0];
41+
Arguments.RemoveAt(0);
42+
// Calculates and removes (codeStr+' ') from (raw) which prints into (newRaw):
43+
RawBuffer = RawBuffer[(Encoding.UTF8.GetByteCount(codeStr) + (Arguments.Count > 0 ? 1 : 0))..];
44+
45+
while (codeStr.Length < 4) codeStr += 0x00;
46+
code = Product.FromBytes(Encoding.UTF8.GetBytes(codeStr[0..Math.Min(4, codeStr.Length)]), true);
47+
}
48+
49+
bool valid = code switch
50+
{
51+
Product.ProductCode.StarcraftOriginal => true,
52+
Product.ProductCode.StarcraftBroodwar => true,
53+
Product.ProductCode.WarcraftIIBNE => true,
54+
Product.ProductCode.WarcraftIIIReignOfChaos => true,
55+
Product.ProductCode.WarcraftIIIFrozenThrone => true,
56+
_ => false,
57+
};
58+
59+
if (!valid)
60+
{
61+
new ChatEvent(ChatEvent.EventIds.EID_ERROR, gs.ChannelFlags, gs.Client.RemoteIPAddress, gs.Ping, gs.OnlineName, Resources.StatsCommandInvalid).WriteTo(gs.Client);
62+
return;
63+
}
64+
65+
var buffer = Resources.StatsCommand;
66+
buffer += Battlenet.Common.NewLine + Resources.StatsCommandNormal;
67+
buffer += Battlenet.Common.NewLine + Resources.StatsCommandLadder;
68+
if (code == Product.ProductCode.WarcraftIIBNE) buffer += Battlenet.Common.NewLine + Resources.StatsCommandIronMan;
69+
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);
79+
buffer = buffer.Replace("{user}", targetStr, true, CultureInfo.InvariantCulture);
80+
81+
foreach (var line in buffer.Split(Battlenet.Common.NewLine))
82+
new ChatEvent(ChatEvent.EventIds.EID_INFO, gs.ChannelFlags, gs.Client.RemoteIPAddress, gs.Ping, gs.OnlineName, line).WriteTo(gs.Client);
83+
}
84+
}
85+
}

src/Atlasd/Localization/Resources.Designer.cs

Lines changed: 52 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Atlasd/Localization/Resources.resx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,28 @@ Displays the current {realm} Time ({realmTimezone}).</value>
422422
<data name="ServerStatistics" xml:space="preserve">
423423
<value>There are currently {gameUsers} users playing {gameAds} games of {game}, and {totalUsers} users playing {totalGameAds} games on {realm}.</value>
424424
</data>
425+
<data name="StatsCommand" xml:space="preserve">
426+
<value>{user}'s record:</value>
427+
</data>
428+
<data name="StatsCommandInvalid" xml:space="preserve">
429+
<value>You must supply a user name and a valid program id.
430+
Valid program ids are:
431+
STAR - StarCraft
432+
SEXP - StarCraft Broodwar
433+
W2BN - WarCraft II Battle.net Edittion
434+
WAR3 - WarCraft III
435+
W3XP - WarCraft III The Frozen Throne
436+
Example: /stats joe STAR</value>
437+
</data>
438+
<data name="StatsCommandIronMan" xml:space="preserve">
439+
<value>IronMan games: {ironManWins}-{ironManLosses}-{ironManDraws}</value>
440+
</data>
441+
<data name="StatsCommandLadder" xml:space="preserve">
442+
<value>Ladder games: {ladderWins}-{ladderLosses}-{ladderDraws}</value>
443+
</data>
444+
<data name="StatsCommandNormal" xml:space="preserve">
445+
<value>Normal games: {normalWins}-{normalLosses}-{normalDraws}</value>
446+
</data>
425447
<data name="TheVoid" xml:space="preserve">
426448
<value>The Void</value>
427449
</data>

0 commit comments

Comments
 (0)