Further work on static side

This commit is contained in:
2024-07-20 16:12:39 +02:00
parent 3845f91601
commit 1b501dee7e
29 changed files with 670 additions and 155 deletions

View File

@@ -2,8 +2,12 @@ using FluentResults;
namespace PkmnLib.Static.Utils.Errors;
/// <summary>
/// A result that indicates an index is out of range.
/// </summary>
public class OutOfRange : Error
{
/// <inheritdoc cref="OutOfRange"/>
public OutOfRange(string hint, int index, int max)
: base($"{hint} index {index} is out of range. Must be between 0 and {max - 1}.")
{

View File

@@ -0,0 +1,12 @@
namespace PkmnLib.Static.Utils;
/// <summary>
/// Indicates that a value has a name.
/// </summary>
public interface INamedValue
{
/// <summary>
/// The name of the value.
/// </summary>
StringKey Name { get; }
}

View File

@@ -1,43 +1,95 @@
namespace PkmnLib.Static.Utils;
/// <summary>
/// Wrapper interface for getting random numbers.
/// </summary>
public interface IRandom
{
/// <summary>
/// Get a random integer between min and max.
/// </summary>
/// <param name="min">The minimum value (inclusive).</param>
/// <param name="max">The maximum value (exclusive).</param>
/// <returns>A random integer that is greater than or equal to min and less than max.</returns>
public int GetInt(int min, int max);
/// <summary>
/// Get a random integer between 0 and max.
/// </summary>
/// <param name="max">The maximum value (exclusive).</param>
/// <returns>A random integer that is greater than or equal to 0 and less than max.</returns>
public int GetInt(int max);
/// <summary>
/// Get a random integer between 0 and <see cref="int.MaxValue"/>.
/// </summary>
/// <returns>A random integer that is greater than or equal to 0 and less than <see cref="int.MaxValue"/>.</returns>
public int GetInt();
/// <summary>
/// Get a random float that is greater than or equal to 0.0 and less than 1.0.
/// </summary>
/// <returns>A random float that is greater than or equal to 0.0 and less than 1.0.</returns>
public float GetFloat();
/// <summary>
/// Get a random float that is greater than or equal to min and less than max.
/// </summary>
/// <param name="min">The minimum value (inclusive).</param>
/// <param name="max">The maximum value (exclusive).</param>
/// <returns>A random float that is greater than or equal to min and less than max.</returns>
public float GetFloat(float min, float max);
/// <summary>
/// Get a random boolean. 50% chance of being true.
/// </summary>
public bool GetBool();
}
/// <inheritdoc />
public class RandomImpl : IRandom
{
private Random _random;
private readonly Random _random;
/// <summary>
/// Creates a new instance that uses the given <see cref="Random"/> instance as its source of randomness.
/// </summary>
public RandomImpl(Random random)
{
_random = random;
}
/// <summary>
/// Creates a new instance that uses the given seed to create a new <see cref="Random"/> instance.
/// </summary>
public RandomImpl(int seed)
{
_random = new Random(seed);
}
/// <summary>
/// Creates a new instance that uses a new <see cref="Random"/> instance.
/// </summary>
public RandomImpl()
{
_random = new Random();
}
/// <inheritdoc />
public int GetInt(int min, int max) => _random.Next(min, max);
/// <inheritdoc />
public int GetInt(int max) => _random.Next(max);
/// <inheritdoc />
public int GetInt() => _random.Next();
/// <inheritdoc />
public float GetFloat() => (float)_random.NextDouble();
/// <inheritdoc />
public float GetFloat(float min, float max) => (float)(_random.NextDouble() * (max - min) + min);
/// <inheritdoc />
public bool GetBool() => _random.Next(2) == 1;
}

View File

@@ -1,5 +1,3 @@
using System;
namespace PkmnLib.Static.Utils;
/// <summary>
@@ -12,24 +10,28 @@ public readonly record struct StringKey
{
private readonly string _key;
/// <inheritdoc cref="StringKey"/>
public StringKey(string key)
{
_key = key;
}
/// <summary>
/// Converts a <see cref="StringKey"/> to a <see cref="string"/>.
/// </summary>
public static implicit operator string(StringKey key) => key._key;
/// <summary>
/// Converts a <see cref="string"/> to a <see cref="StringKey"/>.
/// </summary>
public static implicit operator StringKey(string key) => new(key);
/// <inheritdoc cref="string.ToString()"/>
public override string ToString() => _key.ToLowerInvariant();
public bool Equals(StringKey other)
{
return string.Equals(_key, other._key, StringComparison.InvariantCultureIgnoreCase);
}
public override int GetHashCode()
{
return StringComparer.InvariantCultureIgnoreCase.GetHashCode(_key);
}
/// <inheritdoc />
public bool Equals(StringKey other) => string.Equals(_key, other._key, StringComparison.InvariantCultureIgnoreCase);
/// <inheritdoc />
public override int GetHashCode() => StringComparer.InvariantCultureIgnoreCase.GetHashCode(_key);
}