summaryrefslogtreecommitdiff
path: root/server/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/main.rs')
-rw-r--r--server/src/main.rs42
1 files changed, 18 insertions, 24 deletions
diff --git a/server/src/main.rs b/server/src/main.rs
index aeda9c2f..6773bf29 100644
--- a/server/src/main.rs
+++ b/server/src/main.rs
@@ -1,19 +1,19 @@
/*
Undercooked - a game about cooking
Copyright 2024 metamuffin
-
+
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, version 3 of the License only.
-
+
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
-
+
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
-
+
*/
use anyhow::Result;
use futures_util::{SinkExt, StreamExt};
@@ -23,14 +23,13 @@ use tokio::{
net::TcpListener,
spawn,
sync::{broadcast, mpsc::channel, RwLock},
- time::sleep,
+ time::interval,
};
use tokio_tungstenite::tungstenite::Message;
use undercooked::{
customer::customer,
- game::Game,
- load_gamedata,
protocol::{PacketC, PacketS, PlayerID},
+ state::State,
};
#[tokio::main]
@@ -39,29 +38,22 @@ async fn main() -> Result<()> {
let ws_listener = TcpListener::bind("0.0.0.0:27032").await?;
info!("listening for websockets on {}", ws_listener.local_addr()?);
- let data = load_gamedata();
- let game = Arc::new(RwLock::new(Game::new(data.into())));
let (tx, rx) = broadcast::channel::<PacketC>(1024);
+ let state = Arc::new(RwLock::new(State::new(tx)?));
{
- let game = game.clone();
+ let state = state.clone();
spawn(async move {
let dt = 1. / 25.;
+ let mut tick = interval(Duration::from_secs_f32(dt));
loop {
- {
- let mut g = game.write().await;
- g.tick(dt);
- while let Some(p) = g.packet_out() {
- debug!("-> {p:?}");
- let _ = tx.send(p);
- }
- }
- sleep(Duration::from_secs_f32(dt)).await;
+ tick.tick().await;
+ state.write().await.tick(dt).await;
}
});
}
- spawn(customer(game.clone(), rx.resubscribe()));
+ spawn(customer(state.clone(), rx.resubscribe()));
for id in (1..).map(PlayerID) {
let (sock, addr) = ws_listener.accept().await?;
@@ -70,11 +62,12 @@ async fn main() -> Result<()> {
continue;
};
let (mut write, mut read) = sock.split();
- let game = game.clone();
+ let state = state.clone();
let mut rx = rx.resubscribe();
let (error_tx, mut error_rx) = channel::<PacketC>(8);
info!("{addr} connected via ws");
- let init = game.write().await.prime_client(id);
+ let mut init = state.write().await.game.prime_client();
+ init.insert(0, PacketC::Init { id });
spawn(async move {
for p in init {
if let Err(e) = write
@@ -114,7 +107,7 @@ async fn main() -> Result<()> {
break;
};
debug!("<- {id:?} {packet:?}");
- if let Err(e) = game.write().await.packet_in(id, packet) {
+ if let Err(e) = state.write().await.packet_in(id, packet).await {
warn!("client error: {e}");
let _ = error_tx
.send(PacketC::Error {
@@ -127,7 +120,8 @@ async fn main() -> Result<()> {
_ => (),
}
}
- let _ = game.write().await.packet_in(id, PacketS::Leave);
+ info!("{id:?} left");
+ state.write().await.packet_in(id, PacketS::Leave).await.ok();
});
}
Ok(())