~starkingdoms/starkingdoms

ref: 6b3599eccce0aeb36f457c69de06b87ee53f725e starkingdoms/crates/common/src/packet.rs -rw-r--r-- 6.3 KiB
6b3599ec — core format 8 months 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
use std::fmt::{Display, Formatter};
// StarKingdoms.IO, a browser game about drifting through space
//     Copyright (C) 2024 ghostly_zsh, TerraMaster85, core
//
//     This program is free software: you can redistribute it and/or modify
//     it under the terms of the GNU Affero General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
//
//     This program is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU Affero General Public License for more details.
//
//     You should have received a copy of the GNU Affero General Public License
//     along with this program.  If not, see <https://www.gnu.org/licenses/>.
use serde::{Deserialize, Serialize};

use crate::{PartType, PlanetType};

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct ProtoTransform {
    pub x: f32,
    pub y: f32,
    pub rot: f32,
}
#[macro_export]
macro_rules! proto_transform {
    ($e:expr) => {
        $crate::packet::ProtoTransform {
            x: $e.translation.x,
            y: $e.translation.y,
            rot: $e.rotation.to_euler(bevy::math::EulerRot::ZYX).0,
        }
    };
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct Planet {
    pub planet_type: PlanetType,
    pub transform: ProtoTransform,
    pub radius: f32,
}
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct Part {
    pub part_type: PartType,
    pub transform: ProtoTransform,
    pub flags: ProtoPartFlags,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct ProtoPartFlags {
    pub attached: bool,
}

#[macro_export]
macro_rules! proto_part_flags {
    ($e:expr) => {
        $crate::packet::ProtoPartFlags {
            attached: $e.attached,
        }
    };
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub enum MessageType {
    Server,
    Error,
    Chat,
    Direct,
}
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub enum ButtonType {
    Left,
    Middle,
    Right,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct ClientLoginPacket {
    pub username: String,
    pub save: Option<String>,
    pub jwt: Option<String>,
}
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct SendMessagePacket {
    pub target: Option<String>,
    pub content: String,
}
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct PlayerInputPacket {
    pub up: bool,
    pub down: bool,
    pub left: bool,
    pub right: bool,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct PlayerMouseInputPacket {
    pub x: f32,
    pub y: f32,
    pub released: bool,
    pub button: ButtonType,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct RequestSavePacket {
    pub old_save: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct SpecialDisconnectPacket {}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct LoginResponsePacket {
    pub id: u32,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct SpawnPlayerPacket {
    pub id: u32,
    pub username: String,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct PlayerListPacket {
    pub players: Vec<(u32, String)>,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct PlanetPositionsPacket {
    pub planets: Vec<(u32, Planet)>,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct PartPositionsPacket {
    pub parts: Vec<(u32, Part)>,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct SpawnPartPacket {
    pub id: u32,
    pub part: Part,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct DespawnPartPacket {
    pub id: u32,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct PlayerLeavePacket {
    pub id: u32,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct MessagePacket {
    pub message_type: MessageType,
    pub actor: String,
    pub content: String,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct SaveEligibilityPacket {
    pub eligible: bool,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct SaveDataPacket {
    pub payload: String,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct EnergyUpdatePacket {
    pub amount: u32,
    pub max: u32,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct OpenCraftingUiPacket {
    pub id: u32,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
#[serde(tag = "t", content = "c")]
pub enum Packet {
    // serverbound
    ClientLogin(ClientLoginPacket),
    SendMessage(SendMessagePacket),
    PlayerInput(PlayerInputPacket),
    PlayerMouseInput(PlayerMouseInputPacket),
    RequestSave(RequestSavePacket),
    _SpecialDisconnect(SpecialDisconnectPacket),

    // clientbound
    LoginResponse(LoginResponsePacket),
    SpawnPlayer(SpawnPlayerPacket),
    PlayerList(PlayerListPacket),
    PlanetPositions(PlanetPositionsPacket),
    PartPositions(PartPositionsPacket),
    SpawnPart(SpawnPartPacket),
    DespawnPart(DespawnPartPacket),
    PlayerLeave(PlayerLeavePacket),
    Message(MessagePacket),
    SaveEligibility(SaveEligibilityPacket),
    SaveData(SaveDataPacket),
    EnergyUpdate(EnergyUpdatePacket),
    OpenCraftingUi(OpenCraftingUiPacket),
}

impl From<Packet> for String {
    fn from(val: Packet) -> Self {
        serde_json::to_string(&val).expect("failed to serialize packet to json")
    }
}

#[derive(Debug)]
pub enum MsgFromError {
    InvalidMessageType,
    JSONError(serde_json::Error),
}
impl Display for MsgFromError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self)
    }
}

impl TryFrom<&String> for Packet {
    type Error = MsgFromError;

    fn try_from(value: &String) -> Result<Self, Self::Error> {
        serde_json::from_str(value).map_err(MsgFromError::JSONError)
    }
}
impl TryFrom<&Vec<u8>> for Packet {
    type Error = MsgFromError;

    fn try_from(value: &Vec<u8>) -> Result<Self, Self::Error> {
        serde_json::from_slice(value).map_err(MsgFromError::JSONError)
    }
}