mirror of https://github.com/bra1n/townsquare.git
basic player store
This commit is contained in:
parent
7e48036aa3
commit
84e533640b
52
src/App.vue
52
src/App.vue
|
@ -37,11 +37,12 @@ export default {
|
||||||
TownInfo,
|
TownInfo,
|
||||||
RoleSelectionModal
|
RoleSelectionModal
|
||||||
},
|
},
|
||||||
computed: mapState(["grimoire"]),
|
computed: mapState({
|
||||||
|
grimoire: state => state.grimoire,
|
||||||
|
players: state => state.players.players
|
||||||
|
}),
|
||||||
data: function() {
|
data: function() {
|
||||||
return {
|
return {};
|
||||||
players: []
|
|
||||||
};
|
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
takeScreenshot(dimensions) {
|
takeScreenshot(dimensions) {
|
||||||
|
@ -68,49 +69,6 @@ export default {
|
||||||
this.$store.commit("toggleMenu");
|
this.$store.commit("toggleMenu");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
|
||||||
mounted() {
|
|
||||||
if (localStorage.players) {
|
|
||||||
this.players = JSON.parse(localStorage.players).map(player => ({
|
|
||||||
...player,
|
|
||||||
role: this.$store.state.roles.get(player.role) || {}
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
},
|
|
||||||
watch: {
|
|
||||||
players: {
|
|
||||||
handler(newPlayers) {
|
|
||||||
const firstNight = [0];
|
|
||||||
const otherNight = [0];
|
|
||||||
newPlayers.forEach(({ role }) => {
|
|
||||||
if (role.firstNight && !firstNight.includes(role.firstNight)) {
|
|
||||||
firstNight.push(role.firstNight);
|
|
||||||
}
|
|
||||||
if (role.otherNight && !otherNight.includes(role.otherNight)) {
|
|
||||||
otherNight.push(role.otherNight);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
firstNight.sort((a, b) => a - b);
|
|
||||||
otherNight.sort((a, b) => a - b);
|
|
||||||
newPlayers.forEach(player => {
|
|
||||||
player.firstNight = Math.max(
|
|
||||||
firstNight.indexOf(player.role.firstNight),
|
|
||||||
0
|
|
||||||
);
|
|
||||||
player.otherNight = Math.max(
|
|
||||||
otherNight.indexOf(player.role.otherNight),
|
|
||||||
0
|
|
||||||
);
|
|
||||||
});
|
|
||||||
localStorage.players = JSON.stringify(
|
|
||||||
newPlayers.map(player => ({
|
|
||||||
...player,
|
|
||||||
role: player.role.id || {}
|
|
||||||
}))
|
|
||||||
);
|
|
||||||
},
|
|
||||||
deep: true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import Vue from "vue";
|
import Vue from "vue";
|
||||||
import Vuex from "vuex";
|
import Vuex from "vuex";
|
||||||
import persistence from "./persistence";
|
import persistence from "./persistence";
|
||||||
|
import players from "./modules/players";
|
||||||
import editionJSON from "../editions.json";
|
import editionJSON from "../editions.json";
|
||||||
import rolesJSON from "../roles.json";
|
import rolesJSON from "../roles.json";
|
||||||
|
|
||||||
|
@ -20,6 +21,9 @@ const getRolesByEdition = (edition = "tb") => {
|
||||||
};
|
};
|
||||||
|
|
||||||
export default new Vuex.Store({
|
export default new Vuex.Store({
|
||||||
|
modules: {
|
||||||
|
players
|
||||||
|
},
|
||||||
state: {
|
state: {
|
||||||
grimoire: {
|
grimoire: {
|
||||||
isNightOrder: true,
|
isNightOrder: true,
|
||||||
|
@ -35,8 +39,7 @@ export default new Vuex.Store({
|
||||||
roles: false
|
roles: false
|
||||||
},
|
},
|
||||||
edition: "tb",
|
edition: "tb",
|
||||||
roles: getRolesByEdition(),
|
roles: getRolesByEdition()
|
||||||
players: []
|
|
||||||
},
|
},
|
||||||
mutations: {
|
mutations: {
|
||||||
toggleMenu({ grimoire }) {
|
toggleMenu({ grimoire }) {
|
||||||
|
|
|
@ -0,0 +1,56 @@
|
||||||
|
const state = () => ({
|
||||||
|
players: []
|
||||||
|
});
|
||||||
|
const getters = {};
|
||||||
|
const actions = {
|
||||||
|
// recalculate night order for all players
|
||||||
|
updateNightOrder({ state, commit }) {
|
||||||
|
const firstNight = [0];
|
||||||
|
const otherNight = [0];
|
||||||
|
state.players.forEach(({ role }) => {
|
||||||
|
if (role.firstNight && !firstNight.includes(role.firstNight)) {
|
||||||
|
firstNight.push(role.firstNight);
|
||||||
|
}
|
||||||
|
if (role.otherNight && !otherNight.includes(role.otherNight)) {
|
||||||
|
otherNight.push(role.otherNight);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
firstNight.sort((a, b) => a - b);
|
||||||
|
otherNight.sort((a, b) => a - b);
|
||||||
|
state.players.forEach((player, index) => {
|
||||||
|
const first = Math.max(firstNight.indexOf(player.role.firstNight), 0);
|
||||||
|
const other = Math.max(otherNight.indexOf(player.role.otherNight), 0);
|
||||||
|
if (player.firstNight !== first || player.otherNight !== other) {
|
||||||
|
player.firstNight = first;
|
||||||
|
player.otherNight = other;
|
||||||
|
commit("updatePlayer", index, player);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const mutations = {
|
||||||
|
setPlayers(state, players = []) {
|
||||||
|
state.players = players;
|
||||||
|
},
|
||||||
|
updatePlayer(state, index, player) {
|
||||||
|
state.players[index] = player;
|
||||||
|
},
|
||||||
|
addPlayer(state, name) {
|
||||||
|
state.players.push({
|
||||||
|
name,
|
||||||
|
role: {},
|
||||||
|
reminders: []
|
||||||
|
});
|
||||||
|
},
|
||||||
|
removePlayer(state, index) {
|
||||||
|
state.players.splice(index, 1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export default {
|
||||||
|
namespaced: true,
|
||||||
|
state,
|
||||||
|
getters,
|
||||||
|
actions,
|
||||||
|
mutations
|
||||||
|
};
|
|
@ -1,4 +1,5 @@
|
||||||
module.exports = store => {
|
module.exports = store => {
|
||||||
|
// initialize data
|
||||||
if (localStorage.background !== undefined) {
|
if (localStorage.background !== undefined) {
|
||||||
store.commit("setBackground", localStorage.background);
|
store.commit("setBackground", localStorage.background);
|
||||||
}
|
}
|
||||||
|
@ -8,7 +9,17 @@ module.exports = store => {
|
||||||
if (localStorage.edition !== undefined) {
|
if (localStorage.edition !== undefined) {
|
||||||
store.commit("setEdition", localStorage.edition);
|
store.commit("setEdition", localStorage.edition);
|
||||||
}
|
}
|
||||||
|
if (localStorage.players) {
|
||||||
|
store.commit(
|
||||||
|
"players/setPlayers",
|
||||||
|
JSON.parse(localStorage.players).map(player => ({
|
||||||
|
...player,
|
||||||
|
role: store.state.roles.get(player.role) || {}
|
||||||
|
}))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// listen to mutations
|
||||||
store.subscribe(({ type, payload }, state) => {
|
store.subscribe(({ type, payload }, state) => {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case "toggleGrimoire":
|
case "toggleGrimoire":
|
||||||
|
@ -28,6 +39,19 @@ module.exports = store => {
|
||||||
case "setEdition":
|
case "setEdition":
|
||||||
localStorage.setItem("edition", payload);
|
localStorage.setItem("edition", payload);
|
||||||
break;
|
break;
|
||||||
|
case "addPlayer":
|
||||||
|
case "updatePlayer":
|
||||||
|
case "removePlayer":
|
||||||
|
localStorage.setItem(
|
||||||
|
"players",
|
||||||
|
JSON.stringify(
|
||||||
|
state.players.players.map(player => ({
|
||||||
|
...player,
|
||||||
|
role: player.role.id || {}
|
||||||
|
}))
|
||||||
|
)
|
||||||
|
);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
console.log(type, payload);
|
console.log(type, payload);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue