turns init
This commit is contained in:
parent
6b2eda8391
commit
faa5d87f6f
@ -7,12 +7,7 @@ function CrypPanel({ battle }) {
|
||||
<div>{JSON.stringify(battle.a)}</div>
|
||||
<div>{JSON.stringify(battle.b)}</div>
|
||||
<div>
|
||||
{
|
||||
battle.log.map((l, i) => {
|
||||
if (l === '') return (<br/>);
|
||||
return <p key={i} >{l}</p>
|
||||
})
|
||||
}
|
||||
{battle.log.map((l, i) => (<p key={i} >{l}</p>))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
@ -2,72 +2,201 @@ use uuid::Uuid;
|
||||
// use rand::prelude::*;
|
||||
|
||||
use cryp::Cryp;
|
||||
use skill::Skill;
|
||||
|
||||
// impl Attribute {
|
||||
// pub fn as_str(&self) -> &'static str {
|
||||
// match self {
|
||||
// Dmg => "dmg",
|
||||
// Def => "def",
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
pub enum Phase {
|
||||
Skill,
|
||||
Target,
|
||||
}
|
||||
|
||||
#[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)]
|
||||
pub struct Battle {
|
||||
pub a: Cryp,
|
||||
pub b: Cryp,
|
||||
pub team_size: usize,
|
||||
pub team_num: usize,
|
||||
pub teams: Vec<Team>,
|
||||
pub is_pve: bool,
|
||||
pub phase: Phase,
|
||||
pub log: Vec<String>,
|
||||
}
|
||||
|
||||
impl Battle {
|
||||
pub fn new(a: &Cryp, b: &Cryp) -> Battle {
|
||||
pub fn new() -> Battle {
|
||||
return Battle {
|
||||
a: a.clone(),
|
||||
b: b.clone(),
|
||||
team_size: 0,
|
||||
team_num: 0,
|
||||
teams: vec![],
|
||||
is_pve: true,
|
||||
phase: Phase::Skill,
|
||||
log: vec![],
|
||||
};
|
||||
}
|
||||
|
||||
pub fn cryps(&self) -> Vec<&Cryp> {
|
||||
vec![&self.a, &self.b]
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
pub fn set_team_num(&mut self, size: usize) -> &mut Battle {
|
||||
self.team_num = size;
|
||||
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 {
|
||||
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 {
|
||||
match self.cryps().iter().find(|c| c.id == id) {
|
||||
Some(c) => c,
|
||||
None => panic!("cryp not in battle {:?}", 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),
|
||||
// }
|
||||
// }
|
||||
|
||||
pub fn winner(&self) -> Option<&Cryp> {
|
||||
if self.cryps().iter().all(|c| c.is_ko()) {
|
||||
return None
|
||||
}
|
||||
// pub fn winner(&self) -> Option<&Cryp> {
|
||||
// if self.cryps().iter().all(|c| c.is_ko()) {
|
||||
// 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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -13,69 +13,6 @@ use cryp::{Cryp, cryp_write};
|
||||
use battle::Battle;
|
||||
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 {
|
||||
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> {
|
||||
let query = "
|
||||
SELECT *
|
||||
FROM cryps
|
||||
WHERE id = $1
|
||||
AND account = $2;
|
||||
";
|
||||
return Err(err_msg("cryp is ko"));
|
||||
|
||||
let result = tx
|
||||
.query(query, &[¶ms.id, &account.id])?;
|
||||
// let query = "
|
||||
// SELECT *
|
||||
// FROM cryps
|
||||
// WHERE id = $1
|
||||
// AND account = $2;
|
||||
// ";
|
||||
|
||||
let returned = match result.iter().next() {
|
||||
Some(row) => row,
|
||||
None => return Err(err_msg("cryp not found")),
|
||||
};
|
||||
// let result = tx
|
||||
// .query(query, &[¶ms.id, &account.id])?;
|
||||
|
||||
// tells from_slice to cast into a cryp
|
||||
let cryp_bytes: Vec<u8> = returned.get("data");
|
||||
let plr: Cryp = from_slice::<Cryp>(&cryp_bytes)?;
|
||||
// let returned = match result.iter().next() {
|
||||
// Some(row) => row,
|
||||
// None => return Err(err_msg("cryp not found")),
|
||||
// };
|
||||
|
||||
// TEMP
|
||||
if plr.hp.value == 0 {
|
||||
return Err(err_msg("cryp is ko"));
|
||||
// plr.rez();
|
||||
}
|
||||
// // tells from_slice to cast into a cryp
|
||||
// let cryp_bytes: Vec<u8> = returned.get("data");
|
||||
// let plr: Cryp = from_slice::<Cryp>(&cryp_bytes)?;
|
||||
|
||||
let mob = generate_mob(&plr);
|
||||
let mut battle = Battle::new(&plr, &mob);
|
||||
// // TEMP
|
||||
// if plr.hp.value == 0 {
|
||||
// return Err(err_msg("cryp is ko"));
|
||||
// // plr.rez();
|
||||
// }
|
||||
|
||||
loop {
|
||||
battle.next();
|
||||
// let mob = generate_mob(&plr);
|
||||
// let mut battle = Battle::new(&plr, &mob);
|
||||
|
||||
if battle.finished() {
|
||||
let success = match battle.winner() {
|
||||
Some(c) => c.id == plr.id,
|
||||
None => false,
|
||||
};
|
||||
// loop {
|
||||
// battle.next();
|
||||
|
||||
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 {
|
||||
post_battle_plr = post_battle_plr.add_xp();
|
||||
}
|
||||
// let mut post_battle_plr = battle.cryp_by_id(plr.id).clone();
|
||||
|
||||
cryp_write(post_battle_plr, tx)?;
|
||||
|
||||
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;
|
||||
}
|
||||
// if success {
|
||||
// post_battle_plr = post_battle_plr.add_xp();
|
||||
// }
|
||||
|
||||
// cryp_write(post_battle_plr, tx)?;
|
||||
|
||||
// break Ok(battle)
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
@ -7,16 +7,15 @@ pub enum Skill {
|
||||
}
|
||||
|
||||
impl Skill {
|
||||
pub fn apply(&self, mut roll: Roll) -> Roll {
|
||||
pub fn apply(&self, roll: Roll) -> Roll {
|
||||
match self {
|
||||
Skill::Stoney => stoney(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;
|
||||
match roll.kind {
|
||||
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 {
|
||||
StatKind::Def => {
|
||||
if roll.result.is_power_of_two() {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user