aboutsummaryrefslogtreecommitdiff
path: root/games/rstnode/rst-core/src/wire/resp.rs
blob: ef2e192a60443106321bf6f068ad88bb0a010eab (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//! Response values that the server can reply with

use super::{Lobby, LobbyId, User, UserId};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

/// A response values from the server
#[derive(Serialize, Deserialize)]
pub enum Response {
    /// Response to the register request
    Register(Result<UserId, RegErr>),
    /// Response to login request
    Login(Result<User, AuthErr>),
    /// Response to login request
    Logout(Result<(), AuthErr>),
    /// Get a list of available <name-room> pairs
    Rooms(Vec<(String, LobbyId)>),
    /// A user joins a game lobby
    Join(Result<Lobby, LobbyErr>),
    /// A user leaves a game lobby
    Leave(Result<(), LobbyErr>),
    /// Get the new set of ready states
    Ready(LobbyUpdate),
    /// Receiving a start request time
    StartReq(DateTime<Utc>),
    /// Response to the action with the state update
    GameUpdate(UpdateState),
    /// Leave the match (forfeit)
    LeaveGame(Result<(), MatchErr>),
}

#[derive(Serialize, Deserialize)]
pub enum RegErr {
    /// The password is way too bad
    BadPassword,
    /// THe username is already taken
    UsernameTaken,
    /// Other internal error, try again?
    OtherError,
}

#[derive(Serialize, Deserialize)]
pub enum AuthErr {
    /// Wrong password for the user
    WrongPassword,
    /// The requested user doesn't exist
    UserNotFound,
    /// No session currently exists
    NoSossion,
    /// Other internal error, try again?
    OtherError,
}

#[derive(Serialize, Deserialize)]
pub enum LobbyErr {
    /// The requested room is already full
    RoomFull,
    /// The room id is unknown
    NoSuchRoom,
    /// Previously not in room
    NotInRoom,
    /// Not everybody was ready
    NotAllReady,
    /// A request was sent by someone who isn't authorised
    NotAuthorized,
    /// Other internal error, try again?
    OtherError,
}

#[derive(Serialize, Deserialize)]
pub enum LobbyUpdate {
    /// The set of ready users
    Ready(Vec<UserId>),
}

/// The way the update was applied
#[derive(Serialize, Deserialize)]
pub enum UpdateState {
    /// The update was applied seamlessly
    Success,
    /// The update was inserted, but had to be re-ordered with another update
    Reordered,
    /// The sent request was invalid and was not applied
    Invalid,
}

/// An error that can occur in a match
#[derive(Serialize, Deserialize)]
pub enum MatchErr {
    /// The provided player wasn't in the match (anymore?)
    NotInMatch,
    /// The requested match had already ended
    MatchAlreadyEnded,
}