finished moving stuff to store

This commit is contained in:
Steffen 2020-05-04 23:05:36 +02:00
parent ca91112b26
commit 33c9edf339
No known key found for this signature in database
GPG Key ID: 764D74E98267DFC6
8 changed files with 64 additions and 54 deletions

View File

@ -232,10 +232,10 @@ export default {
font-weight: bold; font-weight: bold;
background: linear-gradient( background: linear-gradient(
to right, to right,
$demon 0%, $townsfolk 0%,
rgba(0, 0, 0, 0.5) 20%, rgba(0, 0, 0, 0.5) 20%,
rgba(0, 0, 0, 0.5) 80%, rgba(0, 0, 0, 0.5) 80%,
$townsfolk 100% $demon 100%
); );
} }
} }

View File

@ -14,18 +14,18 @@
<div <div
class="night first" class="night first"
v-if="player.firstNight && grimoire.isNightOrder" v-if="nightOrder.get(player).first && grimoire.isNightOrder"
> >
<em>{{ player.firstNight }}.</em> <em>{{ nightOrder.get(player).first }}.</em>
<span v-if="player.role.firstNightReminder">{{ <span v-if="player.role.firstNightReminder">{{
player.role.firstNightReminder | handleEmojis player.role.firstNightReminder | handleEmojis
}}</span> }}</span>
</div> </div>
<div <div
class="night other" class="night other"
v-if="player.otherNight && grimoire.isNightOrder" v-if="nightOrder.get(player).other && grimoire.isNightOrder"
> >
<em>{{ player.otherNight }}.</em> <em>{{ nightOrder.get(player).other }}.</em>
<span v-if="player.role.otherNightReminder">{{ <span v-if="player.role.otherNightReminder">{{
player.role.otherNightReminder | handleEmojis player.role.otherNightReminder | handleEmojis
}}</span> }}</span>
@ -72,7 +72,7 @@
<script> <script>
import Token from "./Token"; import Token from "./Token";
import { mapState } from "vuex"; import { mapGetters, mapState } from "vuex";
export default { export default {
components: { components: {
@ -84,7 +84,10 @@ export default {
required: true required: true
} }
}, },
computed: mapState(["grimoire"]), computed: {
...mapState(["grimoire"]),
...mapGetters({ nightOrder: "players/nightOrder" })
},
data() { data() {
return {}; return {};
}, },
@ -99,23 +102,32 @@ export default {
toggleStatus() { toggleStatus() {
if (this.grimoire.isPublic) { if (this.grimoire.isPublic) {
if (!this.player.hasDied) { if (!this.player.hasDied) {
this.$set(this.player, "hasDied", true); this.updatePlayer("hasDied", true);
} else if (this.player.hasVoted) { } else if (this.player.hasVoted) {
this.$set(this.player, "hasVoted", false); this.updatePlayer("hasVoted", false);
this.$set(this.player, "hasDied", false); this.updatePlayer("hasDied", false);
} else { } else {
this.$set(this.player, "hasVoted", true); this.updatePlayer("hasVoted", true);
} }
} else { } else {
this.$set(this.player, "hasDied", !this.player.hasDied); this.updatePlayer("hasDied", !this.player.hasDied);
} }
}, },
changeName() { changeName() {
const name = prompt("Player name", this.player.name); const name = prompt("Player name", this.player.name) || this.player.name;
this.player.name = name || this.player.name; this.updatePlayer("name", name);
}, },
removeReminder(reminder) { removeReminder(reminder) {
this.player.reminders.splice(this.player.reminders.indexOf(reminder), 1); const reminders = [...this.player.reminders];
reminders.splice(this.player.reminders.indexOf(reminder), 1);
this.updatePlayer("reminders", reminders);
},
updatePlayer(property, value) {
this.$store.commit("players/update", {
player: this.player,
property,
value
});
} }
} }
}; };

View File

@ -80,7 +80,6 @@ export default {
) )
) { ) {
this.$store.commit("players/remove", playerIndex); this.$store.commit("players/remove", playerIndex);
this.$store.dispatch("players/updateNightOrder");
} }
} }
} }

View File

@ -56,8 +56,12 @@ export default {
methods: { methods: {
addReminder(reminder) { addReminder(reminder) {
const player = this.$store.state.players.players[this.playerIndex]; const player = this.$store.state.players.players[this.playerIndex];
player.reminders.push(reminder); const value = [...player.reminders, reminder];
this.$store.commit("players/update", { index: this.playerIndex, player }); this.$store.commit("players/update", {
player,
property: "reminders",
value
});
this.$store.commit("toggleModal", "reminder"); this.$store.commit("toggleModal", "reminder");
}, },
...mapMutations(["toggleModal"]) ...mapMutations(["toggleModal"])

View File

@ -56,12 +56,11 @@ export default {
} else { } else {
// assign to player // assign to player
const player = this.$store.state.players.players[this.playerIndex]; const player = this.$store.state.players.players[this.playerIndex];
player.role = role;
this.$store.commit("players/update", { this.$store.commit("players/update", {
index: this.playerIndex, player,
player property: "role",
value: role
}); });
this.$store.dispatch("players/updateNightOrder");
} }
this.$store.commit("toggleModal", "role"); this.$store.commit("toggleModal", "role");
}, },

View File

@ -114,13 +114,16 @@ export default {
.map(a => [Math.random(), a]) .map(a => [Math.random(), a])
.sort((a, b) => a[0] - b[0]) .sort((a, b) => a[0] - b[0])
.map(a => a[1]); .map(a => a[1]);
this.players.forEach((player, index) => { this.players.forEach(player => {
if (player.role.team !== "traveler" && roles.length) { if (player.role.team !== "traveler" && roles.length) {
player.role = roles.pop(); const value = roles.pop();
this.$store.commit("players/update", { index, player }); this.$store.commit("players/update", {
player,
property: "role",
value
});
} }
}); });
this.$store.dispatch("players/updateNightOrder");
this.$store.commit("toggleModal", "roles"); this.$store.commit("toggleModal", "roles");
} }
}, },

View File

@ -2,9 +2,7 @@ const NEWPLAYER = {
role: {}, role: {},
reminders: [], reminders: [],
hasVoted: false, hasVoted: false,
hasDied: false, hasDied: false
firstNight: 0,
otherNight: 0
}; };
const state = () => ({ const state = () => ({
@ -17,15 +15,12 @@ const getters = {
player => player.role.team !== "traveler" player => player.role.team !== "traveler"
); );
return Math.min(nonTravelers.length, 15); return Math.min(nonTravelers.length, 15);
} },
}; // calculate a Map of player => night order
nightOrder({ players }) {
const actions = {
// recalculate night order for all players
updateNightOrder({ state, commit }) {
const firstNight = [0]; const firstNight = [0];
const otherNight = [0]; const otherNight = [0];
state.players.forEach(({ role }) => { players.forEach(({ role }) => {
if (role.firstNight && !firstNight.includes(role.firstNight)) { if (role.firstNight && !firstNight.includes(role.firstNight)) {
firstNight.push(role.firstNight); firstNight.push(role.firstNight);
} }
@ -35,17 +30,17 @@ const actions = {
}); });
firstNight.sort((a, b) => a - b); firstNight.sort((a, b) => a - b);
otherNight.sort((a, b) => a - b); otherNight.sort((a, b) => a - b);
state.players.forEach((player, index) => { const nightOrder = new Map();
players.forEach(player => {
const first = Math.max(firstNight.indexOf(player.role.firstNight), 0); const first = Math.max(firstNight.indexOf(player.role.firstNight), 0);
const other = Math.max(otherNight.indexOf(player.role.otherNight), 0); const other = Math.max(otherNight.indexOf(player.role.otherNight), 0);
if (player.firstNight !== first || player.otherNight !== other) { nightOrder.set(player, { first, other });
player.firstNight = first;
player.otherNight = other;
commit("update", { index, player });
console.log("updated night order for player", player.name);
}
}); });
}, return nightOrder;
}
};
const actions = {
randomize({ state, commit }) { randomize({ state, commit }) {
const players = state.players const players = state.players
.map(a => [Math.random(), a]) .map(a => [Math.random(), a])
@ -69,8 +64,11 @@ const mutations = {
set(state, players = []) { set(state, players = []) {
state.players = players; state.players = players;
}, },
update(state, { index, player }) { update(state, { player, property, value }) {
state.players[index] = player; const index = state.players.indexOf(player);
if (index >= 0) {
state.players[index][property] = value;
}
}, },
add(state, name) { add(state, name) {
state.players.push({ state.players.push({

View File

@ -26,8 +26,6 @@ module.exports = store => {
role: store.state.roles.get(player.role) || {} role: store.state.roles.get(player.role) || {}
})) }))
); );
// recalculate night order
store.dispatch("players/updateNightOrder");
} }
// listen to mutations // listen to mutations
@ -68,9 +66,7 @@ module.exports = store => {
state.players.players.map(player => ({ state.players.players.map(player => ({
...player, ...player,
// simplify the stored data // simplify the stored data
role: player.role.id || {}, role: player.role.id || {}
firstNight: undefined,
otherNight: undefined
})) }))
) )
); );
@ -79,6 +75,5 @@ module.exports = store => {
} }
break; break;
} }
console.log("persistance", type, payload);
}); });
}; };