hack for player-lobby and Res<Player> race?

This commit is contained in:
Tao Tien 2026-02-08 23:47:57 -08:00
parent 9f6a5b6423
commit 47735931b4
34 changed files with 536 additions and 1340 deletions

View file

@ -0,0 +1,53 @@
use spacetimedb::{ReducerContext, Table, ViewContext, reducer, view};
use crate::tables::*;
use jong_types::*;
pub fn deal_hands(ctx: &ReducerContext, lobby_id: u32) {
let players = ctx.db.player().lobby_id().filter(lobby_id);
let bots = ctx.db.bot().lobby_id().filter(lobby_id);
let mut wall = ctx.db.wall().lobby_id().find(lobby_id).unwrap();
// FIXME rectify deal orders
for mut player in players {
let mut tiles = wall.tiles.split_off(wall.tiles.len() - 13);
tiles.sort();
wall = ctx.db.wall().lobby_id().update(wall);
let hand = ctx.db.hand().insert(Hand {
id: 0,
owner: PlayerOrBot::Player { id: player.id },
sort: true,
tiles,
});
player.hand_id = hand.id;
ctx.db.player().id().update(player);
}
for mut bot in bots {
let mut tiles = wall.tiles.split_off(wall.tiles.len() - 13);
tiles.sort();
wall = ctx.db.wall().lobby_id().update(wall);
let hand = ctx.db.hand().insert(Hand {
id: 0,
owner: PlayerOrBot::Bot { id: bot.id },
sort: true,
tiles,
});
bot.hand_id = hand.id;
ctx.db.bot().id().update(bot);
}
}
#[view(name = view_player_hand, public)]
pub fn view_player_hand(ctx: &ViewContext) -> Option<Hand> {
ctx.db
.player()
.identity()
.find(ctx.sender)
.map(|p| ctx.db.hand().id().find(p.hand_id))?
}
// #[reducer]
// pub fn sort_hand(ctx: &ReducerContext) {
// todo!()
// }

View file

@ -0,0 +1,36 @@
use log::debug;
use spacetimedb::{ReducerContext, Table, rand::seq::SliceRandom, reducer};
use super::hand::deal_hands;
use crate::tables::*;
use jong_types::*;
#[reducer]
pub fn shuffle_deal(ctx: &ReducerContext, lobby_id: u32) {
debug!("lobby_id: {lobby_id}");
let mut lobby = ctx.db.lobby().id().find(lobby_id).unwrap();
lobby.game_state = GameState::Deal;
let mut lobby = ctx.db.lobby().id().update(lobby);
let tiles = new_shuffled_wall(ctx);
ctx.db.wall().insert(Wall {
// id: 0,
lobby_id,
tiles,
});
deal_hands(ctx, lobby_id);
lobby.game_state = GameState::Play;
ctx.db.lobby().id().update(lobby);
}
pub fn new_shuffled_wall(ctx: &ReducerContext) -> Vec<Tile> {
let mut rng = ctx.rng();
let mut wall = tiles();
wall.shuffle(&mut rng);
wall
}