jong/spacetimedb/src/game/hand.rs

54 lines
1.5 KiB
Rust
Raw Normal View History

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