upgrade spacetimedb to v2.0.1
This commit is contained in:
parent
5ebf3f6c05
commit
baab16144b
38 changed files with 481 additions and 1647 deletions
|
|
@ -29,14 +29,14 @@ pub fn clear_all(ctx: &ReducerContext) {
|
|||
|
||||
#[reducer(client_connected)]
|
||||
pub fn connect(ctx: &ReducerContext) -> Result<(), String> {
|
||||
let player = if let Some(player) = ctx.db.logged_out_player().identity().find(ctx.sender) {
|
||||
let player = if let Some(player) = ctx.db.logged_out_player().identity().find(ctx.sender()) {
|
||||
let player = ctx.db.player().insert(player);
|
||||
ctx.db.logged_out_player().identity().delete(ctx.sender);
|
||||
ctx.db.logged_out_player().identity().delete(ctx.sender());
|
||||
player
|
||||
} else {
|
||||
debug!("inserting new player with identity {:?}", ctx.sender);
|
||||
debug!("inserting new player with identity {:?}", ctx.sender());
|
||||
ctx.db.player().try_insert(Player {
|
||||
identity: ctx.sender,
|
||||
identity: ctx.sender(),
|
||||
id: 0,
|
||||
name: None,
|
||||
lobby_id: 0,
|
||||
|
|
@ -56,11 +56,11 @@ pub fn disconnect(ctx: &ReducerContext) -> Result<(), String> {
|
|||
.db
|
||||
.player()
|
||||
.identity()
|
||||
.find(ctx.sender)
|
||||
.ok_or_else(|| format!("can't find player {} to disconnect", ctx.sender))?;
|
||||
.find(ctx.sender())
|
||||
.ok_or_else(|| format!("can't find player {} to disconnect", ctx.sender()))?;
|
||||
|
||||
let player = ctx.db.logged_out_player().insert(player);
|
||||
if !ctx.db.player().identity().delete(ctx.sender) {
|
||||
if !ctx.db.player().identity().delete(ctx.sender()) {
|
||||
Err("can't delete row")?
|
||||
}
|
||||
|
||||
|
|
@ -97,7 +97,7 @@ pub fn disconnect(ctx: &ReducerContext) -> 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) {
|
||||
// if let Some(player) = ctx.db.player().identity().find(ctx.sender()) {
|
||||
// ctx.db.player().identity().update(Player {
|
||||
// name: Some(name),
|
||||
// ..player
|
||||
|
|
|
|||
|
|
@ -16,13 +16,14 @@ mod lobby;
|
|||
|
||||
#[reducer]
|
||||
pub fn advance_game(ctx: &ReducerContext, mut game_timer: GameTimer) -> Result<(), String> {
|
||||
advance_game_private(ctx, game_timer)
|
||||
}
|
||||
|
||||
#[reducer]
|
||||
pub fn advance_game_private(ctx: &ReducerContext, mut game_timer: GameTimer) -> Result<(), String> {
|
||||
// checks every second (or more? when users make moves) on whether to advance the game's various states
|
||||
// TODO this, or allow player/debug to call this?
|
||||
|
||||
// if !ctx.sender_auth().is_internal() {
|
||||
// return Err("This reducer can only be called by the scheduler".to_string());
|
||||
// }
|
||||
|
||||
if let Some(mut lobby) = ctx.db.lobby().id().find(game_timer.lobby_id) {
|
||||
trace!("running schedule for lobby {}", lobby.id);
|
||||
match lobby.game_state {
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ use crate::tables::*;
|
|||
// 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 player = ctx.db.player().identity().find(ctx.sender).unwrap();
|
||||
let player = ctx.db.player().identity().find(ctx.sender()).unwrap();
|
||||
let mut hand = ctx.db.player_hand().player_id().find(player.id).unwrap();
|
||||
|
||||
// TODO we can probably remove a buncha these errors
|
||||
|
|
@ -51,7 +51,7 @@ pub fn discard_tile(ctx: &ReducerContext, tile_id: u32) -> Result<(), String> {
|
|||
|
||||
let mut clock = ctx.db.player_clock().player_id().find(player.id).unwrap();
|
||||
clock.renew();
|
||||
ctx.db.player_clock().player_id().update(clock);
|
||||
ctx.db.player_clock().id().update(clock);
|
||||
|
||||
let mut lobby = ctx.db.lobby().id().find(player.lobby_id).unwrap();
|
||||
lobby.next_player();
|
||||
|
|
|
|||
|
|
@ -13,8 +13,8 @@ pub fn join_or_create_lobby(ctx: &ReducerContext, mut lobby_id: u32) -> Result<(
|
|||
.db
|
||||
.player()
|
||||
.identity()
|
||||
.find(ctx.sender)
|
||||
.ok_or(format!("cannot find player {}", ctx.sender))?;
|
||||
.find(ctx.sender())
|
||||
.ok_or(format!("cannot find player {}", ctx.sender()))?;
|
||||
|
||||
if lobby_id == 0 && player.lobby_id == 0 {
|
||||
// TODO check first if player is already in a lobby
|
||||
|
|
@ -74,7 +74,7 @@ pub fn add_bot(ctx: &ReducerContext, lobby_id: u32) -> Result<(), String> {
|
|||
|
||||
#[reducer]
|
||||
pub fn set_ready(ctx: &ReducerContext, ready: bool) -> Result<(), String> {
|
||||
let mut player = ctx.db.player().identity().find(ctx.sender).unwrap();
|
||||
let mut player = ctx.db.player().identity().find(ctx.sender()).unwrap();
|
||||
player.ready = ready;
|
||||
player = ctx.db.player().identity().update(player);
|
||||
|
||||
|
|
|
|||
|
|
@ -6,10 +6,10 @@ use jong_types::{
|
|||
tiles::Tile,
|
||||
};
|
||||
|
||||
use crate::reducers::advance_game;
|
||||
use crate::reducers::advance_game_private;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[table(name = lobby, public)]
|
||||
#[table(accessor = lobby, public)]
|
||||
pub struct Lobby {
|
||||
#[primary_key]
|
||||
#[auto_inc]
|
||||
|
|
@ -23,7 +23,7 @@ pub struct Lobby {
|
|||
// pub open_hands: bool,
|
||||
}
|
||||
|
||||
#[table(name = wall)]
|
||||
#[table(accessor = wall)]
|
||||
pub struct DbWall {
|
||||
#[primary_key]
|
||||
pub lobby_id: u32,
|
||||
|
|
@ -31,7 +31,7 @@ pub struct DbWall {
|
|||
pub tiles: Vec<DbTile>,
|
||||
}
|
||||
|
||||
#[table(name = tile)]
|
||||
#[table(accessor = tile)]
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct DbTile {
|
||||
#[primary_key]
|
||||
|
|
@ -41,8 +41,8 @@ pub struct DbTile {
|
|||
pub tile: Tile,
|
||||
}
|
||||
|
||||
#[table(name = player, public)]
|
||||
#[table(name = logged_out_player)]
|
||||
#[table(accessor = player, public)]
|
||||
#[table(accessor = logged_out_player)]
|
||||
#[derive(Debug)]
|
||||
pub struct Player {
|
||||
#[unique]
|
||||
|
|
@ -61,7 +61,7 @@ pub struct Player {
|
|||
pub sort: bool,
|
||||
}
|
||||
|
||||
#[table(name = player_clock, public)]
|
||||
#[table(accessor = player_clock, public)]
|
||||
pub struct PlayerClock {
|
||||
#[primary_key]
|
||||
pub id: u32,
|
||||
|
|
@ -73,7 +73,7 @@ pub struct PlayerClock {
|
|||
pub total: u16,
|
||||
}
|
||||
|
||||
#[table(name = player_hand)]
|
||||
#[table(accessor = player_hand)]
|
||||
pub struct PlayerHand {
|
||||
#[primary_key]
|
||||
#[auto_inc]
|
||||
|
|
@ -91,7 +91,7 @@ pub struct PlayerHand {
|
|||
pub working_tile: Option<DbTile>,
|
||||
}
|
||||
|
||||
#[table(name = bot, public)]
|
||||
#[table(accessor = bot, public)]
|
||||
pub struct Bot {
|
||||
#[primary_key]
|
||||
#[auto_inc]
|
||||
|
|
@ -108,7 +108,7 @@ pub struct Bot {
|
|||
pub working_tile: Option<DbTile>,
|
||||
}
|
||||
|
||||
#[table(name = game_timer, scheduled(advance_game), public)]
|
||||
#[table(accessor = game_timer, scheduled(advance_game_private), public)]
|
||||
pub struct GameTimer {
|
||||
#[primary_key]
|
||||
#[auto_inc]
|
||||
|
|
@ -120,12 +120,12 @@ pub struct GameTimer {
|
|||
pub scheduled_at: spacetimedb::ScheduleAt,
|
||||
}
|
||||
|
||||
#[view(name = view_hand, public)]
|
||||
#[view(accessor = view_hand, public)]
|
||||
fn view_hand(ctx: &ViewContext) -> Option<PlayerHand> {
|
||||
ctx.db
|
||||
.player()
|
||||
.identity()
|
||||
.find(ctx.sender)
|
||||
.find(ctx.sender())
|
||||
.and_then(|p| ctx.db.player_hand().player_id().find(p.id))
|
||||
}
|
||||
|
||||
|
|
@ -137,9 +137,9 @@ pub struct HandView {
|
|||
pub drawn: bool,
|
||||
}
|
||||
|
||||
#[view(name = view_closed_hands, public)]
|
||||
#[view(accessor = view_closed_hands, public)]
|
||||
fn view_closed_hands(ctx: &ViewContext) -> Vec<HandView> {
|
||||
let this_player = ctx.db.player().identity().find(ctx.sender).unwrap();
|
||||
let this_player = ctx.db.player().identity().find(ctx.sender()).unwrap();
|
||||
if let Some(lobby) = ctx.db.lobby().id().find(this_player.lobby_id) {
|
||||
lobby
|
||||
.players
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue