summaryrefslogtreecommitdiff
path: root/src/to_clt/kick.rs
blob: 6389df612367b7ef4a14cc1a939842d1d957bef0 (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
use super::*;

#[mt_derive(to = "clt", repr = "u8", tag = "reason")]
pub enum KickReason {
    WrongPasswd,
    UnexpectedData,
    SrvIsSingleplayer,
    UnsupportedVersion,
    BadNameChars,
    BadName,
    TooManyClts,
    EmptyPasswd,
    AlreadyConnected,
    SrvErr,
    Custom { custom: String },
    Shutdown { custom: String, reconnect: bool },
    Crash { custom: String, reconnect: bool },
}

impl KickReason {
    pub fn reconnect(&self) -> bool {
        use KickReason::*;

        match self {
            Shutdown { reconnect, .. } | Crash { reconnect, .. } => *reconnect,
            _ => false,
        }
    }
}

impl fmt::Display for KickReason {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use KickReason::*;

        match self {
            WrongPasswd => write!(f, "wrong password"),
            UnexpectedData => write!(f, "unexpected data"),
            SrvIsSingleplayer => write!(f, "server is singleplayer"),
            UnsupportedVersion => write!(f, "unsupported client version"),
            BadNameChars => write!(f, "disallowed character(s) in player name"),
            BadName => write!(f, "disallowed player name"),
            TooManyClts => write!(f, "too many clients"),
            EmptyPasswd => write!(f, "empty password"),
            AlreadyConnected => write!(f, "another client is already connected with the same name"),
            SrvErr => write!(f, "unsupported client version"),
            Custom { custom } => write!(f, "{custom}"),
            Shutdown { custom, .. } => {
                if custom.is_empty() {
                    write!(f, "server shutdown")
                } else {
                    write!(f, "server shutdown: {custom}")
                }
            }
            Crash { custom, .. } => {
                if custom.is_empty() {
                    write!(f, "server crash")
                } else {
                    write!(f, "server crash: {custom}")
                }
            }
        }
    }
}