diff --git a/server/src/cryp.rs b/server/src/cryp.rs index 5e27aa23..df5b3d8a 100755 --- a/server/src/cryp.rs +++ b/server/src/cryp.rs @@ -15,8 +15,8 @@ type Cooldown = Option; #[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)] pub struct CrypSkill { - skill: Skill, - cd: Cooldown, + pub skill: Skill, + pub cd: Cooldown, } impl CrypSkill { @@ -200,6 +200,32 @@ impl Cryp { self.skills.iter().any(|s| s.skill == skill && s.can_cast(self)) } + pub fn skill_set_cd(&mut self, skill: Skill) -> &mut Cryp { + let i = self.skills.iter().position(|s| s.skill == skill).unwrap(); + self.skills.remove(i); + self.skills.push(CrypSkill::new(skill)); + + self + } + + pub fn decrease_cooldowns(&mut self) -> &mut Cryp { + for skill in self.skills.iter_mut() { + if let Some(cd) = skill.cd { + // if the cd is 1 it becomes none + if cd == 1 { + println!("{:?} is now off cd", skill.skill); + skill.cd = None; + continue; + } + + // otherwise decrement it + skill.cd = Some(cd.saturating_sub(1)); + } + } + + self + } + pub fn rez(&mut self) -> &mut Cryp { self.hp.set(self.stam.value); self diff --git a/server/src/game.rs b/server/src/game.rs index 94dcf67e..6a0c8500 100755 --- a/server/src/game.rs +++ b/server/src/game.rs @@ -60,6 +60,28 @@ impl Team { None => panic!("abiltity not in game"), } } + + pub fn progress_cooldowns(&mut self) -> &mut Team { + // get a copy of the used skills + for skill in self.skills.clone().iter() { + // copy the creep cause we will replace it + let mut cryp = self.cryp_by_id(skill.cryp_id).unwrap().clone(); + + if skill.used_cooldown() { + cryp.skill_set_cd(skill.skill); + self.update_cryp(cryp); + continue; + } + + cryp.decrease_cooldowns(); + + println!("{:?}", cryp); + + self.update_cryp(cryp); + } + + self + } } #[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)] @@ -333,12 +355,20 @@ impl Game { let mut cryp = self.cryp_by_id(incoming.cryp_id).clone(); let mut target_cryp = self.cryp_by_id(incoming.target_cryp_id.unwrap()).clone(); let resolution = incoming.resolve(&mut cryp, &mut target_cryp); + + // push the resolved spell on team.resolved.push(*resolution); team.update_cryp(target_cryp); self.update_team(team); } } + // now damage has all been assigned + // handle cooldowns + for team in self.teams.iter_mut() { + team.progress_cooldowns(); + } + self } @@ -648,8 +678,6 @@ mod tests { .learn(Skill::Block) .create(); - let _x_id = x.id; - let y = Cryp::new() .named(&"lemongrass tea".to_string()) .level(8) @@ -658,8 +686,6 @@ mod tests { .learn(Skill::Block) .create(); - let _y_id = y.id; - let mut game = Game::new(); game @@ -688,69 +714,62 @@ mod tests { return game; } - #[test] - fn game_test() { - let x = Cryp::new() - .named(&"pronounced \"creeep\"".to_string()) - .level(8) - .create(); + // #[test] + // fn phase_test() { + // let mut game = create_test_game(); - let x_id = x.id; + // let x_team = game.teams[0].clone(); + // let y_team = game.teams[1].clone(); - let y = Cryp::new() - .named(&"lemongrass tea".to_string()) - .level(8) - .create(); + // let x_cryp = x_team.cryps[0].clone(); + // let y_cryp = y_team.cryps[0].clone(); - let y_id = y.id; + // let x_attack_id = game.add_skill(x_team.id, x_cryp.id, y_team.id, Skill::Attack).unwrap(); + // let y_attack_id = game.add_skill(y_team.id, y_cryp.id, x_team.id, Skill::Attack).unwrap(); - let mut game = Game::new(); + // assert!(game.skill_phase_finished()); - game - .set_team_num(2) - .set_team_size(1) - .set_pve(false); + // game.target_phase_start(); - let x_team_id = Uuid::new_v4(); - let mut x_team = Team::new(x_team_id); - x_team - .set_cryps(vec![x]); + // game.add_target(x_team.id, x_cryp.id, y_attack_id).unwrap(); + // game.add_target(y_team.id, y_cryp.id, x_attack_id).unwrap(); - let y_team_id = Uuid::new_v4(); - let mut y_team = Team::new(y_team_id); - y_team - .set_cryps(vec![y]); + // assert!(game.target_phase_finished()); - game - .add_team(x_team).unwrap() - .add_team(y_team).unwrap(); + // game.damage_phase_start(); - assert!(game.can_start()); + // assert!([Phase::Skill, Phase::Finish].contains(&game.phase)); - game.start(); + // return; + // } - let x_attack_id = game.add_skill(x_team_id, x_id, y_team_id, Skill::Attack); - let y_attack_id = game.add_skill(y_team_id, y_id, x_team_id, Skill::Attack); + // #[test] + // fn stun_test() { + // let mut game = create_test_game(); - assert!(game.skill_phase_finished()); + // let x_team = game.teams[0].clone(); + // let y_team = game.teams[1].clone(); - game.target_phase_start(); + // let x_cryp = x_team.cryps[0].clone(); + // let y_cryp = y_team.cryps[0].clone(); - println!("{:?}", game); + // let x_stun_id = game.add_skill(x_team.id, x_cryp.id, y_team.id, Skill::TestStun).unwrap(); + // let y_attack_id = game.add_skill(y_team.id, y_cryp.id, x_team.id, Skill::TestTouch).unwrap(); - game.add_target(x_team_id, x_id, y_attack_id.unwrap()).unwrap(); - game.add_target(y_team_id, y_id, x_attack_id.unwrap()).unwrap(); + // assert!(game.skill_phase_finished()); + // game.target_phase_start(); - assert!(game.target_phase_finished()); + // game.add_target(x_team.id, x_cryp.id, y_attack_id).unwrap(); + // game.add_target(y_team.id, y_cryp.id, x_stun_id).unwrap(); - game.damage_phase_start(); + // assert!(game.target_phase_finished()); + // game.damage_phase_start(); - assert!([Phase::Skill, Phase::Finish].contains(&game.phase)); - - println!("{:?}", game); - - return; - } + // // should auto progress back to skill phase + // assert!(game.phase == Phase::Skill); + // assert!(game.team_by_id(y_team.id).cryps[0].is_stunned()); + // assert!(game.team_by_id(y_team.id).skills_required() == 0); + // } #[test] fn cooldown_test() { @@ -762,28 +781,32 @@ mod tests { let x_cryp = x_team.cryps[0].clone(); let y_cryp = y_team.cryps[0].clone(); - let x_stun_id = game.add_skill(x_team.id, x_cryp.id, y_team.id, Skill::TestStun).unwrap(); + let x_stun_id = game.add_skill(x_team.id, x_cryp.id, y_team.id, Skill::TestTouch).unwrap(); let y_attack_id = game.add_skill(y_team.id, y_cryp.id, x_team.id, Skill::TestTouch).unwrap(); - assert!(game.skill_phase_finished()); game.target_phase_start(); - game.add_target(x_team.id, x_cryp.id, y_attack_id).unwrap(); game.add_target(y_team.id, y_cryp.id, x_stun_id).unwrap(); - assert!(game.target_phase_finished()); game.damage_phase_start(); // should auto progress back to skill phase assert!(game.phase == Phase::Skill); - assert!(game.team_by_id(y_team.id).cryps[0].is_stunned()); - assert!(game.team_by_id(y_team.id).skills_required() == 0); - // should be on cooldown now - assert!(game.add_skill(x_team.id, x_cryp.id, y_team.id, Skill::Stun).is_err()); - - println!("{:#?}", game); // after 1 turn block should be off cooldown assert!(game.team_by_id(y_team.id).cryps[0].skill_on_cd(Skill::Block).is_none()); + + // second round + // now we block and it should go back on cd + let x_block_id = game.add_skill(x_team.id, x_cryp.id, y_team.id, Skill::Block).unwrap(); + let y_block_id = game.add_skill(y_team.id, y_cryp.id, x_team.id, Skill::Block).unwrap(); + + game.target_phase_start(); + game.add_target(x_team.id, x_cryp.id, y_block_id).unwrap(); + game.add_target(y_team.id, y_cryp.id, x_block_id).unwrap(); + game.damage_phase_start(); + + assert!(game.team_by_id(y_team.id).cryps[0].skill_on_cd(Skill::Block).is_some()); + println!("{:#?}", game); } } diff --git a/server/src/skill.rs b/server/src/skill.rs index cb65ee6e..0e2d2fc3 100755 --- a/server/src/skill.rs +++ b/server/src/skill.rs @@ -1,7 +1,7 @@ use rand::prelude::*; use uuid::Uuid; -use cryp::{Cryp}; +use cryp::{Cryp, CrypSkill}; #[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)] pub struct Roll { @@ -72,6 +72,11 @@ impl Cast { self.target_cryp_id = Some(cryp_id); self } + + pub fn used_cooldown(self) -> bool { + let cs = CrypSkill::new(self.skill); + return cs.cd.is_some(); + } } // #[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]