added dekay config
This commit is contained in:
parent
8d8deecfe7
commit
096e44d588
1 changed files with 20 additions and 13 deletions
33
src/main.rs
33
src/main.rs
|
@ -1,8 +1,4 @@
|
|||
use std::{
|
||||
collections::HashMap,
|
||||
env,
|
||||
time::{self, Duration},
|
||||
};
|
||||
use std::{env, time::Duration};
|
||||
|
||||
use rs48_lib::{game::Game, grid_displayer::GridDisplayer, prelude::*};
|
||||
use termion::{clear, cursor};
|
||||
|
@ -17,14 +13,23 @@ struct Config {
|
|||
delay: Duration,
|
||||
}
|
||||
|
||||
async fn game(mut write: OwnedWriteHalf) -> Option<()> {
|
||||
async fn game(mut write: OwnedWriteHalf, config: Config) -> Option<()> {
|
||||
'session: loop {
|
||||
let rules = GameRules::default();
|
||||
let mut game = Game::new(rules);
|
||||
let mut controller = SimulatedController::new(50, 20);
|
||||
let displayer = GridDisplayer::new(1234);
|
||||
'_game: loop {
|
||||
if let None = turn(&mut game, &displayer, &mut write, &mut controller).await {
|
||||
if turn(
|
||||
&mut game,
|
||||
config.delay,
|
||||
&displayer,
|
||||
&mut write,
|
||||
&mut controller,
|
||||
)
|
||||
.await
|
||||
.is_none()
|
||||
{
|
||||
continue 'session;
|
||||
}
|
||||
}
|
||||
|
@ -33,19 +38,20 @@ async fn game(mut write: OwnedWriteHalf) -> Option<()> {
|
|||
|
||||
async fn turn(
|
||||
game: &mut Game,
|
||||
delay: Duration,
|
||||
displayer: &GridDisplayer,
|
||||
write: &mut OwnedWriteHalf,
|
||||
controller: &mut SimulatedController,
|
||||
) -> Option<()> {
|
||||
let grid = game.get_board();
|
||||
let display = displayer.display(&grid);
|
||||
let display = displayer.display(game.get_board());
|
||||
let clear = clear::All;
|
||||
let goto = cursor::Goto(1, 1);
|
||||
let frame = format!("{clear}{goto}{display}");
|
||||
write.write_all(frame.as_bytes()).await.ok()?;
|
||||
|
||||
let movement = controller.next_move(&*game).ok()?;
|
||||
game.turn(movement).ok()?;
|
||||
sleep(Duration::from_millis(500)).await;
|
||||
sleep(delay).await;
|
||||
Some(())
|
||||
}
|
||||
|
||||
|
@ -53,14 +59,15 @@ async fn turn(
|
|||
async fn main() {
|
||||
let url = env::var("URL").unwrap_or_else(|_| "localhost:2048".to_string());
|
||||
let delay = env::var("DELAY")
|
||||
.map(|s| s.parse::<i32>().unwrap())
|
||||
.map(|s| s.parse::<u64>().unwrap())
|
||||
.unwrap_or(500);
|
||||
let convif = { delay };
|
||||
let delay = Duration::from_millis(delay);
|
||||
let config = Config { delay };
|
||||
let server = TcpListener::bind(url).await.unwrap();
|
||||
loop {
|
||||
let (stream, address) = server.accept().await.unwrap();
|
||||
println!("[server] connection from '{address}'");
|
||||
let (_, write) = stream.into_split();
|
||||
tokio::spawn(game(write));
|
||||
tokio::spawn(game(write, config.clone()));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue