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-await-in-loop': [0],
'indent': ['error', 4],
'keyword-spacing': ['error'],
'key-spacing': ['error'],
// for preact
"react/react-in-jsx-scope": [0],

View File

@ -6,7 +6,7 @@
"scripts": {
"start": "parcel index.html --port 40080",
"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"
},
"author": "",

View File

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

View File

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

View File

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

View File

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

View File

@ -10,89 +10,14 @@ use failure::err_msg;
use account::Account;
use rpc::{GameStateParams, GameSkillParams, GamePveParams, GamePvpParams, GameTargetParams, GameJoinParams};
use cryp::{Cryp, cryp_get};
#[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
}
}
use skill::{Skill, Turn};
#[derive(Debug,Clone,Serialize,Deserialize)]
pub struct Team {
id: Uuid,
cryps: Vec<Cryp>,
skills: Vec<GameSkill>,
incoming: Vec<GameSkill>,
skills: Vec<Turn>,
incoming: Vec<Turn>,
}
impl Team {
@ -114,7 +39,7 @@ impl Team {
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) {
Some(a) => a,
None => panic!("abiltity not in game"),
@ -278,9 +203,13 @@ impl Game {
};
// 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"));
}
if cryp.has_cooldown(skill) {
return Err(err_msg("abiltity on cooldown"));
}
}
// replace cryp skill
@ -288,7 +217,7 @@ impl Game {
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);
return Ok(skill.id);
@ -338,7 +267,7 @@ impl Game {
// 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?
let team = self.team_by_id(team_id);
@ -349,8 +278,8 @@ impl Game {
};
// set the target
let skill = team.skill_by_id(skill_id);
Ok(skill.set_target(cryp_id))
let turn = team.turn_by_id(skill_id);
Ok(turn.set_target(cryp_id))
}
fn target_phase_finished(&self) -> bool {

View File

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

View File

@ -11,9 +11,10 @@ use failure::err_msg;
use net::Db;
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 item::{Item, items_list, item_use};
use skill::{Skill};
pub struct Rpc;

View File

@ -1,40 +1,120 @@
use cryp::{StatKind, Roll};
#[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,
}
}
use rand::prelude::*;
use uuid::Uuid;
use cryp::{Cryp};
#[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 Turn {
pub id: Uuid,
pub skill: Skill,
pub cryp_id: Uuid,
pub roll: Option<Roll>,
pub target_cryp_id: Option<Uuid>,
pub target_team_id: Uuid,
}
impl Turn {
pub fn new(cryp_id: Uuid, target_team_id: Uuid, skill: Skill) -> Turn {
return Turn {
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 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,
// }
// }