mirror of
https://github.com/rafi1212122/BLHX.Server.git
synced 2025-12-13 06:54:51 +01:00
utils for JSON files and RNG/rolling, abstracted arg parsing for commands
This commit is contained in:
@@ -16,6 +16,7 @@ public class commandHandler : Attribute
|
||||
Description = description;
|
||||
Example = example;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[AttributeUsage(AttributeTargets.Property)]
|
||||
@@ -48,6 +49,22 @@ public abstract class Command
|
||||
prop.SetValue(this, arg.Value);
|
||||
}
|
||||
}
|
||||
|
||||
protected T Parse<T>(string? value, T fallback = default)
|
||||
{
|
||||
var tryParseMethod = typeof(T).GetMethod("TryParse", [typeof(string), typeof(T).MakeByRefType()]);
|
||||
|
||||
if (tryParseMethod != null)
|
||||
{
|
||||
var parameters = new object[] { value, null };
|
||||
bool success = (bool)tryParseMethod.Invoke(null, parameters);
|
||||
|
||||
if (success)
|
||||
return (T)parameters[1];
|
||||
}
|
||||
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
|
||||
public static class CommandHandler
|
||||
|
||||
62
BLHX.Server.Game/Commands/TestCommand.cs
Normal file
62
BLHX.Server.Game/Commands/TestCommand.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using BLHX.Server.Common.Utils;
|
||||
|
||||
namespace BLHX.Server.Game.Commands;
|
||||
|
||||
[commandHandler("test", "Test command", "test type=gacha")]
|
||||
public class TestCommand : Command
|
||||
{
|
||||
static readonly string[] RarityStrings = { "Unknown", "Unused", "Normal", "Rare", "Elite", "SSR", "UR" };
|
||||
|
||||
[Argument("type")]
|
||||
public string? Type { get; set; }
|
||||
|
||||
[Argument("count")]
|
||||
public string? Count { get; set; }
|
||||
|
||||
[Argument("verbose")]
|
||||
public string? Verbose { get; set; }
|
||||
|
||||
public override void Execute(Dictionary<string, string> args)
|
||||
{
|
||||
base.Execute(args);
|
||||
|
||||
switch (Type)
|
||||
{
|
||||
case "gacha":
|
||||
TestGacha(Parse(Count, 1000000), Parse(Verbose, false));
|
||||
break;
|
||||
default:
|
||||
Logger.c.Warn("Unknown test type");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void TestGacha(int count, bool verbose)
|
||||
{
|
||||
var stopwatch = System.Diagnostics.Stopwatch.StartNew();
|
||||
var counts = new int[7];
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
int rarity = RNG.NextShipRarity();
|
||||
|
||||
counts[rarity]++;
|
||||
|
||||
if (verbose)
|
||||
Logger.c.Log($"Roll {i + 1}: {rarity} - {RarityStrings[rarity]}");
|
||||
}
|
||||
|
||||
stopwatch.Stop();
|
||||
|
||||
Logger.c.Log("----------------------------------------");
|
||||
Logger.c.Log($"TOTAL ROLLS: {count}");
|
||||
Logger.c.Log($"PROCESSING TIME: {stopwatch.Elapsed}");
|
||||
|
||||
for (int i = 2; i < counts.Length; i++)
|
||||
{
|
||||
double percentage = (double)Math.Round(counts[i] / (double)count * 100, 2);
|
||||
|
||||
Logger.c.Log($"{RarityStrings[i]}: {counts[i]} ({percentage}%)");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user