···216216217217#[cfg(test)]
218218mod tests {
219219- use std::{sync::Arc, u64};
219219+ use std::sync::Arc;
220220221221 use crate::game::{location::Location, settings::PingStartCondition};
222222···238238 }
239239240240 async fn send_message(&self, msg: GameEvent) {
241241- for (_id, tx) in self.txs.iter().enumerate() {
241241+ for tx in self.txs.iter() {
242242 tx.send(msg.clone()).await.expect("Failed to send msg");
243243 }
244244 }
···270270 impl MockMatch {
271271 pub fn new(settings: GameSettings, players: u32, seekers: u32) -> Self {
272272 let uuids = (0..players)
273273- .into_iter()
274273 .map(|_| uuid::Uuid::new_v4())
275274 .collect::<Vec<_>>();
276275277276 let channels = (0..players)
278278- .into_iter()
279277 .map(|_| tokio::sync::mpsc::channel(10))
280278 .collect::<Vec<_>>();
281279282280 let initial_caught_state = (0..players)
283283- .into_iter()
284281 .map(|id| (uuids[id as usize], id < seekers))
285282 .collect::<HashMap<_, _>>();
286283 let txs = channels
···319316 }
320317321318 pub async fn start(&self) {
322322- for (_id, game) in &self.games {
319319+ for game in self.games.values() {
323320 let game = game.clone();
324321 tokio::spawn(async move {
325322 game.main_loop().await;
···333330 }
334331335332 pub async fn assert_all_states(&self, f: impl Fn(&GameState)) {
336336- for (_, game) in &self.games {
333333+ for game in self.games.values() {
337334 let state = game.state.read().await;
338335 f(&state);
339336 }
···356353 }
357354358355 async fn tick_all(&self, now: UtcDT) {
359359- for (_, game) in &self.games {
356356+ for game in self.games.values() {
360357 game.force_tick(now).await;
361358 }
362359 }
···478475 settings.powerup_minutes_cooldown = 1;
479476 settings.powerup_start = PingStartCondition::Instant;
480477 settings.powerup_locations = (1..1000)
481481- .into_iter()
482478 .map(|x| Location {
483479 lat: x as f64,
484480 long: 1.0,
+1-2
backend/src/game/state.rs
···280280281281 /// Create a [PlayerPing] with the latest location as another player
282282 pub fn create_ping(&self, id: Id) -> Option<PlayerPing> {
283283- self.get_loc()
284284- .map(|loc| PlayerPing::new(loc.clone(), id, self.id))
283283+ self.get_loc().map(|loc| PlayerPing::new(*loc, id, self.id))
285284 }
286285287286 /// Player has gotten a powerup, rolls to see which powerup and stores it
+34-41
backend/src/lib.rs
···3131fn generate_join_code() -> String {
3232 // 5 character sequence of A-Z
3333 (0..5)
3434- .into_iter()
3535- .map(|_| (('A' as u8) + rand::random_range(0..26)) as char)
3434+ .map(|_| (b'A' + rand::random_range(0..26)) as char)
3635 .collect::<String>()
3736}
3837···48474948impl AppState {
5049 pub fn start_game(&mut self, app: AppHandle, my_id: Uuid, start: StartGameInfo) {
5151- match self {
5252- AppState::Lobby(lobby) => {
5353- let transport = lobby.clone_transport();
5454- let location = TauriLocation::new(app.clone());
5555- let game = Arc::new(Game::new(
5656- my_id,
5757- GAME_TICK_RATE,
5858- start.initial_caught_state,
5959- start.settings,
6060- transport,
6161- location,
6262- ));
6363- *self = AppState::Game(game.clone());
6464- tokio::spawn(async move {
6565- game.main_loop().await;
6666- });
6767- }
6868- _ => {}
5050+ if let AppState::Lobby(lobby) = self {
5151+ let transport = lobby.clone_transport();
5252+ let location = TauriLocation::new(app.clone());
5353+ let game = Arc::new(Game::new(
5454+ my_id,
5555+ GAME_TICK_RATE,
5656+ start.initial_caught_state,
5757+ start.settings,
5858+ transport,
5959+ location,
6060+ ));
6161+ *self = AppState::Game(game.clone());
6262+ tokio::spawn(async move {
6363+ game.main_loop().await;
6464+ });
6965 }
7066 }
7167···7571 app: AppHandle,
7672 settings: GameSettings,
7773 ) {
7878- match self {
7979- AppState::Menu(profile) => {
8080- let host = join_code.is_none();
8181- let room_code = join_code.unwrap_or_else(generate_join_code);
8282- let lobby = Arc::new(Lobby::new(
8383- server_url(),
8484- &room_code,
8585- host,
8686- profile.clone(),
8787- settings,
8888- ));
8989- *self = AppState::Lobby(lobby.clone());
9090- tokio::spawn(async move {
9191- let (my_id, start) = lobby.open().await;
9292- let app_game = app.clone();
9393- let state_handle = app.state::<AppStateHandle>();
9494- let mut state = state_handle.write().await;
9595- state.start_game(app_game, my_id, start);
9696- });
9797- }
9898- _ => {}
7474+ if let AppState::Menu(profile) = self {
7575+ let host = join_code.is_none();
7676+ let room_code = join_code.unwrap_or_else(generate_join_code);
7777+ let lobby = Arc::new(Lobby::new(
7878+ server_url(),
7979+ &room_code,
8080+ host,
8181+ profile.clone(),
8282+ settings,
8383+ ));
8484+ *self = AppState::Lobby(lobby.clone());
8585+ tokio::spawn(async move {
8686+ let (my_id, start) = lobby.open().await;
8787+ let app_game = app.clone();
8888+ let state_handle = app.state::<AppStateHandle>();
8989+ let mut state = state_handle.write().await;
9090+ state.start_game(app_game, my_id, start);
9191+ });
9992 }
10093 }
10194}