-
Notifications
You must be signed in to change notification settings - Fork 200
/
lobby.js
85 lines (78 loc) · 2.45 KB
/
lobby.js
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
import { createSlice } from '@reduxjs/toolkit';
import _ from 'lodash';
const initialState = {
activeGames: [],
presenceList: [],
loaded: false,
newGame: { timeoutSeconds: null },
createGameModal: {
show: false,
gameOptions: {},
opponentInfo: null,
},
};
const lobby = createSlice({
name: 'lobby',
initialState,
reducers: {
initGameList: (
state,
{ payload: { activeGames, tournaments } },
) => ({
...state,
activeGames,
liveTournaments: tournaments.filter(x => x.isLive),
completedTournaments: tournaments.filter(x => !x.isLive),
loaded: true,
}),
updateCheckResult: (state, { payload }) => {
state.activeGames = state.activeGames.map(game => {
if (game.id === payload.gameId) {
const newPlayers = game.players.map(player => (player.id === payload.userId
? { ...player, checkResult: payload.checkResult }
: player));
return { ...game, players: newPlayers };
}
return game;
});
},
syncPresenceList: (state, { payload }) => {
state.presenceList = payload;
},
removeGameLobby: (state, { payload: { gameId } }) => {
state.activeGames = _.reject(state.activeGames, { id: gameId });
},
upsertGameLobby: (state, { payload: { game } }) => {
const gameToUpdate = _.find(state.activeGames, { id: game.id });
if (gameToUpdate) {
Object.assign(gameToUpdate, game);
} else {
state.activeGames.push(game);
}
},
selectNewGameTimeout: (state, { payload: { timeoutSeconds } }) => {
state.newGame.timeoutSeconds = timeoutSeconds;
},
finishGame: (state, { payload: { game } }) => {
state.activeGames = _.reject(state.activeGames, { id: game.id });
},
showCreateGameModal: state => {
state.createGameModal.show = true;
state.createGameModal.gameOptions = {};
state.createGameModal.opponentInfo = null;
},
closeCreateGameModal: state => {
state.createGameModal.show = false;
state.createGameModal.gameOptions = {};
state.createGameModal.opponentInfo = null;
},
showCreateGameInviteModal: (state, { payload: { opponentInfo } }) => {
state.createGameModal.show = true;
state.createGameModal.gameOptions = { type: 'invite' };
state.createGameModal.opponentInfo = opponentInfo;
},
},
});
const { actions, reducer } = lobby;
export { actions };
export default reducer;