diff --git a/client/src/components/battle.jsx b/client/src/components/battle.jsx
index 8e2c8fe4..99a0f410 100644
--- a/client/src/components/battle.jsx
+++ b/client/src/components/battle.jsx
@@ -3,14 +3,17 @@ const preact = require('preact');
function CrypPanel({ battle }) {
if (!battle) return
...
;
return (
-
-
{JSON.stringify(battle.a)}
-
{JSON.stringify(battle.b)}
-
- {battle.log.map((l, i) => (
{l}
))}
-
-
- );
+ {JSON.stringify(battle)}
+ )
+ // return (
+ //
+ //
{JSON.stringify(battle.a)}
+ //
{JSON.stringify(battle.b)}
+ //
+ // {battle.log.map((l, i) => (
{l}
))}
+ //
+ //
+ // );
}
module.exports = CrypPanel;
diff --git a/server/src/battle.rs b/server/src/battle.rs
index 8e8499a5..fe0f1b33 100755
--- a/server/src/battle.rs
+++ b/server/src/battle.rs
@@ -1,6 +1,14 @@
use uuid::Uuid;
use rand::prelude::*;
+// Db Commons
+use serde_cbor::{from_slice, to_vec};
+use postgres::transaction::Transaction;
+use failure::Error;
+use failure::err_msg;
+
+use account::Account;
+use rpc::BattleAbilityParams;
use cryp::{Cryp, CrypStat, Stat};
#[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
@@ -54,12 +62,12 @@ impl Ability {
// roll = c.skills.iter().fold(roll, |roll, s| s.apply(roll));
// finally combine with stat
- // log.push(format!("{:064b} <- finalised", roll.result));
+ println!("{:064b} <- finalised", roll.result);
roll.result = roll.result & stat.value;
- // log.push(format!("{:064b} & <- attribute roll", self.value));
+ println!("{:064b} & <- attribute roll", stat.value);
println!("{:064b} = {:?}", roll.result, roll.result);
- // log.push(format!(""));
+ println!("");
return roll;
}
@@ -96,9 +104,9 @@ pub struct Team {
}
impl Team {
- pub fn new() -> Team {
+ pub fn new(account: Uuid) -> Team {
return Team {
- id: Uuid::new_v4(),
+ id: account,
cryps: vec![],
abilities: vec![],
incoming: vec![],
@@ -124,6 +132,7 @@ impl Team {
#[derive(Debug,Clone,Serialize,Deserialize)]
pub struct Battle {
+ pub id: Uuid,
pub team_size: usize,
pub team_num: usize,
pub teams: Vec,
@@ -135,6 +144,7 @@ pub struct Battle {
impl Battle {
pub fn new() -> Battle {
return Battle {
+ id: Uuid::new_v4(),
team_size: 0,
team_num: 0,
teams: vec![],
@@ -287,7 +297,7 @@ impl Battle {
if self.is_finished() {
self.phase = Phase::Finish;
- return self
+ return self;
}
self.ability_phase_start();
@@ -300,11 +310,6 @@ impl Battle {
panic!("battle not in damage phase");
}
- // go through every team
- // roll the incoming ability
- // resolve
- // check if battle is over
-
// sometimes... you just gotta
for team in self.teams.clone().iter_mut() {
for incoming in team.incoming.clone().iter_mut() {
@@ -321,42 +326,56 @@ impl Battle {
pub fn is_finished(&self) -> bool {
self.teams.iter().any(|t| t.cryps.iter().all(|c| c.is_ko()))
}
-
- // pub fn next(&mut self) -> &mut Battle {
- // if self.finished() {
- // panic!("{:?} is finished", self);
- // }
-
- // let mut a_turn = self.a.turn();
- // let mut b_turn = self.b.turn();
-
- // self.a.assign_dmg(&self.b, &mut a_turn, &b_turn);
- // self.b.assign_dmg(&self.a, &mut b_turn, &a_turn);
-
- // self.log.append(&mut a_turn.log);
- // self.log.append(&mut b_turn.log);
-
- // self
- // }
+}
- // pub fn cryp_by_id(&self, id: Uuid) -> &Cryp {
- // match self.cryps().iter().find(|c| c.id == id) {
- // Some(c) => c,
- // None => panic!("cryp not in battle {:?}", self),
- // }
- // }
+// add the ability
+// add client function call
+// check for cryp ability ownership
+// check for battle participation
- // pub fn winner(&self) -> Option<&Cryp> {
- // if self.cryps().iter().all(|c| c.is_ko()) {
- // return None
- // }
+pub fn battle_ability(params: BattleAbilityParams, tx: &mut Transaction, account: &Account) -> Result {
+ let query = "
+ SELECT *
+ FROM battles
+ WHERE id = $1
+ ";
- // match self.cryps().iter().find(|c| !c.is_ko()) {
- // Some(w) => Some(w),
- // None => panic!("no winner found {:?}", self),
- // }
- // }
+ let result = tx
+ .query(query, &[¶ms.battle_id])?;
+
+ let returned = match result.iter().next() {
+ Some(row) => row,
+ None => return Err(err_msg("battle not found")),
+ };
+
+ // tells from_slice to cast into a cryp
+ let battle_bytes: Vec = returned.get("data");
+ let mut battle = from_slice::(&battle_bytes)?;
+
+ battle.add_ability(account.id, params.cryp_id, params.target_team_id, params.kind);
+
+ return battle_write(battle, tx);
+}
+
+pub fn battle_write(battle: Battle, tx: &mut Transaction) -> Result {
+ let battle_bytes = to_vec(&battle)?;
+
+ let query = "
+ UPDATE battles
+ SET data = $1
+ WHERE id = $2
+ RETURNING id, data;
+ ";
+
+ let result = tx
+ .query(query, &[&battle_bytes, &battle.id])?;
+
+ let _returned = result.iter().next().expect("no row returned");
+
+ println!("{:?} wrote battle", battle.id);
+
+ return Ok(battle);
}
@@ -387,13 +406,13 @@ mod tests {
.set_team_num(2)
.set_team_size(1);
- let mut x_team = Team::new();
- let x_team_id = x_team.id;
+ let x_team_id = Uuid::new_v4();
+ let mut x_team = Team::new(x_team_id);
x_team
.set_cryps(vec![x]);
- let mut y_team = Team::new();
- let y_team_id = y_team.id;
+ let y_team_id = Uuid::new_v4();
+ let mut y_team = Team::new(y_team_id);
y_team
.set_cryps(vec![y]);
@@ -423,6 +442,8 @@ mod tests {
assert!([Phase::Ability, Phase::Finish].contains(&battle.phase));
+ println!("{:?}", battle);
+
return;
}
}
diff --git a/server/src/combat.rs b/server/src/combat.rs
index 03d2dd18..d061be0d 100755
--- a/server/src/combat.rs
+++ b/server/src/combat.rs
@@ -1,3 +1,4 @@
+use uuid::Uuid;
use rand::prelude::*;
use serde_cbor::{from_slice};
@@ -10,7 +11,7 @@ use account::Account;
use rpc::{CombatPveParams};
use cryp::{Cryp, cryp_write};
-use battle::Battle;
+use battle::{Battle, Team};
// use skill::Skill;
fn generate_mob(plr: &Cryp) -> Cryp {
@@ -30,54 +31,52 @@ fn generate_mob(plr: &Cryp) -> Cryp {
}
pub fn pve(params: CombatPveParams, tx: &mut Transaction, account: &Account) -> Result {
- return Err(err_msg("cryp is ko"));
+ let query = "
+ SELECT *
+ FROM cryps
+ WHERE id = $1
+ AND account = $2;
+ ";
- // let query = "
- // SELECT *
- // FROM cryps
- // WHERE id = $1
- // AND account = $2;
- // ";
+ let result = tx
+ .query(query, &[¶ms.id, &account.id])?;
- // 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")),
+ };
- // 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)?;
- // // 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();
+ }
- // // TEMP
- // if plr.hp.value == 0 {
- // return Err(err_msg("cryp is ko"));
- // // plr.rez();
- // }
+ let mob = generate_mob(&plr);
- // let mob = generate_mob(&plr);
- // let mut battle = Battle::new(&plr, &mob);
+ let mut battle = Battle::new();
- // loop {
- // battle.next();
+ battle
+ .set_team_num(2)
+ .set_team_size(1);
- // if battle.finished() {
- // let success = match battle.winner() {
- // Some(c) => c.id == plr.id,
- // None => false,
- // };
+ let mut plr_team = Team::new(account.id);
+ plr_team
+ .set_cryps(vec![plr]);
- // let mut post_battle_plr = battle.cryp_by_id(plr.id).clone();
+ let mut mob_team = Team::new(Uuid::nil());
+ mob_team
+ .set_cryps(vec![mob]);
- // if success {
- // post_battle_plr = post_battle_plr.add_xp();
- // }
+ battle
+ .add_team(plr_team)
+ .add_team(mob_team);
- // cryp_write(post_battle_plr, tx)?;
+ battle.start();
- // break Ok(battle)
- // }
- // }
+ Ok(battle)
}
diff --git a/server/src/item.rs b/server/src/item.rs
index 3d46bf4b..a86ac114 100644
--- a/server/src/item.rs
+++ b/server/src/item.rs
@@ -36,7 +36,6 @@ impl Item {
fn apply(&mut self, tx: &mut Transaction, target: Uuid) -> Result<(), Error> {
match self.action {
ItemAction::Revive => revive(self, tx, target),
- _ => panic!("missing item action"),
}
}
}
diff --git a/server/src/main.rs b/server/src/main.rs
index eb5373b7..ccd142eb 100755
--- a/server/src/main.rs
+++ b/server/src/main.rs
@@ -4,7 +4,6 @@ extern crate tungstenite;
extern crate env_logger;
extern crate bcrypt;
-#[macro_use]
extern crate dotenv;
extern crate postgres;
extern crate r2d2;
diff --git a/server/src/rpc.rs b/server/src/rpc.rs
index 0414ef4a..33a4c6a6 100755
--- a/server/src/rpc.rs
+++ b/server/src/rpc.rs
@@ -11,7 +11,7 @@ use failure::err_msg;
use net::Db;
use cryp::{Cryp, cryp_spawn};
-use battle::{Battle};
+use battle::{Battle, AbilityKind, battle_ability};
use combat::{pve};
use account::{Account, account_create, account_login, account_from_token, account_cryps};
use item::{Item, items_list, item_use};
@@ -40,6 +40,7 @@ impl Rpc {
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),
+ "battle_ability" => Rpc::battle_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),
"account_cryps" => Rpc::account_cryps(data, &mut tx, account, client),
@@ -86,7 +87,28 @@ impl Rpc {
return Ok(battle_response);
}
- fn cryp_spawn(data: Vec, tx: &mut Transaction, account: Option, client: &mut WebSocket) -> Result {
+ fn battle_ability(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 battle_response = RpcResponse {
+ method: "battle_state".to_string(),
+ params: RpcResult::Pve(battle_ability(msg.params, tx, &a)?)
+ };
+
+ // Rpc::send_msg(client, RpcResponse {
+ // method: "account_cryps".to_string(),
+ // params: RpcResult::CrypList(account_cryps(tx, &a)?)
+ // })?;
+
+ return Ok(battle_response);
+ }
+
+ fn cryp_spawn(data: Vec, tx: &mut Transaction, account: Option, _client: &mut WebSocket) -> Result {
match from_slice::(&data) {
Ok(v) => {
match account {
@@ -101,7 +123,7 @@ impl Rpc {
}
}
- fn account_create(data: Vec, tx: &mut Transaction, account: Option, client: &mut WebSocket) -> Result {
+ fn account_create(data: Vec, tx: &mut Transaction, _account: Option, _client: &mut WebSocket) -> Result {
match from_slice::(&data) {
Ok(v) => Ok(RpcResponse {
method: v.method,
@@ -206,6 +228,20 @@ pub struct CombatPveParams {
pub id: Uuid,
}
+#[derive(Debug,Clone,Serialize,Deserialize)]
+struct BattleAbilityMsg {
+ method: String,
+ params: BattleAbilityParams,
+}
+
+#[derive(Debug,Clone,Serialize,Deserialize)]
+pub struct BattleAbilityParams {
+ pub battle_id: Uuid,
+ pub cryp_id: Uuid,
+ pub target_team_id: Uuid,
+ pub kind: AbilityKind,
+}
+
#[derive(Debug,Clone,Serialize,Deserialize)]
struct AccountCreateMsg {
method: String,