diff --git a/client/src/scenes/item.list.js b/client/src/scenes/item.list.js index 1aafda61..6b23d259 100644 --- a/client/src/scenes/item.list.js +++ b/client/src/scenes/item.list.js @@ -160,7 +160,7 @@ class ItemList extends Phaser.Scene { .rectangle(WIDTH * 0.01, Y + HEIGHT * 0.775, ITEM_WIDTH * 1.25, ITEM_HEIGHT * 1.25, 0x222222) .setInteractive() .setOrigin(0) - .on('pointerdown', () => this.registry.get('ws').sendVboxDiscard(vbox.game)); + .on('pointerdown', () => this.registry.get('ws').sendVboxDiscard(vbox.instance)); this.add.text(reroll.getCenter().x, reroll.getCenter().y, 'Reroll', TEXT.HEADER) .setOrigin(0.5, 0.5); @@ -169,7 +169,7 @@ class ItemList extends Phaser.Scene { .setInteractive() .setOrigin(0) .on('pointerdown', () => { - ws.sendVboxCombine(vbox.game, this.combinerItems); + ws.sendVboxCombine(vbox.instance, this.combinerItems); this.combinerItems = [-1, -1, -1]; this.children.list.filter(obj => obj instanceof CombinerHitBox).forEach(cBox => cBox.deallocate()); }); @@ -201,7 +201,7 @@ class ItemList extends Phaser.Scene { itemBox .setInteractive() .on('pointerdown', () => { - ws.sendVboxAccept(vbox.game, i); + ws.sendVboxAccept(vbox.instance, i); }); this.add.existing(itemBox); }); @@ -277,8 +277,8 @@ class ItemList extends Phaser.Scene { return true; } // Allocate to cryp hitbox - if (hitBox instanceof DeleteHitBox) ws.sendVboxDrop(vbox.game, item.index); - else ws.sendVboxApply(vbox.game, hitBox.cryp.id, item.index); + if (hitBox instanceof DeleteHitBox) ws.sendVboxDrop(vbox.instance, item.index); + else ws.sendVboxApply(vbox.instance, hitBox.cryp.id, item.index); } // If the item hasn't been allocated deallocate the item // Scene will restart if there is vbox change diff --git a/client/src/scenes/menu.js b/client/src/scenes/menu.js index 77a1c53b..74dd42f8 100644 --- a/client/src/scenes/menu.js +++ b/client/src/scenes/menu.js @@ -38,7 +38,7 @@ class Menu extends Phaser.Scene { // When we load the menu request the latest items // Item list will restart when the data comes in - this.registry.get('ws').sendVboxState(NULL_UUID); + this.registry.get('ws').sendPlayerState(NULL_UUID); this.scene.manager.add('MenuCrypList', MenuCrypList, true); this.scene.manager.add('MenuNavigation', MenuNavigation, true); diff --git a/client/src/socket.js b/client/src/socket.js index 2f81629d..13b21d01 100644 --- a/client/src/socket.js +++ b/client/src/socket.js @@ -85,29 +85,29 @@ function createSocket(events) { send({ method: 'cryp_unspec', params: { id, spec } }); } - function sendVboxState(gameId) { - send({ method: 'vbox_state', params: { game_id: gameId } }); + function sendPlayerState(instanceId) { + send({ method: 'player_state', params: { instance_id: instanceId } }); } - function sendVboxAccept(gameId, i) { - send({ method: 'vbox_accept', params: { game_id: gameId, index: i } }); + function sendVboxAccept(instanceId, i) { + send({ method: 'player_vbox_accept', params: { instance_id: instanceId, index: i } }); } - function sendVboxApply(gameId, crypId, index) { - send({ method: 'vbox_apply', params: { game_id: gameId, cryp_id: crypId, index } }); + function sendVboxApply(instanceId, crypId, index) { + send({ method: 'player_vbox_apply', params: { instance_id: instanceId, cryp_id: crypId, index } }); } - function sendVboxDiscard(gameId) { - send({ method: 'vbox_discard', params: { game_id: gameId } }); + function sendVboxDiscard(instanceId) { + send({ method: 'player_vbox_discard', params: { instance_id: instanceId } }); } - function sendVboxCombine(gameId, indices) { - send({ method: 'vbox_combine', params: { game_id: gameId, indices } }); + function sendVboxCombine(instanceId, indices) { + send({ method: 'player_vbox_combine', params: { instance_id: instanceId, indices } }); } - function sendVboxDrop(gameId, index) { - send({ method: 'vbox_drop', params: { game_id: gameId, index } }); + function sendVboxDrop(instanceId, index) { + send({ method: 'player_vbox_drop', params: { instance_id: instanceId, index } }); } @@ -185,10 +185,9 @@ function createSocket(events) { events.setZone(zone); } - function vboxState(response) { - const [structName, vbox] = response; - console.log(vbox); - events.setVbox(vbox); + function playerState(response) { + const [structName, player] = response; + events.setVbox(player.vbox); } // ------------- @@ -210,7 +209,7 @@ function createSocket(events) { zone_create: res => console.log(res), zone_state: zoneState, zone_close: res => console.log(res), - vbox_state: vboxState, + player_state: playerState, }; function errHandler(error) { @@ -301,7 +300,7 @@ function createSocket(events) { sendZoneCreate, sendZoneJoin, sendZoneClose, - sendVboxState, + sendPlayerState, sendVboxAccept, sendVboxApply, sendVboxDrop, diff --git a/ops/migrations/20181020104420_games.js b/ops/migrations/20181020104420_games.js index f836ddb3..ed19db27 100644 --- a/ops/migrations/20181020104420_games.js +++ b/ops/migrations/20181020104420_games.js @@ -8,7 +8,7 @@ exports.up = async knex => { table.binary('data').notNullable(); }); - await knex.schema.createTable('instances', table => { + await knex.schema.createTable('instances', async table => { table.uuid('id').primary(); table.index('id'); table.timestamps(); @@ -17,16 +17,18 @@ exports.up = async knex => { table.boolean('open') .defaultTo(true) .notNullable(); - - await knex.schema.raw( - // eslint-disable-next-line max-len - 'CREATE UNIQUE INDEX instances_joinable ON instances WHERE open = true;' - ); }); + await knex.schema.raw( + // eslint-disable-next-line max-len + 'CREATE UNIQUE INDEX instances_open ON instances (open) WHERE open = true;' + ); + + await knex.schema.createTable('players', table => { table.uuid('id').primary(); table.index('id'); + table.binary('data').notNullable(); // the instance table.uuid('instance').notNullable() @@ -52,7 +54,7 @@ exports.up = async knex => { await knex('instances').insert({ id: NULL_UUID, data: 'INVALID', - joinable: false, + open: false, }); }; diff --git a/server/src/player.rs b/server/src/player.rs index 3dfba59a..1fb19389 100644 --- a/server/src/player.rs +++ b/server/src/player.rs @@ -56,7 +56,7 @@ pub fn player_get(tx: &mut Transaction, account_id: Uuid, instance_id: Uuid) -> let returned = match result.iter().next() { Some(row) => row, - None => return Err(err_msg("instance not found")), + None => return Err(err_msg("player not found")), }; // tells from_slice to cast into a cryp @@ -104,5 +104,5 @@ pub fn player_update(tx: &mut Transaction, player: Player) -> Result Result { - player_get(tx, params.instance_id, account.id) + player_get(tx, account.id, params.instance_id) } diff --git a/server/src/rpc.rs b/server/src/rpc.rs index f9c693c6..c1ebc729 100644 --- a/server/src/rpc.rs +++ b/server/src/rpc.rs @@ -21,7 +21,7 @@ use account::{Account, account_create, account_login, account_from_token, accoun use skill::{Skill}; use zone::{Zone, zone_create, zone_join, zone_close}; use spec::{Spec}; -use player::{player_state, Player}; +use player::{player_state, player_create, Player}; use instance::{instance_join}; use vbox::{vbox_accept, vbox_apply, vbox_discard, vbox_combine, vbox_drop}; @@ -158,13 +158,16 @@ impl Rpc { } fn account_create(data: Vec, tx: &mut Transaction, _client: &mut WebSocket) -> Result { - match from_slice::(&data) { - Ok(v) => Ok(RpcResponse { - method: v.method, - params: RpcResult::Account(account_create(v.params, tx)?) - }), - Err(_e) => Err(err_msg("invalid params")), - } + let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; + + let account = account_create(msg.params, tx)?; + let player = Player::new(account.id, Uuid::nil(), vec![]); + player_create(tx, &player, &account)?; + + Ok(RpcResponse { + method: "account_create".to_string(), + params: RpcResult::Account(account) + }) } fn account_login(data: Vec, tx: &mut Transaction, _client: &mut WebSocket) -> Result { diff --git a/server/src/vbox.rs b/server/src/vbox.rs index 35eb43ee..05674889 100644 --- a/server/src/vbox.rs +++ b/server/src/vbox.rs @@ -277,31 +277,31 @@ impl Vbox { } pub fn vbox_discard(params: VboxDiscardParams, tx: &mut Transaction, account: &Account) -> Result { - let mut player = player_get(tx, params.instance_id, account.id)?; + let mut player = player_get(tx, account.id, params.instance_id)?; player.vbox.fill(); return player_update(tx, player); } pub fn vbox_accept(params: VboxAcceptParams, tx: &mut Transaction, account: &Account) -> Result { - let mut player = player_get(tx, params.instance_id, account.id)?; + let mut player = player_get(tx, account.id, params.instance_id)?; player.vbox.accept(params.index)?; return player_update(tx, player); } pub fn vbox_combine(params: VboxCombineParams, tx: &mut Transaction, account: &Account) -> Result { - let mut player = player_get(tx, params.instance_id, account.id)?; + let mut player = player_get(tx, account.id, params.instance_id)?; player.vbox.combine(params.indices)?; return player_update(tx, player); } pub fn vbox_drop(params: VboxDropParams, tx: &mut Transaction, account: &Account) -> Result { - let mut player = player_get(tx, params.instance_id, account.id)?; + let mut player = player_get(tx, account.id, params.instance_id)?; player.vbox.drop(params.index)?; return player_update(tx, player); } pub fn vbox_apply(params: VboxApplyParams, tx: &mut Transaction, account: &Account) -> Result { - let mut player = player_get(tx, params.instance_id, account.id)?; + let mut player = player_get(tx, account.id, params.instance_id)?; let mut cryp = cryp_get(tx, params.cryp_id, account.id)?; let var = player.vbox.bound.remove(params.index);