rpc battle cmd
This commit is contained in:
parent
3478d98c82
commit
f825b26b37
@ -1,4 +1,6 @@
|
||||
* Battling
|
||||
* Levelling
|
||||
* Global rolls
|
||||
* Logins ✔️
|
||||
* Cryp Ownership ✔
|
||||
* Matchmaking
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
// use uuid::Uuid;
|
||||
use rand::prelude::*;
|
||||
// use rand::prelude::*;
|
||||
|
||||
use cryp::Cryp;
|
||||
|
||||
@ -12,10 +12,10 @@ use cryp::Cryp;
|
||||
// }
|
||||
// }
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
pub struct Battle {
|
||||
a: Cryp,
|
||||
b: Cryp,
|
||||
pub a: Cryp,
|
||||
pub b: Cryp,
|
||||
}
|
||||
|
||||
impl Battle {
|
||||
@ -31,6 +31,10 @@ impl Battle {
|
||||
}
|
||||
|
||||
pub fn next(&mut self) -> &mut Battle {
|
||||
if self.finished() {
|
||||
panic!("{:?} is finished", self);
|
||||
}
|
||||
|
||||
let a_turn = self.a.turn();
|
||||
let b_turn = self.b.turn();
|
||||
|
||||
|
||||
@ -1,4 +1,14 @@
|
||||
use rand::prelude::*;
|
||||
use serde_cbor::{from_slice};
|
||||
|
||||
// Db Commons
|
||||
use failure::Error;
|
||||
use failure::err_msg;
|
||||
|
||||
use net::Db;
|
||||
use account::Account;
|
||||
use rpc::{CombatPveParams};
|
||||
|
||||
use cryp::Cryp;
|
||||
use battle::Battle;
|
||||
use skill::Skill;
|
||||
@ -10,7 +20,7 @@ struct Encounter {
|
||||
}
|
||||
|
||||
|
||||
pub fn battle(a: &Cryp, b: &Cryp) -> Battle {
|
||||
pub fn battle_resolve(a: &Cryp, b: &Cryp) -> Battle {
|
||||
let mut battle = Battle::new(a, b);
|
||||
loop {
|
||||
battle.next();
|
||||
@ -20,16 +30,16 @@ pub fn battle(a: &Cryp, b: &Cryp) -> Battle {
|
||||
}
|
||||
}
|
||||
|
||||
fn pve(plr: Cryp) -> Encounter {
|
||||
fn pve_completion(plr: Cryp) -> Encounter {
|
||||
let mut rng = thread_rng();
|
||||
let mob_lvl: u8 = rng.gen_range(1, plr.lvl);
|
||||
|
||||
let mob = Cryp::new()
|
||||
.named("bamboo basher".to_string())
|
||||
.named(&"bamboo basher".to_string())
|
||||
.level(mob_lvl)
|
||||
.create();
|
||||
|
||||
let outcome = battle(&plr, &mob);
|
||||
let outcome = battle_resolve(&plr, &mob);
|
||||
|
||||
let success = match outcome.winner() {
|
||||
Some(c) => c.id == plr.id,
|
||||
@ -43,9 +53,9 @@ fn pve(plr: Cryp) -> Encounter {
|
||||
};
|
||||
}
|
||||
|
||||
pub fn levelling(mut c: Cryp) -> Cryp {
|
||||
pub fn keep_levelling(mut c: Cryp) -> Cryp {
|
||||
loop {
|
||||
let enc = pve(c);
|
||||
let enc = pve_completion(c);
|
||||
c = enc.player;
|
||||
|
||||
if !enc.success {
|
||||
@ -66,19 +76,54 @@ pub fn levelling(mut c: Cryp) -> Cryp {
|
||||
}
|
||||
}
|
||||
|
||||
fn generate_mob(plr: &Cryp) -> Cryp {
|
||||
let mut rng = thread_rng();
|
||||
let mob_lvl: u8 = rng.gen_range(1, plr.lvl);
|
||||
|
||||
return Cryp::new()
|
||||
.named(&"bamboo basher".to_string())
|
||||
.level(mob_lvl)
|
||||
.create();
|
||||
|
||||
}
|
||||
|
||||
pub fn pve(params: CombatPveParams, db: Db, account: Account) -> Result<Battle, Error> {
|
||||
let query = "
|
||||
SELECT *
|
||||
FROM cryps
|
||||
WHERE id = $1;
|
||||
";
|
||||
|
||||
let result = db
|
||||
.query(query, &[¶ms.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)?;
|
||||
|
||||
let mob = generate_mob(&plr);
|
||||
return Ok(Battle::new(&plr, &mob));
|
||||
}
|
||||
|
||||
|
||||
pub fn test_battle() {
|
||||
let mut a = Cryp::new()
|
||||
.named("pronounced \"creeep\"".to_string())
|
||||
let a = Cryp::new()
|
||||
.named(&"pronounced \"creeep\"".to_string())
|
||||
.level(8)
|
||||
.learn(Skill::Stoney)
|
||||
.create();
|
||||
|
||||
let b = Cryp::new()
|
||||
.named("lemongrass tea".to_string())
|
||||
.named(&"lemongrass tea".to_string())
|
||||
.level(8)
|
||||
.create();
|
||||
|
||||
let outcome = battle(&a, &b);
|
||||
let outcome = battle_resolve(&a, &b);
|
||||
|
||||
match outcome.winner() {
|
||||
Some(w) => println!("{:?} is the winner with {:?} hp remaining", w.name, w.hp),
|
||||
@ -91,19 +136,32 @@ pub fn test_battle() {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use combat::*;
|
||||
|
||||
#[test]
|
||||
fn pve_test() {
|
||||
fn pve_completion_test() {
|
||||
let player = Cryp::new()
|
||||
.named("ca phe sua da".to_string())
|
||||
.named(&"ca phe sua da".to_string())
|
||||
.level(2)
|
||||
.create();
|
||||
|
||||
levelling(player);
|
||||
keep_levelling(player);
|
||||
return;
|
||||
}
|
||||
|
||||
// #[test]
|
||||
// fn pve_test() {
|
||||
// let name = "ca phe sua da".to_string();
|
||||
// let player = Cryp::new()
|
||||
// .named(&name)
|
||||
// .level(2)
|
||||
// .create();
|
||||
|
||||
// let _battle = pve(player);
|
||||
// return;
|
||||
// }
|
||||
|
||||
|
||||
#[test]
|
||||
fn battle_test() {
|
||||
test_battle();
|
||||
|
||||
@ -108,7 +108,7 @@ impl Cryp {
|
||||
};
|
||||
}
|
||||
|
||||
pub fn named(mut self, name: String) -> Cryp {
|
||||
pub fn named(mut self, name: &String) -> Cryp {
|
||||
self.name = name.clone();
|
||||
self
|
||||
}
|
||||
@ -190,7 +190,7 @@ impl Cryp {
|
||||
|
||||
pub fn spawn(params: CrypSpawnParams, db: Db, account: Account) -> Result<Cryp, Error> {
|
||||
let cryp = Cryp::new()
|
||||
.named(params.name)
|
||||
.named(¶ms.name)
|
||||
.level(1)
|
||||
.set_account(account.id)
|
||||
.create();
|
||||
@ -226,7 +226,7 @@ mod tests {
|
||||
#[test]
|
||||
fn create_cryp_test() {
|
||||
let max_level = Cryp::new()
|
||||
.named("hatchling".to_string())
|
||||
.named(&"hatchling".to_string())
|
||||
.level(64)
|
||||
.learn(Skill::Stoney)
|
||||
.create();
|
||||
|
||||
@ -1,10 +1,13 @@
|
||||
use ws::{Message};
|
||||
use serde_cbor::{from_slice};
|
||||
use uuid::Uuid;
|
||||
use failure::Error;
|
||||
use failure::err_msg;
|
||||
|
||||
use net::Db;
|
||||
use cryp::{Cryp, spawn};
|
||||
use battle::{Battle};
|
||||
use combat::{pve};
|
||||
use account::{Account, create, login, from_token};
|
||||
|
||||
pub struct Rpc;
|
||||
@ -42,6 +45,20 @@ impl Rpc {
|
||||
Err(_e) => Err(err_msg("invalid params")),
|
||||
}
|
||||
},
|
||||
"combat_pve" => {
|
||||
match from_slice::<CombatPveMsg>(&data) {
|
||||
Ok(v) => {
|
||||
match account {
|
||||
Some(u) => Ok(RpcResponse {
|
||||
method: v.method,
|
||||
params: RpcResult::Pve(pve(v.params, db, u)?)
|
||||
}),
|
||||
None => Err(err_msg("auth required")),
|
||||
}
|
||||
}
|
||||
Err(_e) => Err(err_msg("invalid params")),
|
||||
}
|
||||
},
|
||||
"account_create" => {
|
||||
match from_slice::<AccountCreateMsg>(&data) {
|
||||
Ok(v) => Ok(RpcResponse {
|
||||
@ -79,6 +96,7 @@ pub struct RpcResponse {
|
||||
pub enum RpcResult {
|
||||
SpawnCryp(Cryp),
|
||||
Account(Account),
|
||||
Pve(Battle),
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
@ -98,6 +116,17 @@ pub struct CrypSpawnParams {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
struct CombatPveMsg {
|
||||
method: String,
|
||||
params: CombatPveParams,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
pub struct CombatPveParams {
|
||||
pub id: Uuid,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
struct AccountCreateMsg {
|
||||
method: String,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user