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
+30 -34
View File
@@ -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<Game, Error> {
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::<Result<Vec<Cryp>, Error>>()?;
let result = tx
.query(query, &[&params.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<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();
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::<Vec<Cryp>>();
// 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::<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);
team.set_cryps(cryps);
+1 -1
View File
@@ -390,7 +390,7 @@ struct GamePveMsg {
#[derive(Debug,Clone,Serialize,Deserialize)]
pub struct GamePveParams {
pub id: Uuid,
pub cryp_ids: Vec<Uuid>,
}
#[derive(Debug,Clone,Serialize,Deserialize)]