diff --git a/server/src/combat.rs b/server/src/combat.rs deleted file mode 100755 index 6975288d..00000000 --- a/server/src/combat.rs +++ /dev/null @@ -1,82 +0,0 @@ -use uuid::Uuid; -use rand::prelude::*; -use serde_cbor::{from_slice}; - -// Db Commons -use postgres::transaction::Transaction; -use failure::Error; -use failure::err_msg; - -use account::Account; -use rpc::{CombatPveParams}; - -use cryp::{Cryp, cryp_write}; -use game::{Game, Team}; -// use skill::Skill; - -fn generate_mob(plr: &Cryp) -> Cryp { - let mut rng = thread_rng(); - - // rng panics on min == max - let mob_lvl: u8 = match plr.lvl { - 1 => 1, - _ => rng.gen_range(1, plr.lvl) - }; - - return Cryp::new() - .named(&"bamboo basher".to_string()) - .level(mob_lvl) - .create(); - -} - -pub fn pve(params: CombatPveParams, tx: &mut Transaction, account: &Account) -> Result { - let query = " - SELECT * - FROM cryps - WHERE id = $1 - AND account = $2; - "; - - 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.hp.value == 0 { - return Err(err_msg("cryp is ko")); - // plr.rez(); - } - - let mob = generate_mob(&plr); - - let mut game = Game::new(); - - game - .set_team_num(2) - .set_team_size(1); - - let mut plr_team = Team::new(account.id); - plr_team - .set_cryps(vec![plr]); - - let mut mob_team = Team::new(Uuid::nil()); - mob_team - .set_cryps(vec![mob]); - - game - .add_team(plr_team) - .add_team(mob_team); - - game.start(); - - Ok(game) -} diff --git a/server/src/game.rs b/server/src/game.rs index 3222f246..654feab5 100755 --- a/server/src/game.rs +++ b/server/src/game.rs @@ -8,7 +8,7 @@ use failure::Error; use failure::err_msg; use account::Account; -use rpc::GameAbilityParams; +use rpc::{GameAbilityParams, GamePveParams}; use cryp::{Cryp, CrypStat, Stat}; #[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)] @@ -396,6 +396,72 @@ pub fn game_write(game: Game, tx: &mut Transaction) -> Result { return Ok(game); } +fn generate_mob(plr: &Cryp) -> Cryp { + let mut rng = thread_rng(); + + // rng panics on min == max + let mob_lvl: u8 = match plr.lvl { + 1 => 1, + _ => rng.gen_range(1, plr.lvl) + }; + + return Cryp::new() + .named(&"bamboo basher".to_string()) + .level(mob_lvl) + .create(); + +} + +pub fn game_pve(params: GamePveParams, tx: &mut Transaction, account: &Account) -> Result { + let query = " + SELECT * + FROM cryps + WHERE id = $1 + AND account = $2; + "; + + 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.hp.value == 0 { + return Err(err_msg("cryp is ko")); + // plr.rez(); + } + + let mob = generate_mob(&plr); + + let mut game = Game::new(); + + game + .set_team_num(2) + .set_team_size(1); + + let mut plr_team = Team::new(account.id); + plr_team + .set_cryps(vec![plr]); + + let mut mob_team = Team::new(Uuid::nil()); + mob_team + .set_cryps(vec![mob]); + + game + .add_team(plr_team) + .add_team(mob_team); + + game.start(); + + Ok(game) +} #[cfg(test)] mod tests { diff --git a/server/src/main.rs b/server/src/main.rs index 57730425..709a5e77 100755 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -20,7 +20,6 @@ extern crate failure; mod cryp; mod game; mod net; -mod combat; // mod skill; mod rpc; mod account; diff --git a/server/src/rpc.rs b/server/src/rpc.rs index d9297fd4..63a49cf4 100755 --- a/server/src/rpc.rs +++ b/server/src/rpc.rs @@ -11,8 +11,7 @@ use failure::err_msg; use net::Db; use cryp::{Cryp, cryp_spawn}; -use game::{Game, AbilityKind, game_ability}; -use combat::{pve}; +use game::{Game, AbilityKind, game_pve, game_ability}; use account::{Account, account_create, account_login, account_from_token, account_cryps}; use item::{Item, items_list, item_use}; @@ -39,7 +38,7 @@ impl Rpc { // match on that to determine what fn to call let response = match v.method.as_ref() { "cryp_spawn" => Rpc::cryp_spawn(data, &mut tx, account, client), - "combat_pve" => Rpc::combat_pve(data, &mut tx, account, client), + "game_pve" => Rpc::game_pve(data, &mut tx, account, client), "game_ability" => Rpc::game_ability(data, &mut tx, account, client), "account_create" => Rpc::account_create(data, &mut tx, account, client), "account_login" => Rpc::account_login(data, &mut tx, account, client), @@ -66,17 +65,17 @@ impl Rpc { } } - fn combat_pve(data: Vec, tx: &mut Transaction, account: Option, client: &mut WebSocket) -> Result { + fn game_pve(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 msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; let game_response = RpcResponse { method: "game_state".to_string(), - params: RpcResult::Pve(pve(msg.params, tx, &a)?) + params: RpcResult::Pve(game_pve(msg.params, tx, &a)?) }; Rpc::send_msg(client, RpcResponse { @@ -218,13 +217,13 @@ pub struct CrypSpawnParams { } #[derive(Debug,Clone,Serialize,Deserialize)] -struct CombatPveMsg { +struct GamePveMsg { method: String, - params: CombatPveParams, + params: GamePveParams, } #[derive(Debug,Clone,Serialize,Deserialize)] -pub struct CombatPveParams { +pub struct GamePveParams { pub id: Uuid, }