Skip to content

Commit

Permalink
respect game mode when getting games
Browse files Browse the repository at this point in the history
  • Loading branch information
Timtam committed Apr 15, 2024
1 parent 5c84d97 commit ddef6d8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
21 changes: 11 additions & 10 deletions server/src/routes/games.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ pub fn create_game(
) -> Created<Json<Game>> {
let game_svc = serv.game_service();
let games = game_svc.lock();
let mode = if data.is_some() {
data.as_ref().unwrap().game_mode.unwrap_or(GameMode::Public)
let mode = if let Some(data) = data.as_ref() {
data.game_mode.unwrap_or(GameMode::Public)
} else {
GameMode::Public
};
Expand All @@ -66,9 +66,9 @@ pub fn create_game(
#[openapi(tag = "Games")]
#[get("/games")]
pub fn get_all_games(serv: &State<ServiceStore>) -> Json<GamesResponse> {
pub fn get_all_games(user: Option<User>, serv: &State<ServiceStore>) -> Json<GamesResponse> {
Json(GamesResponse {
games: serv.game_service().lock().get_all(),
games: serv.game_service().lock().get_all(user.as_ref()),
})
}

Expand All @@ -87,7 +87,7 @@ pub async fn join_game(
let _ = queue.send(GameEvent {
game_id: game_id.clone(),
event: "join".into(),
players: Some(games.get(&game_id).unwrap().players),
players: Some(games.get(&game_id, Some(&user)).unwrap().players),
..Default::default()
});
Json(MessageResponse {
Expand All @@ -109,20 +109,20 @@ pub async fn leave_game(
let games = game_svc.lock();

let state = games
.get(&game_id)
.get(&game_id, Some(&user))
.map(|g| g.state)
.unwrap_or(GameState::Open);

games.leave(&game_id, &user).map(|_| {
let _ = queue.send(GameEvent {
game_id: game_id.clone(),
event: "leave".into(),
players: games.get(&game_id).map(|g| g.players),
players: games.get(&game_id, Some(&user)).map(|g| g.players),
..Default::default()
});

let new_state = games
.get(&game_id)
.get(&game_id, Some(&user))
.map(|g| g.state)
.unwrap_or(GameState::Open);

Expand Down Expand Up @@ -208,12 +208,13 @@ pub async fn stop_game(
#[get("/games/<game_id>")]
pub fn get_game(
game_id: String,
user: Option<User>,
serv: &State<ServiceStore>,
) -> Result<Json<Game>, NotFound<Json<MessageResponse>>> {
let game_svc = serv.game_service();
let games = game_svc.lock();

match games.get(&game_id) {
match games.get(&game_id, user.as_ref()) {
Some(g) => Ok(Json(g)),
None => Err(NotFound(Json(MessageResponse {
message: "game id not found".into(),
Expand Down Expand Up @@ -279,7 +280,7 @@ pub fn guess_slot(
let state = serv
.game_service()
.lock()
.get(&game_id)
.get(&game_id, Some(&user))
.map(|g| g.state)
.unwrap_or(GameState::Guessing);
serv.game_service()
Expand Down
2 changes: 1 addition & 1 deletion server/src/routes/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ pub async fn logout(
let game_srv = serv.game_service();
let games = game_srv.lock();

for game in games.get_all().iter() {
for game in games.get_all(Some(&user)).iter() {
let _ = games.leave(&game.id, &user);
}

Expand Down
28 changes: 25 additions & 3 deletions server/src/services/games.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,40 @@ impl GameService {
game
}

pub fn get_all(&self) -> Vec<Game> {
pub fn get_all(&self, user: Option<&User>) -> Vec<Game> {
self.data
.lock()
.unwrap()
.games
.clone()
.into_values()
.filter(|g| {
g.mode == GameMode::Public
|| (user.is_some()
&& (g.mode == GameMode::Private
&& g.players.iter().any(|p| p.id == user.as_ref().unwrap().id))
|| (g.mode == GameMode::Local
&& g.players
.iter()
.any(|p| p.id == user.as_ref().unwrap().id && p.creator)))
})
.collect::<_>()
}

pub fn get(&self, id: &str) -> Option<Game> {
self.data.lock().unwrap().games.get(id).cloned()
pub fn get(&self, id: &str, user: Option<&User>) -> Option<Game> {
self.data
.lock()
.unwrap()
.games
.get(id)
.cloned()
.filter(|g| {
g.mode != GameMode::Local
|| (user.is_some()
&& g.players
.iter()
.any(|p| p.id == user.as_ref().unwrap().id && p.creator))
})
}

pub fn join(&self, game_id: &str, user: &User) -> Result<(), JoinGameError> {
Expand Down

0 comments on commit ddef6d8

Please sign in to comment.