diff options
Diffstat (limited to 'Impostor-dev/src/Impostor.Api/Games/GameCode.cs')
-rw-r--r-- | Impostor-dev/src/Impostor.Api/Games/GameCode.cs | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/Impostor-dev/src/Impostor.Api/Games/GameCode.cs b/Impostor-dev/src/Impostor.Api/Games/GameCode.cs new file mode 100644 index 0000000..2c30e7e --- /dev/null +++ b/Impostor-dev/src/Impostor.Api/Games/GameCode.cs @@ -0,0 +1,74 @@ +using System; +using Impostor.Api.Innersloth; + +namespace Impostor.Api.Games +{ + public readonly struct GameCode : IEquatable<GameCode> + { + public GameCode(int value) + { + Value = value; + Code = GameCodeParser.IntToGameName(value); + } + + public GameCode(string code) + { + Value = GameCodeParser.GameNameToInt(code); + Code = code; + } + + public string Code { get; } + + public int Value { get; } + + public static implicit operator string(GameCode code) => code.Code; + + public static implicit operator int(GameCode code) => code.Value; + + public static implicit operator GameCode(string code) => From(code); + + public static implicit operator GameCode(int value) => From(value); + + public static bool operator ==(GameCode left, GameCode right) + { + return left.Equals(right); + } + + public static bool operator !=(GameCode left, GameCode right) + { + return !left.Equals(right); + } + + public static GameCode Create() + { + return new GameCode(GameCodeParser.GenerateCode(6)); + } + + public static GameCode From(int value) => new GameCode(value); + + public static GameCode From(string value) => new GameCode(value); + + /// <inheritdoc/> + public bool Equals(GameCode other) + { + return Code == other.Code && Value == other.Value; + } + + /// <inheritdoc/> + public override bool Equals(object? obj) + { + return obj is GameCode other && Equals(other); + } + + /// <inheritdoc/> + public override int GetHashCode() + { + return HashCode.Combine(Code, Value); + } + + public override string ToString() + { + return Code; + } + } +}
\ No newline at end of file |