diff options
Diffstat (limited to 'server/src/game.rs')
-rw-r--r-- | server/src/game.rs | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/server/src/game.rs b/server/src/game.rs index 96cfa93a..599aadbd 100644 --- a/server/src/game.rs +++ b/server/src/game.rs @@ -28,7 +28,7 @@ use std::{ collections::{HashMap, VecDeque}, ops::Deref, sync::Arc, - time::Instant, + time::{Duration, Instant}, }; #[derive(Debug, PartialEq)] @@ -65,7 +65,8 @@ pub struct Game { players: HashMap<PlayerID, Player>, packet_out: VecDeque<PacketC>, demand: Option<DemandState>, - points: i64, + pub points: i64, + end: Option<Instant>, } impl Game { @@ -76,6 +77,7 @@ impl Game { players: Default::default(), tiles: Default::default(), demand: None, + end: None, points: 0, } } @@ -95,7 +97,7 @@ impl Game { } self.demand = None; } - pub fn load(&mut self, gamedata: Gamedata) { + pub fn load(&mut self, gamedata: Gamedata, timer: Option<Duration>) { let players = self .players .iter() @@ -107,6 +109,7 @@ impl Game { self.data = gamedata.into(); self.points = 0; + self.end = timer.map(|dur| Instant::now() + dur); for (&p, (tile, item)) in &self.data.initial_map { self.tiles.insert( @@ -201,6 +204,7 @@ impl Game { 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.demand.as_ref().map(|d| d.failed).unwrap_or_default(), demands_completed: self @@ -375,6 +379,9 @@ impl Game { } self.packet_out.push_back({ PacketC::Score { + time_remaining: self + .end + .map(|t| (t - Instant::now()).as_secs_f32()), points: self.points, demands_failed: self .demand @@ -423,7 +430,8 @@ impl Game { Ok(()) } - pub fn tick(&mut self, dt: f32) { + /// Returns true if the game should end + pub fn tick(&mut self, dt: f32) -> bool { if let Some(demand) = &mut self.demand { let mut packet_out = Vec::new(); if let Err(err) = demand.tick( @@ -436,12 +444,8 @@ impl Game { warn!("demand tick {err}"); } if demand.score_changed { - self.packet_out.push_back(PacketC::Score { - points: self.points, - demands_failed: demand.failed, - demands_completed: demand.completed, - }); - demand.score_changed = false + demand.score_changed = false; + self.packet_out.push_back(self.score()); } for (player, packet) in packet_out { if let Err(err) = self.packet_in(player, packet) { @@ -473,6 +477,8 @@ impl Game { } } } + + return self.end.map(|t| t < Instant::now()).unwrap_or_default(); } } |