diff --git a/core/fixme.md b/core/fixme.md index 3bb87f4a..1370b752 100644 --- a/core/fixme.md +++ b/core/fixme.md @@ -1,5 +1,6 @@ # FIXME +game ready not auto starting resolve phase remove immunity banish dmg aoe event diff --git a/core/src/game.rs b/core/src/game.rs index b1947ccb..8a6dd536 100644 --- a/core/src/game.rs +++ b/core/src/game.rs @@ -57,9 +57,9 @@ pub struct Game { pub stack: Vec, pub events: Vec>, pub instance: Option, - time_control: TimeControl, - phase_start: DateTime, - phase_end: Option>, + pub time_control: TimeControl, + pub phase_start: DateTime, + pub phase_end: Option>, } impl Game { @@ -285,7 +285,7 @@ impl Game { self } - fn add_skill(&mut self, player_id: Uuid, source: Uuid, target: Uuid, skill: Skill) -> Result<&mut Game, Error> { + pub fn add_skill(&mut self, player_id: Uuid, source: Uuid, target: Uuid, skill: Skill) -> Result<&mut Game, Error> { // check player in game self.player_by_id(player_id)?; @@ -343,7 +343,7 @@ impl Game { return Ok(self); } - fn offer_draw(mut self, player_id: Uuid) -> Result { + pub fn offer_draw(mut self, player_id: Uuid) -> Result { if self.phase != Phase::Skill { return Err(err_msg("game not in skill phase")); } @@ -367,7 +367,7 @@ impl Game { return Ok(self); } - fn concede(mut self, player_id: Uuid) -> Result { + pub fn concede(mut self, player_id: Uuid) -> Result { if self.phase != Phase::Skill { return Err(err_msg("game not in skill phase")); } @@ -379,7 +379,7 @@ impl Game { } - fn clear_skill(&mut self, player_id: Uuid) -> Result<&mut Game, Error> { + pub fn clear_skill(&mut self, player_id: Uuid) -> Result<&mut Game, Error> { self.player_by_id(player_id)?; if self.phase != Phase::Skill { return Err(err_msg("game not in skill phase")); @@ -390,7 +390,7 @@ impl Game { return Ok(self); } - fn player_ready(&mut self, player_id: Uuid) -> Result<&mut Game, Error> { + pub fn player_ready(&mut self, player_id: Uuid) -> Result<&mut Game, Error> { if self.phase != Phase::Skill { return Err(err_msg("game not in skill phase")); } @@ -401,7 +401,7 @@ impl Game { Ok(self) } - fn skill_phase_finished(&self) -> bool { + pub fn skill_phase_finished(&self) -> bool { self.players.iter().all(|t| t.ready) // self.players.iter() // // for every player @@ -413,7 +413,7 @@ impl Game { // ) } - fn resolve_phase_start(mut self) -> Game { + pub fn resolve_phase_start(mut self) -> Game { if self.phase != Phase::Skill { panic!("game not in skill phase"); } @@ -722,11 +722,11 @@ mod tests { let x_player_id = Uuid::new_v4(); x.account = x_player_id; - let x_player = Player::new(x_player_id, &"ntr".to_string(), vec![x]); + let x_player = Player::new(x_player_id, None, &"ntr".to_string(), vec![x]); let y_player_id = Uuid::new_v4(); y.account = y_player_id; - let y_player = Player::new(y_player_id, &"mash".to_string(), vec![y]); + let y_player = Player::new(y_player_id, None, &"mash".to_string(), vec![y]); game .player_add(x_player).unwrap() @@ -767,12 +767,12 @@ mod tests { let i_player_id = Uuid::new_v4(); i.account = i_player_id; j.account = i_player_id; - let i_player = Player::new(i_player_id, &"ntr".to_string(), vec![i, j]); + let i_player = Player::new(i_player_id, None, &"ntr".to_string(), vec![i, j]); let x_player_id = Uuid::new_v4(); x.account = x_player_id; y.account = x_player_id; - let x_player = Player::new(x_player_id, &"mashy".to_string(), vec![x, y]); + let x_player = Player::new(x_player_id, None, &"mashy".to_string(), vec![x, y]); game .player_add(i_player).unwrap() diff --git a/core/src/instance.rs b/core/src/instance.rs index 83d7875e..27d09bc0 100644 --- a/core/src/instance.rs +++ b/core/src/instance.rs @@ -99,14 +99,14 @@ pub struct Instance { time_control: TimeControl, phase: InstancePhase, - phase_end: Option>, - phase_start: DateTime, + pub phase_end: Option>, + pub phase_start: DateTime, winner: Option, } impl Instance { - fn new() -> Instance { + pub fn new() -> Instance { Instance { id: Uuid::new_v4(), players: vec![], @@ -170,7 +170,7 @@ impl Instance { (self, new_game) } - fn set_name(mut self, name: String) -> Result { + pub fn set_name(mut self, name: String) -> Result { if name.len() == 0 { return Err(err_msg("name must have a length")); } @@ -179,12 +179,12 @@ impl Instance { Ok(self) } - fn set_time_control(mut self, tc: TimeControl) -> Instance { + pub fn set_time_control(mut self, tc: TimeControl) -> Instance { self.time_control = tc; self } - fn add_player(&mut self, player: Player) -> Result<&mut Instance, Error> { + pub fn add_player(&mut self, player: Player) -> Result<&mut Instance, Error> { if self.players.len() >= self.max_players { return Err(err_msg("game full")) } @@ -198,7 +198,7 @@ impl Instance { Ok(self) } - fn player_ready(&mut self, player_id: Uuid) -> Result, Error> { + pub fn player_ready(&mut self, player_id: Uuid) -> Result, Error> { if ![InstancePhase::InProgress, InstancePhase::Lobby].contains(&self.phase) { return Err(err_msg("instance not in start or vbox phase")); } @@ -300,7 +300,7 @@ impl Instance { self.next_round() } - fn next_round(&mut self) -> &mut Instance { + pub fn next_round(&mut self) -> &mut Instance { if self.finish_condition() { return self.finish(); } @@ -342,7 +342,7 @@ impl Instance { self } - fn finished(&self) -> bool { + pub fn finished(&self) -> bool { self.phase == InstancePhase::Finished } @@ -370,7 +370,7 @@ impl Instance { self } - fn current_game_id(&self) -> Option { + pub fn current_game_id(&self) -> Option { if self.phase != InstancePhase::InProgress { return None; } @@ -386,7 +386,7 @@ impl Instance { return current_round.game_id; } - fn game_finished(&mut self, game: &Game) -> Result<&mut Instance, Error> { + pub fn game_finished(&mut self, game: &Game) -> Result<&mut Instance, Error> { { let current_round = self.rounds .iter_mut() @@ -428,14 +428,14 @@ impl Instance { } // PLAYER ACTIONS - fn account_player(&mut self, account: Uuid) -> Result<&mut Player, Error> { + pub fn account_player(&mut self, account: Uuid) -> Result<&mut Player, Error> { self.players .iter_mut() .find(|p| p.id == account) .ok_or(err_msg("account not in instance")) } - fn account_opponent(&mut self, account: Uuid) -> Result<&mut Player, Error> { + pub fn account_opponent(&mut self, account: Uuid) -> Result<&mut Player, Error> { self.players .iter_mut() .find(|p| p.id != account) @@ -529,7 +529,7 @@ mod tests { let _instance = Instance::new(); let player_account = Uuid::new_v4(); let constructs = instance_mobs(player_account); - let _player = Player::new(player_account, &"test".to_string(), constructs).set_bot(true); + let _player = Player::new(player_account, None, &"test".to_string(), constructs).set_bot(true); } #[test] @@ -540,7 +540,7 @@ mod tests { let player_account = Uuid::new_v4(); let constructs = instance_mobs(player_account); - let player = Player::new(player_account, &"a".to_string(), constructs); + let player = Player::new(player_account, None, &"a".to_string(), constructs); let a_id = player.id; instance.add_player(player).expect("could not add player"); @@ -548,7 +548,7 @@ mod tests { let player_account = Uuid::new_v4(); let constructs = instance_mobs(player_account); - let player = Player::new(player_account, &"b".to_string(), constructs); + let player = Player::new(player_account, None, &"b".to_string(), constructs); let b_id = player.id; instance.add_player(player).expect("could not add player"); @@ -577,7 +577,7 @@ mod tests { let player_account = Uuid::new_v4(); let constructs = instance_mobs(player_account); - let player = Player::new(player_account, &"a".to_string(), constructs); + let player = Player::new(player_account, None, &"a".to_string(), constructs); let a_id = player.id; instance.add_player(player).expect("could not add player"); @@ -585,7 +585,7 @@ mod tests { let player_account = Uuid::new_v4(); let constructs = instance_mobs(player_account); - let player = Player::new(player_account, &"b".to_string(), constructs); + let player = Player::new(player_account, None, &"b".to_string(), constructs); let b_id = player.id; instance.add_player(player).expect("could not add player"); @@ -617,7 +617,7 @@ mod tests { let player_account = Uuid::new_v4(); let constructs = instance_mobs(player_account); - let player = Player::new(player_account, &"a".to_string(), constructs); + let player = Player::new(player_account, None, &"a".to_string(), constructs); let _a_id = player.id; instance.add_player(player).expect("could not add player"); diff --git a/core/src/mob.rs b/core/src/mob.rs index 2cab954b..2dc40a27 100644 --- a/core/src/mob.rs +++ b/core/src/mob.rs @@ -25,6 +25,6 @@ pub fn instance_mobs(player_id: Uuid) -> Vec { pub fn bot_player() -> Player { let bot_id = Uuid::new_v4(); let constructs = instance_mobs(bot_id); - Player::new(bot_id, &name(), constructs).set_bot(true) + Player::new(bot_id, None, &name(), constructs).set_bot(true) } diff --git a/core/src/player.rs b/core/src/player.rs index 0c1bad1d..cfab3bb9 100644 --- a/core/src/player.rs +++ b/core/src/player.rs @@ -66,10 +66,10 @@ pub struct Player { } impl Player { - pub fn new(account: Uuid, name: &String, constructs: Vec) -> Player { + pub fn new(account: Uuid, img: Option, name: &String, constructs: Vec) -> Player { Player { id: account, - img: Some(account), + img, name: name.clone(), vbox: Vbox::new(), constructs, @@ -401,7 +401,7 @@ mod tests { fn player_bot_vbox_test() { let player_account = Uuid::new_v4(); let constructs = instance_mobs(player_account); - let mut player = Player::new(player_account, &"test".to_string(), constructs).set_bot(true); + let mut player = Player::new(player_account, None, &"test".to_string(), constructs).set_bot(true); player.vbox.fill(); player.autobuy(); @@ -413,7 +413,7 @@ mod tests { fn player_score_test() { let player_account = Uuid::new_v4(); let constructs = instance_mobs(player_account); - let mut player = Player::new(player_account, &"test".to_string(), constructs).set_bot(true); + let mut player = Player::new(player_account, None, &"test".to_string(), constructs).set_bot(true); player.score = player.score.add_win(&Score::Zero); player.score = player.score.add_win(&Score::Zero); diff --git a/server/src/account.rs b/server/src/account.rs index e2dca2fc..7373d0f6 100644 --- a/server/src/account.rs +++ b/server/src/account.rs @@ -13,7 +13,7 @@ use failure::{err_msg, format_err}; use mnml_core::construct::{Construct, ConstructSkeleton}; use mnml_core::instance::{Instance}; -use mnml_core::instance; +use mnml_core::player::{Player}; use http::MnmlHttpError; use names::{name as generate_name}; @@ -33,6 +33,19 @@ pub struct Account { pub subscribed: bool, } +impl Account { + pub fn to_player(&self, tx: &mut Transaction) -> Result { + let constructs = team(tx, self)?; + + let img = match self.subscribed { + true => Some(self.img), + false => None, + }; + + Ok(Player::new(self.id, Some(self.img), &self.name, constructs)) + } +} + impl<'a> TryFrom> for Account { type Error = Error; diff --git a/server/src/pg.rs b/server/src/pg.rs index 7a2297ca..7a9f9495 100644 --- a/server/src/pg.rs +++ b/server/src/pg.rs @@ -679,7 +679,7 @@ pub fn instance_practice(tx: &mut Transaction, account: &Account) -> Result Result Instance { let player_account = Uuid::new_v4(); let constructs = instance_mobs(player_account); - let player = Player::new(player_account, &"test".to_string(), constructs).set_bot(true); + let player = Player::new(player_account, None, &"test".to_string(), constructs).set_bot(true); instance.add_player(player).expect("could not add player"); instance.player_ready(player_account).unwrap();