1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
/*
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};
use log::{debug, info, warn};
use std::{sync::Arc, time::Duration};
use tokio::{
net::TcpListener,
spawn,
sync::{broadcast, mpsc::channel, RwLock},
time::sleep,
};
use tokio_tungstenite::tungstenite::Message;
use undercooked::{
customer::customer,
game::Game,
load_gamedata,
protocol::{PacketC, PacketS, PlayerID},
};
#[tokio::main]
async fn main() -> Result<()> {
env_logger::init_from_env("LOG");
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 game = game.clone();
spawn(async move {
let dt = 1. / 25.;
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;
}
});
}
spawn(customer(game.clone(), rx.resubscribe()));
for id in (1..).map(PlayerID) {
let (sock, addr) = ws_listener.accept().await?;
let Ok(sock) = tokio_tungstenite::accept_async(sock).await else {
warn!("invalid ws handshake");
continue;
};
let (mut write, mut read) = sock.split();
let game = game.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);
spawn(async move {
for p in init {
if let Err(e) = write
.send(tokio_tungstenite::tungstenite::Message::Text(
serde_json::to_string(&p).unwrap(),
))
.await
{
warn!("ws error on init: {e}");
return;
}
}
loop {
let Some(packet) = tokio::select!(
p = rx.recv() => p.ok(),
p = error_rx.recv() => p,
) else {
break;
};
if let Err(e) = write
.send(tokio_tungstenite::tungstenite::Message::Text(
serde_json::to_string(&packet).unwrap(),
))
.await
{
warn!("ws error: {e}");
break;
}
}
});
spawn(async move {
while let Some(Ok(message)) = read.next().await {
match message {
Message::Text(line) => {
let Ok(packet): Result<PacketS, _> = serde_json::from_str(&line) else {
warn!("invalid json over ws");
break;
};
debug!("<- {id:?} {packet:?}");
if let Err(e) = game.write().await.packet_in(id, packet) {
warn!("client error: {e}");
let _ = error_tx
.send(PacketC::Error {
message: format!("{e}"),
})
.await;
}
}
Message::Close(_) => break,
_ => (),
}
}
let _ = game.write().await.packet_in(id, PacketS::Leave);
});
}
Ok(())
}
|