utils for JSON files and RNG/rolling, abstracted arg parsing for commands

This commit is contained in:
Kyle Belanger
2024-02-18 23:11:58 -05:00
parent d2552b74b7
commit 7445a1f5d8
9 changed files with 232 additions and 10 deletions

View File

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

View 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}%)");
}
}
}

View File

@@ -16,7 +16,7 @@ namespace BLHX.Server.Game
// Preload
System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(PacketFactory).TypeHandle);
EndPoint = new(IPAddress.Any, 20000);
EndPoint = new(IPAddress.Any, (int)Config.Instance.Port);
listener = new TcpListener(EndPoint);
}

View File

@@ -1,5 +1,6 @@
using BHXY.Server.Common.Proto.p10;
using BLHX.Server.Common.Proto;
using BLHX.Server.Common.Utils;
namespace BLHX.Server.Game.Handlers
{
@@ -11,11 +12,11 @@ namespace BLHX.Server.Game.Handlers
var req = packet.Decode<Cs10800>();
connection.Send(new Sc10801()
{
GatewayIp = "192.168.1.4",
GatewayPort = 20000,
Url = "http://192.168.1.4",
ProxyIp = "192.168.1.4",
ProxyPort = 20000,
GatewayIp = Config.Instance.Address,
GatewayPort = Config.Instance.Port,
Url = "http://" + Config.Instance.Address,
ProxyIp = Config.Instance.Address,
ProxyPort = Config.Instance.Port,
Versions = [
"$azhash$7$1$459$470aa097fec844d6",
"$cvhash$467$98edcdd4e7dac668",
@@ -48,10 +49,10 @@ namespace BLHX.Server.Game.Handlers
{
Ids = [0],
Name = "BLHX.Server",
Ip = "192.168.1.4",
Port = 20000,
ProxyIp = "192.168.1.4",
ProxyPort = 20000
Ip = Config.Instance.Address,
Port = Config.Instance.Port,
ProxyIp = Config.Instance.Address,
ProxyPort = Config.Instance.Port
}
],
ServerTicket = req.Arg3