more refactor, start using stdb for everything???
4.5th rewrite for tui
This commit is contained in:
parent
034e543d40
commit
c3686221aa
29 changed files with 478 additions and 744 deletions
|
|
@ -4,7 +4,9 @@ use spacetimedb::{ReducerContext, Table, reducer};
|
|||
use crate::tables::{player::player, *};
|
||||
|
||||
mod reducers {
|
||||
mod game;
|
||||
mod deal;
|
||||
mod hand;
|
||||
mod lobby;
|
||||
}
|
||||
mod tables;
|
||||
|
||||
|
|
@ -13,6 +15,18 @@ pub fn clear_all(ctx: &ReducerContext) {
|
|||
for row in ctx.db.player().iter() {
|
||||
ctx.db.player().delete(row);
|
||||
}
|
||||
for row in ctx.db.lobby().iter() {
|
||||
ctx.db.lobby().delete(row);
|
||||
}
|
||||
for row in ctx.db.bot().iter() {
|
||||
ctx.db.bot().delete(row);
|
||||
}
|
||||
for row in ctx.db.wall().iter() {
|
||||
ctx.db.wall().delete(row);
|
||||
}
|
||||
for row in ctx.db.tile().iter() {
|
||||
ctx.db.tile().delete(row);
|
||||
}
|
||||
}
|
||||
|
||||
#[reducer(client_connected)]
|
||||
|
|
|
|||
|
|
@ -3,15 +3,14 @@ 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();
|
||||
|
||||
if lobby.game_state == GameState::Setup {
|
||||
lobby.game_state = GameState::Deal;
|
||||
if lobby.game_state == jong_types::states::GameState::Setup {
|
||||
lobby.game_state = jong_types::states::GameState::Deal;
|
||||
lobby = ctx.db.lobby().id().update(lobby);
|
||||
|
||||
let tiles = new_shuffled_wall(ctx);
|
||||
|
|
@ -24,15 +23,15 @@ pub fn shuffle_deal(ctx: &ReducerContext, lobby_id: u32) {
|
|||
|
||||
deal_hands(ctx, lobby_id);
|
||||
|
||||
lobby.game_state = GameState::Play;
|
||||
lobby.turn_state = TurnState::Tsumo;
|
||||
lobby.game_state = jong_types::states::GameState::Play;
|
||||
lobby.turn_state = jong_types::states::TurnState::Tsumo;
|
||||
ctx.db.lobby().id().update(lobby);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_shuffled_wall(ctx: &ReducerContext) -> Vec<DbTile> {
|
||||
let mut rng = ctx.rng();
|
||||
let mut wall: Vec<_> = tiles()
|
||||
let mut wall: Vec<_> = jong_types::tiles::tiles()
|
||||
.into_iter()
|
||||
.map(|tile| ctx.db.tile().insert(DbTile { id: 0, tile }))
|
||||
.collect();
|
||||
|
|
@ -1,8 +1,8 @@
|
|||
use log::{debug, trace};
|
||||
use spacetimedb::{ReducerContext, Table, reducer};
|
||||
use spacetimedb::{ReducerContext, reducer};
|
||||
|
||||
use crate::tables::{player::player, *};
|
||||
use jong_types::*;
|
||||
use jong_types::states::TurnState;
|
||||
|
||||
pub fn deal_hands(ctx: &ReducerContext, lobby_id: u32) {
|
||||
let players = ctx.db.player().lobby_id().filter(lobby_id);
|
||||
|
|
@ -79,7 +79,7 @@ pub fn discard_tile(ctx: &ReducerContext, tile_id: u32) -> Result<(), String> {
|
|||
#[reducer]
|
||||
pub fn skip_call(ctx: &ReducerContext) {
|
||||
trace!("skip_call");
|
||||
|
||||
|
||||
let player = ctx.db.player().identity().find(ctx.sender).unwrap();
|
||||
let mut lobby = ctx.db.lobby().id().find(player.lobby_id).unwrap();
|
||||
|
||||
|
|
@ -2,10 +2,6 @@ use log::info;
|
|||
use spacetimedb::{ReducerContext, Table, rand::seq::SliceRandom, reducer};
|
||||
|
||||
use crate::tables::{player::player, *};
|
||||
use jong_types::*;
|
||||
|
||||
mod deal;
|
||||
mod hand;
|
||||
|
||||
#[reducer]
|
||||
pub fn join_or_create_lobby(ctx: &ReducerContext, mut lobby_id: u32) -> Result<(), String> {
|
||||
|
|
@ -22,12 +18,12 @@ pub fn join_or_create_lobby(ctx: &ReducerContext, mut lobby_id: u32) -> Result<(
|
|||
id: 0,
|
||||
host_player_id: player.id,
|
||||
players: vec![PlayerOrBot::Player { id: player.id }],
|
||||
game_state: GameState::Lobby,
|
||||
turn_state: TurnState::None,
|
||||
game_state: jong_types::states::GameState::Lobby,
|
||||
turn_state: jong_types::states::TurnState::None,
|
||||
dealer_idx: 0,
|
||||
current_idx: 0,
|
||||
});
|
||||
info!("created lobby: {:?}", lobby);
|
||||
info!("created lobby: {}", lobby.id);
|
||||
|
||||
lobby_id = lobby.id;
|
||||
}
|
||||
|
|
@ -83,7 +79,7 @@ pub fn start_game(ctx: &ReducerContext) {
|
|||
PlayerOrBot::Bot { id } => ctx.db.bot().id().find(id).is_some(),
|
||||
})
|
||||
{
|
||||
lobby.game_state = GameState::Setup;
|
||||
lobby.game_state = jong_types::states::GameState::Setup;
|
||||
lobby.players.shuffle(&mut ctx.rng());
|
||||
lobby.dealer_idx += 1;
|
||||
if lobby.dealer_idx > 3 {
|
||||
|
|
@ -1,6 +1,9 @@
|
|||
use spacetimedb::table;
|
||||
use spacetimedb::{SpacetimeType, table};
|
||||
|
||||
use jong_types::*;
|
||||
use jong_types::{
|
||||
tiles::Tile,
|
||||
states::{GameState, TurnState},
|
||||
};
|
||||
|
||||
pub mod player;
|
||||
pub use player::*;
|
||||
|
|
@ -14,7 +17,7 @@ pub struct Lobby {
|
|||
|
||||
#[unique]
|
||||
pub host_player_id: u32,
|
||||
pub players: Vec<player::PlayerOrBot>,
|
||||
pub players: Vec<PlayerOrBot>,
|
||||
pub dealer_idx: u8,
|
||||
pub current_idx: u8,
|
||||
|
||||
|
|
@ -37,5 +40,11 @@ pub struct DbTile {
|
|||
#[auto_inc]
|
||||
pub id: u32,
|
||||
|
||||
pub tile: jong_types::Tile,
|
||||
pub tile: Tile,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, SpacetimeType)]
|
||||
pub enum PlayerOrBot {
|
||||
Player { id: u32 },
|
||||
Bot { id: u32 },
|
||||
}
|
||||
|
|
|
|||
|
|
@ -42,9 +42,3 @@ pub struct Bot {
|
|||
|
||||
pub drawn_tile: Option<DbTile>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, SpacetimeType)]
|
||||
pub enum PlayerOrBot {
|
||||
Player { id: u32 },
|
||||
Bot { id: u32 },
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue