aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKatharina Fey <kookie@spacekookie.de>2020-03-24 22:51:38 +0100
committerKatharina Fey <kookie@spacekookie.de>2020-03-24 22:51:38 +0100
commit78d813b2a119b314349555387669f9c33727c5b1 (patch)
treee3b0278d64ab84aa67423a6036edd091b6593ee6
parent581f9e80898e3df63868538e05648f05a4c03972 (diff)
Updating game loop stuff
-rw-r--r--src/_loop.rs23
-rw-r--r--src/_match.rs25
-rw-r--r--src/runner.rs1
3 files changed, 40 insertions, 9 deletions
diff --git a/src/_loop.rs b/src/_loop.rs
index 1fc1bf8fcc28..7ba7ad7cdd27 100644
--- a/src/_loop.rs
+++ b/src/_loop.rs
@@ -2,21 +2,36 @@
use async_std::{future::Future, task};
use chrono::{DateTime, Utc};
-use std::{thread, time::Duration};
+use std::{
+ sync::{
+ atomic::{AtomicBool, Ordering},
+ Arc,
+ },
+ time::Duration,
+};
/// Number of ticks per second
const TICKS: u64 = 100;
const TICK_TIME: Duration = Duration::from_millis(1000 / TICKS);
-pub fn block_loop<F>(f: F)
+/// Run a timed loop until you no longer want to
+pub fn block_loop<F>(run: Arc<AtomicBool>, f: F)
where
F: Future<Output = ()> + Send + Copy + 'static,
{
- loop {
+ while run.load(Ordering::Relaxed) {
let t1 = Utc::now();
task::block_on(f);
let t2 = Utc::now();
let t3 = (t2 - t1).to_std().unwrap();
- thread::sleep(TICK_TIME - t3);
+ task::block_on(async { task::sleep(TICK_TIME - t3) });
}
}
+
+/// Run a detached timed loop until you no longer want to
+pub fn spawn_loop<F>(run: Arc<AtomicBool>, f: F)
+where
+ F: Future<Output = ()> + Send + Copy + 'static,
+{
+ task::spawn(async move { block_loop(run, f) });
+}
diff --git a/src/_match.rs b/src/_match.rs
index 23d3be8aeb0d..53f4d2604fe4 100644
--- a/src/_match.rs
+++ b/src/_match.rs
@@ -2,11 +2,12 @@ use crate::{
data::Player,
lobby::MetaLobby,
map::Map,
- wire::{LobbyUser, MatchId, UserId},
+ wire::{Action, LobbyUser, MatchId, UserId},
};
-use async_std::sync::Arc;
+use async_std::sync::{Arc, RwLock};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
+use std::collections::VecDeque;
/// Describes a match for the server
pub struct Match {
@@ -16,10 +17,12 @@ pub struct Match {
pub players: Vec<Player>,
/// The active game map
pub map: Map,
+ /// Input inbox,
+ inbox: RwLock<VecDeque<Action>>,
/// The time the match was initialised
- pub init_t: DateTime<Utc>,
+ init_t: DateTime<Utc>,
/// The synced time the match was started
- pub start_t: Option<DateTime<Utc>>,
+ start_t: Option<DateTime<Utc>>,
}
impl From<MetaLobby> for Match {
@@ -38,6 +41,7 @@ impl From<MetaLobby> for Match {
})
.collect(),
map: Map::new(),
+ inbox: Default::default(),
init_t: Utc::now(),
start_t: None,
}
@@ -48,5 +52,16 @@ impl Match {
/// Set the start time of the match, which may be in the future
pub fn set_start(&mut self, t: DateTime<Utc>) {
self.start_t = Some(t);
- }
+ }
+
+ /// Queue a new game action
+ pub async fn queue(&self, cmd: Action) {
+ self.inbox.write().await.push_back(cmd);
+ }
+
+ pub async fn handle_inbex(&mut self) {
+ for act in self.inbox.write().await.drain() {
+
+ }
+ }
}
diff --git a/src/runner.rs b/src/runner.rs
new file mode 100644
index 000000000000..8b137891791f
--- /dev/null
+++ b/src/runner.rs
@@ -0,0 +1 @@
+