diff --git a/src/_loop.rs b/src/_loop.rs index 1fc1bf8fcc2..7ba7ad7cdd2 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) +/// Run a timed loop until you no longer want to +pub fn block_loop(run: Arc, f: F) where F: Future + 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(run: Arc, f: F) +where + F: Future + Send + Copy + 'static, +{ + task::spawn(async move { block_loop(run, f) }); +} diff --git a/src/_match.rs b/src/_match.rs index 23d3be8aeb0..53f4d2604fe 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, /// The active game map pub map: Map, + /// Input inbox, + inbox: RwLock>, /// The time the match was initialised - pub init_t: DateTime, + init_t: DateTime, /// The synced time the match was started - pub start_t: Option>, + start_t: Option>, } impl From for Match { @@ -38,6 +41,7 @@ impl From 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) { 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 00000000000..8b137891791 --- /dev/null +++ b/src/runner.rs @@ -0,0 +1 @@ +