|
| 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 | +} |
0 commit comments