TODO render other players

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

View file

@ -5,12 +5,11 @@ use spacetimedb::{
ReducerContext, ScheduleAt::Interval, Table as _, rand::seq::SliceRandom, reducer,
};
use jong_types::{GameState, TurnState};
use crate::tables::{
DbTile, DbWall, GameTimer, Lobby, PlayerClock, PlayerHand, PlayerOrBot, bot, game_timer,
lobby as _, player, player_clock, player_hand, tile as _, wall,
DbTile, DbWall, GameTimer, Lobby, PlayerClock, PlayerHand, bot, game_timer, lobby as _, player,
player_clock, player_hand, tile as _, wall,
};
use jong_types::{GameState, PlayerOrBot, TurnState};
mod hand;
mod lobby;

View file

@ -47,9 +47,12 @@ pub fn discard_tile(ctx: &ReducerContext, tile_id: u32) -> Result<(), String> {
hand.pond.push(dealt_tile);
hand.working_tile = None;
hand.turn_state = TurnState::None;
ctx.db.player_hand().id().update(hand);
let mut clock = ctx.db.player_clock().player_id().find(player.id).unwrap();
clock.renew();
ctx.db.player_clock().player_id().update(clock);
let mut lobby = ctx.db.lobby().id().find(player.lobby_id).unwrap();
lobby.next_player();
ctx.db.lobby().id().update(lobby);

View file

@ -3,6 +3,8 @@ use std::time::Duration;
use log::info;
use spacetimedb::{ReducerContext, Table, rand::seq::SliceRandom, reducer};
use jong_types::PlayerOrBot;
use crate::tables::*;
#[reducer]

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()
}