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::{
|
use std::{env, time::Duration};
|
||||||
collections::HashMap,
|
|
||||||
env,
|
|
||||||
time::{self, Duration},
|
|
||||||
};
|
|
||||||
|
|
||||||
use rs48_lib::{game::Game, grid_displayer::GridDisplayer, prelude::*};
|
use rs48_lib::{game::Game, grid_displayer::GridDisplayer, prelude::*};
|
||||||
use termion::{clear, cursor};
|
use termion::{clear, cursor};
|
||||||
|
@ -17,14 +13,23 @@ struct Config {
|
||||||
delay: Duration,
|
delay: Duration,
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn game(mut write: OwnedWriteHalf) -> Option<()> {
|
async fn game(mut write: OwnedWriteHalf, config: Config) -> Option<()> {
|
||||||
'session: loop {
|
'session: loop {
|
||||||
let rules = GameRules::default();
|
let rules = GameRules::default();
|
||||||
let mut game = Game::new(rules);
|
let mut game = Game::new(rules);
|
||||||
let mut controller = SimulatedController::new(50, 20);
|
let mut controller = SimulatedController::new(50, 20);
|
||||||
let displayer = GridDisplayer::new(1234);
|
let displayer = GridDisplayer::new(1234);
|
||||||
'_game: loop {
|
'_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;
|
continue 'session;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -33,19 +38,20 @@ async fn game(mut write: OwnedWriteHalf) -> Option<()> {
|
||||||
|
|
||||||
async fn turn(
|
async fn turn(
|
||||||
game: &mut Game,
|
game: &mut Game,
|
||||||
|
delay: Duration,
|
||||||
displayer: &GridDisplayer,
|
displayer: &GridDisplayer,
|
||||||
write: &mut OwnedWriteHalf,
|
write: &mut OwnedWriteHalf,
|
||||||
controller: &mut SimulatedController,
|
controller: &mut SimulatedController,
|
||||||
) -> Option<()> {
|
) -> Option<()> {
|
||||||
let grid = game.get_board();
|
let display = displayer.display(game.get_board());
|
||||||
let display = displayer.display(&grid);
|
|
||||||
let clear = clear::All;
|
let clear = clear::All;
|
||||||
let goto = cursor::Goto(1, 1);
|
let goto = cursor::Goto(1, 1);
|
||||||
let frame = format!("{clear}{goto}{display}");
|
let frame = format!("{clear}{goto}{display}");
|
||||||
write.write_all(frame.as_bytes()).await.ok()?;
|
write.write_all(frame.as_bytes()).await.ok()?;
|
||||||
|
|
||||||
let movement = controller.next_move(&*game).ok()?;
|
let movement = controller.next_move(&*game).ok()?;
|
||||||
game.turn(movement).ok()?;
|
game.turn(movement).ok()?;
|
||||||
sleep(Duration::from_millis(500)).await;
|
sleep(delay).await;
|
||||||
Some(())
|
Some(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,14 +59,15 @@ async fn turn(
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let url = env::var("URL").unwrap_or_else(|_| "localhost:2048".to_string());
|
let url = env::var("URL").unwrap_or_else(|_| "localhost:2048".to_string());
|
||||||
let delay = env::var("DELAY")
|
let delay = env::var("DELAY")
|
||||||
.map(|s| s.parse::<i32>().unwrap())
|
.map(|s| s.parse::<u64>().unwrap())
|
||||||
.unwrap_or(500);
|
.unwrap_or(500);
|
||||||
let convif = { delay };
|
let delay = Duration::from_millis(delay);
|
||||||
|
let config = Config { delay };
|
||||||
let server = TcpListener::bind(url).await.unwrap();
|
let server = TcpListener::bind(url).await.unwrap();
|
||||||
loop {
|
loop {
|
||||||
let (stream, address) = server.accept().await.unwrap();
|
let (stream, address) = server.accept().await.unwrap();
|
||||||
println!("[server] connection from '{address}'");
|
println!("[server] connection from '{address}'");
|
||||||
let (_, write) = stream.into_split();
|
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