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

@@ -0,0 +1,25 @@
namespace BLHX.Server.Common.Utils;
public class Config : Singleton<Config>
{
public string Address { get; set; } = "192.168.1.4";
public uint Port { get; set; } = 20000;
public static void Load()
{
Instance = JSON.Load<Config>(JSON.ConfigPath);
#if DEBUG
Logger.c.Log($"Loaded Config:\n{JSON.Stringify(Instance)}");
#endif
}
public static void Save()
{
JSON.Save(JSON.ConfigPath, Instance);
#if DEBUG
Logger.c.Log("Saved Config");
#endif
}
}

View File

@@ -0,0 +1,35 @@
using System.Text.Json;
namespace BLHX.Server.Common.Utils;
public static class JSON
{
public static string ConfigPath => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config.json");
public static T Load<T>(string path) where T : new()
{
if (!File.Exists(path))
{
T obj = new T();
Save(path, obj);
}
return JsonSerializer.Deserialize<T>(File.ReadAllText(path));
}
public static void Save<T>(string path, T obj)
{
File.WriteAllText(path, JsonSerializer.Serialize(obj, new JsonSerializerOptions()
{
WriteIndented = true
}));
}
public static string Stringify<T>(T obj)
{
return JsonSerializer.Serialize(obj, new JsonSerializerOptions()
{
WriteIndented = true
});
}
}

View File

@@ -0,0 +1,63 @@
namespace BLHX.Server.Common.Utils;
public static class RNG
{
public static readonly SortedDictionary<int, float> ShipRarityRates = new()
{
{6, 1.2f}, // UR
{5, 7f}, // SSR
{4, 12f}, // Elite
{2, 28.8f}, // Normal
{3, 51f}, // Rare
};
static readonly Random random = new Random((int)DateTime.Now.Ticks);
public static int Next(int min, int max)
=> random.Next(min, max);
public static int Next(int max)
=> random.Next(max);
public static float NextFloat(float min, float max)
{
double range = (double)max - min;
double sample = random.NextDouble();
double scaled = (sample * range) + min;
return (float)scaled;
}
public static float NextFloat(float max)
=> NextFloat(0f, max);
public static bool NextBool()
=> random.Next(2) == 0;
public static float NextRoll()
=> NextFloat(100f);
public static T NextFromList<T>(IList<T> list)
=> list[random.Next(list.Count)];
public static U NextFromDict<T, U>(IDictionary<T, U> dict)
=> dict.ElementAt(random.Next(dict.Count)).Value;
public static int NextFromRarityDict(SortedDictionary<int, float> dict)
{
float roll = NextRoll();
float sum = 0f;
foreach (var pair in dict)
{
sum += pair.Value;
if (roll <= sum)
return pair.Key;
}
throw new Exception("NextFromRarityDict() roll failed");
}
public static int NextShipRarity()
=> NextFromRarityDict(ShipRarityRates);
}

View File

@@ -0,0 +1,17 @@
namespace BLHX.Server.Common.Utils;
public abstract class Singleton<T> where T : new()
{
static T instance;
public static T Instance
{
get
{
if (instance == null)
instance = new T();
return instance;
}
set => instance = value;
}
}