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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
/*
Hurry Curry! - a game about cooking
Copyright (C) 2025 Hurry Curry! Contributors
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 crate::{
server::{AnnounceState, ConnectionData, GameServerExt, Server},
ConnectionID,
};
use anyhow::Result;
use hurrycurry_locale::{tre, trm, TrError};
use hurrycurry_protocol::{Menu, Message, PacketC, PacketS, PlayerID, VERSION};
use log::{debug, info, trace};
impl Server {
pub fn tick_outer(&mut self, dt: f32) -> anyhow::Result<()> {
if !self.paused {
let r = self.tick(dt);
if let Some((name, timer)) = r {
self.scoreboard.save()?;
self.load(self.index.generate_with_book(&name)?, timer);
}
}
match &mut self.announce_state {
AnnounceState::Queued if !self.paused => {
self.announce_state = AnnounceState::Running(3.5);
self.packet_out
.push_back(PacketC::Menu(Menu::AnnounceStart));
self.update_paused();
}
AnnounceState::Running(timer) => {
*timer -= dt;
if *timer <= 0. {
self.announce_state = AnnounceState::Done;
self.update_paused();
}
}
_ => (),
}
while let Some(p) = self.packet_out.pop_front() {
if matches!(p, PacketC::UpdateMap { .. } | PacketC::Movement { .. }) {
trace!("-> {p:?}");
} else {
debug!("-> {p:?}");
}
self.tx.send(p).unwrap();
}
Ok(())
}
pub async fn connect(&mut self, id: ConnectionID) -> Vec<PacketC> {
let mut init = self.game.prime_client();
init.insert(
0,
PacketC::Version {
major: VERSION.0,
minor: VERSION.1,
supports_bincode: true,
},
);
self.connections.insert(id, ConnectionData::default());
self.update_paused();
init
}
pub async fn disconnect(&mut self, conn: ConnectionID) {
if let Some(cd) = self.connections.get(&conn) {
for player in cd.players.clone() {
let _ = self.packet_in_outer(conn, PacketS::Leave { player }).await;
}
}
self.connections.remove(&conn);
self.update_paused();
}
pub async fn packet_in_outer(
&mut self,
conn: ConnectionID,
packet: PacketS,
) -> Result<Vec<PacketC>, TrError> {
if let Some(p) = get_packet_player(&packet) {
if !self
.connections
.entry(conn)
.or_default()
.players
.contains(&p)
{
return Err(tre!("s.error.packet_sender_invalid"));
}
}
let mut replies = Vec::new();
match &packet {
PacketS::Communicate {
message: Some(Message::Text(text)),
timeout: None,
player,
..
} if let Some(command) = text.strip_prefix("/") => {
match self.handle_command_parse(*player, command) {
Ok(packets) => return Ok(packets),
Err(e) => {
return Ok(vec![PacketC::ServerMessage {
message: e.into(),
error: true,
}]);
}
}
}
PacketS::Ready => {
self.connections.entry(conn).or_default().ready = true;
self.update_paused();
}
PacketS::Idle { paused } => {
self.connections.entry(conn).or_default().idle = *paused;
self.update_paused();
}
PacketS::Leave { player } => {
self.connections
.entry(conn)
.or_default()
.players
.remove(player);
}
PacketS::Join { .. } => {
if self.connections.entry(conn).or_default().players.len() > 8 {
return Err(tre!("s.error.conn_too_many_players"));
}
}
_ => (),
}
self.packet_in(packet, &mut replies)?;
for p in &replies {
if let PacketC::Joined { id } = p {
self.connections
.entry(conn)
.or_default()
.players
.insert(*id);
}
}
if self.count_chefs() == 0 && !self.game.lobby {
self.tx
.send(PacketC::ServerMessage {
message: trm!("s.state.abort_no_players"),
error: false,
})
.ok();
self.load(
self.index
.generate_with_book("lobby")
.map_err(|m| tre!("s.error.map_load", s = format!("{m}")))?,
None,
);
}
Ok(replies)
}
pub fn update_paused(&mut self) {
let all_idle = self.connections.values().all(|c| c.idle);
let mut not_ready = self.connections.values().filter(|c| !c.ready).count();
let announcing = matches!(self.announce_state, AnnounceState::Running(_));
if self.game.lobby {
not_ready = 0; // not waiting for players in lobby
}
let should_pause = all_idle || not_ready > 0 || announcing;
let reason = if not_ready > 0 {
info!("Game paused: {not_ready} player are not ready");
Some(trm!(
"s.state.paused.any_not_ready",
s = not_ready.to_string()
))
} else if all_idle {
if self.connections.is_empty() {
info!("Game paused: Server empty");
} else {
info!("Game paused: All players idle");
}
Some(trm!("s.state.paused.all_idle"))
} else if announcing {
info!("Game paused: Waiting for announcement");
None
} else {
if self.paused {
info!("Game unpaused");
}
None
};
self.paused = should_pause;
for p in self.game.players.keys() {
self.packet_out
.push_back(PacketC::Pause { state: self.paused });
self.packet_out.push_back(PacketC::ServerHint {
position: None,
message: reason.clone(),
player: *p,
});
}
}
}
fn get_packet_player(packet: &PacketS) -> Option<PlayerID> {
match packet {
PacketS::Join { .. } => None,
PacketS::Idle { .. } => None,
PacketS::Leave { player } => Some(*player),
PacketS::Movement { player, .. } => Some(*player),
PacketS::Interact { player, .. } => Some(*player),
PacketS::Communicate { player, .. } => Some(*player),
PacketS::ReplaceHand { player, .. } => Some(*player),
PacketS::Effect { player, .. } => Some(*player),
PacketS::Ready => None,
PacketS::ApplyScore(_) => None,
PacketS::ReplayTick { .. } => None,
}
}
|