From 62abc6a784245d6215bc5249e5bbc6950c790251 Mon Sep 17 00:00:00 2001 From: rafi1212122 Date: Tue, 30 May 2023 19:58:15 +0700 Subject: [PATCH] yes command yesyes --- Common/Utils/Logger.cs | 12 +++-- GameServer/Commands/Command.cs | 56 +++++++++++++++++++++-- GameServer/Commands/HelpCommand.cs | 24 ++++++++++ GameServer/Commands/ReadLine.cs | 53 +++++++++++++++++++++ GameServer/Commands/TestCommand.cs | 8 ++-- GameServer/Handlers/StageEndReqHandler.cs | 1 - Program.cs | 20 +------- 7 files changed, 142 insertions(+), 32 deletions(-) create mode 100644 GameServer/Commands/HelpCommand.cs create mode 100644 GameServer/Commands/ReadLine.cs diff --git a/Common/Utils/Logger.cs b/Common/Utils/Logger.cs index 536f776..1665827 100644 --- a/Common/Utils/Logger.cs +++ b/Common/Utils/Logger.cs @@ -5,12 +5,14 @@ namespace Common.Utils public class Logger { private readonly string _name; + private readonly bool TraceOnError; private readonly ConsoleColor _color; - public Logger(string name, ConsoleColor color = ConsoleColor.Cyan) + public Logger(string name, ConsoleColor color = ConsoleColor.Cyan, bool traceOnError = true) { _name = name; _color = color; + TraceOnError = traceOnError; } public void Log(params string[] message) @@ -44,7 +46,7 @@ namespace Common.Utils public void Trail(params string[] msg) { Console.ForegroundColor = ConsoleColor.DarkGray; - Console.WriteLine($"\t→ {string.Join(' ', msg)}"); + Console.WriteLine($"\t└── {string.Join(' ', msg)}"); Console.ResetColor(); } @@ -59,12 +61,14 @@ namespace Common.Utils Console.ResetColor(); Console.Write("> "); Console.ForegroundColor = ConsoleColor.White; - Console.BackgroundColor = ConsoleColor.DarkRed; + if(TraceOnError) + Console.BackgroundColor = ConsoleColor.DarkRed; Console.WriteLine(string.Join("\t", message)); Console.ResetColor(); #if DEBUG StackTrace trace = new(true); - Trail(trace.ToString()); + if(TraceOnError) + Trail(trace.ToString()); #endif } diff --git a/GameServer/Commands/Command.cs b/GameServer/Commands/Command.cs index 6f36f76..9abc26e 100644 --- a/GameServer/Commands/Command.cs +++ b/GameServer/Commands/Command.cs @@ -1,26 +1,74 @@ using Common.Utils; using System.Reflection; +using PemukulPaku.GameServer.Game; namespace PemukulPaku.GameServer.Commands { + /// + /// What methods should be overriden based on CommandType: + /// All, Handler should override all virtual methods in Command. + /// Console, Handler should override Run method with string[] args. + /// Player, Handler should override Run method with Player / Session args. + /// public abstract class Command { - public static readonly Logger c = new("Command", ConsoleColor.Cyan); + public static readonly Logger c = new("Command", ConsoleColor.Cyan, false); public string Name = string.Empty; + public string Description = string.Empty; + public CommandType CmdType = CommandType.Player; - public virtual void Run(Session? session, string[] args) + /// + /// Call this when player is online or have active session. + /// + /// The Session instance + /// + public virtual void Run(Session session, string[] args) { + throw new NotImplementedException(); + } + + /// + /// Only call this when player have no session. + /// + /// The Player instance + /// + public virtual void Run(Player player, string[] args) + { + throw new NotImplementedException(); + } + + /// + /// Please only call this on ReadLine. + /// + /// + public virtual void Run(string[] args) + { + throw new NotImplementedException(); } } + /// + /// Command types used to defines what action should be taken by command interpreter or handler. + /// + public enum CommandType + { + All, + Console, + Player + }; + [AttributeUsage(AttributeTargets.Class)] public class CommandHandler : Attribute { public string Name { get; } + public string Description { get; } + public CommandType CmdType { get; } - public CommandHandler(string name) + public CommandHandler(string name, string description, CommandType type = CommandType.Player) { Name = name; + Description = description; + CmdType = type; } } @@ -47,6 +95,8 @@ namespace PemukulPaku.GameServer.Commands if (cmd is not null) { cmd.Name = attr.Name; + cmd.Description = attr.Description; + cmd.CmdType = attr.CmdType; Commands.Add(cmd); #if DEBUG diff --git a/GameServer/Commands/HelpCommand.cs b/GameServer/Commands/HelpCommand.cs new file mode 100644 index 0000000..bf23d53 --- /dev/null +++ b/GameServer/Commands/HelpCommand.cs @@ -0,0 +1,24 @@ +namespace PemukulPaku.GameServer.Commands +{ + + [CommandHandler("help", "Shows the help page", CommandType.All)] + internal class HelpCommand : Command + { + public override void Run(Session session, string[] args) + { + // TODO: Implement online help + base.Run(session, args); + } + + public override void Run(string[] args) + { + foreach (Command Cmd in CommandFactory.Commands) + { + Console.ForegroundColor= ConsoleColor.White; + Console.WriteLine(" " + Cmd.Name); + Console.ResetColor(); + c.Trail(Cmd.Description); + } + } + } +} diff --git a/GameServer/Commands/ReadLine.cs b/GameServer/Commands/ReadLine.cs new file mode 100644 index 0000000..e1b0309 --- /dev/null +++ b/GameServer/Commands/ReadLine.cs @@ -0,0 +1,53 @@ +namespace PemukulPaku.GameServer.Commands +{ + public class ReadLine + { + public static ReadLine? Instance { get; private set; } + public Session? session = null; + + public static ReadLine GetInstance() + { + return Instance ??= new(); + } + + public void Start() + { + while (true) + { + string? line = Console.ReadLine(); + + if (!string.IsNullOrEmpty(line)) + { + List args = line.Split(' ').ToList(); + Command? Cmd = CommandFactory.Commands.Find(cmd => args[0] == cmd.Name.ToLower()); + + if(Cmd != null) + { + args.RemoveAt(0); + + if (Cmd.CmdType == CommandType.All) + { + Cmd.Run(args.ToArray()); + } + else if(session != null) + { + + Command.c.Log("Command executed"); + } + else + { + Command.c.Error("Invalid usage, try selecting session first with target"); + continue; + } + + continue; + } + else + { + Command.c.Error("Command not found, try using help"); + } + } + } + } + } +} diff --git a/GameServer/Commands/TestCommand.cs b/GameServer/Commands/TestCommand.cs index 143419a..90a5f56 100644 --- a/GameServer/Commands/TestCommand.cs +++ b/GameServer/Commands/TestCommand.cs @@ -1,11 +1,9 @@ -using Common; - -namespace PemukulPaku.GameServer.Commands +namespace PemukulPaku.GameServer.Commands { - [CommandHandler("test")] + [CommandHandler("test", "Test command used for testing")] public class TestCommand : Command { - public override void Run(Session? session, string[] args) + public override void Run(Session session, string[] args) { if (args.Length < 3) { diff --git a/GameServer/Handlers/StageEndReqHandler.cs b/GameServer/Handlers/StageEndReqHandler.cs index be8627f..e78cdc0 100644 --- a/GameServer/Handlers/StageEndReqHandler.cs +++ b/GameServer/Handlers/StageEndReqHandler.cs @@ -47,7 +47,6 @@ namespace PemukulPaku.GameServer.Handlers session.ProcessPacket(Packet.FromProto(new GetMainDataReq() { }, CmdId.GetMainDataReq)); session.ProcessPacket(Packet.FromProto(new GetEquipmentDataReq() { }, CmdId.GetEquipmentDataReq)); session.ProcessPacket(Packet.FromProto(new GetWorldMapDataReq() { }, CmdId.GetWorldMapDataReq)); - session.ProcessPacket(Packet.FromProto(new ChapterGroupGetDataReqHandler() { }, CmdId.ChapterGroupGetDataReq)); session.ProcessPacket(Packet.FromProto(new GetStageDataReq() { }, CmdId.GetStageDataReq)); Rsp.PlayerExpReward = 100; diff --git a/Program.cs b/Program.cs index 330ffe8..323c4b0 100644 --- a/Program.cs +++ b/Program.cs @@ -25,25 +25,7 @@ namespace PemukulPaku Player Player = new(User.FromName("test")); - while (true) - { - string? line = Console.ReadLine(); - - if (!string.IsNullOrEmpty(line)) - { - foreach (Command cmd in CommandFactory.Commands) - { - if (line.StartsWith(cmd.Name.ToLower())) - { - List args = line.Split(' ').ToList(); - args.RemoveAt(0); - - cmd.Run(null, args.ToArray()); - break; - } - } - } - } + ReadLine.GetInstance().Start(); } } } \ No newline at end of file