~starkingdoms/starkingdoms

ref: fecb8b0d7b8a02c546424981fce709f5fcf17b94 starkingdoms/protocol/src/legacy.rs -rw-r--r-- 1.8 KiB
fecb8b0d — ghostlyzsh almost nothing changed but server now sends ModuleAdd packet 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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 {},

    PlayersUpdate {
        players: Vec<ProtocolPlayer>,
    },

    PlanetData {
        planets: Vec<ProtocolPlanet>,
    },
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ProtocolPlayer {
    pub rotation: f64,
    pub x: f64,
    pub y: f64,
    pub username: String,
}

#[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 ProtocolPlanet {
    pub planet_type: PlanetType,
    pub x: f64,
    pub y: f64,
    pub radius: f64,
}

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

impl PlanetType {
    pub fn as_texture_id(&self) -> String {
        match self {
            PlanetType::Earth => "earth".to_string(),
        }
    }
}

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()
}