diff --git a/client/src/scenes/game.list.js b/client/src/scenes/game.list.js index d0d4905d..ca7d778c 100755 --- a/client/src/scenes/game.list.js +++ b/client/src/scenes/game.list.js @@ -22,26 +22,34 @@ class GameList extends Phaser.GameObjects.Group { const WIDTH = GAME_LIST.width(); const HEIGHT = GAME_LIST.height(); - console.log(GAME_LIST.y(0)) - const pvp = list.add .rectangle(X, GAME_LIST.y(0), WIDTH, HEIGHT, 0x440000) .setInteractive() .setOrigin(0); this - .add(list.add.text(pvp.getCenter().x, pvp.getCenter().y, 'PVP', TEXT.HEADER)); + .add(list.add.text(pvp.getCenter().x, pvp.getCenter().y, 'NEW', TEXT.HEADER)); const pve = list.add - .rectangle(X, GAME_LIST.y(1), WIDTH, HEIGHT, 0x004400) + .rectangle(X, GAME_LIST.y(1), Math.floor(WIDTH / 2), HEIGHT, 0x004400) .setInteractive() .setOrigin(0); this .add(list.add.text(pve.getCenter().x, pve.getCenter().y, 'PVE', TEXT.HEADER)); + const refresh = list.add + .rectangle(X + Math.floor(WIDTH / 2), GAME_LIST.y(1), Math.floor(WIDTH / 2), HEIGHT, 0x000044) + .setInteractive() + .setOrigin(0); + + this + .add(list.add.text(refresh.getCenter().x, refresh.getCenter().y, 'REFRESH', TEXT.HEADER)); + + pvp.on('pointerdown', () => { const team = cryps.filter(c => c.active).map(c => c.id); + list.scene.switch('Combat'); return ws.sendGamePvp(team); }); @@ -51,8 +59,10 @@ class GameList extends Phaser.GameObjects.Group { return ws.sendGamePve(team); }); - this.add(pve); - this.add(pvp); + refresh.on('pointerdown', () => { + return ws.sendGameJoinableList(); + }); + return true; } diff --git a/client/src/socket.js b/client/src/socket.js index 6166fd64..b8655eed 100644 --- a/client/src/socket.js +++ b/client/src/socket.js @@ -22,6 +22,23 @@ function createSocket(store) { let gameStateTimeout; + function bootstrap() { + send({ method: 'account_create', params: { name: 'ntr', password: 'grepgrepgrep' } }); + send({ method: 'cryp_spawn', params: { name: 'muji' } }); + send({ method: 'cryp_spawn', params: { name: 'drake' } }); + send({ method: 'cryp_spawn', params: { name: 'xray' } }); + } + + function bootstrapMashy() { + send({ method: 'account_create', params: { name: 'mashy', password: 'grepgrepgrep' } }); + send({ method: 'cryp_spawn', params: { name: 'maury' } }); + send({ method: 'cryp_spawn', params: { name: 'midshaker' } }); + send({ method: 'cryp_spawn', params: { name: 'artour' } }); + } + + window.bootstrap = bootstrap; + window.bootstrapMashy = bootstrapMashy; + function connect() { ws = new WebSocket(SOCKET_URL); ws.binaryType = 'arraybuffer'; @@ -61,7 +78,6 @@ function createSocket(store) { account = login; store.dispatch(actions.setAccount(login)); - // send({ method: 'cryp_spawn', params: { name: 'bees' } }); send({ method: 'account_cryps', params: {} }); send({ method: 'item_list', params: {} }); console.log(account); @@ -80,6 +96,12 @@ function createSocket(store) { store.dispatch(actions.setGame(game)); } + function gameJoinableList(response) { + const [structName, gameList] = response; + gameStateTimeout = setTimeout(() => sendGameState(game.id), 1000); + store.dispatch(actions.setGame(game)); + } + function crypSpawn(response) { const [structName, cryp] = response; console.log('got a new cryp', cryp); @@ -140,6 +162,11 @@ function createSocket(store) { send({ method: 'game_join', params: { game_id: gameId, cryp_ids: crypIds } }); } + function sendGameJoinableList() { + send({ method: 'game_joinable_list', params: { } }); + } + + function sendGameSkill(gameId, crypId, targetTeamId, skill) { send({ method: 'game_skill', @@ -179,6 +206,7 @@ function createSocket(store) { cryp_spawn: crypSpawn, game_pve: gamePve, game_state: gameState, + game_joinable_list: gameJoinableList, account_login: accountLogin, account_create: accountLogin, account_cryps: accountCryps, @@ -208,6 +236,7 @@ function createSocket(store) { sendGamePve, sendGamePvp, sendGameJoin, + sendGameJoinableList, sendGameSkill, sendGameTarget, sendCrypSpawn, @@ -215,6 +244,7 @@ function createSocket(store) { sendCrypForget, sendItemUse, connect, + bootstrap, }; } diff --git a/ops/migrations/20181020104420_games.js b/ops/migrations/20181020104420_games.js index 2704e2bb..1297bd48 100644 --- a/ops/migrations/20181020104420_games.js +++ b/ops/migrations/20181020104420_games.js @@ -5,6 +5,11 @@ exports.up = async knex => { table.timestamps(); table.binary('data').notNullable(); + table.boolean('open') + .defaultTo(true) + .notNullable(); + + // table.boolean('active').notNullable(); }); await knex.schema.createTable('players', table => { diff --git a/server/src/account.rs b/server/src/account.rs index fae0d333..dda3537f 100755 --- a/server/src/account.rs +++ b/server/src/account.rs @@ -7,13 +7,11 @@ use serde_cbor::{from_slice}; use postgres::transaction::Transaction; -use std::str; - -use net::Db; use rpc::{AccountCreateParams, AccountLoginParams}; use item::{Item, ItemAction, item_create}; use cryp::Cryp; +use game::Game; use failure::Error; use failure::err_msg; @@ -177,4 +175,30 @@ pub fn account_cryps(tx: &mut Transaction, account: &Account) -> Result Result, Error> { + let query = " + SELECT games.data + FROM players join games + ON (players.game = games.id) + WHERE account = $1; + "; + + let result = tx + .query(query, &[&account.id])?; + + let games: Result, _> = result.iter().map(|row| { + let cryp_bytes: Vec = row.get(0); + from_slice::(&cryp_bytes) + }).collect(); + + // catch any errors + if games.is_err() { + return Err(err_msg("could not deserialize a game")); + } + + // now unwrap is safe + return Ok(games.unwrap()); + +} + diff --git a/server/src/game.rs b/server/src/game.rs index aec9b5e9..64fc9e5f 100755 --- a/server/src/game.rs +++ b/server/src/game.rs @@ -702,6 +702,30 @@ pub fn game_join(params: GameJoinParams, tx: &mut Transaction, account: &Account Ok(game) } +pub fn game_joinable_list(tx: &mut Transaction, _account: &Account) -> Result, Error> { + let query = " + SELECT games.data + FROM games + WHERE open; + "; + + let result = tx + .query(query, &[])?; + + let games: Result, _> = result.iter().map(|row| { + let cryp_bytes: Vec = row.get(0); + from_slice::(&cryp_bytes) + }).collect(); + + // catch any errors + if games.is_err() { + return Err(err_msg("could not deserialize a game")); + } + + // now unwrap is safe + return Ok(games.unwrap()); +} + #[cfg(test)] mod tests { diff --git a/server/src/rpc.rs b/server/src/rpc.rs index f96cbb3c..77459d88 100755 --- a/server/src/rpc.rs +++ b/server/src/rpc.rs @@ -11,7 +11,7 @@ use failure::err_msg; use net::Db; use cryp::{Cryp, cryp_spawn, cryp_learn, cryp_forget}; -use game::{Game, game_state, game_pve, game_pvp, game_join, game_skill, game_target}; +use game::{Game, game_state, game_pve, game_pvp, game_join, game_joinable_list, game_skill, game_target}; use account::{Account, account_create, account_login, account_from_token, account_cryps}; use item::{Item, items_list, item_use}; use skill::{Skill}; @@ -45,6 +45,7 @@ impl Rpc { "game_pve" => Rpc::game_pve(data, &mut tx, account, client), "game_pvp" => Rpc::game_pvp(data, &mut tx, account, client), "game_join" => Rpc::game_join(data, &mut tx, account, client), + "game_joinable_list" => Rpc::game_joinable_list(data, &mut tx, account, client), "game_skill" => Rpc::game_skill(data, &mut tx, account, client), "game_target" => Rpc::game_target(data, &mut tx, account, client), "account_create" => Rpc::account_create(data, &mut tx, account, client), @@ -143,6 +144,23 @@ impl Rpc { return Ok(game_response); } + fn game_joinable_list(_data: Vec, tx: &mut Transaction, account: Option, _client: &mut WebSocket) -> Result { + let a = match account { + Some(a) => a, + None => return Err(err_msg("auth required")), + }; + + // let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; + + let game_list = RpcResponse { + method: "game_joinable_list".to_string(), + params: RpcResult::GameJoinableList(game_joinable_list(tx, &a)?) + }; + + return Ok(game_list); + } + + fn game_skill(data: Vec, tx: &mut Transaction, account: Option, _client: &mut WebSocket) -> Result { let a = match account { @@ -326,6 +344,7 @@ pub enum RpcResult { Account(Account), CrypList(Vec), GameState(Game), + GameJoinableList(Vec), ItemList(Vec), ItemUse(()), } @@ -416,6 +435,12 @@ pub struct GameJoinParams { pub cryp_ids: Vec, } +#[derive(Debug,Clone,Serialize,Deserialize)] +struct GameJoinableListMsg { + method: String, + params: (), +} + #[derive(Debug,Clone,Serialize,Deserialize)] struct GameTargetMsg { method: String,