Updating game loop stuff

wip/yesman
Katharina Fey 4 years ago
parent 581f9e8089
commit 78d813b2a1
Signed by: kookie
GPG Key ID: F972AEEA2887D547
  1. 23
      src/_loop.rs
  2. 25
      src/_match.rs
  3. 1
      src/runner.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) });
}

@ -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() {
}
}
}

Loading…
Cancel
Save