diff options
Diffstat (limited to 'server/src/game.rs')
-rw-r--r-- | server/src/game.rs | 69 |
1 files changed, 33 insertions, 36 deletions
diff --git a/server/src/game.rs b/server/src/game.rs index 9a01e4a3..1f368b00 100644 --- a/server/src/game.rs +++ b/server/src/game.rs @@ -25,8 +25,8 @@ use anyhow::{anyhow, bail, Result}; use hurrycurry_protocol::{ glam::{IVec2, Vec2}, movement::MovementBase, - ClientGamedata, ItemIndex, ItemLocation, Message, PacketC, PacketS, PlayerID, RecipeIndex, - TileIndex, + ClientGamedata, ItemIndex, ItemLocation, Menu, Message, PacketC, PacketS, PlayerID, + RecipeIndex, Score, TileIndex, }; use log::{info, warn}; use std::{ @@ -78,9 +78,7 @@ pub struct Game { pub lobby: bool, pub score_changed: bool, - pub points: i64, - pub demands_failed: usize, - pub demands_completed: usize, + pub score: Score, } impl Default for Game { @@ -101,9 +99,7 @@ impl Game { end: None, entities: Arc::new(RwLock::new(vec![])), players_spatial_index: SpatialIndex::default(), - points: 0, - demands_failed: 0, - demands_completed: 0, + score: Score::default(), score_changed: false, } } @@ -137,7 +133,7 @@ impl Game { self.lobby = gamedata.map_name == "lobby"; self.data = gamedata.into(); - self.points = 0; + self.score = Score::default(); self.end = timer.map(|dur| Instant::now() + dur); self.entities = Arc::new(RwLock::new(self.data.entities.clone())); @@ -187,10 +183,6 @@ impl Game { self.packet_out.extend(self.prime_client()); } - pub fn packet_out(&mut self) -> Option<PacketC> { - self.packet_out.pop_front() - } - pub fn prime_client(&self) -> Vec<PacketC> { let mut out = Vec::new(); out.push(PacketC::Data { @@ -256,7 +248,7 @@ impl Game { }) } } - out.push(self.score()); + out.push(PacketC::Score(self.score.clone())); out.push(PacketC::SetIngame { state: true, lobby: self.lobby, @@ -264,22 +256,12 @@ impl Game { out } - pub fn score(&self) -> PacketC { - PacketC::Score { - time_remaining: self.end.map(|t| (t - Instant::now()).as_secs_f32()), - points: self.points, - demands_failed: self.demands_failed, - demands_completed: self.demands_completed, - } - } pub fn packet_in( &mut self, player: PlayerID, packet: PacketS, replies: &mut Vec<PacketC>, ) -> Result<()> { - let points_before = self.points; - match packet { PacketS::Join { name, character } => { if self.players.contains_key(&player) { @@ -429,7 +411,7 @@ impl Game { ItemLocation::Player(pid), None, &mut self.packet_out, - &mut self.points, + &mut self.score, false, ) } else { @@ -447,7 +429,7 @@ impl Game { ItemLocation::Player(pid), Some(tile.kind), &mut self.packet_out, - &mut self.points, + &mut self.score, false, ) } @@ -481,10 +463,6 @@ impl Game { } PacketS::ReplayTick { .. } => bail!("packet not supported in this session"), } - - if self.points != points_before { - self.packet_out.push_back(self.score()) - } Ok(()) } @@ -492,11 +470,18 @@ impl Game { pub fn tick(&mut self, dt: f32) -> bool { if self.score_changed { self.score_changed = false; - self.packet_out.push_back(self.score()); + self.packet_out + .push_back(PacketC::Score(self.score.clone())); } for (&pos, tile) in &mut self.tiles { - if let Some(effect) = tick_slot(dt, &self.data, Some(tile.kind), &mut tile.item) { + if let Some(effect) = tick_slot( + dt, + &self.data, + Some(tile.kind), + &mut tile.item, + &mut self.score, + ) { match effect { TickEffect::Progress(warn) => self.packet_out.push_back(PacketC::SetProgress { warn, @@ -544,7 +529,8 @@ impl Game { rot: player.movement.rotation, }); - if let Some(effect) = tick_slot(dt, &self.data, None, &mut player.item) { + if let Some(effect) = tick_slot(dt, &self.data, None, &mut player.item, &mut self.score) + { match effect { TickEffect::Progress(warn) => self.packet_out.push_back(PacketC::SetProgress { warn, @@ -591,7 +577,18 @@ impl Game { } } - self.end.map(|t| t < Instant::now()).unwrap_or_default() + if let Some(end) = self.end { + self.score.time_remaining = (end - Instant::now()).as_secs_f64(); + if end < Instant::now() { + self.packet_out + .push_back(PacketC::Menu(Menu::Score(self.score.clone()))); + true + } else { + false + } + } else { + false + } } pub fn count_chefs(&self) -> usize { @@ -617,13 +614,13 @@ pub fn interact_effect( other_loc: ItemLocation, this_tile_kind: Option<TileIndex>, packet_out: &mut VecDeque<PacketC>, - points: &mut i64, + score: &mut Score, automated: bool, ) { let this_had_item = this.is_some(); let other_had_item = other.is_some(); - if let Some(effect) = interact(data, edge, this_tile_kind, this, other, points, automated) { + if let Some(effect) = interact(data, edge, this_tile_kind, this, other, score, automated) { match effect { InteractEffect::Put => { info!("put {this_loc} <- {other_loc}"); |