···6565 }
6666 }
67676868+ pub fn get_menu(&self) -> Result<&PlayerProfile> {
6969+ match self {
7070+ AppState::Menu(player_profile) => Ok(player_profile),
7171+ _ => Err("Not on menu screen".to_string()),
7272+ }
7373+ }
7474+7575+ pub fn get_menu_mut(&mut self) -> Result<&mut PlayerProfile> {
7676+ match self {
7777+ AppState::Menu(player_profile) => Ok(player_profile),
7878+ _ => Err("Not on menu screen".to_string()),
7979+ }
8080+ }
8181+8282+ pub fn get_lobby(&self) -> Result<Arc<Lobby>> {
8383+ match self {
8484+ AppState::Lobby(lobby) => Ok(lobby.clone()),
8585+ _ => Err("Not on lobby screen".to_string()),
8686+ }
8787+ }
8888+8989+ pub fn get_game(&self) -> Result<Arc<Game>> {
9090+ match self {
9191+ AppState::Game(game) => Ok(game.clone()),
9292+ _ => Err("Not on game screen".to_string()),
9393+ }
9494+ }
9595+6896 pub fn start_lobby(
6997 &mut self,
7098 join_code: Option<String>,
···142170143171#[tauri::command]
144172#[specta::specta]
173173+/// (Screen: Menu) Get the user's player profile
174174+async fn get_profile(state: State<'_, AppStateHandle>) -> Result<PlayerProfile> {
175175+ let state = state.read().await;
176176+ let profile = state.get_menu()?;
177177+ Ok(profile.clone())
178178+}
179179+180180+#[tauri::command]
181181+#[specta::specta]
145182/// (Screen: Menu) Update the player's profile and persist it
146183async fn update_profile(
147184 new_profile: PlayerProfile,
···150187) -> Result {
151188 new_profile.write_to_store(&app);
152189 let mut state = state.write().await;
153153- if let AppState::Menu(profile) = &mut *state {
154154- *profile = new_profile;
155155- Ok(())
156156- } else {
157157- Err("Profile can only be updated on Menu screen".to_string())
158158- }
190190+ let profile = state.get_menu_mut()?;
191191+ *profile = new_profile;
192192+ Ok(())
159193}
160194161195#[tauri::command]
···179213#[specta::specta]
180214/// (Screen: Lobby) Get the current state of the lobby, call after receiving an update event
181215async fn get_lobby_state(state: State<'_, AppStateHandle>) -> Result<LobbyState> {
182182- let state = state.read().await;
183183- if let AppState::Lobby(lobby) = &*state {
184184- Ok(lobby.clone_state().await)
185185- } else {
186186- Err("Must be called on Lobby screen".to_string())
187187- }
216216+ let lobby = state.read().await.get_lobby()?;
217217+ Ok(lobby.clone_state().await)
188218}
189219190220#[tauri::command]
191221#[specta::specta]
192222/// (Screen: Lobby) Switch teams between seekers and hiders, returns the new [LobbyState]
193223async fn switch_teams(seeker: bool, state: State<'_, AppStateHandle>) -> Result<LobbyState> {
194194- let state = state.read().await;
195195- if let AppState::Lobby(lobby) = &*state {
196196- lobby.switch_teams(seeker).await;
197197- Ok(lobby.clone_state().await)
198198- } else {
199199- Err("Must be called on Lobby screen".to_string())
200200- }
224224+ let lobby = state.read().await.get_lobby()?;
225225+ lobby.switch_teams(seeker).await;
226226+ Ok(lobby.clone_state().await)
201227}
202228203229#[tauri::command]
···208234 settings: GameSettings,
209235 state: State<'_, AppStateHandle>,
210236) -> Result<LobbyState> {
211211- let state = state.read().await;
212212- if let AppState::Lobby(lobby) = &*state {
213213- lobby.update_settings(settings).await;
214214- Ok(lobby.clone_state().await)
215215- } else {
216216- Err("Must be called on Lobby screen".to_string())
217217- }
237237+ let lobby = state.read().await.get_lobby()?;
238238+ lobby.update_settings(settings).await;
239239+ Ok(lobby.clone_state().await)
218240}
219241220242#[tauri::command]
···222244/// (Screen: Lobby) HOST ONLY: Start the game, stops anyone else from joining and switched screen
223245/// to AppScreen::Game.
224246async fn host_start_game(state: State<'_, AppStateHandle>) -> Result {
225225- let state = state.read().await;
226226- if let AppState::Lobby(lobby) = &*state {
227227- lobby.start_game().await;
228228- Ok(())
229229- } else {
230230- Err("Must be called on Lobby screen".to_string())
231231- }
247247+ state.read().await.get_lobby()?.start_game().await;
248248+ Ok(())
232249}
233250234251// AppScreen::Game COMMANDS
···237254#[specta::specta]
238255/// (Screen: Game) Mark this player as caught, this player will become a seeker. Returns the new game state
239256async fn mark_caught(state: State<'_, AppStateHandle>) -> Result<GameState> {
240240- let state = state.read().await;
241241- if let AppState::Game(game) = &*state {
242242- game.mark_caught().await;
243243- Ok(game.clone_state().await)
244244- } else {
245245- Err("Must be called on Game screen".to_string())
246246- }
257257+ let game = state.read().await.get_game()?;
258258+ game.mark_caught().await;
259259+ Ok(game.clone_state().await)
247260}
248261249262#[tauri::command]
···251264/// (Screen: Game) Grab a powerup on the map, this should be called when the user is *in range* of
252265/// the powerup. Returns the new game state after rolling for the powerup
253266async fn grab_powerup(state: State<'_, AppStateHandle>) -> Result<GameState> {
254254- let state = state.read().await;
255255- if let AppState::Game(game) = &*state {
256256- game.get_powerup().await;
257257- Ok(game.clone_state().await)
258258- } else {
259259- Err("Must be called on Game screen".to_string())
260260- }
267267+ let game = state.read().await.get_game()?;
268268+ game.get_powerup().await;
269269+ Ok(game.clone_state().await)
261270}
262271263272#[tauri::command]
···265274/// (Screen: Game) Use the currently held powerup in the player's held_powerup. Does nothing if the
266275/// player has none. Returns the updated game state
267276async fn use_powerup(state: State<'_, AppStateHandle>) -> Result<GameState> {
268268- let state = state.read().await;
269269- if let AppState::Game(game) = &*state {
270270- game.use_powerup().await;
271271- Ok(game.clone_state().await)
272272- } else {
273273- Err("Must be called on Game screen".to_string())
274274- }
277277+ let game = state.read().await.get_game()?;
278278+ game.use_powerup().await;
279279+ Ok(game.clone_state().await)
275280}
276281277282pub fn mk_specta() -> tauri_specta::Builder {
278283 tauri_specta::Builder::<tauri::Wry>::new().commands(collect_commands![
279284 start_lobby,
285285+ get_profile,
280286 quit_game_or_lobby,
281287 get_current_screen,
282288 update_profile,
+11
frontend/src/bindings.ts
···2424 }
2525 },
2626 /**
2727+ * (Screen: Menu) Get the user's player profile
2828+ */
2929+ async getProfile(): Promise<Result<PlayerProfile, string>> {
3030+ try {
3131+ return { status: "ok", data: await TAURI_INVOKE("get_profile") };
3232+ } catch (e) {
3333+ if (e instanceof Error) throw e;
3434+ else return { status: "error", error: e as any };
3535+ }
3636+ },
3737+ /**
2738 * Quit a running game or leave a lobby
2839 */
2940 async quitGameOrLobby(): Promise<Result<null, string>> {