results for player actions

This commit is contained in:
ntr 2018-10-21 12:54:33 +11:00
parent c3d20e888b
commit 257fb860bb
3 changed files with 62 additions and 30 deletions

View File

@ -6,6 +6,7 @@
# WORK WORK # WORK WORK
* QOL * QOL
* auto login * auto login
* cryp list on spawn
* ws reconnect ✔ * ws reconnect ✔
* Levelling ✔ * Levelling ✔
@ -32,6 +33,7 @@
* check for cryp skill ownership * check for cryp skill ownership
* check for game participation * check for game participation
* write players row for every team+cryp added * write players row for every team+cryp added
* return results<>
* defensive * defensive

View File

@ -7,9 +7,9 @@ use postgres::transaction::Transaction;
use failure::Error; use failure::Error;
use failure::err_msg; use failure::err_msg;
use net::Db;
use account::Account; use account::Account;
use rpc::{CrypSpawnParams}; use rpc::{CrypSpawnParams};
use game::Skill;
// use skill::{Skill}; // use skill::{Skill};
#[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)] #[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
@ -86,7 +86,7 @@ pub struct Cryp {
pub hp: CrypStat, pub hp: CrypStat,
pub xp: u64, pub xp: u64,
pub lvl: u8, pub lvl: u8,
// pub skills: Vec<Skill>, pub skills: Vec<Skill>,
pub name: String, pub name: String,
} }
@ -108,7 +108,7 @@ impl Cryp {
hp: CrypStat { value: 0, stat: Stat::Hp }, hp: CrypStat { value: 0, stat: Stat::Hp },
lvl: 0, lvl: 0,
xp: 0, xp: 0,
// skills: vec![], skills: vec![Skill::Attack],
name: String::new() name: String::new()
}; };
} }
@ -129,10 +129,10 @@ impl Cryp {
self self
} }
// pub fn learn(mut self, s: Skill) -> Cryp { pub fn learn(mut self, s: Skill) -> Cryp {
// self.skills.push(s); self.skills.push(s);
// self self
// } }
pub fn add_xp(mut self) -> Cryp { pub fn add_xp(mut self) -> Cryp {
self.xp = self.xp.saturating_add(1); self.xp = self.xp.saturating_add(1);

View File

@ -164,6 +164,11 @@ impl Game {
self self
} }
pub fn set_pve(&mut self, pve: bool) -> &mut Game {
self.is_pve = pve;
self
}
pub fn add_team(&mut self, team: Team) -> &mut Game { pub fn add_team(&mut self, team: Team) -> &mut Game {
if self.teams.len() == self.team_num { if self.teams.len() == self.team_num {
panic!("maximum number of teams"); panic!("maximum number of teams");
@ -187,11 +192,21 @@ impl Game {
return in_team.unwrap(); return in_team.unwrap();
} }
} }
panic!("cryp not in game"); panic!("cryp not in game");
} }
fn replace_cryp(&mut self, updated: Cryp) -> &mut Game { fn update_team(&mut self, updated: Team) -> &mut Game {
match self.teams.iter().position(|t| t.id == updated.id) {
Some(index) => {
self.teams.remove(index);
self.teams.push(updated);
self
}
None => panic!("team not in game"),
}
}
fn update_cryp(&mut self, updated: Cryp) -> &mut Game {
for team in self.teams.iter_mut() { for team in self.teams.iter_mut() {
if let Some(index) = team.cryps.iter().position(|c| c.id == updated.id) { if let Some(index) = team.cryps.iter().position(|c| c.id == updated.id) {
team.cryps.remove(index); team.cryps.remove(index);
@ -237,7 +252,7 @@ impl Game {
// TODO attack multiple players based on some criteria // TODO attack multiple players based on some criteria
let player_team = teams.iter().find(|t| t.id != mob_team_id).unwrap(); let player_team = teams.iter().find(|t| t.id != mob_team_id).unwrap();
for mob in &mobs.cryps { for mob in &mobs.cryps {
self.add_skill(mob_team_id, mob.id, player_team.id, Skill::Attack); self.add_skill(mob_team_id, mob.id, player_team.id, Skill::Attack).unwrap();
} }
} }
@ -246,20 +261,34 @@ impl Game {
// skills can target any team, but we have to check if the caller is the owner of the cryp // skills can target any team, but we have to check if the caller is the owner of the cryp
// and that the cryp has the skill they are trying to add // and that the cryp has the skill they are trying to add
pub fn add_skill(&mut self, team_id: Uuid, cryp_id: Uuid, target_team_id: Uuid, skill: Skill) -> Uuid { pub fn add_skill(&mut self, team_id: Uuid, cryp_id: Uuid, target_team_id: Uuid, skill: Skill) -> Result<Uuid, Error> {
let team = self.team_by_id(team_id); if self.phase != Phase::Skill {
match team.cryp_by_id(cryp_id) { return Err(err_msg("game not in skill phase"));
Some(c) => c, }
None => panic!("cryp not in team"),
}; let team = self.team_by_id(team_id);
{
let cryp = match team.cryp_by_id(cryp_id) {
Some(c) => c,
None => return Err(err_msg("cryp not in team")),
};
// check the cryp has the skill
if !cryp.skills.contains(&skill) {
return Err(err_msg("cryp does not have that skill"));
}
}
// replace cryp skill
if let Some(s) = team.skills.iter_mut().position(|s| s.cryp_id == cryp_id) {
team.skills.remove(s);
}
// TODO check cryp ownership
// TODO check cryp skill already used
// TODO check cryp has skill
let skill = GameSkill::new(cryp_id, target_team_id, skill); let skill = GameSkill::new(cryp_id, target_team_id, skill);
team.skills.push(skill); team.skills.push(skill);
return skill.id; return Ok(skill.id);
} }
pub fn skill_phase_finished(&self) -> bool { pub fn skill_phase_finished(&self) -> bool {
@ -297,7 +326,7 @@ impl Game {
let mobs = self.team_by_id(mob_team_id).clone(); let mobs = self.team_by_id(mob_team_id).clone();
// TODO attack multiple players based on some criteria // TODO attack multiple players based on some criteria
for incoming in &mobs.incoming { for incoming in &mobs.incoming {
self.add_target(mob_team_id, mobs.cryps[0].id, incoming.id); self.add_target(mob_team_id, mobs.cryps[0].id, incoming.id).unwrap();
} }
} }
@ -306,19 +335,19 @@ impl Game {
// targets can only be added by the owner of the team // targets can only be added by the owner of the team
pub fn add_target(&mut self, team_id: Uuid, cryp_id: Uuid, skill_id: Uuid) -> &mut GameSkill { pub fn add_target(&mut self, team_id: Uuid, cryp_id: Uuid, skill_id: Uuid) -> Result<&mut GameSkill, Error> {
// whose team is this? // whose team is this?
let team = self.team_by_id(team_id); let team = self.team_by_id(team_id);
// is the target in the team? // is the target in the team?
match team.cryp_by_id(cryp_id) { match team.cryp_by_id(cryp_id) {
Some(c) => c, Some(c) => c,
None => panic!("cryp not in team"), None => return Err(err_msg("cryp not in team")),
}; };
// set the target // set the target
let skill = team.skill_by_id(skill_id); let skill = team.skill_by_id(skill_id);
skill.set_target(cryp_id) Ok(skill.set_target(cryp_id))
} }
pub fn target_phase_finished(&self) -> bool { pub fn target_phase_finished(&self) -> bool {
@ -357,7 +386,7 @@ impl Game {
let mut cryp = self.cryp_by_id(incoming.target_cryp_id.unwrap()).clone(); let mut cryp = self.cryp_by_id(incoming.target_cryp_id.unwrap()).clone();
let mut target_cryp = self.cryp_by_id(incoming.target_cryp_id.unwrap()).clone(); let mut target_cryp = self.cryp_by_id(incoming.target_cryp_id.unwrap()).clone();
incoming.resolve(&mut cryp, &mut target_cryp); incoming.resolve(&mut cryp, &mut target_cryp);
self.replace_cryp(target_cryp); self.update_cryp(target_cryp);
} }
} }
self self
@ -398,7 +427,7 @@ pub fn game_skill(params: GameSkillParams, tx: &mut Transaction, account: &Accou
let game_bytes: Vec<u8> = returned.get("data"); let game_bytes: Vec<u8> = returned.get("data");
let mut game = from_slice::<Game>(&game_bytes)?; let mut game = from_slice::<Game>(&game_bytes)?;
game.add_skill(account.id, params.cryp_id, params.target_team_id, params.skill); game.add_skill(account.id, params.cryp_id, params.target_team_id, params.skill)?;
if game.skill_phase_finished() { if game.skill_phase_finished() {
game.target_phase_start(); game.target_phase_start();
@ -426,7 +455,7 @@ pub fn game_target(params: GameTargetParams, tx: &mut Transaction, account: &Acc
let game_bytes: Vec<u8> = returned.get("data"); let game_bytes: Vec<u8> = returned.get("data");
let mut game = from_slice::<Game>(&game_bytes)?; let mut game = from_slice::<Game>(&game_bytes)?;
game.add_target(account.id, params.cryp_id, params.skill_id); game.add_target(account.id, params.cryp_id, params.skill_id)?;
if game.target_phase_finished() { if game.target_phase_finished() {
game.damage_phase_start(); game.damage_phase_start();
@ -597,7 +626,8 @@ mod tests {
game game
.set_team_num(2) .set_team_num(2)
.set_team_size(1); .set_team_size(1)
.set_pve(false);
let x_team_id = Uuid::new_v4(); let x_team_id = Uuid::new_v4();
let mut x_team = Team::new(x_team_id); let mut x_team = Team::new(x_team_id);
@ -626,8 +656,8 @@ mod tests {
println!("{:?}", game); println!("{:?}", game);
game.add_target(x_team_id, x_id, y_attack_id); game.add_target(x_team_id, x_id, y_attack_id.unwrap());
game.add_target(y_team_id, y_id, x_attack_id); game.add_target(y_team_id, y_id, x_attack_id.unwrap());
assert!(game.target_phase_finished()); assert!(game.target_phase_finished());