~starkingdoms/starkingdoms

ref: 71ce7ed846f05352b63458d4a63a97cca36169dd starkingdoms/protocol/src/lib.rs -rw-r--r-- 1.5 KiB
71ce7ed8 — c0repwn3r spritesheets 2 years ago
                                                                                
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
use serde::{Deserialize, Serialize};

pub const PROTOCOL_VERSION: u32 = 1;

#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub enum State {
    Handshake,
    Play
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum MessageC2S {
    Hello {
        version: u32,
        requested_username: String,
        next_state: State
    },

    Goodbye {
        reason: GoodbyeReason
    },

    Chat {
        message: String
    },

    Ping {},
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum MessageS2C {
    Hello {
        version: u32,
        given_username: String,
        next_state: State
    },

    Goodbye {
        reason: GoodbyeReason
    },

    Chat {
        from: String,
        message: String
    },

    Pong {},

    Position {
        x: f64,
        y: f64,
    },

    PlanetData {
        planets: Vec<Planet>
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum GoodbyeReason {
    UnsupportedProtocol { supported: u32, got: u32 },
    UnexpectedPacket,
    UnexpectedNextState,
    UsernameTaken,
    PingPongTimeout,
    Done,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Planet {
    pub planet_type: PlanetType,
    pub x: f64,
    pub y: f64,
    pub radius: f64
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum PlanetType {
    Earth
}

pub fn pc2s(pkt: &MessageC2S) -> Vec<u8> {
    rmp_serde::to_vec(pkt).unwrap()
}
pub fn ps2c(pkt: &MessageS2C) -> Vec<u8> {
    rmp_serde::to_vec(pkt).unwrap()
}