remove combat.rs
This commit is contained in:
parent
5a3a126086
commit
110616c22a
@ -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<Game, Error> {
|
||||
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<u8> = returned.get("data");
|
||||
let plr: Cryp = from_slice::<Cryp>(&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)
|
||||
}
|
||||
@ -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<Game, Error> {
|
||||
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<Game, Error> {
|
||||
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<u8> = returned.get("data");
|
||||
let plr: Cryp = from_slice::<Cryp>(&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 {
|
||||
|
||||
@ -20,7 +20,6 @@ extern crate failure;
|
||||
mod cryp;
|
||||
mod game;
|
||||
mod net;
|
||||
mod combat;
|
||||
// mod skill;
|
||||
mod rpc;
|
||||
mod account;
|
||||
|
||||
@ -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<u8>, tx: &mut Transaction, account: Option<Account>, client: &mut WebSocket<TcpStream>) -> Result<RpcResponse, Error> {
|
||||
fn game_pve(data: Vec<u8>, tx: &mut Transaction, account: Option<Account>, client: &mut WebSocket<TcpStream>) -> Result<RpcResponse, Error> {
|
||||
let a = match account {
|
||||
Some(a) => a,
|
||||
None => return Err(err_msg("auth required")),
|
||||
};
|
||||
|
||||
let msg = from_slice::<CombatPveMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||
let msg = from_slice::<GamePveMsg>(&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,
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user