townsquare/src/store/modules/session.js

117 lines
3.2 KiB
JavaScript
Raw Normal View History

/**
* Handle a vote request.
* If the vote is from a seat that is already locked, ignore it.
* @param state session state
* @param index seat of the player in the circle
* @param vote true or false
*/
2020-06-16 08:58:36 +00:00
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 20:47:42 +00:00
const state = () => ({
sessionId: "",
isSpectator: false,
isReconnecting: false,
2020-05-30 20:47:42 +00:00
playerCount: 0,
ping: 0,
2020-05-30 20:47:42 +00:00
playerId: "",
2020-05-31 21:42:08 +00:00
claimedSeat: -1,
2020-06-03 20:25:23 +00:00
nomination: false,
votes: [],
lockedVote: 0,
2020-12-05 21:24:55 +00:00
votingSpeed: 3000,
2020-12-06 21:27:52 +00:00
isVoteInProgress: false,
voteHistory: [],
markedPlayerId: -1,
2021-05-05 06:58:13 +00:00
isVoteHistoryAllowed: true,
isRolesDistributed: false
2020-05-30 20:47:42 +00:00
});
const getters = {};
const actions = {};
2021-02-01 21:33:59 +00:00
// mutations helper functions
const set = key => (state, val) => {
state[key] = val;
};
2020-05-30 20:47:42 +00:00
const mutations = {
2020-06-16 08:58:36 +00:00
setPlayerId: set("playerId"),
setSpectator: set("isSpectator"),
setReconnecting: set("isReconnecting"),
2020-06-16 08:58:36 +00:00
setPlayerCount: set("playerCount"),
setPing: set("ping"),
setVotingSpeed: set("votingSpeed"),
2020-12-05 21:24:55 +00:00
setVoteInProgress: set("isVoteInProgress"),
setMarkedPlayerId: set("markedPlayerId"),
setNomination: set("nomination"),
2021-05-05 06:58:13 +00:00
setVoteHistoryAllowed: set("isVoteHistoryAllowed"),
2020-06-16 08:58:36 +00:00
claimSeat: set("claimedSeat"),
distributeRoles: set("isRolesDistributed"),
2020-12-25 19:30:31 +00:00
setSessionId(state, sessionId) {
state.sessionId = sessionId
.toLocaleLowerCase()
.replace(/[^0-9a-z]/g, "")
.substr(0, 10);
},
2020-12-05 21:24:55 +00:00
nomination(
state,
{ nomination, votes, votingSpeed, lockedVote, isVoteInProgress } = {}
) {
state.nomination = nomination || false;
state.votes = votes || [];
state.votingSpeed = votingSpeed || state.votingSpeed;
state.lockedVote = lockedVote || 0;
2020-12-05 21:24:55 +00:00
state.isVoteInProgress = isVoteInProgress || false;
2020-06-03 20:25:23 +00:00
},
2020-12-06 21:27:52 +00:00
/**
* Create an entry in the vote history log. Requires current player array because it might change later in the game.
* Only stores votes that were completed.
* @param state
* @param players
*/
addHistory(state, players) {
2021-05-05 06:58:13 +00:00
if (!state.isVoteHistoryAllowed && state.isSpectator) return;
2020-12-06 21:27:52 +00:00
if (!state.nomination || state.lockedVote <= players.length) return;
const isExile = players[state.nomination[1]].role.team === "traveler";
2020-12-06 21:27:52 +00:00
state.voteHistory.push({
2021-01-07 19:25:12 +00:00
timestamp: new Date(),
2020-12-06 21:27:52 +00:00
nominator: players[state.nomination[0]].name,
nominee: players[state.nomination[1]].name,
type: isExile ? "Exile" : "Execution",
2020-12-06 21:27:52 +00:00
majority: Math.ceil(
players.filter(player => !player.isDead || isExile).length / 2
2020-12-06 21:27:52 +00:00
),
votes: players
.filter((player, index) => state.votes[index])
.map(({ name }) => name)
});
},
clearVoteHistory(state) {
2020-12-06 21:27:52 +00:00
state.voteHistory = [];
},
2020-06-16 08:58:36 +00: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 21:04:50 +00:00
lockVote(state, lock) {
state.lockedVote = lock !== undefined ? lock : state.lockedVote + 1;
2020-05-30 20:47:42 +00:00
}
};
export default {
namespaced: true,
state,
getters,
actions,
mutations
};