(stash) render other players

This commit is contained in:
Tao Tien 2026-02-24 00:56:34 -08:00
parent 33cb9c7f97
commit cc07cc89c6
10 changed files with 249 additions and 14 deletions

View file

@ -1,6 +1,7 @@
use spacetimedb::{SpacetimeType, ViewContext, table, view};
use jong_types::{
PlayerOrBot,
states::{GameState, TurnState},
tiles::Tile,
};
@ -19,6 +20,7 @@ pub struct Lobby {
pub current_idx: u8,
pub game_state: GameState,
// pub open_hands: bool,
}
#[table(name = wall)]
@ -39,12 +41,6 @@ pub struct DbTile {
pub tile: Tile,
}
#[derive(Debug, Clone, SpacetimeType)]
pub enum PlayerOrBot {
Player { id: u32 },
Bot { id: u32 },
}
#[table(name = player, public)]
#[table(name = logged_out_player)]
#[derive(Debug)]
@ -132,3 +128,48 @@ fn view_hand(ctx: &ViewContext) -> Option<PlayerHand> {
.find(ctx.sender)
.and_then(|p| ctx.db.player_hand().player_id().find(p.id))
}
#[derive(SpacetimeType, Clone, Copy)]
pub struct HandView {
pub player: PlayerOrBot,
pub hand_length: u8,
// pub melds: u8,
pub drawn: bool,
}
#[view(name = view_closed_hands, public)]
fn view_closed_hands(ctx: &ViewContext) -> Vec<HandView> {
let this_player = ctx.db.player().identity().find(ctx.sender).unwrap();
ctx.db
.lobby()
.id()
.find(this_player.lobby_id)
.unwrap()
.players
.iter()
.filter_map(|&player| {
let (hand_length, drawn) = match player {
PlayerOrBot::Player { id } => {
let player_hand = ctx.db.player_hand().player_id().find(id).unwrap();
(
player_hand.hand.len() as u8,
player_hand.turn_state == TurnState::Tsumo
&& player_hand.working_tile.is_some(),
)
}
PlayerOrBot::Bot { id } => {
let bot = ctx.db.bot().id().find(id).unwrap();
(
bot.hand.len() as u8,
bot.turn_state == TurnState::Tsumo && bot.working_tile.is_some(),
)
}
};
Some(HandView {
player,
hand_length,
drawn,
})
})
.collect()
}