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 {
@ -86,7 +109,7 @@ pub struct Cryp {
pub hp: CrypStat,
pub xp: u64,
pub lvl: u8,
pub skills: Vec<Skill>,
pub skills: Vec<CrypSkill>,
pub name: String,
}
@ -108,7 +131,7 @@ impl Cryp {
hp: CrypStat { value: 0, stat: Stat::Hp },
lvl: 0,
xp: 0,
skills: vec![Skill::Attack],
skills: vec![CrypSkill::new(Skill::Attack)],
name: String::new()
};
}
@ -130,7 +153,7 @@ impl Cryp {
}
pub fn learn(mut self, s: Skill) -> Cryp {
self.skills.push(s);
self.skills.push(CrypSkill::new(s));
self
}
@ -147,15 +170,6 @@ impl Cryp {
self.create()
}
// pub fn turn(&self) -> Turn {
// let mut log = vec![format!("{:?}'s turn:", self.name)];
// let str = self.str.roll(self, &mut log);
// let agi = self.agi.roll(self, &mut log);
// return Turn { str, agi, log }
// }
pub fn create(mut self) -> Cryp {
let mut rng = thread_rng();
let max = match self.lvl == 64 {
@ -170,6 +184,7 @@ impl Cryp {
self.int.set(rng.gen_range(1, max));
self.stam.set(rng.gen_range(1, max));
self.hp.set(self.stam.value);
self
}
@ -201,11 +216,18 @@ impl Cryp {
self.hp.value == 0
}
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();

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};
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 {
Stoney,
Evasive,
Attack,
Block,
Heal,
}
impl Skill {
pub fn apply(&self, roll: Roll) -> Roll {
match self {
Skill::Stoney => stoney(self, roll),
Skill::Evasive => evasive(self, roll),
#[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
}
}
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,
}
}
// #[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
// pub enum Skill {
// Stoney,
// Evasive,
// }
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,
}
}
// 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,
// }
// }