turns init

This commit is contained in:
ntr 2018-10-17 20:23:43 +11:00
parent 6b2eda8391
commit faa5d87f6f
4 changed files with 245 additions and 243 deletions

View File

@ -7,12 +7,7 @@ function CrypPanel({ battle }) {
<div>{JSON.stringify(battle.a)}</div> <div>{JSON.stringify(battle.a)}</div>
<div>{JSON.stringify(battle.b)}</div> <div>{JSON.stringify(battle.b)}</div>
<div> <div>
{ {battle.log.map((l, i) => (<p key={i} >{l}</p>))}
battle.log.map((l, i) => {
if (l === '') return (<br/>);
return <p key={i} >{l}</p>
})
}
</div> </div>
</div> </div>
); );

View File

@ -2,72 +2,201 @@ use uuid::Uuid;
// use rand::prelude::*; // use rand::prelude::*;
use cryp::Cryp; use cryp::Cryp;
use skill::Skill;
// impl Attribute { #[derive(Debug,Clone,Serialize,Deserialize)]
// pub fn as_str(&self) -> &'static str { pub enum Phase {
// match self { Skill,
// Dmg => "dmg", Target,
// Def => "def", }
// }
// } #[derive(Debug,Clone,Serialize,Deserialize)]
// } pub struct Team {
player: Uuid,
cryps: Vec<Cryp>,
skills: Vec<Skill>,
targets: Vec<Skill>,
}
impl Team {
pub fn new(player: Uuid) -> Team {
return Team {
player,
cryps: vec![],
skills: vec![],
targets: vec![],
};
}
pub fn set_cryps(&mut self, cryps: Vec<Cryp>) -> &mut Team {
self.cryps = cryps;
self
}
}
#[derive(Debug,Clone,Serialize,Deserialize)] #[derive(Debug,Clone,Serialize,Deserialize)]
pub struct Battle { pub struct Battle {
pub a: Cryp, pub team_size: usize,
pub b: Cryp, pub team_num: usize,
pub teams: Vec<Team>,
pub is_pve: bool,
pub phase: Phase,
pub log: Vec<String>, pub log: Vec<String>,
} }
impl Battle { impl Battle {
pub fn new(a: &Cryp, b: &Cryp) -> Battle { pub fn new() -> Battle {
return Battle { return Battle {
a: a.clone(), team_size: 0,
b: b.clone(), team_num: 0,
teams: vec![],
is_pve: true,
phase: Phase::Skill,
log: vec![], log: vec![],
}; };
} }
pub fn cryps(&self) -> Vec<&Cryp> { pub fn set_team_num(&mut self, size: usize) -> &mut Battle {
vec![&self.a, &self.b] self.team_num = size;
}
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 self
} }
pub fn set_team_size(&mut self, size: usize) -> &mut Battle {
self.team_size = size;
self
}
pub fn add_team(&mut self, team: Team) -> &mut Battle {
if self.teams.len() == self.team_num {
panic!("maximum number of teams");
}
self.teams.push(team);
self
}
pub fn start(&mut self) -> &mut Battle {
self
}
pub fn add_skill(&mut self, player: Uuid, skill: Skill) -> &mut Battle {
// check if cryp actually has this skill
self.team_by_player(player).skills.push(skill);
self
}
pub fn team_by_player(&mut self, player: Uuid) -> &mut Team {
match self.teams.iter_mut().find(|t| t.player == player) {
Some(t) => t,
None => panic!("player not in battle {:?}", player),
}
}
pub fn can_start(&mut self) -> bool {
self.teams.len() == self.team_num
}
// battle.new()
// battle.set_team_size(1)
// battle.add_team(vec<plr>)
// battle.can_start() -> enough_teams() -> false
// battle.add_team(vec<mob>)
// battle.can_start() -> enough_teams() -> true
// battle.start() -> start_move_phase() -> is_pve() ? mob_moves()
// battle.skill_phase() -> send_state()
// battle.add_skill(team_id, cryp_id, skill)
// battle.skill_phase_finished() -> enough_moves()
// battle.targets_phase() -> is_pve() ? mob_targets()
// battle.select_target(skill, target)
// battle.targets_phase_finished() -> enough_targets()
// battle.assign_dmg()
// battle.is_finished()
// 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 skill_phase_finished(&self) -> bool {
self.teams.iter().all(|t| t.skills.len() == self.team_size)
}
pub fn finished(&self) -> bool { pub fn finished(&self) -> bool {
self.cryps().iter().any(|c| c.is_ko()) self.teams.iter().any(|t| t.cryps.iter().all(|c| c.is_ko()))
} }
pub fn cryp_by_id(&self, id: Uuid) -> &Cryp { // pub fn cryp_by_id(&self, id: Uuid) -> &Cryp {
match self.cryps().iter().find(|c| c.id == id) { // match self.cryps().iter().find(|c| c.id == id) {
Some(c) => c, // Some(c) => c,
None => panic!("cryp not in battle {:?}", self), // None => panic!("cryp not in battle {:?}", self),
} // }
} // }
pub fn winner(&self) -> Option<&Cryp> { // pub fn winner(&self) -> Option<&Cryp> {
if self.cryps().iter().all(|c| c.is_ko()) { // if self.cryps().iter().all(|c| c.is_ko()) {
return None // return None
} // }
// match self.cryps().iter().find(|c| !c.is_ko()) {
// Some(w) => Some(w),
// None => panic!("no winner found {:?}", self),
// }
// }
}
#[cfg(test)]
mod tests {
use battle::*;
use cryp::*;
#[test]
fn battle_test() {
let x = Cryp::new()
.named(&"pronounced \"creeep\"".to_string())
.level(8)
.learn(Skill::Stoney)
.create();
let y = Cryp::new()
.named(&"lemongrass tea".to_string())
.level(8)
.create();
let mut battle = Battle::new();
battle
.set_team_num(2)
.set_team_size(1);
let x_id = Uuid::new_v4();
let mut x_team = Team::new(x_id);
x_team
.set_cryps(vec![x]);
let y_id = Uuid::new_v4();
let mut y_team = Team::new(y_id);
y_team
.set_cryps(vec![y]);
battle
.add_team(x_team)
.add_team(y_team);
assert!(battle.can_start());
return;
}
match self.cryps().iter().find(|c| !c.is_ko()) {
Some(w) => Some(w),
None => panic!("no winner found {:?}", self),
}
}
} }

View File

@ -13,69 +13,6 @@ use cryp::{Cryp, cryp_write};
use battle::Battle; use battle::Battle;
use skill::Skill; use skill::Skill;
// struct Encounter {
// mob: Cryp,
// success: bool,
// player: Cryp,
// }
pub fn battle_resolve(a: &Cryp, b: &Cryp) -> Battle {
let mut battle = Battle::new(a, b);
loop {
battle.next();
if battle.finished() {
break battle
}
}
}
// 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())
// .level(mob_lvl)
// .create();
// let outcome = battle_resolve(&plr, &mob);
// let success = match outcome.winner() {
// Some(c) => c.id == plr.id,
// None => false,
// };
// return Encounter {
// mob: mob,
// success,
// player: plr,
// };
// }
// pub fn keep_levelling(mut c: Cryp) -> Cryp {
// loop {
// let enc = pve_completion(c);
// c = enc.player;
// if !enc.success {
// println!("{:?} has been KO'd", c.name);
// break c;
// }
// println!("{:?} rekt {:?}", c.name, enc.mob.name);
// c = c.add_xp();
// println!("{:?} now has {:?} xp and is lvl {:?}", c.name, c.xp, c.lvl);
// // LEVEL CAP
// if c.lvl == 12 {
// break c;
// }
// continue;
// }
// }
fn generate_mob(plr: &Cryp) -> Cryp { fn generate_mob(plr: &Cryp) -> Cryp {
let mut rng = thread_rng(); let mut rng = thread_rng();
@ -93,112 +30,54 @@ fn generate_mob(plr: &Cryp) -> Cryp {
} }
pub fn pve(params: CombatPveParams, tx: &mut Transaction, account: &Account) -> Result<Battle, Error> { pub fn pve(params: CombatPveParams, tx: &mut Transaction, account: &Account) -> Result<Battle, Error> {
let query = " return Err(err_msg("cryp is ko"));
SELECT *
FROM cryps
WHERE id = $1
AND account = $2;
";
let result = tx // let query = "
.query(query, &[&params.id, &account.id])?; // SELECT *
// FROM cryps
// WHERE id = $1
// AND account = $2;
// ";
let returned = match result.iter().next() { // let result = tx
Some(row) => row, // .query(query, &[&params.id, &account.id])?;
None => return Err(err_msg("cryp not found")),
};
// tells from_slice to cast into a cryp // let returned = match result.iter().next() {
let cryp_bytes: Vec<u8> = returned.get("data"); // Some(row) => row,
let plr: Cryp = from_slice::<Cryp>(&cryp_bytes)?; // None => return Err(err_msg("cryp not found")),
// };
// TEMP // // tells from_slice to cast into a cryp
if plr.hp.value == 0 { // let cryp_bytes: Vec<u8> = returned.get("data");
return Err(err_msg("cryp is ko")); // let plr: Cryp = from_slice::<Cryp>(&cryp_bytes)?;
// plr.rez();
}
let mob = generate_mob(&plr); // // TEMP
let mut battle = Battle::new(&plr, &mob); // if plr.hp.value == 0 {
// return Err(err_msg("cryp is ko"));
// // plr.rez();
// }
loop { // let mob = generate_mob(&plr);
battle.next(); // let mut battle = Battle::new(&plr, &mob);
if battle.finished() { // loop {
let success = match battle.winner() { // battle.next();
Some(c) => c.id == plr.id,
None => false,
};
let mut post_battle_plr = battle.cryp_by_id(plr.id).clone(); // if battle.finished() {
// let success = match battle.winner() {
// Some(c) => c.id == plr.id,
// None => false,
// };
if success { // let mut post_battle_plr = battle.cryp_by_id(plr.id).clone();
post_battle_plr = post_battle_plr.add_xp();
}
cryp_write(post_battle_plr, tx)?; // if success {
// post_battle_plr = post_battle_plr.add_xp();
break Ok(battle) // }
}
}
}
pub fn test_battle() {
let a = Cryp::new()
.named(&"pronounced \"creeep\"".to_string())
.level(8)
.learn(Skill::Stoney)
.create();
let b = Cryp::new()
.named(&"lemongrass tea".to_string())
.level(8)
.create();
let outcome = battle_resolve(&a, &b);
match outcome.winner() {
Some(w) => println!("{:?} is the winner with {:?} hp remaining", w.name, w.hp),
None => println!("{:?} was a draw", outcome),
};
return
}
#[cfg(test)]
mod tests {
use combat::*;
// #[test]
// fn pve_completion_test() {
// let player = Cryp::new()
// .named(&"ca phe sua da".to_string())
// .level(2)
// .create();
// 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();
return;
}
// cryp_write(post_battle_plr, tx)?;
// break Ok(battle)
// }
// }
} }

View File

@ -7,16 +7,15 @@ pub enum Skill {
} }
impl Skill { impl Skill {
pub fn apply(&self, mut roll: Roll) -> Roll { pub fn apply(&self, roll: Roll) -> Roll {
match self { match self {
Skill::Stoney => stoney(self, roll), Skill::Stoney => stoney(self, roll),
Skill::Evasive => evasive(self, roll), Skill::Evasive => evasive(self, roll),
_ => panic!("missing skill"),
} }
} }
} }
fn stoney(s: &Skill, mut roll: Roll) -> Roll { fn stoney(_s: &Skill, mut roll: Roll) -> Roll {
let effect = 0b11110000; let effect = 0b11110000;
match roll.kind { match roll.kind {
StatKind::Def => { StatKind::Def => {
@ -28,7 +27,7 @@ fn stoney(s: &Skill, mut roll: Roll) -> Roll {
} }
} }
fn evasive(s: &Skill, mut roll: Roll) -> Roll { fn evasive(_s: &Skill, mut roll: Roll) -> Roll {
match roll.kind { match roll.kind {
StatKind::Def => { StatKind::Def => {
if roll.result.is_power_of_two() { if roll.result.is_power_of_two() {