From 41c7c790f283d5e8bec18c6128fbbfa8a7119ea3 Mon Sep 17 00:00:00 2001 From: ntr Date: Wed, 21 Nov 2018 16:32:39 +1100 Subject: [PATCH] pve and pvp back --- client/src/scenes/lobbies.js | 8 +++-- client/src/socket.js | 4 +-- server/WORKLOG.md | 23 +++++++------ server/src/game.rs | 64 +++++++++++++++++------------------- server/src/rpc.rs | 2 +- 5 files changed, 52 insertions(+), 49 deletions(-) diff --git a/client/src/scenes/lobbies.js b/client/src/scenes/lobbies.js index 0a36c47d..76b99400 100644 --- a/client/src/scenes/lobbies.js +++ b/client/src/scenes/lobbies.js @@ -17,6 +17,8 @@ class Lobbies extends Phaser.GameObjects.Group { super(list); this.keyboard = list.input.keyboard; + const ws = list.registry.get('ws'); + const X_POS = ROW_WIDTH + 50; const pvp = list.add @@ -36,11 +38,13 @@ class Lobbies extends Phaser.GameObjects.Group { .add(list.add.text(pve.getCenter().x, pve.getCenter().y, 'PVE', TEXT.HEADER)); pvp.on('pointerdown', () => { - console.log(cryps.filter(c => c.active)); + const team = cryps.filter(c => c.active).map(c => c.id); + return ws.sendGamePvp(team); }); pve.on('pointerdown', () => { - console.log(cryps.filter(c => c.active)); + const team = cryps.filter(c => c.active).map(c => c.id); + return ws.sendGamePve(team); }); this.add(pve); diff --git a/client/src/socket.js b/client/src/socket.js index 9ee64e0c..6166fd64 100644 --- a/client/src/socket.js +++ b/client/src/socket.js @@ -128,8 +128,8 @@ function createSocket(store) { send({ method: 'game_state', params: { id } }); } - function sendGamePve(id) { - send({ method: 'game_pve', params: { id } }); + function sendGamePve(crypIds) { + send({ method: 'game_pve', params: { cryp_ids: crypIds } }); } function sendGamePvp(crypIds) { diff --git a/server/WORKLOG.md b/server/WORKLOG.md index 623e73ea..3a5237b4 100755 --- a/server/WORKLOG.md +++ b/server/WORKLOG.md @@ -5,7 +5,6 @@ # WORK WORK -* move rpc functions out * unwrap account for all functions except list * handle unserializable cryps @@ -16,25 +15,23 @@ * Scrabble grid * skills - * offensive -> choose target ✔ + * handle setting account better maybe? * ensure cryp untargetable and doesn't resolve when KO * calculate * hp increase/decrease - * spell/phys dmg * private fields for opponents - * cooldowns reduce each turn ✔ - * statuses reduce each turn - * teach cyps skills ✔ + * attack * can you attack yourself? * fetch existing battles * check for game participation * write players row for every team+cryp added * return results<> - * defensive +* Cryp Generation + * + * Items - * rez ✔ * unselect item with esc + button * Grid reroll * Colour scheme @@ -43,9 +40,14 @@ * Bosses -* Cryp Generation - * + * rez ✔ +* move rpc functions out ✔ +* cooldowns reduce each turn ✔ +* statuses reduce each turn ✔ +* teach cyps skills ✔ +* spell/phys dmg ✔ +* offensive -> choose target ✔ * ws reconnect ✔ * Levelling ✔ * Logins ✔️ @@ -65,6 +67,7 @@ # Art Styles * Aztec +* youkai * Pixel * Industrial diff --git a/server/src/game.rs b/server/src/game.rs index 61e0580a..aec9b5e9 100755 --- a/server/src/game.rs +++ b/server/src/game.rs @@ -1,6 +1,8 @@ use uuid::Uuid; use rand::prelude::*; +use std::iter; + // Db Commons use serde_cbor::{from_slice, to_vec}; use postgres::transaction::Transaction; @@ -581,13 +583,13 @@ pub fn game_update(game: &Game, tx: &mut Transaction) -> Result<(), Error> { return Ok(()); } -fn generate_mob(plr: &Cryp) -> Cryp { +fn generate_mob(lvl: u8) -> Cryp { let mut rng = thread_rng(); // rng panics on min == max - let mob_lvl: u8 = match plr.lvl { + let mob_lvl: u8 = match lvl { 1 => 1, - _ => rng.gen_range(plr.lvl.saturating_sub(2), plr.lvl) + _ => rng.gen_range(lvl.saturating_sub(2), lvl) }; return Cryp::new() @@ -598,48 +600,38 @@ fn generate_mob(plr: &Cryp) -> Cryp { } pub fn game_pve(params: GamePveParams, tx: &mut Transaction, account: &Account) -> Result { - let query = " - SELECT * - FROM cryps - WHERE id = $1 - AND account = $2; - "; + let cryps = params.cryp_ids + .iter() + .map(|id| cryp_get(tx, *id, account.id)) + .collect::, Error>>()?; - let result = tx - .query(query, &[¶ms.id, &account.id])?; - - let returned = match result.iter().next() { - Some(row) => row, - None => return Err(err_msg("cryp not found")), - }; - - // tells from_slice to cast into a cryp - let cryp_bytes: Vec = returned.get("data"); - let plr: Cryp = from_slice::(&cryp_bytes)?; - - // TEMP - if plr.is_ko() { - return Err(err_msg("cryp is ko")); - // plr.rez(); + if cryps.len() > 3 { + return Err(err_msg("team size too large (3 max)")); } - let mob = generate_mob(&plr); - + // create the game let mut game = Game::new(); - - let game_id = game.id; + // let game_id = game.id; game + .set_pve(true) .set_team_num(2) - .set_team_size(1); + .set_team_size(cryps.len()); + // create the mob team + let mut mob_team = Team::new(Uuid::nil()); + let mob_lvl = cryps.iter().max_by_key(|c| c.lvl).unwrap().lvl; + let mobs = iter::repeat_with(|| generate_mob(mob_lvl).set_account(Uuid::nil())) + .take(cryps.len()) + .collect::>(); + + // add the players let mut plr_team = Team::new(account.id); plr_team - .set_cryps(vec![plr]); + .set_cryps(cryps); - let mut mob_team = Team::new(Uuid::nil()); mob_team - .set_cryps(vec![mob]); + .set_cryps(mobs); game .add_team(plr_team)? @@ -649,7 +641,7 @@ pub fn game_pve(params: GamePveParams, tx: &mut Transaction, account: &Account) // persist game_new(&game, tx)?; - players_write(&game.team_by_id(account.id), game_id, tx)?; + // players_write(&game.team_by_id(account.id), game_id, tx)?; Ok(game) } @@ -691,6 +683,10 @@ pub fn game_join(params: GameJoinParams, tx: &mut Transaction, account: &Account .map(|id| cryp_get(tx, *id, account.id)) .collect::, Error>>()?; + if cryps.len() != game.team_size { + return Err(format_err!("incorrect team size. ({:?})", game.team_size)); + } + let mut team = Team::new(account.id); team.set_cryps(cryps); diff --git a/server/src/rpc.rs b/server/src/rpc.rs index b9503126..f96cbb3c 100755 --- a/server/src/rpc.rs +++ b/server/src/rpc.rs @@ -390,7 +390,7 @@ struct GamePveMsg { #[derive(Debug,Clone,Serialize,Deserialize)] pub struct GamePveParams { - pub id: Uuid, + pub cryp_ids: Vec, } #[derive(Debug,Clone,Serialize,Deserialize)]