mirror of
https://codeberg.org/Ikatono/TierMaker.git
synced 2025-10-28 20:45:35 -05:00
forgot to commit for a while
This commit is contained in:
146
Command.cs
Normal file
146
Command.cs
Normal file
@@ -0,0 +1,146 @@
|
||||
using Godot;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
public partial class Command : GodotObject
|
||||
{
|
||||
public string User;
|
||||
public bool IsStreamer;
|
||||
public bool IsModerator;
|
||||
// public CommandType Type;
|
||||
private string _Args;
|
||||
public CommandArguments GetArgs()
|
||||
=> new(_Args);
|
||||
public Command(string user, bool isStreamer, bool isModerator,
|
||||
// CommandType type,
|
||||
string args)
|
||||
{
|
||||
User = user;
|
||||
IsStreamer = isStreamer;
|
||||
IsModerator = isModerator;
|
||||
// Type = type;
|
||||
_Args = args;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Manages a list of whitespace-delimited arguments. Does not
|
||||
/// respect quotes or escape characters. A single argument containing
|
||||
/// whitespace can be placed at the end and retrieved with <see cref="Remaining"/>
|
||||
/// after the previous arguments are popped.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Use <see cref="DuplicateAtState"/> to safely handle subcommands.
|
||||
/// </remarks>
|
||||
public class CommandArguments
|
||||
{
|
||||
private static readonly Regex _Regex = new(@"\s+");
|
||||
private readonly string OriginalArgs;
|
||||
private string Args;
|
||||
public CommandArguments(string args)
|
||||
{
|
||||
OriginalArgs = args;
|
||||
Args = args;
|
||||
}
|
||||
public string Pop()
|
||||
{
|
||||
var spl = _Regex.Split(Args, 2);
|
||||
Args = spl.ElementAtOrDefault(1) ?? "";
|
||||
return spl[0];
|
||||
}
|
||||
public string Remaining()
|
||||
=> Args;
|
||||
public void Reset()
|
||||
{
|
||||
Args = OriginalArgs;
|
||||
}
|
||||
/// <summary>
|
||||
/// Enumerates arguments from current state, without changing state
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public IEnumerable<string> Enumerate()
|
||||
{
|
||||
var a = DuplicateAtState();
|
||||
while (true)
|
||||
{
|
||||
var s = a.Pop();
|
||||
if (string.IsNullOrWhiteSpace(s))
|
||||
break;
|
||||
yield return s;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Creates a new arguments object that returns to this state when reset.
|
||||
/// Any arguments that have already been popped will not return.
|
||||
/// </summary>
|
||||
public CommandArguments DuplicateAtState()
|
||||
{
|
||||
return new(Args);
|
||||
}
|
||||
}
|
||||
|
||||
public enum CommandType
|
||||
{
|
||||
MoveCard,
|
||||
MoveRow,
|
||||
DeleteCard,
|
||||
DeleteRow,
|
||||
CreateCard,
|
||||
CreateRow,
|
||||
RenameCard,
|
||||
RenameRow,
|
||||
RecolorRow,
|
||||
ChangeCardImage,
|
||||
}
|
||||
|
||||
public static class CommandTypeHelper
|
||||
{
|
||||
public static CommandType? ParseCommand(string commandType)
|
||||
{
|
||||
// if (Enum.TryParse(typeof(CommandType), commandType, true, out object ct))
|
||||
// return (CommandType)ct;
|
||||
// return null;
|
||||
var c = commandType.ToLower();
|
||||
if (c == "movecard")
|
||||
{
|
||||
return CommandType.MoveCard;
|
||||
}
|
||||
else if (c == "moverow")
|
||||
{
|
||||
return CommandType.MoveRow;
|
||||
}
|
||||
else if (c == "deletecard")
|
||||
{
|
||||
return CommandType.DeleteCard;
|
||||
}
|
||||
else if (c == "createcard")
|
||||
{
|
||||
return CommandType.CreateCard;
|
||||
}
|
||||
else if (c == "createrow")
|
||||
{
|
||||
return CommandType.CreateRow;
|
||||
}
|
||||
else if (c == "renamecard")
|
||||
{
|
||||
return CommandType.RenameCard;
|
||||
}
|
||||
else if (c == "renamerow")
|
||||
{
|
||||
return CommandType.RenameRow;
|
||||
}
|
||||
else if (c == "recolorrow")
|
||||
{
|
||||
return CommandType.RecolorRow;
|
||||
}
|
||||
else if (c == "changecardimage")
|
||||
{
|
||||
return CommandType.ChangeCardImage;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user