-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathactions.rs
373 lines (327 loc) · 11 KB
/
actions.rs
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
use std::borrow::Borrow;
use crate::{
dto::{CreateGameRequest, GameMessage, GameOverview, UpdateGameRequest},
models::{AppState, Game, GameEntry, GameLog, GameState, Player, PlayerRole, Question},
util::get_time,
};
use dashmap::mapref::one::RefMut;
use rand::seq::SliceRandom;
use serde::{Deserialize, Serialize};
use tokio::{sync::broadcast, time::Instant};
#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum GameError {
InsufficientPermissions,
InvalidGameState,
FailedToCreateGame,
GameNotFound,
UsernameTooLong,
QuestionNotFound,
PlayerNotFound,
MissingQuestions,
NewPlayerCannotJoinAfterStart,
AlreadyAnswered,
}
impl AppState {
pub fn create_game(self, request: CreateGameRequest) -> Result<Game, GameError> {
if request.rounds.is_empty()
|| request.rounds.iter().any(|x| x.is_empty())
|| request
.rounds
.iter()
.flat_map(|x| x)
.any(|x| x.questions.is_empty())
|| request
.rounds
.iter()
.flat_map(|x| x)
.flat_map(|x| x.questions.iter())
.any(|x| x.correct_answer.is_empty() || x.detail.is_empty())
{
return Err(GameError::MissingQuestions);
}
// Chars A-Z in ASCII
let id_chars: Vec<char> = (65..90u32).map(|x| char::from_u32(x).unwrap()).collect();
let mut rng = rand::thread_rng();
let id: String = (0..4)
.map(|_x| id_chars.choose(&mut rng).unwrap_or(&'A'))
.collect::<String>();
let mut game: Game = request.into();
game.id = id.clone();
let (sender, receiver) = broadcast::channel(16);
let game_entry = GameEntry {
game: game.clone(),
sender,
receiver,
last_updated: Instant::now(),
};
match self.games.insert(id, game_entry) {
Some(_) => return Err(GameError::FailedToCreateGame), // Game already exists, might be an ID clash. So fail.
None => return Ok(game),
}
}
pub fn join_game(
self,
game_id: String,
username: String,
role: PlayerRole,
) -> Result<(), GameError> {
if game_id.len() > 4 {
return Err(GameError::GameNotFound);
}
if username.len() > 20 {
return Err(GameError::UsernameTooLong);
}
let mut entry = match self.games.get_mut(&game_id.to_ascii_uppercase()) {
Some(x) => x,
None => return Err(GameError::GameNotFound),
};
let existing = get_player(&mut entry.game, username.clone());
if let Some(_player) = existing {
// If the player already exists, then don't add them again.
return Ok(());
}
if role != PlayerRole::Spectator && entry.game.state != GameState::WaitingToStart {
return Err(GameError::NewPlayerCannotJoinAfterStart);
}
let game_entry = entry.value_mut();
let new_player = Player {
username,
role,
score: 0,
};
game_entry.game.players.push(new_player);
game_entry.last_updated = Instant::now();
let _ = game_entry.sender.send(GameMessage::GameUpdate {
game: game_entry.game.borrow().into(),
});
return Ok(());
}
}
pub fn handle_game_request(
game_entry: &mut RefMut<String, GameEntry>,
username: String,
role: PlayerRole,
request: UpdateGameRequest,
) -> () {
game_entry.last_updated = Instant::now();
let result = match request {
UpdateGameRequest::StartGame => start_game(game_entry, role),
UpdateGameRequest::LeaveGame => leave_game(game_entry, username.clone()),
UpdateGameRequest::PickQuestion { question_id } => pick_question(game_entry, question_id),
UpdateGameRequest::AllowAnswering => allow_answering(game_entry, role),
UpdateGameRequest::AnswerQuestion => answer_question(game_entry, username.clone()),
UpdateGameRequest::ConfirmAnswer { is_correct } => confirm_answer(game_entry, is_correct),
UpdateGameRequest::EndQuestion => end_question(game_entry, role),
};
match result {
Ok(_) => {
let update: GameOverview = game_entry.game.borrow().into();
let _ = game_entry
.sender
.send(GameMessage::GameUpdate { game: update });
}
Err(e) => {
tracing::error!("Failed to process the game request: {e:?}");
let _ = game_entry.sender.send(GameMessage::ReportError {
message: e.get_message(),
error: e,
username,
});
}
}
}
fn start_game(
game_entry: &mut RefMut<String, GameEntry>,
role: PlayerRole,
) -> Result<(), GameError> {
if role != PlayerRole::Host {
return Err(GameError::InsufficientPermissions);
}
if game_entry.game.state != GameState::WaitingToStart {
return Err(GameError::InvalidGameState);
}
game_entry.game.state = GameState::PickAQuestion;
game_entry
.game
.log
.push(GameLog::GameStarted { time: get_time() });
return Ok(());
}
fn leave_game(
game_entry: &mut RefMut<String, GameEntry>,
username: String,
) -> Result<(), GameError> {
// Send a message to the websocket to tell it to cleanly close the session for the specified user
let _ = game_entry.sender.send(GameMessage::EndSession { username });
return Ok(());
}
fn pick_question(
game_entry: &mut RefMut<String, GameEntry>,
question_id: String,
) -> Result<(), GameError> {
if game_entry.game.state != GameState::PickAQuestion {
return Err(GameError::InvalidGameState);
}
let game = &mut game_entry.value_mut().game;
let question = game.rounds[game.current_round]
.iter()
.flat_map(|x| x.questions.iter())
.find(|x| x.question_id == question_id && x.answered == false);
if let Some(question) = question {
game.state = GameState::ReadQuestion {
question: question.clone(),
};
game.log.push(GameLog::QuestionPicked {
time: get_time(),
question_id: question.clone().question_id,
});
} else {
return Err(GameError::QuestionNotFound);
}
return Ok(());
}
fn allow_answering(
game_entry: &mut RefMut<String, GameEntry>,
role: PlayerRole,
) -> Result<(), GameError> {
if role != PlayerRole::Host {
return Err(GameError::InsufficientPermissions);
}
if let GameState::ReadQuestion { question } = &game_entry.game.state {
game_entry.game.state = GameState::WaitingForAnswer {
question: question.clone(),
};
} else {
return Err(GameError::InvalidGameState);
}
return Ok(());
}
fn answer_question(
game_entry: &mut RefMut<String, GameEntry>,
username: String,
) -> Result<(), GameError> {
let game = &mut game_entry.value_mut().game;
match game.state.clone() {
GameState::WaitingForAnswer { question } => {
let player = match game.players.iter().find(|x| x.username == username) {
Some(x) => x,
None => return Err(GameError::PlayerNotFound),
};
game.state = GameState::CheckAnswer {
question: question.clone(),
player: player.clone(),
};
game.log.push(GameLog::PlayerBuzzedIn {
time: get_time(),
username: username.clone(),
});
}
GameState::CheckAnswer {
question: _,
player: _,
} => {
return Err(GameError::AlreadyAnswered);
}
_ => return Err(GameError::InvalidGameState),
}
return Ok(());
}
fn confirm_answer(
game_entry: &mut RefMut<String, GameEntry>,
is_correct: bool,
) -> Result<(), GameError> {
let state = game_entry.game.state.clone();
if let GameState::CheckAnswer { question, player } = state {
let player_to_update = match get_player(&mut game_entry.game, player.username) {
Some(x) => x,
None => return Err(GameError::PlayerNotFound),
};
let username = player_to_update.username.clone();
if is_correct {
player_to_update.score += question.value;
game_entry.game.last_winner = username.clone();
mark_question_answered(game_entry, question.question_id)?;
} else {
player_to_update.score -= question.value;
game_entry.game.state = GameState::WaitingForAnswer {
question: question.clone(),
};
}
game_entry.game.log.push(GameLog::AnswerConfirmed {
time: get_time(),
username,
is_correct,
points_change: question.value,
});
} else {
return Err(GameError::InvalidGameState);
}
return Ok(());
}
fn end_question(
game_entry: &mut RefMut<String, GameEntry>,
role: PlayerRole,
) -> Result<(), GameError> {
if role != PlayerRole::Host {
return Err(GameError::InsufficientPermissions);
}
if let GameState::WaitingForAnswer { question } = game_entry.game.state.clone() {
if let Err(e) = mark_question_answered(game_entry, question.question_id) {
return Err(e);
}
game_entry
.game
.log
.push(GameLog::QuestionPassed { time: get_time() });
} else {
return Err(GameError::InvalidGameState);
}
return Ok(());
}
fn get_player(game: &mut Game, username: String) -> Option<&mut Player> {
return game.players.iter_mut().find(|x| x.username == username);
}
fn get_question(game: &mut Game, question_id: String) -> Option<&mut Question> {
return game
.rounds
.iter_mut()
.flat_map(|x| x.iter_mut())
.flat_map(|x| x.questions.iter_mut())
.find(|x| x.question_id == question_id);
}
fn mark_question_answered(
game_entry: &mut GameEntry,
question_id: String,
) -> Result<(), GameError> {
let question = match get_question(&mut game_entry.game, question_id) {
Some(x) => x,
None => return Err(GameError::QuestionNotFound),
};
question.answered = true;
// Inform all clients that this question has been answered
let _ = game_entry.sender.send(GameMessage::QuestionUpdate {
question: question.clone(),
});
let game = &mut game_entry.game;
game.state = GameState::PickAQuestion;
// If all questions are answered, then end the game
if game
.rounds
.iter()
.flat_map(|x| x)
.flat_map(|x| x.questions.iter())
.all(|x| x.answered)
{
game.state = GameState::Finished;
return Ok(());
}
// If all questions inside this round are answered, then go to the next round
if game.rounds[game.current_round]
.iter()
.flat_map(|x| x.questions.iter())
.all(|x| x.answered)
{
game.current_round += 1;
}
return Ok(());
}