cooldowns and linting

This commit is contained in:
ntr 2018-10-25 00:04:48 +11:00
parent 8be14ac546
commit e192edfc26
10 changed files with 292 additions and 255 deletions

View File

@ -33,6 +33,8 @@ module.exports = {
'no-plusplus': [0], 'no-plusplus': [0],
'no-await-in-loop': [0], 'no-await-in-loop': [0],
'indent': ['error', 4], 'indent': ['error', 4],
'keyword-spacing': ['error'],
'key-spacing': ['error'],
// for preact // for preact
"react/react-in-jsx-scope": [0], "react/react-in-jsx-scope": [0],

View File

@ -6,7 +6,7 @@
"scripts": { "scripts": {
"start": "parcel index.html --port 40080", "start": "parcel index.html --port 40080",
"build": "rm -rf dist && parcel build index.html", "build": "rm -rf dist && parcel build index.html",
"lint": "eslint --fix src/", "lint": "eslint --fix --ext .jsx src/",
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
}, },
"author": "", "author": "",

View File

@ -30,8 +30,8 @@ const addState = connect(
function renderBody(props){ function renderBody(props){
const {game, setGame} = props; const {game, setGame} = props;
if(game){ if (game){
return( return (
<div> <div>
<GameContainer /> <GameContainer />
<button <button

View File

@ -126,7 +126,7 @@ function GamePanel(props) {
// style={{ "min-height": "100%" }} // style={{ "min-height": "100%" }}
function phaseText(phase){ function phaseText(phase){
switch(phase){ switch (phase){
case 'Skill': case 'Skill':
return "Choose abilities" return "Choose abilities"
case 'Target': case 'Target':

View File

@ -27,6 +27,7 @@
* skills * skills
* offensive -> choose target ✔ * offensive -> choose target ✔
* private fields for opponents
* teach cyps skills * teach cyps skills
* can you attack yourself? * can you attack yourself?
* fetch existing battles * fetch existing battles

View File

@ -9,8 +9,31 @@ use failure::err_msg;
use account::Account; use account::Account;
use rpc::{CrypSpawnParams}; use rpc::{CrypSpawnParams};
use game::Skill; use skill::{Skill};
// use skill::{Skill};
type Cooldown = Option<u8>;
#[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
pub struct CrypSkill {
skill: Skill,
cd: Cooldown,
}
impl CrypSkill {
pub fn new(skill: Skill) -> CrypSkill {
let turns = match skill {
Skill::Attack => None,
Skill::Block => Some(1),
Skill::Heal => Some(2),
};
CrypSkill {
skill,
cd: turns,
}
}
}
#[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)] #[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
pub enum Stat { pub enum Stat {
@ -35,143 +58,135 @@ pub struct CrypStat {
} }
impl CrypStat { impl CrypStat {
fn set(&mut self, v: u64) -> &CrypStat { fn set(&mut self, v: u64) -> &CrypStat {
self.value = v; self.value = v;
self self
} }
// fn roll(&self, c: &Cryp, log: &mut Vec<String>) -> Roll { // fn roll(&self, c: &Cryp, log: &mut Vec<String>) -> Roll {
// let mut rng = thread_rng(); // let mut rng = thread_rng();
// let base: u64 = rng.gen(); // let base: u64 = rng.gen();
// let mut roll = Roll { kind: self.kind, base, result: base }; // let mut roll = Roll { kind: self.kind, base, result: base };
// log.push(format!("{:?}", self.kind)); // log.push(format!("{:?}", self.kind));
// log.push(format!("{:064b} <- base roll", base)); // log.push(format!("{:064b} <- base roll", base));
// // apply skills // // apply skills
// roll = c.skills.iter().fold(roll, |roll, s| s.apply(roll)); // roll = c.skills.iter().fold(roll, |roll, s| s.apply(roll));
// // finally combine with CrypStat // // finally combine with CrypStat
// log.push(format!("{:064b} <- finalised", roll.result)); // log.push(format!("{:064b} <- finalised", roll.result));
// roll.result = roll.result & self.value; // roll.result = roll.result & self.value;
// log.push(format!("{:064b} & <- attribute roll", self.value)); // log.push(format!("{:064b} & <- attribute roll", self.value));
// log.push(format!("{:064b} = {:?}", roll.result, roll.result)); // log.push(format!("{:064b} = {:?}", roll.result, roll.result));
// log.push(format!("")); // log.push(format!(""));
// return roll; // return roll;
// } // }
pub fn reduce(&mut self, dmg: u64) -> &mut CrypStat { pub fn reduce(&mut self, dmg: u64) -> &mut CrypStat {
self.value = self.value.saturating_sub(dmg); self.value = self.value.saturating_sub(dmg);
self self
} }
} }
pub struct Turn { pub struct Turn {
pub str: Roll, pub str: Roll,
pub agi: Roll, pub agi: Roll,
pub log: Vec<String>, pub log: Vec<String>,
} }
#[derive(Debug,Clone,Serialize,Deserialize)] #[derive(Debug,Clone,Serialize,Deserialize)]
pub struct Cryp { pub struct Cryp {
pub id: Uuid, pub id: Uuid,
pub account: Uuid, pub account: Uuid,
pub str: CrypStat, pub str: CrypStat,
pub agi: CrypStat, pub agi: CrypStat,
pub int: CrypStat, pub int: CrypStat,
pub stam: CrypStat, pub stam: CrypStat,
pub hp: CrypStat, pub hp: CrypStat,
pub xp: u64, pub xp: u64,
pub lvl: u8, pub lvl: u8,
pub skills: Vec<Skill>, pub skills: Vec<CrypSkill>,
pub name: String, pub name: String,
} }
fn check_lvl(lvl: u8) -> u8 { fn check_lvl(lvl: u8) -> u8 {
if lvl > 64 { return 64; } if lvl > 64 { return 64; }
return lvl; return lvl;
} }
impl Cryp { impl Cryp {
pub fn new() -> Cryp { pub fn new() -> Cryp {
let id = Uuid::new_v4(); let id = Uuid::new_v4();
return Cryp { return Cryp {
id, id,
account: id, account: id,
str: CrypStat { value: 0, stat: Stat::Str }, str: CrypStat { value: 0, stat: Stat::Str },
agi: CrypStat { value: 0, stat: Stat::Agi }, agi: CrypStat { value: 0, stat: Stat::Agi },
int: CrypStat { value: 0, stat: Stat::Int }, int: CrypStat { value: 0, stat: Stat::Int },
stam: CrypStat { value: 0, stat: Stat::Stam }, stam: CrypStat { value: 0, stat: Stat::Stam },
hp: CrypStat { value: 0, stat: Stat::Hp }, hp: CrypStat { value: 0, stat: Stat::Hp },
lvl: 0, lvl: 0,
xp: 0, xp: 0,
skills: vec![Skill::Attack], skills: vec![CrypSkill::new(Skill::Attack)],
name: String::new() name: String::new()
}; };
}
pub fn named(mut self, name: &String) -> Cryp {
self.name = name.clone();
self
}
pub fn set_account(mut self, account: Uuid) -> Cryp {
self.account = account;
self
}
pub fn level(mut self, lvl: u8) -> Cryp {
self.lvl = check_lvl(lvl);
self
}
pub fn learn(mut self, s: Skill) -> Cryp {
self.skills.push(s);
self
}
pub fn add_xp(mut self) -> Cryp {
self.xp = self.xp.saturating_add(1);
if self.xp.is_power_of_two() {
return self.level_up();
} }
self
}
pub fn level_up(mut self) -> Cryp { pub fn named(mut self, name: &String) -> Cryp {
self.lvl = self.lvl.saturating_add(1); self.name = name.clone();
self.create() self
} }
// pub fn turn(&self) -> Turn { pub fn set_account(mut self, account: Uuid) -> Cryp {
// let mut log = vec![format!("{:?}'s turn:", self.name)]; self.account = account;
self
}
// let str = self.str.roll(self, &mut log);
// let agi = self.agi.roll(self, &mut log);
// return Turn { str, agi, log } pub fn level(mut self, lvl: u8) -> Cryp {
// } self.lvl = check_lvl(lvl);
self
}
pub fn create(mut self) -> Cryp { pub fn learn(mut self, s: Skill) -> Cryp {
let mut rng = thread_rng(); self.skills.push(CrypSkill::new(s));
let max = match self.lvl == 64 { self
true => u64::max_value(), }
false => 2_u64.pow(self.lvl.into()),
};
self.xp = max; pub fn add_xp(mut self) -> Cryp {
self.xp = self.xp.saturating_add(1);
if self.xp.is_power_of_two() {
return self.level_up();
}
self
}
self.str.set(rng.gen_range(1, max)); pub fn level_up(mut self) -> Cryp {
self.agi.set(rng.gen_range(1, max)); self.lvl = self.lvl.saturating_add(1);
self.int.set(rng.gen_range(1, max)); self.create()
self.stam.set(rng.gen_range(1, max)); }
self.hp.set(self.stam.value);
self pub fn create(mut self) -> Cryp {
} let mut rng = thread_rng();
let max = match self.lvl == 64 {
true => u64::max_value(),
false => 2_u64.pow(self.lvl.into()),
};
self.xp = max;
self.str.set(rng.gen_range(1, max));
self.agi.set(rng.gen_range(1, max));
self.int.set(rng.gen_range(1, max));
self.stam.set(rng.gen_range(1, max));
self.hp.set(self.stam.value);
self
}
// pub fn assign_str(&mut self, opp: &Cryp, plr_t: &mut Turn, opp_t: &Turn) -> &mut Cryp { // pub fn assign_str(&mut self, opp: &Cryp, plr_t: &mut Turn, opp_t: &Turn) -> &mut Cryp {
// // let final_str = opp_t.str.result.saturating_sub(plr_t.agi.result); // // let final_str = opp_t.str.result.saturating_sub(plr_t.agi.result);
@ -197,15 +212,22 @@ impl Cryp {
// self // self
// } // }
pub fn is_ko(&self) -> bool { pub fn is_ko(&self) -> bool {
self.hp.value == 0 self.hp.value == 0
} }
pub fn rez(&mut self) -> &mut Cryp { pub fn knows(&self, skill: Skill) -> bool {
self.hp.set(self.stam.value); self.skills.iter().any(|s| s.skill == skill)
self }
}
pub fn has_cooldown(&self, skill: Skill) -> bool {
self.skills.iter().any(|s| s.skill == skill && s.cd.is_some())
}
pub fn rez(&mut self) -> &mut Cryp {
self.hp.set(self.stam.value);
self
}
} }
pub fn cryp_get(tx: &mut Transaction, id: Uuid, account_id: Uuid) -> Result<Cryp, Error> { pub fn cryp_get(tx: &mut Transaction, id: Uuid, account_id: Uuid) -> Result<Cryp, Error> {
@ -230,6 +252,8 @@ pub fn cryp_spawn(params: CrypSpawnParams, tx: &mut Transaction, account: &Accou
let cryp = Cryp::new() let cryp = Cryp::new()
.named(&params.name) .named(&params.name)
.level(10) .level(10)
.learn(Skill::Block)
.learn(Skill::Heal)
.set_account(account.id) .set_account(account.id)
.create(); .create();
@ -278,13 +302,13 @@ mod tests {
#[test] #[test]
fn create_cryp_test() { fn create_cryp_test() {
let max_level = Cryp::new() let max_level = Cryp::new()
.named(&"hatchling".to_string()) .named(&"hatchling".to_string())
.level(64) .level(64)
// .learn(Skill::Stoney) // .learn(Skill::Stoney)
.create(); .create();
assert_eq!(max_level.lvl, 64); assert_eq!(max_level.lvl, 64);
return; return;
} }
} }

View File

@ -10,89 +10,14 @@ use failure::err_msg;
use account::Account; use account::Account;
use rpc::{GameStateParams, GameSkillParams, GamePveParams, GamePvpParams, GameTargetParams, GameJoinParams}; use rpc::{GameStateParams, GameSkillParams, GamePveParams, GamePvpParams, GameTargetParams, GameJoinParams};
use cryp::{Cryp, cryp_get}; use cryp::{Cryp, cryp_get};
use skill::{Skill, Turn};
#[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
pub struct Roll {
pub base: u64,
pub result: u64,
}
#[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
pub enum Skill {
Attack,
Block,
Heal,
}
#[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
pub struct GameSkill {
id: Uuid,
skill: Skill,
cryp_id: Uuid,
roll: Option<Roll>,
target_cryp_id: Option<Uuid>,
target_team_id: Uuid,
}
impl GameSkill {
pub fn new(cryp_id: Uuid, target_team_id: Uuid, skill: Skill) -> GameSkill {
return GameSkill {
id: Uuid::new_v4(),
cryp_id,
target_cryp_id: None,
target_team_id,
roll: None,
skill,
};
}
fn roll(&self, c: &Cryp) -> Roll {
let mut rng = thread_rng();
let base: u64 = rng.gen();
let stat = match self.skill {
Skill::Attack => &c.str,
Skill::Block => &c.str,
Skill::Heal => &c.int,
};
let mut roll = Roll { base, result: base };
// // apply skills
// roll = c.skills.iter().fold(roll, |roll, s| s.apply(roll));
// finally combine with stat
println!("{:?}'s stats", c.name);
println!("{:064b} <- finalised", roll.result);
roll.result = roll.result & stat.value;
println!("{:064b} & <- attribute roll", stat.value);
println!("{:064b} = {:?}", roll.result, roll.result);
println!("");
return roll;
}
pub fn resolve(&mut self, cryp: &mut Cryp, target: &mut Cryp) -> &mut GameSkill {
let roll = self.roll(&cryp);
println!("{:?} gettin clapped for {:?}", target.name, roll.result);
target.hp.reduce(roll.result);
self
}
pub fn set_target(&mut self, cryp_id: Uuid) -> &mut GameSkill {
self.target_cryp_id = Some(cryp_id);
self
}
}
#[derive(Debug,Clone,Serialize,Deserialize)] #[derive(Debug,Clone,Serialize,Deserialize)]
pub struct Team { pub struct Team {
id: Uuid, id: Uuid,
cryps: Vec<Cryp>, cryps: Vec<Cryp>,
skills: Vec<GameSkill>, skills: Vec<Turn>,
incoming: Vec<GameSkill>, incoming: Vec<Turn>,
} }
impl Team { impl Team {
@ -114,7 +39,7 @@ impl Team {
self.cryps.iter_mut().find(|c| c.id == id) self.cryps.iter_mut().find(|c| c.id == id)
} }
pub fn skill_by_id(&mut self, id: Uuid) -> &mut GameSkill { pub fn turn_by_id(&mut self, id: Uuid) -> &mut Turn {
match self.incoming.iter_mut().find(|a| a.id == id) { match self.incoming.iter_mut().find(|a| a.id == id) {
Some(a) => a, Some(a) => a,
None => panic!("abiltity not in game"), None => panic!("abiltity not in game"),
@ -278,9 +203,13 @@ impl Game {
}; };
// check the cryp has the skill // check the cryp has the skill
if !cryp.skills.contains(&skill) { if !cryp.knows(skill) {
return Err(err_msg("cryp does not have that skill")); return Err(err_msg("cryp does not have that skill"));
} }
if cryp.has_cooldown(skill) {
return Err(err_msg("abiltity on cooldown"));
}
} }
// replace cryp skill // replace cryp skill
@ -288,7 +217,7 @@ impl Game {
team.skills.remove(s); team.skills.remove(s);
} }
let skill = GameSkill::new(cryp_id, target_team_id, skill); let skill = Turn::new(cryp_id, target_team_id, skill);
team.skills.push(skill); team.skills.push(skill);
return Ok(skill.id); return Ok(skill.id);
@ -338,7 +267,7 @@ impl Game {
// targets can only be added by the owner of the team // targets can only be added by the owner of the team
fn add_target(&mut self, team_id: Uuid, cryp_id: Uuid, skill_id: Uuid) -> Result<&mut GameSkill, Error> { fn add_target(&mut self, team_id: Uuid, cryp_id: Uuid, skill_id: Uuid) -> Result<&mut Turn, Error> {
// whose team is this? // whose team is this?
let team = self.team_by_id(team_id); let team = self.team_by_id(team_id);
@ -349,8 +278,8 @@ impl Game {
}; };
// set the target // set the target
let skill = team.skill_by_id(skill_id); let turn = team.turn_by_id(skill_id);
Ok(skill.set_target(cryp_id)) Ok(turn.set_target(cryp_id))
} }
fn target_phase_finished(&self) -> bool { fn target_phase_finished(&self) -> bool {

View File

@ -20,7 +20,7 @@ extern crate serde_derive;
mod cryp; mod cryp;
mod game; mod game;
mod net; mod net;
// mod skill; mod skill;
mod rpc; mod rpc;
mod account; mod account;
mod item; mod item;

View File

@ -11,9 +11,10 @@ use failure::err_msg;
use net::Db; use net::Db;
use cryp::{Cryp, cryp_spawn}; use cryp::{Cryp, cryp_spawn};
use game::{Game, Skill, game_state, game_pve, game_pvp, game_join, game_skill, game_target}; use game::{Game, game_state, game_pve, game_pvp, game_join, game_skill, game_target};
use account::{Account, account_create, account_login, account_from_token, account_cryps}; use account::{Account, account_create, account_login, account_from_token, account_cryps};
use item::{Item, items_list, item_use}; use item::{Item, items_list, item_use};
use skill::{Skill};
pub struct Rpc; pub struct Rpc;

View File

@ -1,40 +1,120 @@
use cryp::{StatKind, Roll}; use rand::prelude::*;
use uuid::Uuid;
#[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
pub enum Skill { use cryp::{Cryp};
Stoney,
Evasive, #[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
} pub struct Roll {
pub base: u64,
impl Skill { pub result: u64,
pub fn apply(&self, roll: Roll) -> Roll { }
match self {
Skill::Stoney => stoney(self, roll), #[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
Skill::Evasive => evasive(self, roll), pub enum Skill {
} Attack,
} Block,
} Heal,
}
fn stoney(_s: &Skill, mut roll: Roll) -> Roll {
let effect = 0b11110000;
match roll.kind { #[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
StatKind::Def => { pub struct Turn {
// println!("{:064b} | <- {:?}", effect, s); pub id: Uuid,
roll.result = roll.result | effect; pub skill: Skill,
roll pub cryp_id: Uuid,
}, pub roll: Option<Roll>,
_ => roll, pub target_cryp_id: Option<Uuid>,
} pub target_team_id: Uuid,
} }
fn evasive(_s: &Skill, mut roll: Roll) -> Roll { impl Turn {
match roll.kind { pub fn new(cryp_id: Uuid, target_team_id: Uuid, skill: Skill) -> Turn {
StatKind::Def => { return Turn {
if roll.result.is_power_of_two() { id: Uuid::new_v4(),
roll.result = u64::max_value() cryp_id,
} target_cryp_id: None,
roll target_team_id,
}, roll: None,
_ => roll, skill,
} };
} }
fn roll(&self, c: &Cryp) -> Roll {
let mut rng = thread_rng();
let base: u64 = rng.gen();
let stat = match self.skill {
Skill::Attack => &c.str,
Skill::Block => &c.str,
Skill::Heal => &c.int,
};
let mut roll = Roll { base, result: base };
// // apply skills
// roll = c.skills.iter().fold(roll, |roll, s| s.apply(roll));
// finally combine with stat
println!("{:?}'s stats", c.name);
println!("{:064b} <- finalised", roll.result);
roll.result = roll.result & stat.value;
println!("{:064b} & <- attribute roll", stat.value);
println!("{:064b} = {:?}", roll.result, roll.result);
println!("");
return roll;
}
pub fn resolve(&mut self, cryp: &mut Cryp, target: &mut Cryp) -> &mut Turn {
let roll = self.roll(&cryp);
println!("{:?} gettin clapped for {:?}", target.name, roll.result);
target.hp.reduce(roll.result);
self
}
pub fn set_target(&mut self, cryp_id: Uuid) -> &mut Turn {
self.target_cryp_id = Some(cryp_id);
self
}
}
// #[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
// pub enum Skill {
// Stoney,
// Evasive,
// }
// impl Skill {
// pub fn apply(&self, roll: Roll) -> Roll {
// match self {
// Skill::Stoney => stoney(self, roll),
// Skill::Evasive => evasive(self, roll),
// }
// }
// }
// fn stoney(_s: &Skill, mut roll: Roll) -> Roll {
// let effect = 0b11110000;
// match roll.kind {
// StatKind::Def => {
// // println!("{:064b} | <- {:?}", effect, s);
// roll.result = roll.result | effect;
// roll
// },
// _ => roll,
// }
// }
// fn evasive(_s: &Skill, mut roll: Roll) -> Roll {
// match roll.kind {
// StatKind::Def => {
// if roll.result.is_power_of_two() {
// roll.result = u64::max_value()
// }
// roll
// },
// _ => roll,
// }
// }