This commit is contained in:
Hamcha 2020-10-04 17:26:42 +02:00
parent 36b5063637
commit f7255b3617
Signed by: hamcha
GPG key ID: 41467804B19A3315
2 changed files with 20 additions and 6 deletions

View file

@ -1,11 +1,8 @@
use crate::network::NetworkMessage;
use async_std::sync::{Receiver, Sender};
use shipyard::World;
use shipyard::{error, World};
use std::collections::HashMap;
// Minimum delay between updates, in milliseconds
const MIN_UPDATE_MS: u64 = 10;
pub struct Player {}
pub struct Game {
@ -29,10 +26,13 @@ impl Game {
}
}
pub fn update(&self) -> Result<(), error::RunWorkload> {
self.world.try_run_workload("update")
}
pub async fn read_loop(&self, net_in: Receiver<NetworkMessage>) {
loop {
let message = net_in.recv().await.expect("could not read from channel");
log::info!("Message received: {:?}", &message);
self.net_out
.send(NetworkMessage {
conn_id: message.conn_id,

View file

@ -10,6 +10,10 @@ use crate::network::listen;
use async_std::sync::Arc;
use async_std::{sync::channel, task};
use env_logger::Env;
use std::time::Duration;
// Minimum delay between updates, in milliseconds
const MIN_UPDATE_MS: u64 = 10;
async fn run() {
let env = Env::default().filter_or("LOG_LEVEL", "info");
@ -21,8 +25,18 @@ async fn run() {
let (out_s, out_r) = channel(10);
let game = Arc::new(Game::new(out_s));
let game_read_loop = game.clone();
task::spawn(async move {
game.clone().read_loop(in_r).await;
game_read_loop.read_loop(in_r).await;
});
task::spawn(async move {
loop {
let game = game.clone();
task::spawn(async move { game.update().expect("update failed") });
task::sleep(Duration::from_millis(MIN_UPDATE_MS)).await;
}
});
task::block_on(listen(settings.bind, in_s, out_r));