2020-10-01 12:01:03 +00:00
|
|
|
mod components;
|
2020-09-30 14:45:22 +00:00
|
|
|
mod config;
|
2020-10-03 13:41:11 +00:00
|
|
|
mod game;
|
2020-10-05 13:11:41 +00:00
|
|
|
mod messages;
|
2020-10-02 09:37:08 +00:00
|
|
|
mod network;
|
2020-10-01 12:01:03 +00:00
|
|
|
mod systems;
|
2020-09-30 14:45:22 +00:00
|
|
|
|
|
|
|
use crate::config::Settings;
|
2020-10-03 13:41:11 +00:00
|
|
|
use crate::game::Game;
|
2020-10-04 13:45:24 +00:00
|
|
|
use crate::network::listen;
|
2020-10-04 02:14:23 +00:00
|
|
|
use async_std::sync::Arc;
|
2020-10-05 00:24:23 +00:00
|
|
|
use async_std::sync::RwLock;
|
2020-10-04 02:14:23 +00:00
|
|
|
use async_std::{sync::channel, task};
|
2020-10-01 13:14:35 +00:00
|
|
|
use env_logger::Env;
|
2020-10-04 15:26:42 +00:00
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
// Minimum delay between updates, in milliseconds
|
|
|
|
const MIN_UPDATE_MS: u64 = 10;
|
2020-09-30 14:45:22 +00:00
|
|
|
|
2020-10-04 02:14:23 +00:00
|
|
|
async fn run() {
|
2020-10-01 14:47:55 +00:00
|
|
|
let env = Env::default().filter_or("LOG_LEVEL", "info");
|
2020-10-01 13:14:35 +00:00
|
|
|
env_logger::init_from_env(env);
|
|
|
|
|
2020-09-30 15:32:55 +00:00
|
|
|
let settings = Settings::new().unwrap();
|
|
|
|
|
2020-10-04 02:14:23 +00:00
|
|
|
let (in_s, in_r) = channel(10);
|
|
|
|
let (out_s, out_r) = channel(10);
|
2020-10-01 13:03:55 +00:00
|
|
|
|
2020-10-05 00:24:23 +00:00
|
|
|
let game = Arc::new(RwLock::new(Game::new(out_s)));
|
2020-10-04 15:26:42 +00:00
|
|
|
|
|
|
|
let game_read_loop = game.clone();
|
2020-10-05 00:24:23 +00:00
|
|
|
task::spawn(Game::read_loop(game_read_loop, in_r));
|
2020-10-04 15:26:42 +00:00
|
|
|
|
2020-10-04 02:14:23 +00:00
|
|
|
task::spawn(async move {
|
2020-10-04 15:26:42 +00:00
|
|
|
loop {
|
|
|
|
let game = game.clone();
|
2020-10-05 00:24:23 +00:00
|
|
|
task::spawn(async move { game.read().await.update().expect("update failed") });
|
2020-10-04 15:26:42 +00:00
|
|
|
task::sleep(Duration::from_millis(MIN_UPDATE_MS)).await;
|
|
|
|
}
|
2020-10-04 02:14:23 +00:00
|
|
|
});
|
2020-10-01 14:33:59 +00:00
|
|
|
|
2020-10-04 02:14:23 +00:00
|
|
|
task::block_on(listen(settings.bind, in_s, out_r));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
task::block_on(run());
|
2020-10-01 13:03:55 +00:00
|
|
|
}
|