re-organize crates
This commit is contained in:
parent
1afb7f4e3d
commit
c709fb5851
50 changed files with 148 additions and 121 deletions
12
jong-line/Cargo.toml
Normal file
12
jong-line/Cargo.toml
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
[package]
|
||||
name = "jong-line"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
jong-types.workspace = true
|
||||
spacetimedb.workspace = true
|
||||
log.workspace = true
|
||||
83
jong-line/src/lib.rs
Normal file
83
jong-line/src/lib.rs
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
use log::debug;
|
||||
use spacetimedb::{ReducerContext, Table, reducer};
|
||||
|
||||
use crate::tables::{player::player, *};
|
||||
|
||||
mod reducers {
|
||||
mod game;
|
||||
}
|
||||
mod tables;
|
||||
|
||||
#[reducer]
|
||||
pub fn clear_all(ctx: &ReducerContext) {
|
||||
for row in ctx.db.player().iter() {
|
||||
ctx.db.player().delete(row);
|
||||
}
|
||||
}
|
||||
|
||||
#[reducer(client_connected)]
|
||||
pub fn login_or_add_player(ctx: &ReducerContext) {
|
||||
let identity = ctx.sender;
|
||||
|
||||
// TODO remove player on disconnect
|
||||
if let Ok(player) = ctx.db.player().try_insert(Player {
|
||||
identity,
|
||||
id: 0,
|
||||
name: None,
|
||||
lobby_id: 0,
|
||||
ready: false,
|
||||
sort: true,
|
||||
hand: vec![],
|
||||
pond: vec![],
|
||||
drawn_tile: None,
|
||||
}) {
|
||||
debug!("added player: {:?}", player);
|
||||
} else {
|
||||
let player = ctx.db.player().identity().find(identity).unwrap();
|
||||
debug!("player {:?} has reconnected", player)
|
||||
}
|
||||
}
|
||||
|
||||
// #[reducer(init)]
|
||||
// pub fn init(_ctx: &ReducerContext) {
|
||||
// // Called when the module is initially published
|
||||
// }
|
||||
|
||||
// #[reducer(client_connected)]
|
||||
// pub fn identity_connected(_ctx: &ReducerContext) {
|
||||
// // Called everytime a new client connects
|
||||
// }
|
||||
|
||||
// #[reducer(client_disconnected)]
|
||||
// pub fn identity_disconnected(_ctx: &ReducerContext) {
|
||||
// // Called everytime a client disconnects
|
||||
// }
|
||||
|
||||
// #[reducer]
|
||||
// pub fn add(ctx: &ReducerContext, name: String) {
|
||||
// ctx.db.player().insert(Player { name });
|
||||
// }
|
||||
|
||||
// #[reducer]
|
||||
// pub fn say_hello(ctx: &ReducerContext) {
|
||||
// for person in ctx.db.person().iter() {
|
||||
// log::info!("Hello, {}!", person.name);
|
||||
// }
|
||||
// log::info!("Hello, World!");
|
||||
// }
|
||||
|
||||
// #[reducer]
|
||||
// pub fn set_name(ctx: &ReducerContext, name: String) -> Result<(), String> {
|
||||
// if name.is_empty() {
|
||||
// return Err("names must not be empty".into());
|
||||
// }
|
||||
// if let Some(player) = ctx.db.player().identity().find(ctx.sender) {
|
||||
// ctx.db.player().identity().update(Player {
|
||||
// name: Some(name),
|
||||
// ..player
|
||||
// });
|
||||
// Ok(())
|
||||
// } else {
|
||||
// Err("cannot set name for unknown user".into())
|
||||
// }
|
||||
// }
|
||||
94
jong-line/src/reducers/game.rs
Normal file
94
jong-line/src/reducers/game.rs
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
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> {
|
||||
let ok_or = ctx
|
||||
.db
|
||||
.player()
|
||||
.identity()
|
||||
.find(ctx.sender)
|
||||
.ok_or(format!("cannot find player {}", ctx.sender))?;
|
||||
let mut player = ok_or;
|
||||
|
||||
if lobby_id == 0 {
|
||||
let lobby = ctx.db.lobby().insert(Lobby {
|
||||
id: 0,
|
||||
host_player_id: player.id,
|
||||
players: vec![PlayerOrBot::Player { id: player.id }],
|
||||
game_state: GameState::Lobby,
|
||||
turn_state: TurnState::None,
|
||||
dealer_idx: 0,
|
||||
current_idx: 0,
|
||||
});
|
||||
info!("created lobby: {:?}", lobby);
|
||||
|
||||
lobby_id = lobby.id;
|
||||
}
|
||||
|
||||
player.lobby_id = lobby_id;
|
||||
|
||||
let player = ctx.db.player().identity().update(player);
|
||||
|
||||
info!("player {} joined lobby {}", player.id, lobby_id);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[reducer]
|
||||
pub fn add_bot(ctx: &ReducerContext, lobby_id: u32) -> Result<(), String> {
|
||||
if lobby_id == 0 {
|
||||
Err("cannot add a bot without a lobby".into())
|
||||
} else if let Some(mut lobby) = ctx.db.lobby().id().find(lobby_id)
|
||||
&& (ctx.db.player().lobby_id().filter(lobby_id).count()
|
||||
+ ctx.db.bot().lobby_id().filter(lobby_id).count()
|
||||
< 4)
|
||||
{
|
||||
let bot = ctx.db.bot().insert(Bot {
|
||||
id: 0,
|
||||
lobby_id,
|
||||
hand: vec![],
|
||||
pond: vec![],
|
||||
drawn_tile: None,
|
||||
});
|
||||
lobby.players.push(PlayerOrBot::Bot { id: bot.id });
|
||||
ctx.db.lobby().id().update(lobby);
|
||||
info!("added bot {} to lobby {}", bot.id, lobby_id);
|
||||
Ok(())
|
||||
} else {
|
||||
Err("lobby doesn't exist".into())
|
||||
}
|
||||
}
|
||||
|
||||
#[reducer]
|
||||
pub fn set_ready(ctx: &ReducerContext, ready: bool) {
|
||||
let mut player = ctx.db.player().identity().find(ctx.sender).unwrap();
|
||||
player.ready = ready;
|
||||
|
||||
ctx.db.player().identity().update(player);
|
||||
}
|
||||
|
||||
#[reducer]
|
||||
pub fn start_game(ctx: &ReducerContext) {
|
||||
let player = ctx.db.player().identity().find(ctx.sender).unwrap();
|
||||
if let Some(mut lobby) = ctx.db.lobby().host_player_id().find(player.id)
|
||||
&& lobby.players.len() == 4
|
||||
&& lobby.players.iter().all(|p| match p {
|
||||
PlayerOrBot::Player { id } => ctx.db.player().id().find(id).is_some_and(|p| p.ready),
|
||||
PlayerOrBot::Bot { id } => ctx.db.bot().id().find(id).is_some(),
|
||||
})
|
||||
{
|
||||
lobby.game_state = GameState::Setup;
|
||||
lobby.players.shuffle(&mut ctx.rng());
|
||||
lobby.dealer_idx += 1;
|
||||
if lobby.dealer_idx > 3 {
|
||||
lobby.dealer_idx = 0;
|
||||
}
|
||||
ctx.db.lobby().id().update(lobby);
|
||||
}
|
||||
}
|
||||
0
jong-line/src/reducers/game/bot.rs
Normal file
0
jong-line/src/reducers/game/bot.rs
Normal file
42
jong-line/src/reducers/game/deal.rs
Normal file
42
jong-line/src/reducers/game/deal.rs
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
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();
|
||||
|
||||
if lobby.game_state == GameState::Setup {
|
||||
lobby.game_state = GameState::Deal;
|
||||
lobby = ctx.db.lobby().id().update(lobby);
|
||||
|
||||
let tiles = new_shuffled_wall(ctx);
|
||||
|
||||
ctx.db.wall().insert(DbWall {
|
||||
// id: 0,
|
||||
lobby_id,
|
||||
tiles,
|
||||
});
|
||||
|
||||
deal_hands(ctx, lobby_id);
|
||||
|
||||
lobby.game_state = GameState::Play;
|
||||
lobby.turn_state = 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()
|
||||
.into_iter()
|
||||
.map(|tile| ctx.db.tile().insert(DbTile { id: 0, tile }))
|
||||
.collect();
|
||||
wall.shuffle(&mut rng);
|
||||
|
||||
wall
|
||||
}
|
||||
128
jong-line/src/reducers/game/hand.rs
Normal file
128
jong-line/src/reducers/game/hand.rs
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
use log::{debug, trace};
|
||||
use spacetimedb::{ReducerContext, Table, reducer};
|
||||
|
||||
use crate::tables::{player::player, *};
|
||||
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);
|
||||
wall = ctx.db.wall().lobby_id().update(wall);
|
||||
tiles.sort_by_key(|t| t.tile);
|
||||
player.hand = tiles;
|
||||
ctx.db.player().id().update(player);
|
||||
}
|
||||
for mut bot in bots {
|
||||
let mut tiles = wall.tiles.split_off(wall.tiles.len() - 13);
|
||||
wall = ctx.db.wall().lobby_id().update(wall);
|
||||
tiles.sort_by_key(|t| t.tile);
|
||||
bot.hand = tiles;
|
||||
ctx.db.bot().id().update(bot);
|
||||
}
|
||||
}
|
||||
|
||||
#[reducer]
|
||||
pub fn draw_tile(ctx: &ReducerContext) {
|
||||
let mut player = ctx.db.player().identity().find(ctx.sender).unwrap();
|
||||
let mut wall = ctx.db.wall().lobby_id().find(player.lobby_id).unwrap();
|
||||
|
||||
// TODO if no more tiles, exhaust somehow
|
||||
|
||||
player.drawn_tile = wall.tiles.pop();
|
||||
|
||||
ctx.db.wall().lobby_id().update(wall);
|
||||
ctx.db.player().id().update(player);
|
||||
}
|
||||
|
||||
// TODO make sure this can't be called or just error here?
|
||||
#[reducer]
|
||||
pub fn discard_tile(ctx: &ReducerContext, tile_id: u32) -> Result<(), String> {
|
||||
let mut player = ctx.db.player().identity().find(ctx.sender).unwrap();
|
||||
let mut lobby = ctx.db.lobby().id().find(player.lobby_id).unwrap();
|
||||
|
||||
let dealt_tile = if let Some(drawn) = player.drawn_tile
|
||||
&& drawn.id == tile_id
|
||||
{
|
||||
drawn
|
||||
} else if let Some((i, _)) = player
|
||||
.hand
|
||||
.iter()
|
||||
.enumerate()
|
||||
.find(|(_, t)| t.id == tile_id)
|
||||
{
|
||||
player.hand.remove(i)
|
||||
} else {
|
||||
return Err(format!(
|
||||
"player {} attempted to deal nonexistant tile {}",
|
||||
player.id, tile_id
|
||||
));
|
||||
};
|
||||
|
||||
player.pond.push(dealt_tile);
|
||||
player.drawn_tile = None;
|
||||
lobby.turn_state = TurnState::RonChiiPonKan;
|
||||
|
||||
let player = ctx.db.player().id().update(player);
|
||||
ctx.db.lobby().id().update(lobby);
|
||||
|
||||
debug!("player {} discarded tile {:?}", player.id, dealt_tile.tile);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[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();
|
||||
|
||||
lobby.turn_state = TurnState::Tsumo;
|
||||
lobby.current_idx += 1;
|
||||
if lobby.current_idx >= 3 {
|
||||
lobby.current_idx = 0;
|
||||
}
|
||||
|
||||
// FIXME where better can this go
|
||||
bot_moves(ctx, &mut lobby);
|
||||
|
||||
ctx.db.player().id().update(player);
|
||||
ctx.db.lobby().id().update(lobby);
|
||||
}
|
||||
|
||||
fn bot_moves(ctx: &ReducerContext, lobby: &mut Lobby) {
|
||||
let mut wall = ctx.db.wall().lobby_id().find(lobby.id).unwrap();
|
||||
if let Some(PlayerOrBot::Bot { id }) = lobby.players.get(lobby.current_idx as usize + 1) {
|
||||
let mut bot = ctx.db.bot().id().find(id).unwrap();
|
||||
bot.pond.push(wall.tiles.pop().unwrap());
|
||||
ctx.db.bot().id().update(bot);
|
||||
lobby.turn_state = TurnState::RonChiiPonKan;
|
||||
} else {
|
||||
lobby.turn_state = TurnState::Tsumo;
|
||||
}
|
||||
|
||||
lobby.current_idx += 1;
|
||||
if lobby.current_idx >= 3 {
|
||||
lobby.current_idx = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// #[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!()
|
||||
// }
|
||||
41
jong-line/src/tables.rs
Normal file
41
jong-line/src/tables.rs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
use spacetimedb::table;
|
||||
|
||||
use jong_types::*;
|
||||
|
||||
pub mod player;
|
||||
pub use player::*;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[table(name = lobby, public)]
|
||||
pub struct Lobby {
|
||||
#[primary_key]
|
||||
#[auto_inc]
|
||||
pub id: u32,
|
||||
|
||||
#[unique]
|
||||
pub host_player_id: u32,
|
||||
pub players: Vec<player::PlayerOrBot>,
|
||||
pub dealer_idx: u8,
|
||||
pub current_idx: u8,
|
||||
|
||||
pub game_state: GameState,
|
||||
pub turn_state: TurnState,
|
||||
}
|
||||
|
||||
#[table(name = wall)]
|
||||
pub struct DbWall {
|
||||
#[primary_key]
|
||||
pub lobby_id: u32,
|
||||
|
||||
pub tiles: Vec<DbTile>,
|
||||
}
|
||||
|
||||
#[table(name = tile)]
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct DbTile {
|
||||
#[primary_key]
|
||||
#[auto_inc]
|
||||
pub id: u32,
|
||||
|
||||
pub tile: jong_types::Tile,
|
||||
}
|
||||
50
jong-line/src/tables/player.rs
Normal file
50
jong-line/src/tables/player.rs
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
use spacetimedb::Identity;
|
||||
use spacetimedb::{SpacetimeType, table};
|
||||
|
||||
use super::DbTile;
|
||||
|
||||
// FIXME this shant be public, use views
|
||||
#[table(name = player, public)]
|
||||
#[derive(Debug)]
|
||||
pub struct Player {
|
||||
#[primary_key]
|
||||
pub identity: Identity,
|
||||
|
||||
#[unique]
|
||||
#[auto_inc]
|
||||
pub id: u32,
|
||||
|
||||
pub name: Option<String>,
|
||||
|
||||
#[index(btree)]
|
||||
pub lobby_id: u32,
|
||||
pub ready: bool,
|
||||
|
||||
pub sort: bool,
|
||||
|
||||
pub hand: Vec<DbTile>,
|
||||
pub pond: Vec<DbTile>,
|
||||
|
||||
pub drawn_tile: Option<DbTile>,
|
||||
}
|
||||
|
||||
#[table(name = bot)]
|
||||
pub struct Bot {
|
||||
#[primary_key]
|
||||
#[auto_inc]
|
||||
pub id: u32,
|
||||
|
||||
#[index(btree)]
|
||||
pub lobby_id: u32,
|
||||
|
||||
pub hand: Vec<DbTile>,
|
||||
pub pond: Vec<DbTile>,
|
||||
|
||||
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