players on join

This commit is contained in:
ntr 2019-02-21 15:02:19 +11:00
parent d5d6254daf
commit e8fa2aec30
7 changed files with 50 additions and 46 deletions

View File

@ -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) .rectangle(WIDTH * 0.01, Y + HEIGHT * 0.775, ITEM_WIDTH * 1.25, ITEM_HEIGHT * 1.25, 0x222222)
.setInteractive() .setInteractive()
.setOrigin(0) .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) this.add.text(reroll.getCenter().x, reroll.getCenter().y, 'Reroll', TEXT.HEADER)
.setOrigin(0.5, 0.5); .setOrigin(0.5, 0.5);
@ -169,7 +169,7 @@ class ItemList extends Phaser.Scene {
.setInteractive() .setInteractive()
.setOrigin(0) .setOrigin(0)
.on('pointerdown', () => { .on('pointerdown', () => {
ws.sendVboxCombine(vbox.game, this.combinerItems); ws.sendVboxCombine(vbox.instance, this.combinerItems);
this.combinerItems = [-1, -1, -1]; this.combinerItems = [-1, -1, -1];
this.children.list.filter(obj => obj instanceof CombinerHitBox).forEach(cBox => cBox.deallocate()); this.children.list.filter(obj => obj instanceof CombinerHitBox).forEach(cBox => cBox.deallocate());
}); });
@ -201,7 +201,7 @@ class ItemList extends Phaser.Scene {
itemBox itemBox
.setInteractive() .setInteractive()
.on('pointerdown', () => { .on('pointerdown', () => {
ws.sendVboxAccept(vbox.game, i); ws.sendVboxAccept(vbox.instance, i);
}); });
this.add.existing(itemBox); this.add.existing(itemBox);
}); });
@ -277,8 +277,8 @@ class ItemList extends Phaser.Scene {
return true; return true;
} }
// Allocate to cryp hitbox // Allocate to cryp hitbox
if (hitBox instanceof DeleteHitBox) ws.sendVboxDrop(vbox.game, item.index); if (hitBox instanceof DeleteHitBox) ws.sendVboxDrop(vbox.instance, item.index);
else ws.sendVboxApply(vbox.game, hitBox.cryp.id, item.index); else ws.sendVboxApply(vbox.instance, hitBox.cryp.id, item.index);
} }
// If the item hasn't been allocated deallocate the item // If the item hasn't been allocated deallocate the item
// Scene will restart if there is vbox change // Scene will restart if there is vbox change

View File

@ -38,7 +38,7 @@ class Menu extends Phaser.Scene {
// When we load the menu request the latest items // When we load the menu request the latest items
// Item list will restart when the data comes in // 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('MenuCrypList', MenuCrypList, true);
this.scene.manager.add('MenuNavigation', MenuNavigation, true); this.scene.manager.add('MenuNavigation', MenuNavigation, true);

View File

@ -85,29 +85,29 @@ function createSocket(events) {
send({ method: 'cryp_unspec', params: { id, spec } }); send({ method: 'cryp_unspec', params: { id, spec } });
} }
function sendVboxState(gameId) { function sendPlayerState(instanceId) {
send({ method: 'vbox_state', params: { game_id: gameId } }); send({ method: 'player_state', params: { instance_id: instanceId } });
} }
function sendVboxAccept(gameId, i) { function sendVboxAccept(instanceId, i) {
send({ method: 'vbox_accept', params: { game_id: gameId, index: i } }); send({ method: 'player_vbox_accept', params: { instance_id: instanceId, index: i } });
} }
function sendVboxApply(gameId, crypId, index) { function sendVboxApply(instanceId, crypId, index) {
send({ method: 'vbox_apply', params: { game_id: gameId, cryp_id: crypId, index } }); send({ method: 'player_vbox_apply', params: { instance_id: instanceId, cryp_id: crypId, index } });
} }
function sendVboxDiscard(gameId) { function sendVboxDiscard(instanceId) {
send({ method: 'vbox_discard', params: { game_id: gameId } }); send({ method: 'player_vbox_discard', params: { instance_id: instanceId } });
} }
function sendVboxCombine(gameId, indices) { function sendVboxCombine(instanceId, indices) {
send({ method: 'vbox_combine', params: { game_id: gameId, indices } }); send({ method: 'player_vbox_combine', params: { instance_id: instanceId, indices } });
} }
function sendVboxDrop(gameId, index) { function sendVboxDrop(instanceId, index) {
send({ method: 'vbox_drop', params: { game_id: gameId, index } }); send({ method: 'player_vbox_drop', params: { instance_id: instanceId, index } });
} }
@ -185,10 +185,9 @@ function createSocket(events) {
events.setZone(zone); events.setZone(zone);
} }
function vboxState(response) { function playerState(response) {
const [structName, vbox] = response; const [structName, player] = response;
console.log(vbox); events.setVbox(player.vbox);
events.setVbox(vbox);
} }
// ------------- // -------------
@ -210,7 +209,7 @@ function createSocket(events) {
zone_create: res => console.log(res), zone_create: res => console.log(res),
zone_state: zoneState, zone_state: zoneState,
zone_close: res => console.log(res), zone_close: res => console.log(res),
vbox_state: vboxState, player_state: playerState,
}; };
function errHandler(error) { function errHandler(error) {
@ -301,7 +300,7 @@ function createSocket(events) {
sendZoneCreate, sendZoneCreate,
sendZoneJoin, sendZoneJoin,
sendZoneClose, sendZoneClose,
sendVboxState, sendPlayerState,
sendVboxAccept, sendVboxAccept,
sendVboxApply, sendVboxApply,
sendVboxDrop, sendVboxDrop,

View File

@ -8,7 +8,7 @@ exports.up = async knex => {
table.binary('data').notNullable(); table.binary('data').notNullable();
}); });
await knex.schema.createTable('instances', table => { await knex.schema.createTable('instances', async table => {
table.uuid('id').primary(); table.uuid('id').primary();
table.index('id'); table.index('id');
table.timestamps(); table.timestamps();
@ -17,16 +17,18 @@ exports.up = async knex => {
table.boolean('open') table.boolean('open')
.defaultTo(true) .defaultTo(true)
.notNullable(); .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 => { await knex.schema.createTable('players', table => {
table.uuid('id').primary(); table.uuid('id').primary();
table.index('id'); table.index('id');
table.binary('data').notNullable();
// the instance // the instance
table.uuid('instance').notNullable() table.uuid('instance').notNullable()
@ -52,7 +54,7 @@ exports.up = async knex => {
await knex('instances').insert({ await knex('instances').insert({
id: NULL_UUID, id: NULL_UUID,
data: 'INVALID', data: 'INVALID',
joinable: false, open: false,
}); });
}; };

View File

@ -56,7 +56,7 @@ pub fn player_get(tx: &mut Transaction, account_id: Uuid, instance_id: Uuid) ->
let returned = match result.iter().next() { let returned = match result.iter().next() {
Some(row) => row, 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 // tells from_slice to cast into a cryp
@ -104,5 +104,5 @@ pub fn player_update(tx: &mut Transaction, player: Player) -> Result<Player, Err
} }
pub fn player_state(params: PlayerStateParams, tx: &mut Transaction, account: &Account) -> Result<Player, Error> { pub fn player_state(params: PlayerStateParams, tx: &mut Transaction, account: &Account) -> Result<Player, Error> {
player_get(tx, params.instance_id, account.id) player_get(tx, account.id, params.instance_id)
} }

View File

@ -21,7 +21,7 @@ use account::{Account, account_create, account_login, account_from_token, accoun
use skill::{Skill}; use skill::{Skill};
use zone::{Zone, zone_create, zone_join, zone_close}; use zone::{Zone, zone_create, zone_join, zone_close};
use spec::{Spec}; use spec::{Spec};
use player::{player_state, Player}; use player::{player_state, player_create, Player};
use instance::{instance_join}; use instance::{instance_join};
use vbox::{vbox_accept, vbox_apply, vbox_discard, vbox_combine, vbox_drop}; use vbox::{vbox_accept, vbox_apply, vbox_discard, vbox_combine, vbox_drop};
@ -158,13 +158,16 @@ impl Rpc {
} }
fn account_create(data: Vec<u8>, tx: &mut Transaction, _client: &mut WebSocket<TcpStream>) -> Result<RpcResponse, Error> { fn account_create(data: Vec<u8>, tx: &mut Transaction, _client: &mut WebSocket<TcpStream>) -> Result<RpcResponse, Error> {
match from_slice::<AccountCreateMsg>(&data) { let msg = from_slice::<AccountCreateMsg>(&data).or(Err(err_msg("invalid params")))?;
Ok(v) => Ok(RpcResponse {
method: v.method, let account = account_create(msg.params, tx)?;
params: RpcResult::Account(account_create(v.params, tx)?) let player = Player::new(account.id, Uuid::nil(), vec![]);
}), player_create(tx, &player, &account)?;
Err(_e) => Err(err_msg("invalid params")),
} Ok(RpcResponse {
method: "account_create".to_string(),
params: RpcResult::Account(account)
})
} }
fn account_login(data: Vec<u8>, tx: &mut Transaction, _client: &mut WebSocket<TcpStream>) -> Result<RpcResponse, Error> { fn account_login(data: Vec<u8>, tx: &mut Transaction, _client: &mut WebSocket<TcpStream>) -> Result<RpcResponse, Error> {

View File

@ -277,31 +277,31 @@ impl Vbox {
} }
pub fn vbox_discard(params: VboxDiscardParams, tx: &mut Transaction, account: &Account) -> Result<Player, Error> { pub fn vbox_discard(params: VboxDiscardParams, tx: &mut Transaction, account: &Account) -> Result<Player, Error> {
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(); player.vbox.fill();
return player_update(tx, player); return player_update(tx, player);
} }
pub fn vbox_accept(params: VboxAcceptParams, tx: &mut Transaction, account: &Account) -> Result<Player, Error> { pub fn vbox_accept(params: VboxAcceptParams, tx: &mut Transaction, account: &Account) -> Result<Player, Error> {
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)?; player.vbox.accept(params.index)?;
return player_update(tx, player); return player_update(tx, player);
} }
pub fn vbox_combine(params: VboxCombineParams, tx: &mut Transaction, account: &Account) -> Result<Player, Error> { pub fn vbox_combine(params: VboxCombineParams, tx: &mut Transaction, account: &Account) -> Result<Player, Error> {
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)?; player.vbox.combine(params.indices)?;
return player_update(tx, player); return player_update(tx, player);
} }
pub fn vbox_drop(params: VboxDropParams, tx: &mut Transaction, account: &Account) -> Result<Player, Error> { pub fn vbox_drop(params: VboxDropParams, tx: &mut Transaction, account: &Account) -> Result<Player, Error> {
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)?; player.vbox.drop(params.index)?;
return player_update(tx, player); return player_update(tx, player);
} }
pub fn vbox_apply(params: VboxApplyParams, tx: &mut Transaction, account: &Account) -> Result<Player, Error> { pub fn vbox_apply(params: VboxApplyParams, tx: &mut Transaction, account: &Account) -> Result<Player, Error> {
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 mut cryp = cryp_get(tx, params.cryp_id, account.id)?;
let var = player.vbox.bound.remove(params.index); let var = player.vbox.bound.remove(params.index);