pve and pvp back

This commit is contained in:
ntr 2018-11-21 16:32:39 +11:00
parent 4d648c8fe7
commit 41c7c790f2
5 changed files with 52 additions and 49 deletions

View File

@ -17,6 +17,8 @@ class Lobbies extends Phaser.GameObjects.Group {
super(list); super(list);
this.keyboard = list.input.keyboard; this.keyboard = list.input.keyboard;
const ws = list.registry.get('ws');
const X_POS = ROW_WIDTH + 50; const X_POS = ROW_WIDTH + 50;
const pvp = list.add 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)); .add(list.add.text(pve.getCenter().x, pve.getCenter().y, 'PVE', TEXT.HEADER));
pvp.on('pointerdown', () => { 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', () => { 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); this.add(pve);

View File

@ -128,8 +128,8 @@ function createSocket(store) {
send({ method: 'game_state', params: { id } }); send({ method: 'game_state', params: { id } });
} }
function sendGamePve(id) { function sendGamePve(crypIds) {
send({ method: 'game_pve', params: { id } }); send({ method: 'game_pve', params: { cryp_ids: crypIds } });
} }
function sendGamePvp(crypIds) { function sendGamePvp(crypIds) {

View File

@ -5,7 +5,6 @@
# WORK WORK # WORK WORK
* move rpc functions out
* unwrap account for all functions except list * unwrap account for all functions except list
* handle unserializable cryps * handle unserializable cryps
@ -16,25 +15,23 @@
* Scrabble grid * Scrabble grid
* skills * skills
* offensive -> choose target ✔ * handle setting account better maybe?
* ensure cryp untargetable and doesn't resolve when KO * ensure cryp untargetable and doesn't resolve when KO
* calculate * calculate
* hp increase/decrease * hp increase/decrease
* spell/phys dmg
* private fields for opponents * private fields for opponents
* cooldowns reduce each turn ✔ * attack
* statuses reduce each turn
* teach cyps skills ✔
* can you attack yourself? * can you attack yourself?
* fetch existing battles * fetch existing battles
* check for game participation * check for game participation
* write players row for every team+cryp added * write players row for every team+cryp added
* return results<> * return results<>
* defensive * defensive
* Cryp Generation
*
* Items * Items
* rez ✔
* unselect item with esc + button * unselect item with esc + button
* Grid reroll * Grid reroll
* Colour scheme * Colour scheme
@ -43,9 +40,14 @@
* Bosses * 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 ✔ * ws reconnect ✔
* Levelling ✔ * Levelling ✔
* Logins ✔️ * Logins ✔️
@ -65,6 +67,7 @@
# Art Styles # Art Styles
* Aztec * Aztec
* youkai
* Pixel * Pixel
* Industrial * Industrial

View File

@ -1,6 +1,8 @@
use uuid::Uuid; use uuid::Uuid;
use rand::prelude::*; use rand::prelude::*;
use std::iter;
// Db Commons // Db Commons
use serde_cbor::{from_slice, to_vec}; use serde_cbor::{from_slice, to_vec};
use postgres::transaction::Transaction; use postgres::transaction::Transaction;
@ -581,13 +583,13 @@ pub fn game_update(game: &Game, tx: &mut Transaction) -> Result<(), Error> {
return Ok(()); return Ok(());
} }
fn generate_mob(plr: &Cryp) -> Cryp { fn generate_mob(lvl: u8) -> Cryp {
let mut rng = thread_rng(); let mut rng = thread_rng();
// rng panics on min == max // rng panics on min == max
let mob_lvl: u8 = match plr.lvl { let mob_lvl: u8 = match lvl {
1 => 1, 1 => 1,
_ => rng.gen_range(plr.lvl.saturating_sub(2), plr.lvl) _ => rng.gen_range(lvl.saturating_sub(2), lvl)
}; };
return Cryp::new() 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<Game, Error> { pub fn game_pve(params: GamePveParams, tx: &mut Transaction, account: &Account) -> Result<Game, Error> {
let query = " let cryps = params.cryp_ids
SELECT * .iter()
FROM cryps .map(|id| cryp_get(tx, *id, account.id))
WHERE id = $1 .collect::<Result<Vec<Cryp>, Error>>()?;
AND account = $2;
";
let result = tx if cryps.len() > 3 {
.query(query, &[&params.id, &account.id])?; return Err(err_msg("team size too large (3 max)"));
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<u8> = returned.get("data");
let plr: Cryp = from_slice::<Cryp>(&cryp_bytes)?;
// TEMP
if plr.is_ko() {
return Err(err_msg("cryp is ko"));
// plr.rez();
} }
let mob = generate_mob(&plr); // create the game
let mut game = Game::new(); let mut game = Game::new();
// let game_id = game.id;
let game_id = game.id;
game game
.set_pve(true)
.set_team_num(2) .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::<Vec<Cryp>>();
// add the players
let mut plr_team = Team::new(account.id); let mut plr_team = Team::new(account.id);
plr_team plr_team
.set_cryps(vec![plr]); .set_cryps(cryps);
let mut mob_team = Team::new(Uuid::nil());
mob_team mob_team
.set_cryps(vec![mob]); .set_cryps(mobs);
game game
.add_team(plr_team)? .add_team(plr_team)?
@ -649,7 +641,7 @@ pub fn game_pve(params: GamePveParams, tx: &mut Transaction, account: &Account)
// persist // persist
game_new(&game, tx)?; 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) 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)) .map(|id| cryp_get(tx, *id, account.id))
.collect::<Result<Vec<Cryp>, Error>>()?; .collect::<Result<Vec<Cryp>, Error>>()?;
if cryps.len() != game.team_size {
return Err(format_err!("incorrect team size. ({:?})", game.team_size));
}
let mut team = Team::new(account.id); let mut team = Team::new(account.id);
team.set_cryps(cryps); team.set_cryps(cryps);

View File

@ -390,7 +390,7 @@ struct GamePveMsg {
#[derive(Debug,Clone,Serialize,Deserialize)] #[derive(Debug,Clone,Serialize,Deserialize)]
pub struct GamePveParams { pub struct GamePveParams {
pub id: Uuid, pub cryp_ids: Vec<Uuid>,
} }
#[derive(Debug,Clone,Serialize,Deserialize)] #[derive(Debug,Clone,Serialize,Deserialize)]