1114 lines
34 KiB
Rust
1114 lines
34 KiB
Rust
use uuid::Uuid;
|
|
use rand::prelude::*;
|
|
use serde_cbor::{from_slice, to_vec};
|
|
|
|
use postgres::transaction::Transaction;
|
|
|
|
use failure::Error;
|
|
use failure::err_msg;
|
|
|
|
use skill::{Skill, Cast, Immunity, Disable, Event};
|
|
use effect::{Cooldown, Effect, Colour};
|
|
use spec::{Spec};
|
|
use item::{Item};
|
|
use img;
|
|
|
|
#[derive(Debug,Clone,Serialize,Deserialize)]
|
|
pub struct Colours {
|
|
pub red: u8,
|
|
pub green: u8,
|
|
pub blue: u8,
|
|
}
|
|
|
|
impl Colours {
|
|
pub fn new() -> Colours {
|
|
Colours { red: 0, green: 0, blue: 0 }
|
|
}
|
|
|
|
pub fn from_construct(construct: &Construct) -> Colours {
|
|
let mut count = Colours::new();
|
|
|
|
for spec in construct.specs.iter() {
|
|
let v = Item::from(*spec);
|
|
v.colours(&mut count);
|
|
}
|
|
|
|
for cs in construct.skills.iter() {
|
|
let v = Item::from(cs.skill);
|
|
v.colours(&mut count);
|
|
}
|
|
|
|
count
|
|
}
|
|
}
|
|
|
|
|
|
#[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
|
|
pub struct ConstructSkill {
|
|
pub skill: Skill,
|
|
pub cd: Cooldown,
|
|
// used for Uon client
|
|
pub disabled: bool,
|
|
}
|
|
|
|
impl ConstructSkill {
|
|
pub fn new(skill: Skill) -> ConstructSkill {
|
|
ConstructSkill {
|
|
skill,
|
|
cd: skill.base_cd(),
|
|
disabled: false,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
|
|
pub enum EffectMeta {
|
|
Skill(Skill),
|
|
TickAmount(u64),
|
|
AddedDamage(u64),
|
|
LinkTarget(Uuid),
|
|
Multiplier(u64),
|
|
}
|
|
|
|
#[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
|
|
pub struct ConstructEffect {
|
|
pub effect: Effect,
|
|
pub duration: u8,
|
|
pub meta: Option<EffectMeta>,
|
|
pub tick: Option<Cast>,
|
|
}
|
|
|
|
impl ConstructEffect {
|
|
pub fn new(effect: Effect, duration: u8) -> ConstructEffect {
|
|
ConstructEffect { effect, duration, meta: None, tick: None }
|
|
}
|
|
|
|
pub fn set_tick(mut self, tick: Cast) -> ConstructEffect {
|
|
self.tick = Some(tick);
|
|
self
|
|
}
|
|
|
|
pub fn set_meta(mut self, meta: EffectMeta) -> ConstructEffect {
|
|
self.meta = Some(meta);
|
|
self
|
|
}
|
|
|
|
pub fn get_duration(&self) -> u8 {
|
|
self.duration
|
|
}
|
|
|
|
pub fn get_multiplier(&self) -> u64 {
|
|
match self.meta {
|
|
Some(EffectMeta::Multiplier(s)) => s,
|
|
_ => 0
|
|
}
|
|
}
|
|
|
|
pub fn get_skill(&self) -> Option<Skill> {
|
|
match self.meta {
|
|
Some(EffectMeta::Skill(s)) => Some(s),
|
|
_ => None,
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
#[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
|
|
pub enum Stat {
|
|
Str,
|
|
Agi,
|
|
Int,
|
|
GreenLife,
|
|
Speed,
|
|
RedPower,
|
|
BluePower,
|
|
GreenPower,
|
|
RedDamageTaken,
|
|
BlueDamageTaken,
|
|
GreenDamageTaken,
|
|
RedLife,
|
|
BlueLife,
|
|
Evasion,
|
|
}
|
|
|
|
#[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
|
|
pub struct ConstructStat {
|
|
base: u64,
|
|
value: u64,
|
|
max: u64,
|
|
pub stat: Stat,
|
|
}
|
|
|
|
impl ConstructStat {
|
|
// pub fn set(&mut self, v: u64, specs: &Vec<Spec>) -> &mut ConstructStat {
|
|
// self.base = v;
|
|
// self.recalculate(specs)
|
|
// }
|
|
|
|
pub fn recalculate(&mut self, specs: &Vec<Spec>, player_colours: &Colours) -> &mut ConstructStat {
|
|
let specs = specs
|
|
.iter()
|
|
.filter(|s| s.affects().contains(&self.stat))
|
|
.map(|s| *s)
|
|
.collect::<Vec<Spec>>();
|
|
|
|
// applied with fold because it can be zeroed or multiplied
|
|
// but still needs access to the base amount
|
|
let value = specs.iter().fold(self.base, |acc, s| s.apply(acc, self.base, player_colours));
|
|
self.value = value;
|
|
self.max = value;
|
|
|
|
self
|
|
}
|
|
|
|
pub fn reduce(&mut self, amt: u64) -> &mut ConstructStat {
|
|
self.value = self.value.saturating_sub(amt);
|
|
self
|
|
}
|
|
|
|
pub fn increase(&mut self, amt: u64) -> &mut ConstructStat {
|
|
self.value = *[
|
|
self.value.saturating_add(amt),
|
|
self.max
|
|
].iter().min().unwrap();
|
|
|
|
self
|
|
}
|
|
|
|
pub fn force(&mut self, v: u64) -> &mut ConstructStat {
|
|
self.base = v;
|
|
self.value = v;
|
|
self.max = v;
|
|
|
|
self
|
|
}
|
|
}
|
|
|
|
#[derive(Debug,Clone,Serialize,Deserialize)]
|
|
pub struct ConstructSkeleton {
|
|
pub id: Uuid,
|
|
pub account: Uuid,
|
|
pub img: Uuid,
|
|
pub name: String,
|
|
}
|
|
|
|
#[derive(Debug,Clone,Serialize,Deserialize)]
|
|
pub struct Construct {
|
|
pub id: Uuid,
|
|
pub account: Uuid,
|
|
pub img: Uuid,
|
|
pub red_power: ConstructStat,
|
|
pub red_life: ConstructStat,
|
|
pub blue_life: ConstructStat,
|
|
pub blue_power: ConstructStat,
|
|
pub green_power: ConstructStat,
|
|
pub speed: ConstructStat,
|
|
pub green_life: ConstructStat,
|
|
// pub evasion: ConstructStat,
|
|
pub skills: Vec<ConstructSkill>,
|
|
pub effects: Vec<ConstructEffect>,
|
|
pub specs: Vec<Spec>,
|
|
pub colours: Colours,
|
|
pub name: String,
|
|
}
|
|
|
|
impl Construct {
|
|
pub fn new() -> Construct {
|
|
let id = Uuid::new_v4();
|
|
return Construct {
|
|
id,
|
|
account: id,
|
|
img: Uuid::new_v4(),
|
|
red_power: ConstructStat { base: 320, value: 320, max: 320, stat: Stat::RedPower },
|
|
red_life: ConstructStat { base: 125, value: 125, max: 125, stat: Stat::RedLife },
|
|
blue_power: ConstructStat { base: 320, value: 320, max: 320, stat: Stat::BluePower },
|
|
blue_life: ConstructStat { base: 125, value: 125, max: 125, stat: Stat::BlueLife },
|
|
green_power: ConstructStat { base: 320, value: 320, max: 320, stat: Stat::GreenPower },
|
|
green_life: ConstructStat { base: 800, value: 800, max: 800, stat: Stat::GreenLife },
|
|
speed: ConstructStat { base: 100, value: 100, max: 100, stat: Stat::Speed },
|
|
// evasion: ConstructStat { base: 0, value: 0, max: 0, stat: Stat::Evasion },
|
|
skills: vec![],
|
|
effects: vec![],
|
|
specs: vec![],
|
|
colours: Colours::new(),
|
|
name: String::new(),
|
|
};
|
|
}
|
|
|
|
pub fn from_skeleton(skeleton: &ConstructSkeleton) -> Construct {
|
|
return Construct {
|
|
id: skeleton.id,
|
|
account: skeleton.account,
|
|
img: skeleton.img,
|
|
name: skeleton.name.clone(),
|
|
|
|
.. Construct::new()
|
|
};
|
|
}
|
|
|
|
pub fn to_skeleton(&self) -> ConstructSkeleton {
|
|
ConstructSkeleton {
|
|
id: self.id,
|
|
account: self.account,
|
|
img: self.img,
|
|
name: self.name.clone(),
|
|
}
|
|
}
|
|
|
|
|
|
pub fn named(mut self, name: &String) -> Construct {
|
|
self.name = name.clone();
|
|
self
|
|
}
|
|
|
|
pub fn set_account(mut self, account: Uuid) -> Construct {
|
|
self.account = account;
|
|
self
|
|
}
|
|
|
|
pub fn new_img(mut self) -> Construct {
|
|
self.img = Uuid::new_v4();
|
|
self
|
|
}
|
|
|
|
pub fn new_name(self, name: String) -> Result<Construct, Error> {
|
|
if name.len() > 20 {
|
|
return Err(err_msg("20 character name maximum"));
|
|
}
|
|
Ok(self.named(&name))
|
|
}
|
|
|
|
pub fn learn(mut self, s: Skill) -> Construct {
|
|
self.skills.push(ConstructSkill::new(s));
|
|
self.colours = Colours::from_construct(&self);
|
|
self
|
|
}
|
|
|
|
pub fn learn_mut(&mut self, s: Skill) -> &mut Construct {
|
|
self.skills.push(ConstructSkill::new(s));
|
|
self.calculate_colours()
|
|
}
|
|
|
|
pub fn forget(&mut self, skill: Skill) -> Result<&mut Construct, Error> {
|
|
match self.skills.iter().position(|s| s.skill == skill) {
|
|
Some(i) => {
|
|
self.skills.remove(i);
|
|
return Ok(self.calculate_colours());
|
|
},
|
|
None => Err(format_err!("{:?} does not know {:?}", self.name, skill)),
|
|
}
|
|
}
|
|
|
|
pub fn spec_add(&mut self, spec: Spec) -> Result<&mut Construct, Error> {
|
|
if self.specs.len() >= 3 {
|
|
return Err(err_msg("maximum specs equipped"));
|
|
}
|
|
|
|
self.specs.push(spec);
|
|
return Ok(self.calculate_colours());
|
|
}
|
|
|
|
pub fn spec_remove(&mut self, spec: Spec) -> Result<&mut Construct, Error> {
|
|
match self.specs.iter().position(|s| *s == spec) {
|
|
Some(p) => self.specs.remove(p),
|
|
None => return Err(err_msg("spec not found")),
|
|
};
|
|
|
|
Ok(self.calculate_colours())
|
|
}
|
|
|
|
fn calculate_colours(&mut self) -> &mut Construct {
|
|
self.colours = Colours::from_construct(&self);
|
|
self
|
|
}
|
|
|
|
pub fn apply_modifiers(&mut self, player_colours: &Colours) -> &mut Construct {
|
|
self.red_power.recalculate(&self.specs, player_colours);
|
|
self.red_life.recalculate(&self.specs, player_colours);
|
|
self.blue_power.recalculate(&self.specs, player_colours);
|
|
self.blue_life.recalculate(&self.specs, player_colours);
|
|
// self.evasion.recalculate(&self.specs, &self.colours, player_colours);
|
|
self.speed.recalculate(&self.specs, player_colours);
|
|
self.green_power.recalculate(&self.specs, player_colours);
|
|
self.green_life.recalculate(&self.specs, player_colours);
|
|
|
|
self
|
|
}
|
|
|
|
pub fn is_ko(&self) -> bool {
|
|
self.green_life.value == 0
|
|
}
|
|
|
|
pub fn force_ko(&mut self) -> &mut Construct {
|
|
self.green_life.value = 0;
|
|
self
|
|
}
|
|
|
|
pub fn immune(&self, skill: Skill) -> Option<Immunity> {
|
|
// also checked in resolve stage so shouldn't happen really
|
|
if self.is_ko() {
|
|
return Some(vec![Effect::Ko]);
|
|
}
|
|
|
|
let immunities = self.effects.iter()
|
|
.filter(|e| e.effect.immune(skill))
|
|
.map(|e| e.effect)
|
|
.collect::<Vec<Effect>>();
|
|
|
|
if immunities.len() > 0 {
|
|
return Some(immunities);
|
|
}
|
|
|
|
None
|
|
}
|
|
|
|
pub fn disabled(&self, skill: Skill) -> Option<Disable> {
|
|
if self.is_ko() && !skill.ko_castable() {
|
|
return Some(vec![Effect::Ko]);
|
|
}
|
|
|
|
let disables = self.effects.iter()
|
|
.filter(|e| e.effect.disables_skill(skill))
|
|
.map(|e| e.effect)
|
|
.collect::<Vec<Effect>>();
|
|
|
|
if disables.len() > 0 {
|
|
return Some(disables);
|
|
}
|
|
|
|
None
|
|
}
|
|
|
|
pub fn is_stunned(&self) -> bool {
|
|
self.available_skills().len() == 0
|
|
}
|
|
|
|
pub fn affected(&self, effect: Effect) -> bool {
|
|
self.effects.iter().any(|s| s.effect == effect)
|
|
}
|
|
|
|
pub fn available_skills(&self) -> Vec<&ConstructSkill> {
|
|
self.skills.iter()
|
|
.filter(|s| s.cd.is_none())
|
|
.filter(|s| self.disabled(s.skill).is_none())
|
|
.collect()
|
|
}
|
|
|
|
pub fn mob_select_skill(&self) -> Option<Skill> {
|
|
let available = self.available_skills();
|
|
|
|
if available.len() == 0 {
|
|
return None;
|
|
}
|
|
|
|
let mut rng = thread_rng();
|
|
|
|
let i = match available.len() {
|
|
1 => 0,
|
|
_ => rng.gen_range(0, available.len()),
|
|
};
|
|
|
|
return Some(available[i].skill);
|
|
|
|
// let highest_cd = available.iter()
|
|
// .filter(|s| s.skill.base_cd().is_some())
|
|
// .max_by_key(|s| s.skill.base_cd().unwrap());
|
|
|
|
// return match highest_cd {
|
|
// Some(s) => Some(s.skill),
|
|
// None => Some(available[0].skill),
|
|
// };
|
|
}
|
|
|
|
pub fn knows(&self, skill: Skill) -> bool {
|
|
self.skills.iter().any(|s| s.skill == skill)
|
|
}
|
|
|
|
pub fn skill_on_cd(&self, skill: Skill) -> Option<&ConstructSkill> {
|
|
self.skills.iter().find(|s| s.skill == skill && s.cd.is_some())
|
|
}
|
|
|
|
pub fn skill_set_cd(&mut self, skill: Skill) -> &mut Construct {
|
|
let i = self.skills.iter().position(|s| s.skill == skill).unwrap();
|
|
self.skills.remove(i);
|
|
self.skills.push(ConstructSkill::new(skill));
|
|
|
|
self
|
|
}
|
|
|
|
pub fn reduce_cooldowns(&mut self) -> &mut Construct {
|
|
for skill in self.skills.iter_mut() {
|
|
// if used cooldown
|
|
if skill.skill.base_cd().is_some() {
|
|
// what is the current cd
|
|
if let Some(current_cd) = skill.cd {
|
|
|
|
// if it's 1 set it to none
|
|
if current_cd == 1 {
|
|
skill.cd = None;
|
|
continue;
|
|
}
|
|
|
|
// otherwise decrement it
|
|
skill.cd = Some(current_cd.saturating_sub(1));
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
self
|
|
}
|
|
|
|
pub fn reduce_effect_durations(&mut self) -> &mut Construct {
|
|
self.effects = self.effects.clone().into_iter().filter_map(|mut effect| {
|
|
effect.duration = effect.duration.saturating_sub(1);
|
|
|
|
if effect.duration == 0 {
|
|
return None;
|
|
}
|
|
|
|
// info!("reduced effect {:?}", effect);
|
|
return Some(effect);
|
|
}).collect::<Vec<ConstructEffect>>();
|
|
|
|
self
|
|
}
|
|
|
|
// Stats
|
|
pub fn red_power(&self) -> u64 {
|
|
let red_power_mods = self.effects.iter()
|
|
.filter(|e| e.effect.modifications().contains(&Stat::RedPower))
|
|
.map(|e| (e.effect, e.meta))
|
|
.collect::<Vec<(Effect, Option<EffectMeta>)>>();
|
|
|
|
let modified_red_power = red_power_mods.iter()
|
|
.fold(self.red_power.value, |acc, fx| fx.0.apply(acc, fx.1));
|
|
return modified_red_power;
|
|
}
|
|
|
|
pub fn blue_power(&self) -> u64 {
|
|
let blue_power_mods = self.effects.iter()
|
|
.filter(|e| e.effect.modifications().contains(&Stat::BluePower))
|
|
.map(|e| (e.effect, e.meta))
|
|
.collect::<Vec<(Effect, Option<EffectMeta>)>>();
|
|
|
|
let modified_blue_power = blue_power_mods.iter()
|
|
.fold(self.blue_power.value, |acc, fx| fx.0.apply(acc, fx.1));
|
|
return modified_blue_power;
|
|
}
|
|
|
|
pub fn green_power(&self) -> u64 {
|
|
let green_power_mods = self.effects.iter()
|
|
.filter(|e| e.effect.modifications().contains(&Stat::GreenPower))
|
|
.map(|e| (e.effect, e.meta))
|
|
.collect::<Vec<(Effect, Option<EffectMeta>)>>();
|
|
|
|
let modified_green_power = green_power_mods.iter()
|
|
.fold(self.green_power.value, |acc, fx| fx.0.apply(acc, fx.1));
|
|
return modified_green_power;
|
|
}
|
|
|
|
pub fn skill_speed(&self, s: Skill) -> u64 {
|
|
self.speed().saturating_mul(s.speed() as u64)
|
|
}
|
|
|
|
// todo complete with specs
|
|
pub fn skill_is_aoe(&self, s: Skill) -> bool {
|
|
s.aoe()
|
|
}
|
|
|
|
pub fn speed(&self) -> u64 {
|
|
let speed_mods = self.effects.iter()
|
|
.filter(|e| e.effect.modifications().contains(&Stat::Speed))
|
|
.map(|e| (e.effect, e.meta))
|
|
.collect::<Vec<(Effect, Option<EffectMeta>)>>();
|
|
|
|
let modified_speed = speed_mods.iter()
|
|
.fold(self.speed.value, |acc, fx| fx.0.apply(acc, fx.1));
|
|
return modified_speed;
|
|
}
|
|
|
|
pub fn red_life(&self) -> u64 {
|
|
self.red_life.value
|
|
}
|
|
|
|
pub fn blue_life(&self) -> u64 {
|
|
self.blue_life.value
|
|
}
|
|
|
|
pub fn green_life(&self) -> u64 {
|
|
self.green_life.value
|
|
}
|
|
|
|
fn reduce_green_life(&mut self, amount: u64) {
|
|
self.green_life.reduce(amount);
|
|
if self.affected(Effect::Sustain) && self.green_life() == 0 {
|
|
self.green_life.value = 1;
|
|
}
|
|
}
|
|
|
|
pub fn recharge(&mut self, skill: Skill, red_amount: u64, blue_amount: u64) -> Vec<Event> {
|
|
let mut events = vec![];
|
|
|
|
// Should red type immunity block recharge???
|
|
if let Some(immunity) = self.immune(skill) {
|
|
if !self.is_ko() {
|
|
events.push(Event::Immunity { skill, immunity });
|
|
}
|
|
return events;
|
|
}
|
|
|
|
match self.affected(Effect::Invert) {
|
|
false => {
|
|
// Do we need inversion?
|
|
let current_red_life = self.red_life();
|
|
self.red_life.increase(red_amount);
|
|
let new_red_life = self.red_life.value;
|
|
let red = new_red_life - current_red_life;
|
|
|
|
let current_blue_life = self.blue_life();
|
|
self.blue_life.increase(blue_amount);
|
|
let new_blue_life = self.blue_life.value;
|
|
let blue = new_blue_life - current_blue_life;
|
|
|
|
if red != 0 || blue != 0 {
|
|
events.push(Event::Recharge { red, blue, skill });
|
|
}
|
|
},
|
|
true => {
|
|
// Recharge takes a red and blue amount so check for them
|
|
if red_amount != 0 {
|
|
let red_mods = self.effects.iter()
|
|
.filter(|e| e.effect.modifications().contains(&Stat::RedDamageTaken))
|
|
.map(|e| (e.effect, e.meta))
|
|
.collect::<Vec<(Effect, Option<EffectMeta>)>>();
|
|
|
|
let red_modified_power = red_mods.iter()
|
|
.fold(red_amount, |acc, fx| fx.0.apply(acc, fx.1));
|
|
|
|
|
|
let red_remainder = red_modified_power.saturating_sub(self.red_life.value);
|
|
let red_mitigation = red_modified_power.saturating_sub(red_remainder);
|
|
|
|
// reduce red_life by mitigation amount
|
|
self.red_life.reduce(red_mitigation);
|
|
|
|
// deal remainder to green_life
|
|
let red_current_green_life = self.green_life();
|
|
self.reduce_green_life(red_remainder);
|
|
let red_damage_amount = red_current_green_life - self.green_life();
|
|
|
|
events.push(Event::Damage {
|
|
skill,
|
|
amount: red_damage_amount,
|
|
mitigation: red_mitigation,
|
|
colour: Colour::Red
|
|
});
|
|
}
|
|
|
|
if blue_amount != 0 {
|
|
let blue_mods = self.effects.iter()
|
|
.filter(|e| e.effect.modifications().contains(&Stat::BlueDamageTaken))
|
|
.map(|e| (e.effect, e.meta))
|
|
.collect::<Vec<(Effect, Option<EffectMeta>)>>();
|
|
|
|
let blue_modified_power = blue_mods.iter()
|
|
.fold(blue_amount, |acc, fx| fx.0.apply(acc, fx.1));
|
|
|
|
|
|
let blue_remainder = blue_modified_power.saturating_sub(self.blue_life.value);
|
|
let blue_mitigation = blue_modified_power.saturating_sub(blue_remainder);
|
|
|
|
// reduce blue_life by mitigation amount
|
|
self.blue_life.reduce(blue_mitigation);
|
|
|
|
// deal remainder to green_life
|
|
let blue_current_green_life = self.green_life();
|
|
self.reduce_green_life(blue_remainder);
|
|
let blue_damage_amount = blue_current_green_life - self.green_life();
|
|
|
|
events.push(Event::Damage {
|
|
skill,
|
|
amount: blue_damage_amount,
|
|
mitigation: blue_mitigation,
|
|
colour: Colour::Blue
|
|
});
|
|
}
|
|
}
|
|
}
|
|
return events;
|
|
}
|
|
|
|
pub fn deal_green_damage(&mut self, skill: Skill, amount: u64) -> Vec<Event> {
|
|
let mut events = vec![];
|
|
if let Some(immunity) = self.immune(skill) {
|
|
if !self.is_ko() {
|
|
events.push(Event::Immunity { skill, immunity });
|
|
}
|
|
return events;
|
|
}
|
|
|
|
let mods = self.effects.iter()
|
|
.filter(|e| e.effect.modifications().contains(&Stat::GreenDamageTaken))
|
|
.map(|e| (e.effect, e.meta))
|
|
.collect::<Vec<(Effect, Option<EffectMeta>)>>();
|
|
|
|
let modified_power = mods.iter()
|
|
.fold(amount, |acc, fx| fx.0.apply(acc, fx.1));
|
|
|
|
match self.affected(Effect::Invert) {
|
|
false => {
|
|
let current_green_life = self.green_life();
|
|
self.green_life.increase(modified_power);
|
|
let new_green_life = self.green_life.value;
|
|
|
|
let healing = new_green_life - current_green_life;
|
|
let overhealing = modified_power - healing;
|
|
|
|
events.push(Event::Healing {
|
|
skill,
|
|
amount: healing,
|
|
overhealing,
|
|
});
|
|
},
|
|
true => {
|
|
// events.push(Event::Inversion { skill });
|
|
|
|
// there is no green shield (yet)
|
|
let current_green_life = self.green_life();
|
|
self.reduce_green_life(modified_power);
|
|
let delta = current_green_life - self.green_life();
|
|
|
|
events.push(Event::Damage {
|
|
skill,
|
|
amount: delta,
|
|
mitigation: 0,
|
|
colour: Colour::Green,
|
|
});
|
|
}
|
|
}
|
|
|
|
return events;
|
|
}
|
|
|
|
pub fn deal_red_damage(&mut self, skill: Skill, amount: u64) -> Vec<Event> {
|
|
let mut events = vec![];
|
|
|
|
if let Some(immunity) = self.immune(skill) {
|
|
if !self.is_ko() {
|
|
events.push(Event::Immunity { skill, immunity });
|
|
}
|
|
return events;
|
|
}
|
|
|
|
let mods = self.effects.iter()
|
|
.filter(|e| e.effect.modifications().contains(&Stat::RedDamageTaken))
|
|
.map(|e| (e.effect, e.meta))
|
|
.collect::<Vec<(Effect, Option<EffectMeta>)>>();
|
|
|
|
let modified_power = mods.iter()
|
|
.fold(amount, |acc, fx| fx.0.apply(acc, fx.1));
|
|
|
|
match self.affected(Effect::Invert) {
|
|
false => {
|
|
// calculate amount of damage red_life will not absorb
|
|
// eg 50 red_life 25 damage -> 0 remainder 25 mitigation
|
|
// 50 red_life 100 damage -> 50 remainder 50 mitigation
|
|
// 50 red_life 5 damage -> 0 remainder 5 mitigation
|
|
let remainder = modified_power.saturating_sub(self.red_life.value);
|
|
let mitigation = modified_power.saturating_sub(remainder);
|
|
|
|
// reduce red_life by mitigation amount
|
|
self.red_life.reduce(mitigation);
|
|
|
|
// deal remainder to green_life
|
|
let current_green_life = self.green_life();
|
|
self.reduce_green_life(remainder);
|
|
let delta = current_green_life - self.green_life();
|
|
|
|
events.push(Event::Damage {
|
|
skill,
|
|
amount: delta,
|
|
mitigation,
|
|
colour: Colour::Red,
|
|
});
|
|
},
|
|
true => {
|
|
// events.push(Event::Inversion { skill });
|
|
|
|
let current_green_life = self.green_life();
|
|
self.green_life.increase(modified_power);
|
|
let new_green_life = self.green_life.value;
|
|
let healing = new_green_life - current_green_life;
|
|
let overhealing = modified_power - healing;
|
|
|
|
let current_life = self.red_life.value;
|
|
self.red_life.increase(overhealing);
|
|
let recharge = self.red_life.value - current_life;
|
|
|
|
if healing > 0 {
|
|
events.push(Event::Healing {
|
|
skill,
|
|
amount: healing,
|
|
overhealing: overhealing - recharge,
|
|
});
|
|
}
|
|
|
|
if recharge > 0 {
|
|
events.push(Event::Recharge { red: recharge, blue: 0, skill });
|
|
}
|
|
}
|
|
};
|
|
|
|
return events;
|
|
}
|
|
|
|
pub fn deal_blue_damage(&mut self, skill: Skill, amount: u64) -> Vec<Event> {
|
|
let mut events = vec![];
|
|
|
|
if let Some(immunity) = self.immune(skill) {
|
|
if !self.is_ko() {
|
|
events.push(Event::Immunity { skill, immunity });
|
|
}
|
|
return events;
|
|
}
|
|
|
|
let mods = self.effects.iter()
|
|
.filter(|e| e.effect.modifications().contains(&Stat::BlueDamageTaken))
|
|
.map(|e| (e.effect, e.meta))
|
|
.collect::<Vec<(Effect, Option<EffectMeta>)>>();
|
|
|
|
let modified_power = mods.iter()
|
|
.fold(amount, |acc, fx| fx.0.apply(acc, fx.1));
|
|
|
|
match self.affected(Effect::Invert) {
|
|
false => {
|
|
let remainder = modified_power.saturating_sub(self.blue_life.value);
|
|
let mitigation = modified_power.saturating_sub(remainder);
|
|
|
|
// reduce blue_life by mitigation amount
|
|
self.blue_life.reduce(mitigation);
|
|
|
|
// deal remainder to green_life
|
|
let current_green_life = self.green_life();
|
|
self.reduce_green_life(remainder);
|
|
let delta = current_green_life - self.green_life();
|
|
|
|
events.push(Event::Damage {
|
|
skill,
|
|
amount: delta,
|
|
mitigation,
|
|
colour: Colour::Blue,
|
|
});
|
|
},
|
|
true => {
|
|
// events.push(Event::Inversion { skill });
|
|
|
|
let current_green_life = self.green_life();
|
|
self.green_life.increase(modified_power);
|
|
let new_green_life = self.green_life.value;
|
|
let healing = new_green_life - current_green_life;
|
|
let overhealing = modified_power - healing;
|
|
|
|
let current_life = self.blue_life.value;
|
|
self.blue_life.increase(overhealing);
|
|
let recharge = self.blue_life.value - current_life;
|
|
|
|
if healing > 0 {
|
|
events.push(Event::Healing {
|
|
skill,
|
|
amount: healing,
|
|
overhealing,
|
|
});
|
|
}
|
|
|
|
if recharge > 0 {
|
|
events.push(Event::Recharge { red: 0, blue: recharge, skill });
|
|
}
|
|
}
|
|
};
|
|
|
|
return events;
|
|
}
|
|
|
|
pub fn add_effect(&mut self, skill: Skill, effect: ConstructEffect) -> Event {
|
|
if let Some(immunity) = self.immune(skill) {
|
|
return Event::Immunity {
|
|
skill,
|
|
immunity,
|
|
};
|
|
}
|
|
|
|
if let Some(p) = self.effects.iter().position(|ce| ce.effect == effect.effect) {
|
|
// duplicate effect
|
|
// replace existing
|
|
|
|
self.effects[p] = effect;
|
|
} else {
|
|
// new effect
|
|
// info!("{:?} {:?} adding effect", self.name, effect.effect);
|
|
self.effects.push(effect);
|
|
}
|
|
|
|
// todo modified durations cause of buffs
|
|
let result = Event::Effect {
|
|
effect: effect.effect,
|
|
duration: effect.duration,
|
|
construct_effects: self.effects.clone(),
|
|
skill,
|
|
};
|
|
return result;
|
|
}
|
|
|
|
// pub fn evade(&self, skill: Skill) -> Option<Event> {
|
|
// if self.evasion.value == 0 {
|
|
// return None;
|
|
// }
|
|
|
|
// let mut rng = thread_rng();
|
|
// let green_life_pct = (self.green_life.value * 100) / self.green_life.value;
|
|
// let evasion_rating = (self.evasion.value * green_life_pct) / 100;
|
|
// let roll = rng.gen_range(0, 100);
|
|
// info!("{:} < {:?}", roll, evasion_rating);
|
|
|
|
// match roll < evasion_rating {
|
|
// true => Some(Event::Evasion {
|
|
// skill,
|
|
// evasion_rating: evasion_rating,
|
|
// }),
|
|
// false => None,
|
|
// }
|
|
// }
|
|
}
|
|
|
|
pub fn construct_delete(tx: &mut Transaction, id: Uuid, account_id: Uuid) -> Result<(), Error> {
|
|
let query = "
|
|
DELETE
|
|
FROM constructs
|
|
WHERE id = $1
|
|
and account = $2;
|
|
";
|
|
|
|
let result = tx
|
|
.execute(query, &[&id, &account_id])?;
|
|
|
|
if result != 1 {
|
|
return Err(format_err!("unable to delete construct {:?}", id));
|
|
}
|
|
|
|
info!("construct deleted {:?}", id);
|
|
|
|
return Ok(());
|
|
}
|
|
|
|
pub fn construct_get(tx: &mut Transaction, id: Uuid, account_id: Uuid) -> Result<Construct, Error> {
|
|
let query = "
|
|
SELECT data
|
|
FROM constructs
|
|
WHERE id = $1
|
|
AND account = $2;
|
|
";
|
|
|
|
let result = tx
|
|
.query(query, &[&id, &account_id])?;
|
|
|
|
let result = result.iter().next().ok_or(format_err!("construct {:} not found", id))?;
|
|
let construct_bytes: Vec<u8> = result.get(0);
|
|
let skeleton = from_slice::<ConstructSkeleton>(&construct_bytes)?;
|
|
|
|
return Ok(Construct::from_skeleton(&skeleton));
|
|
}
|
|
|
|
pub fn construct_select(tx: &mut Transaction, id: Uuid, account_id: Uuid) -> Result<Construct, Error> {
|
|
let query = "
|
|
SELECT data
|
|
FROM constructs
|
|
WHERE id = $1
|
|
AND account = $2
|
|
FOR UPDATE;
|
|
";
|
|
|
|
let result = tx
|
|
.query(query, &[&id, &account_id])?;
|
|
|
|
let result = result.iter().next().ok_or(format_err!("construct {:} not found", id))?;
|
|
let construct_bytes: Vec<u8> = result.get(0);
|
|
let skeleton = from_slice::<ConstructSkeleton>(&construct_bytes)?;
|
|
|
|
return Ok(Construct::from_skeleton(&skeleton));
|
|
}
|
|
|
|
pub fn construct_spawn(tx: &mut Transaction, account: Uuid, name: String, team: bool) -> Result<Construct, Error> {
|
|
let construct = Construct::new()
|
|
.named(&name)
|
|
.set_account(account);
|
|
|
|
let construct_bytes = to_vec(&construct)?;
|
|
|
|
let query = "
|
|
INSERT INTO constructs (id, account, data, team)
|
|
VALUES ($1, $2, $3, $4)
|
|
RETURNING id, account;
|
|
";
|
|
|
|
let result = tx
|
|
.query(query, &[&construct.id, &account, &construct_bytes, &team])?;
|
|
|
|
let _returned = result.iter().next().ok_or(err_msg("no row returned"))?;
|
|
|
|
img::shapes_write(construct.img)?;
|
|
|
|
info!("spawned construct account={:} name={:?}", account, construct.name);
|
|
return Ok(construct);
|
|
}
|
|
|
|
pub fn construct_write(tx: &mut Transaction, construct: Construct) -> Result<Construct, Error> {
|
|
let construct_bytes = to_vec(&construct.to_skeleton())?;
|
|
|
|
let query = "
|
|
UPDATE constructs
|
|
SET data = $1, updated_at = now()
|
|
WHERE id = $2
|
|
RETURNING id, account, data;
|
|
";
|
|
|
|
let result = tx
|
|
.query(query, &[&construct_bytes, &construct.id])?;
|
|
|
|
let _returned = result.iter().next().expect("no row returned");
|
|
|
|
// info!("{:?} wrote construct", construct.id);
|
|
|
|
return Ok(construct);
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use construct::*;
|
|
use util::IntPct;
|
|
|
|
#[test]
|
|
fn create_construct_test() {
|
|
let construct = Construct::new()
|
|
.named(&"hatchling".to_string());
|
|
|
|
assert_eq!(construct.name, "hatchling".to_string());
|
|
return;
|
|
}
|
|
|
|
#[test]
|
|
fn construct_colours_test() {
|
|
let mut construct = Construct::new()
|
|
.named(&"redboi".to_string());
|
|
|
|
construct.learn_mut(Skill::Strike);
|
|
construct.spec_add(Spec::LifeGG).unwrap();
|
|
construct.spec_add(Spec::PowerRR).unwrap();
|
|
construct.spec_add(Spec::LifeBB).unwrap();
|
|
|
|
assert_eq!(construct.colours.red, 4);
|
|
assert_eq!(construct.colours.green, 2);
|
|
assert_eq!(construct.colours.blue, 2);
|
|
|
|
return;
|
|
}
|
|
|
|
#[test]
|
|
fn construct_player_modifiers_test() {
|
|
let mut construct = Construct::new()
|
|
.named(&"player player".to_string());
|
|
|
|
construct.spec_add(Spec::PowerRR).unwrap();
|
|
construct.spec_add(Spec::PowerGG).unwrap();
|
|
construct.spec_add(Spec::PowerBB).unwrap();
|
|
construct.learn_mut(Skill::StrikePlusPlus); // 18 reds (24 total)
|
|
|
|
let player_colours = Colours {
|
|
red: 5,
|
|
green: 15,
|
|
blue: 25,
|
|
};
|
|
|
|
construct.apply_modifiers(&player_colours);
|
|
|
|
assert!(construct.red_power.value == construct.red_power.base + construct.red_power.base.pct(35));
|
|
assert!(construct.green_power.value == construct.green_power.base + construct.green_power.base.pct(50));
|
|
assert!(construct.blue_power.value == construct.blue_power.base + construct.blue_power.base.pct(70));
|
|
|
|
return;
|
|
}
|
|
|
|
#[test]
|
|
fn construct_player_modifiers_base_test() {
|
|
let mut construct = Construct::new()
|
|
.named(&"player player".to_string());
|
|
|
|
construct.spec_add(Spec::Power).unwrap();
|
|
construct.spec_add(Spec::Life).unwrap();
|
|
|
|
let player_colours = Colours {
|
|
red: 5,
|
|
green: 15,
|
|
blue: 25,
|
|
};
|
|
|
|
construct.apply_modifiers(&player_colours);
|
|
|
|
assert!(construct.red_power.value == construct.red_power.base + construct.red_power.base.pct(10));
|
|
assert!(construct.green_power.value == construct.green_power.base + construct.green_power.base.pct(10));
|
|
assert!(construct.blue_power.value == construct.blue_power.base + construct.blue_power.base.pct(10));
|
|
assert!(construct.green_life.value == construct.green_life.base + 125);
|
|
|
|
return;
|
|
}
|
|
|
|
#[test]
|
|
fn construct_colour_calc_test() {
|
|
let mut construct = Construct::new()
|
|
.named(&"player player".to_string());
|
|
|
|
construct.spec_add(Spec::PowerRR).unwrap();
|
|
construct.spec_add(Spec::PowerGG).unwrap();
|
|
construct.spec_add(Spec::PowerBB).unwrap();
|
|
|
|
let colours = Colours::from_construct(&construct);
|
|
assert!(colours.red == 2);
|
|
assert!(colours.blue == 2);
|
|
assert!(colours.green == 2);
|
|
|
|
let construct = construct
|
|
.learn(Skill::Strike)
|
|
.learn(Skill::BlastPlusPlus);
|
|
|
|
let colours = Colours::from_construct(&construct);
|
|
assert!(colours.red == 4);
|
|
assert!(colours.blue == 10);
|
|
assert!(colours.green == 2);
|
|
}
|
|
|
|
#[test]
|
|
fn construct_player_modifiers_spec_bonus_test() {
|
|
let mut construct = Construct::new()
|
|
.named(&"player player".to_string());
|
|
|
|
construct.spec_add(Spec::PowerRR).unwrap();
|
|
construct.spec_add(Spec::PowerGG).unwrap();
|
|
construct.spec_add(Spec::PowerBB).unwrap();
|
|
|
|
let player_colours = Colours {
|
|
red: 5,
|
|
green: 0,
|
|
blue: 0,
|
|
};
|
|
|
|
construct.apply_modifiers(&player_colours);
|
|
|
|
assert!(construct.red_power.value == construct.red_power.base + construct.red_power.base.pct(35));
|
|
assert!(construct.green_power.value == construct.green_power.base + construct.green_power.base.pct(25));
|
|
assert!(construct.blue_power.value == construct.blue_power.base + construct.blue_power.base.pct(25));
|
|
|
|
return;
|
|
}
|
|
|
|
}
|