townsquare/src/store/modules/session.js

60 lines
1.2 KiB
JavaScript
Raw Normal View History

2020-06-16 10:58:36 +02:00
// helper functions
const set = key => (state, val) => {
state[key] = val;
};
const handleVote = (state, [index, vote]) => {
if (!state.nomination) return;
state.votes = [...state.votes];
state.votes[index] = vote === undefined ? !state.votes[index] : vote;
};
2020-05-30 22:47:42 +02:00
const state = () => ({
sessionId: "",
isSpectator: false,
playerCount: 0,
playerId: "",
2020-05-31 23:42:08 +02:00
claimedSeat: -1,
2020-06-03 22:25:23 +02:00
nomination: false,
votes: [],
lockedVote: 0,
votingSpeed: 3
2020-05-30 22:47:42 +02:00
});
const getters = {};
const actions = {};
const mutations = {
2020-06-16 10:58:36 +02:00
setSessionId: set("sessionId"),
setPlayerId: set("playerId"),
setSpectator: set("isSpectator"),
setPlayerCount: set("playerCount"),
setVotingSpeed: set("votingSpeed"),
2020-06-16 10:58:36 +02:00
claimSeat: set("claimedSeat"),
2020-05-31 23:42:08 +02:00
nomination(state, nomination) {
state.nomination = nomination;
2020-06-03 22:25:23 +02:00
state.votes = [];
2020-06-03 23:04:50 +02:00
state.lockedVote = 0;
2020-06-03 22:25:23 +02:00
},
2020-06-16 10:58:36 +02:00
/**
* Store a vote with and without syncing it to the live session.
* This is necessary in order to prevent infinite voting loops.
* @param state
* @param vote
*/
vote: handleVote,
voteSync: handleVote,
2020-06-03 23:04:50 +02:00
lockVote(state, lock) {
state.lockedVote = lock !== undefined ? lock : state.lockedVote + 1;
2020-05-30 22:47:42 +02:00
}
};
export default {
namespaced: true,
state,
getters,
actions,
mutations
};