townsquare/src/components/RoleSelectionModal.vue

195 lines
4.8 KiB
Vue
Raw Normal View History

2020-04-11 22:55:37 +02:00
<template>
2020-04-25 19:25:32 +02:00
<Modal
class="roles"
2020-05-02 21:11:20 +02:00
v-show="modals.roles"
@close="toggleModal('roles')"
2020-04-25 19:25:32 +02:00
v-if="nontravelerPlayers >= 5"
>
<h3>Select the characters for {{ nontravelerPlayers }} players:</h3>
2020-04-11 22:55:37 +02:00
<ul
class="tokens"
v-for="(teamRoles, team) in roleSelection"
v-bind:key="team"
>
<li class="count" v-bind:class="[team]">
{{ teamRoles.filter(role => role.selected).length }} /
{{ game[nontravelerPlayers - 5][team] }}
</li>
<li
v-for="role in teamRoles"
v-bind:class="[role.team, role.selected ? 'selected' : '']"
2020-04-11 22:55:37 +02:00
v-bind:key="role.id"
@click="role.selected = !role.selected"
>
<Token :role="role" />
2020-04-11 22:55:37 +02:00
</li>
</ul>
<div class="warning" v-if="hasSelectedSetupRoles">
Warning: there are characters selected that modify the game setup! The
randomizer does not account for these characters.
</div>
<div class="button-group">
<div
class="button"
@click="assignRoles"
v-bind:class="{
disabled: selectedRoles > nontravelerPlayers || !selectedRoles
}"
>
<font-awesome-icon icon="people-arrows" />
Assign {{ selectedRoles }} characters randomly
</div>
<div class="button" @click="selectRandomRoles">
<font-awesome-icon icon="random" />
2020-04-28 22:22:56 +02:00
Shuffle characters
</div>
</div>
2020-04-11 22:55:37 +02:00
</Modal>
</template>
<script>
import Modal from "./Modal";
import gameJSON from "./../game";
import Token from "./Token";
2020-05-02 21:11:20 +02:00
import { mapMutations, mapState } from "vuex";
2020-04-11 22:55:37 +02:00
const randomElement = arr => arr[Math.floor(Math.random() * arr.length)];
export default {
components: {
Token,
2020-04-11 22:55:37 +02:00
Modal
},
props: {
players: {
type: Array,
required: true
}
},
data: function() {
return {
roleSelection: {},
game: gameJSON
};
},
computed: {
nontravelerPlayers: function() {
2020-04-15 17:30:26 +02:00
return Math.min(
this.players.filter(({ role }) => role && role.team !== "traveler")
.length,
2020-04-15 17:30:26 +02:00
15
);
2020-04-11 22:55:37 +02:00
},
selectedRoles: function() {
return Object.values(this.roleSelection)
.map(roles => roles.filter(role => role.selected).length)
.reduce((a, b) => a + b, 0);
},
hasSelectedSetupRoles: function() {
return Object.values(this.roleSelection).some(roles =>
roles.some(role => role.selected && role.setup)
);
2020-05-02 21:11:20 +02:00
},
...mapState(["roles", "modals"])
2020-04-11 22:55:37 +02:00
},
methods: {
selectRandomRoles() {
2020-04-11 22:55:37 +02:00
this.roleSelection = {};
this.roles.forEach(role => {
if (!this.roleSelection[role.team]) {
this.$set(this.roleSelection, role.team, []);
}
this.roleSelection[role.team].push(role);
this.$set(role, "selected", false);
});
delete this.roleSelection["traveler"];
const playerCount = Math.max(5, this.nontravelerPlayers);
const composition = this.game[playerCount - 5];
Object.keys(composition).forEach(team => {
for (let x = 0; x < composition[team]; x++) {
const available = this.roleSelection[team].filter(
role => role.selected !== true
);
if (available.length) {
randomElement(available).selected = true;
}
}
});
},
assignRoles() {
if (this.selectedRoles <= this.nontravelerPlayers && this.selectedRoles) {
// generate list of selected roles and randomize it
const roles = Object.values(this.roleSelection)
.map(roles => roles.filter(role => role.selected))
.reduce((a, b) => [...a, ...b], [])
.map(a => [Math.random(), a])
.sort((a, b) => a[0] - b[0])
.map(a => a[1]);
this.players.forEach(player => {
if (player.role.team !== "traveler" && roles.length) {
player.role = roles.pop();
}
});
this.close();
}
2020-05-02 21:11:20 +02:00
},
...mapMutations(["toggleModal"])
2020-04-11 22:55:37 +02:00
},
mounted: function() {
if (!Object.keys(this.roleSelection).length) {
this.selectRandomRoles();
2020-04-11 22:55:37 +02:00
}
2020-04-18 21:03:58 +02:00
},
watch: {
roles() {
this.selectRandomRoles();
}
2020-04-11 22:55:37 +02:00
}
};
</script>
<style lang="scss">
@import "../vars.scss";
.roles .modal ul.tokens {
padding-left: 55px;
li {
opacity: 0.5;
transition: all 250ms;
&.selected {
opacity: 1;
}
}
2020-04-11 22:55:37 +02:00
.count {
opacity: 1;
2020-04-11 22:55:37 +02:00
position: absolute;
left: 0;
top: 40px;
2020-04-11 22:55:37 +02:00
font-weight: bold;
line-height: 50px;
text-align: center;
font-size: 100%;
width: 50px;
height: 50px;
2020-04-11 22:55:37 +02:00
&.townsfolk {
color: $townsfolk;
}
&.outsider {
color: $outsider;
}
&.minion {
color: $minion;
}
&.demon {
color: $demon;
}
}
}
.roles .modal .warning {
color: red;
text-align: center;
margin: auto;
}
2020-04-11 22:55:37 +02:00
</style>