summaryrefslogtreecommitdiff
path: root/Impostor-dev/src/Impostor.Api/Games/GameJoinResult.cs
blob: b33a2b669591a0bda8155f3567c4523a466fde57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System;
using System.Diagnostics.CodeAnalysis;
using Impostor.Api.Net;

namespace Impostor.Api.Games
{
    public readonly struct GameJoinResult
    {
        private GameJoinResult(GameJoinError error, string? message = null, IClientPlayer? player = null)
        {
            Error = error;
            Message = message;
            Player = player;
        }

        public GameJoinError Error { get; }

        public bool IsSuccess => Error == GameJoinError.None;

        public bool IsCustomError => Error == GameJoinError.Custom;

        [MemberNotNullWhen(true, nameof(IsCustomError))]
        public string? Message { get; }

        [MemberNotNullWhen(true, nameof(IsSuccess))]
        public IClientPlayer? Player { get; }

        public static GameJoinResult CreateCustomError(string message)
        {
            return new GameJoinResult(GameJoinError.Custom, message);
        }

        public static GameJoinResult CreateSuccess(IClientPlayer player)
        {
            return new GameJoinResult(GameJoinError.None, player: player);
        }

        public static GameJoinResult FromError(GameJoinError error)
        {
            if (error == GameJoinError.Custom)
            {
                throw new InvalidOperationException($"Custom errors should provide a message, use {nameof(CreateCustomError)} instead.");
            }

            return new GameJoinResult(error);
        }
    }
}