sim_pb/
network.rs

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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
use crate::driving::SimRobot;
use crate::{MyApp, RobotReference, Wall};
use bevy::prelude::{error, info, Commands, Entity, Query, ResMut, Resource, Transform};
use bevy_rapier2d::dynamics::{ExternalImpulse, Velocity};
use bevy_rapier2d::na::{Point2, Rotation2};
use core_pb::constants::{GAME_SERVER_MAGIC_NUMBER, GAME_SERVER_PORT, SIMULATION_LISTENER_PORT};
use core_pb::messages::{GameServerCommand, ServerToSimulationMessage, SimulationToServerMessage};
use core_pb::names::RobotName;
use core_pb::pacbot_rs::game_state::GameState;
use core_pb::pacbot_rs::location::Direction::*;
use core_pb::threaded_websocket::TextOrT;
use core_pb::{bin_decode_single, bin_encode};
use simple_websockets::{Event, EventHub, Message, Responder};
use std::collections::HashMap;
use std::time::{Duration, Instant};

pub const GAME_FPS: f32 = 24.0;

#[derive(Resource)]
pub struct PacbotNetworkSimulation {
    pub game_state: GameState,
    pub last_state_update: Instant,

    pub event_hub: EventHub,
    pub game_server_clients: HashMap<u64, Responder>,

    pub simulation_event_hub: EventHub,
    pub simulation_clients: HashMap<u64, Responder>,
}

pub fn update_network(
    app: ResMut<MyApp>,
    mut network: ResMut<PacbotNetworkSimulation>,
    commands: Commands,
    walls: Query<(Entity, &Wall)>,
    robots: Query<(
        Entity,
        &mut Transform,
        &mut Velocity,
        &mut ExternalImpulse,
        &RobotReference,
    )>,
) {
    network.update(app, commands, walls, robots);
}

impl PacbotNetworkSimulation {
    pub fn new() -> Result<Self, simple_websockets::Error> {
        let event_hub = simple_websockets::launch(GAME_SERVER_PORT)?;
        info!("Listening on port {GAME_SERVER_PORT}");
        let simulation_event_hub = simple_websockets::launch(SIMULATION_LISTENER_PORT)?;
        let game_state = GameState {
            paused: true,
            ..Default::default()
        };
        Ok(Self {
            game_state,
            last_state_update: Instant::now(),

            event_hub,
            game_server_clients: HashMap::new(),

            simulation_event_hub,
            simulation_clients: HashMap::new(),
        })
    }

    /// All updates for network, game state, and simulation - will complete quickly, expects
    /// to be called in a loop
    pub fn update(
        &mut self,
        mut app: ResMut<MyApp>,
        mut commands: Commands,
        // mut pos_query: Query<&mut Transform>,
        walls: Query<(Entity, &Wall)>,
        mut robots: Query<(
            Entity,
            &mut Transform,
            &mut Velocity,
            &mut ExternalImpulse,
            &RobotReference,
        )>,
    ) {
        while let Some(event) = self.event_hub.next_event() {
            match event {
                Event::Connect(id, responder) => {
                    info!("Client #{id} connected");
                    // this message lets clients know that this game server supports
                    // extra messages like pause, reset, custom game state
                    if !responder.send(Message::Binary(GAME_SERVER_MAGIC_NUMBER.to_vec())) {
                        error!("Error sending magic numbers, client already closed");
                    };
                    self.game_server_clients.insert(id, responder);
                    info!("{} client(s) connected", self.game_server_clients.len());
                }
                Event::Disconnect(id) => {
                    info!("Client #{id} disconnected");
                    self.game_server_clients.remove(&id);
                    info!("{} client(s) connected", self.game_server_clients.len());
                }
                Event::Message(id, msg) => match msg {
                    Message::Binary(bytes) => {
                        info!("Message received from rust client #{id}");
                        // binary messages originate from rust clients only
                        match bincode::serde::decode_from_slice::<GameServerCommand, _>(
                            &bytes,
                            bincode::config::standard(),
                        ) {
                            Ok((msg, _)) => match msg {
                                GameServerCommand::Pause => self.game_state.paused = true,
                                GameServerCommand::Unpause => self.game_state.paused = false,
                                GameServerCommand::Reset => self.game_state = GameState::default(),
                                GameServerCommand::Direction(dir) => {
                                    self.game_state.move_pacman_dir(dir)
                                }
                                GameServerCommand::SetState(s) => self.game_state = s,
                            },
                            Err(e) => {
                                error!("Couldn't deserialize client command from {:?}: {:?}", id, e)
                            }
                        }
                    }
                    Message::Text(ref s) => {
                        // text messages may originate from web clients
                        let chars = s.chars().collect::<Vec<_>>();
                        info!("Received message from {:?}: {:?}", id, msg.clone());
                        match chars[0] {
                            'p' => self.game_state.paused = true,
                            'P' => self.game_state.paused = false,
                            'r' | 'R' => self.game_state = GameState::default(),
                            'w' => self.game_state.move_pacman_dir(Up),
                            'a' => self.game_state.move_pacman_dir(Left),
                            's' => self.game_state.move_pacman_dir(Down),
                            'd' => self.game_state.move_pacman_dir(Right),
                            'x' => {
                                if s.len() != 3 {
                                    error!(
                                        "Received invalid position message from {:?}: '{:?}'",
                                        id, s
                                    )
                                } else {
                                    self.game_state
                                        .set_pacman_location((chars[1] as i8, chars[2] as i8));
                                }
                            }
                            _ => error!("Received unexpected message from {:?}: {:?}", id, s),
                        }
                    }
                },
            }
        }

        // simulation specific messages
        while let Some(event) = self.simulation_event_hub.next_event() {
            match event {
                Event::Connect(id, responder) => {
                    self.simulation_clients.insert(id, responder);
                }
                Event::Message(_, message) => match message {
                    Message::Binary(bytes) => {
                        match bin_decode_single::<ServerToSimulationMessage>(&bytes) {
                            Ok(msg) => match msg {
                                ServerToSimulationMessage::Spawn(name) => {
                                    app.spawn_robot(&mut commands, name);
                                }
                                ServerToSimulationMessage::Delete(name) => {
                                    app.despawn_robot(name, &mut commands);
                                }
                                ServerToSimulationMessage::SetPacman(name) => {
                                    app.selected_robot = name;
                                }
                                ServerToSimulationMessage::SetStandardGrid(grid) => {
                                    app.standard_grid = grid;
                                    app.grid = app.standard_grid.compute_grid();
                                    app.reset_grid(&walls, &mut robots, &mut commands)
                                }
                                ServerToSimulationMessage::Teleport(name, loc) => {
                                    if !app.grid.wall_at(&loc) {
                                        if let Some((_, mut transforms, ..)) = robots
                                            .iter_mut()
                                            .find(|(_, _, _, _, robot)| robot.0 == name)
                                        {
                                            transforms.translation.x = loc.x as f32;
                                            transforms.translation.y = loc.y as f32;
                                        }
                                    }
                                }
                                ServerToSimulationMessage::RobotButton(name, event) => {
                                    if let Some((_, sim_robot)) = &app.robots[name as usize] {
                                        sim_robot.write().unwrap().button_events.push_back(event)
                                    }
                                }
                                ServerToSimulationMessage::RobotJoystick(name, values) => {
                                    if let Some((_, sim_robot)) = &app.robots[name as usize] {
                                        sim_robot.write().unwrap().joystick = Some(values)
                                    }
                                }
                            },
                            Err(e) => error!("Error decoding simulation message: {e:?}"),
                        }
                    }
                    Message::Text(text) => error!("Unexpected simulation message: {text}"),
                },
                Event::Disconnect(id) => {
                    self.simulation_clients.remove(&id);
                }
            }
        }

        // send status to simulation clients
        for client in self.simulation_clients.values_mut() {
            client.send(Message::Binary(
                bin_encode(
                    false,
                    TextOrT::T(SimulationToServerMessage::RobotPositions(
                        RobotName::get_all().map(|name| {
                            if !name.is_simulated() {
                                None
                            } else {
                                app.robots[name as usize]
                                    .iter()
                                    .next()
                                    .and_then(|(_, _)| {
                                        robots
                                            .iter_mut()
                                            .find(|(_, _, _, _, robot)| robot.0 == name)
                                    })
                                    .map(|(_, t, ..)| t)
                                    .map(|t| {
                                        (
                                            Point2::new(t.translation.x, t.translation.y),
                                            // feels weird, but this does work
                                            Rotation2::new(
                                                2.0 * t.rotation.normalize().w.acos()
                                                    * t.rotation.z.signum(),
                                            ),
                                        )
                                    })
                            }
                        }),
                    )),
                )
                .unwrap(),
            ));
        }

        // send updated displays to clients
        for name in RobotName::get_all() {
            if !name.is_simulated() {
                continue;
            }
            if let Some((_, robot)) = &app.robots[name as usize] {
                let mut updated_display = None;
                {
                    let mut sim_robot = robot.write().unwrap();
                    if sim_robot.display_updated {
                        updated_display = Some(sim_robot.display.pixels);
                        sim_robot.display_updated = false;
                    }
                }
                if let Some(updated_display) = updated_display {
                    let updated_display: Vec<u128> = updated_display
                        .into_iter()
                        .map(|row| {
                            row.into_iter()
                                .enumerate()
                                .fold(0u128, |acc, (i, x)| acc | (u128::from(x) << i))
                        })
                        .collect();
                    for client in self.simulation_clients.values_mut() {
                        client.send(Message::Binary(
                            bin_encode(
                                false,
                                TextOrT::T(SimulationToServerMessage::RobotDisplay(
                                    name,
                                    updated_display.clone(),
                                )),
                            )
                            .unwrap(),
                        ));
                    }
                }
            }
        }

        // robot messages
        for (_, robot) in app.robots.iter_mut().flatten() {
            if let Some((name, swapped)) = {
                let mut sim_robot = robot.write().unwrap();
                if sim_robot.reboot {
                    let (name, swapped) = (sim_robot.name, sim_robot.firmware_updated);
                    sim_robot.destroy();
                    Some((name, swapped))
                } else {
                    None
                }
            } {
                *robot = SimRobot::start(name, swapped);
            }
        }

        // update the game state if it has been long enough
        if self.time_to_update().is_none() {
            if !self.game_state.paused {
                self.game_state.step();
            }
            // send game state to clients
            let serialized_state = self.game_state.to_bytes();
            for (id, responder) in &mut self.game_server_clients {
                if !responder.send(Message::Binary(serialized_state.clone())) {
                    error!("Failed to send game state to {id}: already closed");
                }
            }
            self.last_state_update = Instant::now();
        }
    }

    pub fn time_to_update(&self) -> Option<Duration> {
        let elapsed = self.last_state_update.elapsed();
        let interval = Duration::from_secs_f32(1.0 / GAME_FPS);
        if elapsed > interval {
            None
        } else {
            Some(interval - elapsed)
        }
    }
}