Skip to content

Commit

Permalink
replace creator attribute on game with creator bool on player
Browse files Browse the repository at this point in the history
  • Loading branch information
Timtam committed Apr 1, 2024
1 parent 34b858f commit 4b1d969
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 24 deletions.
8 changes: 4 additions & 4 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"bootstrap": "^5.3.3",
"react": "^18.2.0",
"react-bootstrap": "^2.10.1",
"react-cookie": "^7.1.0",
"react-cookie": "7.1.1",
"react-dom": "^18.2.0",
"react-h5-audio-player": "^3.9.1",
"react-helmet-async": "^2.0.4",
Expand Down
2 changes: 1 addition & 1 deletion client/src/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ export enum GameState {
export const Player = z.object({
id: z.number(),
name: z.string(),
creator: z.boolean(),
})

export type Player = z.infer<typeof Player>

export const Game = z.object({
id: z.number(),
creator: Player,
players: z.array(Player),
state: z.nativeEnum(GameState),
hit_duration: z.number(),
Expand Down
4 changes: 3 additions & 1 deletion client/src/game.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ export function Game() {
const canStartOrStopGame = () => {
return (
cookies.logged_in !== undefined &&
game.creator.id === cookies.logged_in.id &&
game.players.some(
(p) => p.id === cookies.logged_in.id && p.creator === true,
) &&
game.players.length >= 2
)
}
Expand Down
3 changes: 2 additions & 1 deletion server/src/games.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ pub enum GameState {
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct Game {
pub id: u32,
pub creator: usize,
pub players: Vec<Player>,
pub state: GameState,
pub hits_remaining: Vec<Hit>,
Expand All @@ -38,6 +37,7 @@ pub struct Player {
pub id: u32,
pub name: String,
pub state: PlayerState,
pub creator: bool,
}

impl From<&User> for Player {
Expand All @@ -46,6 +46,7 @@ impl From<&User> for Player {
id: u.id,
name: u.username.clone(),
state: PlayerState::Waiting,
creator: false,
}
}
}
Expand Down
1 change: 0 additions & 1 deletion server/src/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,6 @@ pub struct GamesResponse {
#[derive(Serialize, Deserialize, JsonSchema)]
pub struct GameResponse {
pub id: u32,
pub creator: Player,
pub players: Vec<Player>,
pub state: GameState,
pub hit_duration: u8,
Expand Down
3 changes: 0 additions & 3 deletions server/src/routes/games.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ pub fn create_game(user: User, serv: &State<ServiceStore>) -> Created<Json<GameR

Created::new(format!("/games/{}", game.id)).body(Json(GameResponse {
id: game.id,
creator: game.players.get(game.creator).cloned().unwrap(),
players: game.players,
state: game.state,
hit_duration: game.hit_duration,
Expand All @@ -58,7 +57,6 @@ pub fn get_all_games(serv: &State<ServiceStore>) -> Json<GamesResponse> {
.into_iter()
.map(|game| GameResponse {
id: game.id,
creator: game.players.get(game.creator).cloned().unwrap(),
players: game.players,
state: game.state,
hit_duration: game.hit_duration,
Expand Down Expand Up @@ -190,7 +188,6 @@ pub fn get_game(
match games.get(game_id) {
Some(g) => Ok(Json(GameResponse {
id: g.id,
creator: g.players.get(g.creator).cloned().unwrap(),
players: g.players,
state: g.state,
hit_duration: g.hit_duration,
Expand Down
8 changes: 5 additions & 3 deletions server/src/routes/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -618,9 +618,11 @@ pub mod tests {
.games
.get(0)
.unwrap()
.creator
.id,
2
.players
.get(0)
.unwrap()
.creator,
true
);
}

Expand Down
26 changes: 17 additions & 9 deletions server/src/services/games.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
games::{Game, GameState, PlayerState},
games::{Game, GameState, Player, PlayerState},
hits::Hit,
responses::{CurrentHitError, JoinGameError, LeaveGameError, StartGameError, StopGameError},
services::{HitService, ServiceHandle},
Expand Down Expand Up @@ -32,11 +32,13 @@ impl GameService {
pub fn add(&self, creator: &User) -> Game {
let mut data = self.data.lock().unwrap();
data.id += 1;
let mut player: Player = creator.into();

player.creator = true;

let game = Game {
id: data.id,
creator: 0,
players: vec![creator.into()],
players: vec![player],
state: GameState::Open,
hits_remaining: vec![],
hit_duration: 20,
Expand Down Expand Up @@ -139,7 +141,14 @@ impl GameService {
http_status_code: 409,
message: "the game is already running".into(),
})
} else if game.players.get(game.creator).unwrap().id != user.id {
} else if game
.players
.iter()
.find(|p| p.id == user.id)
.map(|p| p.creator)
.unwrap_or(false)
== false
{
Err(StartGameError {
http_status_code: 403,
message: "only the creator can start a game".into(),
Expand Down Expand Up @@ -171,11 +180,10 @@ impl GameService {
if game
.players
.iter()
.enumerate()
.find(|(_, p)| p.id == u.id)
.unwrap()
.0
!= game.creator
.find(|p| p.id == u.id)
.map(|p| p.creator)
.unwrap_or(false)
== false
{
return Err(StopGameError {
http_status_code: 403,
Expand Down

0 comments on commit 4b1d969

Please sign in to comment.