yes command yesyes

This commit is contained in:
rafi1212122
2023-05-30 19:58:15 +07:00
parent 88c69529fd
commit 62abc6a784
7 changed files with 142 additions and 32 deletions

View File

@@ -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
}

View File

@@ -1,26 +1,74 @@
using Common.Utils;
using System.Reflection;
using PemukulPaku.GameServer.Game;
namespace PemukulPaku.GameServer.Commands
{
/// <summary>
/// What methods should be overriden based on CommandType:
/// <para>All, Handler should override all virtual methods in Command.</para>
/// <para>Console, Handler should override Run method with string[] args.</para>
/// <para>Player, Handler should override Run method with Player / Session args.</para>
/// </summary>
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)
/// <summary>
/// Call this when player is online or have active session.
/// </summary>
/// <param name="session">The Session instance</param>
/// <exception cref="NotImplementedException"></exception>
public virtual void Run(Session session, string[] args)
{
throw new NotImplementedException();
}
/// <summary>
/// Only call this when player have no session.
/// </summary>
/// <param name="player">The Player instance</param>
/// <exception cref="NotImplementedException"></exception>
public virtual void Run(Player player, string[] args)
{
throw new NotImplementedException();
}
/// <summary>
/// Please only call this on ReadLine.
/// </summary>
/// <exception cref="NotImplementedException"></exception>
public virtual void Run(string[] args)
{
throw new NotImplementedException();
}
}
/// <summary>
/// Command types used to defines what action should be taken by command interpreter or handler.
/// </summary>
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

View File

@@ -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);
}
}
}
}

View File

@@ -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<string> 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");
}
}
}
}
}
}

View File

@@ -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)
{

View File

@@ -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;

View File

@@ -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<string> args = line.Split(' ').ToList();
args.RemoveAt(0);
cmd.Run(null, args.ToArray());
break;
}
}
}
}
ReadLine.GetInstance().Start();
}
}
}