Webatrice: Adding joined game to persistence layer (#5068)

* Adding joined game to persistence layer

* Linting fixes
This commit is contained in:
Joseph Insalaco
2024-06-26 22:06:47 -04:00
committed by GitHub
parent 1ab723ca64
commit ea8da24215
7 changed files with 63 additions and 12 deletions

View File

@@ -62,4 +62,10 @@ export const Actions = {
type: Types.GAME_CREATED, type: Types.GAME_CREATED,
roomId roomId
}), }),
joinedGame: (roomId, gameId) => ({
type: Types.JOINED_GAME,
roomId,
gameId
}),
} }

View File

@@ -51,4 +51,8 @@ export const Dispatch = {
gameCreated: (roomId) => { gameCreated: (roomId) => {
store.dispatch(Actions.gameCreated(roomId)); store.dispatch(Actions.gameCreated(roomId));
}, },
joinedGame: (roomId, gameId) => {
store.dispatch(Actions.joinedGame(roomId, gameId));
}
} }

View File

@@ -1,8 +1,10 @@
import { GameSortField, Room, SortBy, UserSortField } from 'types'; import { GameSortField, Room, Game, SortBy, UserSortField } from 'types';
export interface RoomsState { export interface RoomsState {
rooms: RoomsStateRooms; rooms: RoomsStateRooms;
joined: JoinedRooms; games: RoomsStateGames;
joinedRoomIds: JoinedRooms;
joinedGameIds: JoinedGames;
messages: RoomsStateMessages; messages: RoomsStateMessages;
sortGamesBy: RoomsStateSortGamesBy; sortGamesBy: RoomsStateSortGamesBy;
sortUsersBy: RoomsStateSortUsersBy; sortUsersBy: RoomsStateSortUsersBy;
@@ -12,10 +14,22 @@ export interface RoomsStateRooms {
[roomId: number]: Room; [roomId: number]: Room;
} }
export interface RoomsStateGames {
[roomId: number]: {
[gameId: number]: Game;
};
}
export interface JoinedRooms { export interface JoinedRooms {
[roomId: number]: boolean; [roomId: number]: boolean;
} }
export interface JoinedGames {
[roomId: number]: {
[gameId: number]: boolean;
};
}
export interface RoomsStateMessages { export interface RoomsStateMessages {
[roomId: number]: Message[]; [roomId: number]: Message[];
} }

View File

@@ -9,7 +9,9 @@ import { MAX_ROOM_MESSAGES, Types } from './rooms.types';
const initialState: RoomsState = { const initialState: RoomsState = {
rooms: {}, rooms: {},
joined: {}, games: {},
joinedRoomIds: {},
joinedGameIds: {},
messages: {}, messages: {},
sortGamesBy: { sortGamesBy: {
field: GameSortField.START_TIME, field: GameSortField.START_TIME,
@@ -56,7 +58,7 @@ export const roomsReducer = (state = initialState, action: any) => {
case Types.JOIN_ROOM: { case Types.JOIN_ROOM: {
const { roomInfo } = action; const { roomInfo } = action;
const { joined, rooms, sortGamesBy, sortUsersBy } = state; const { joinedRoomIds, rooms, sortGamesBy, sortUsersBy } = state;
const { roomId } = roomInfo; const { roomId } = roomInfo;
@@ -83,8 +85,8 @@ export const roomsReducer = (state = initialState, action: any) => {
} }
}, },
joined: { joinedRoomIds: {
...joined, ...joinedRoomIds,
[roomId]: true [roomId]: true
}, },
} }
@@ -92,10 +94,10 @@ export const roomsReducer = (state = initialState, action: any) => {
case Types.LEAVE_ROOM: { case Types.LEAVE_ROOM: {
const { roomId } = action; const { roomId } = action;
const { joined, messages } = state; const { joinedRoomIds, messages } = state;
const _joined = { const _joined = {
...joined ...joinedRoomIds
}; };
const _messages = { const _messages = {
@@ -108,7 +110,7 @@ export const roomsReducer = (state = initialState, action: any) => {
return { return {
...state, ...state,
joined: _joined, joinedRoomIds: _joined,
messages: _messages, messages: _messages,
} }
} }
@@ -302,6 +304,22 @@ export const roomsReducer = (state = initialState, action: any) => {
} }
} }
case Types.JOINED_GAME: {
const { gameId, roomId } = action;
const { joinedGameIds } = state;
return {
...state,
joinedGameIds: {
...joinedGameIds,
[roomId]: {
...joinedGameIds[roomId],
[gameId]: true,
}
}
}
}
default: default:
return state; return state;
} }

View File

@@ -7,17 +7,25 @@ interface State {
export const Selectors = { export const Selectors = {
getRooms: ({ rooms }: State) => rooms.rooms, getRooms: ({ rooms }: State) => rooms.rooms,
getGames: ({ rooms }: State) => rooms.games,
getRoom: ({ rooms }: State, id: number) => getRoom: ({ rooms }: State, id: number) =>
_.find(rooms.rooms, ({ roomId }) => roomId === id), _.find(rooms.rooms, ({ roomId }) => roomId === id),
getJoined: ({ rooms }: State) => rooms.joined, getJoinedRoomIds: ({ rooms }: State) => rooms.joinedRoomIds,
getJoinedGameIds: ({ rooms }: State) => rooms.joinedGameIds,
getMessages: ({ rooms }: State) => rooms.messages, getMessages: ({ rooms }: State) => rooms.messages,
getSortGamesBy: ({ rooms: { sortGamesBy } }: State) => sortGamesBy, getSortGamesBy: ({ rooms: { sortGamesBy } }: State) => sortGamesBy,
getSortUsersBy: ({ rooms: { sortUsersBy } }: State) => sortUsersBy, getSortUsersBy: ({ rooms: { sortUsersBy } }: State) => sortUsersBy,
getJoinedRooms: (state: State) => { getJoinedRooms: (state: State) => {
const joined = Selectors.getJoined(state); const joined = Selectors.getJoinedRoomIds(state);
return _.filter(Selectors.getRooms(state), room => joined[room.roomId]); return _.filter(Selectors.getRooms(state), room => joined[room.roomId]);
}, },
getJoinedGames: (state: State, roomId: number) => {
const joined = Selectors.getJoinedGameIds(state)[roomId];
return _.filter(Selectors.getGames(state)[roomId], game => joined[game.gameId]);
},
getRoomMessages: (state: State, roomId: number) => Selectors.getMessages(state)[roomId], getRoomMessages: (state: State, roomId: number) => Selectors.getMessages(state)[roomId],
getRoomGames: (state: State, roomId: number) => Selectors.getRooms(state)[roomId].gameList, getRoomGames: (state: State, roomId: number) => Selectors.getRooms(state)[roomId].gameList,
getRoomUsers: (state: State, roomId: number) => Selectors.getRooms(state)[roomId].userList getRoomUsers: (state: State, roomId: number) => Selectors.getRooms(state)[roomId].userList

View File

@@ -10,6 +10,7 @@ export const Types = {
SORT_GAMES: '[Rooms] Sort Games', SORT_GAMES: '[Rooms] Sort Games',
REMOVE_MESSAGES: '[Rooms] Remove Messages', REMOVE_MESSAGES: '[Rooms] Remove Messages',
GAME_CREATED: '[Rooms] Game Created', GAME_CREATED: '[Rooms] Game Created',
JOINED_GAME: '[Rooms] Joined Game',
}; };
export const MAX_ROOM_MESSAGES = 1000; export const MAX_ROOM_MESSAGES = 1000;

View File

@@ -58,6 +58,6 @@ export class RoomPersistence {
} }
static joinedGame(roomId: number, gameId: number) { static joinedGame(roomId: number, gameId: number) {
console.log('joinedGame', roomId, gameId); RoomsDispatch.joinedGame(roomId, gameId);
} }
} }