-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathround.vue
318 lines (314 loc) · 8.62 KB
/
round.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<template>
<div>
<b-collapse
class="card"
animation="slide"
aria-id="roundCard"
v-if="game.id"
>
<div
slot="trigger"
slot-scope="props"
class="card-header"
role="button"
aria-controls="roundCard"
id="round-card"
>
<p class="card-header-title">
Round
</p>
<a class="card-header-icon">
<b-icon :icon="props.open ? 'menu-down' : 'menu-up'"> </b-icon>
</a>
</div>
<div class="card-content" v-if="round && round.questionCard">
<img :src="getCardImage(round.questionCard.image)" />
<div v-for="card in round.winnerCards" :key="card['@id']">
<div class="card-answer">
<img :src="getCardImage(card.image)" />
</div>
</div>
<nav class="level">
<div
class="level-item has-text-centered"
:class="roundCardsSelected()"
>
<div>
<p class="heading">Answers needed</p>
<p class="title">
{{ round.questionCard.answerCount }}
</p>
</div>
</div>
<div class="level-item has-text-centered">
<div>
<p class="heading">Cards Played</p>
<p class="title">{{ round.cardsPlayedCount }}</p>
</div>
</div>
<div class="level-item has-text-centered">
<div>
<p class="heading">Status</p>
<p class="title">
{{ round.status }}
</p>
</div>
</div>
<div class="level-item has-text-centered">
<div>
<p class="heading">Winner</p>
<p class="title">
{{ round.winner ? round.winner.name : '--' }}
</p>
</div>
</div>
</nav>
</div>
<footer class="card-footer">
<b-button
:type="getButtonColor('startRound')"
class="card-footer-item"
@click="startRound"
>Start</b-button
>
<b-button
:type="getButtonColor('refreshRound')"
class="card-footer-item"
@click="getRoundAction"
>Refresh</b-button
>
<b-button type="is-light" class="card-footer-item" @click="cancelRound"
>Cancel</b-button
>
<b-button
:type="getButtonColor('finishRound')"
class="card-footer-item"
@click="finishRound"
>Finish</b-button
>
</footer>
</b-collapse>
<b-modal
:active.sync="isRoundModalActive"
has-modal-card
trap-focus
aria-role="dialog"
aria-modal
v-if="round && round.questionCard"
>
<div class="modal-card" style="width: auto">
<header class="modal-card-head">
<p class="modal-card-title">The question was:</p>
</header>
<section class="modal-card-body">
<div class="block">
<img :src="getCardImage(round.questionCard.image)" />
</div>
<header class="modal-card-head">
<p class="modal-card-title">And the winner is:</p>
</header>
<div class="block">
<div
v-for="playerAnswers in roundAnswers"
:key="playerAnswers.player.id"
>
<div v-for="card in playerAnswers.cards" :key="card['@id']">
<div
class="card-answer"
:class="winnerCardSelected(playerAnswers.player['@id'])"
>
<img
:src="getCardImage(card.image)"
@click="setWinner(playerAnswers.player['@id'])"
/>
</div>
</div>
</div>
</div>
</section>
<footer class="modal-card-foot">
<button class="button" type="button" @click="closeWinnerPopup()">
Close
</button>
<button class="button is-primary" @click="setRoundWinner()">
Finish round
</button>
</footer>
</div>
</b-modal>
</div>
</template>
<script>
import { mapActions, mapGetters, mapState } from 'vuex';
import _ from 'lodash';
import { ToastProgrammatic as Toast } from 'buefy';
import { mixin } from '../shared/mixins';
export default {
name: 'Round',
data() {
return {
notification: {},
isRoundModalActive: false,
winner: '',
currentRoundIri: '',
roundAnswers: [],
};
},
created() {
let round = JSON.parse(window.localStorage.getItem('round'));
if (round) {
this.setRound(round);
} else {
this.setRound({});
}
this.startAutoUpdate();
},
beforeDestroy() {
clearInterval(this.timer);
},
mixins: [mixin],
computed: {
...mapState(['game', 'player', 'round', 'cardsSelected']),
},
methods: {
...mapActions([
'startRoundAction',
'cancelRoundAction',
'finishRoundAction',
'setRound',
'getRoundAction',
]),
async startRound() {
await this.startRoundAction();
this.startAutoUpdate();
},
async cancelRound() {
if (!this.round || !this.round.questionCard) {
Toast.open({
message: 'no round in progress',
type: 'is-danger',
});
return false;
}
if (this.round.status === 'finished') {
Toast.open({
message: 'cannot cancel finished round',
type: 'is-danger',
});
return false;
}
await this.cancelRoundAction();
this.setRound(this.round);
},
async finishRound() {
if (this.round.status === 'finished') {
Toast.open({
message: 'round alredy finished',
type: 'is-danger',
});
return false;
}
if (!this.round.questionCard) {
Toast.open({
message: 'no round in progress',
type: 'is-danger',
});
return false;
}
if (!this.round.cardsPlayedCount) {
Toast.open({
message: 'no cards played - cancel round if you want',
type: 'is-danger',
});
return false;
}
if (
this.round.questionCard.answerCount * this.game.playersCount >
this.round.cardsPlayedCount
) {
this.$buefy.dialog.confirm({
title: 'Confirm',
message:
'Not all players ready. Do you still want to finish this round?',
confirmText: 'Finish round',
type: 'is-danger',
hasIcon: true,
onConfirm: () => {
// this.cancelAutoUpdate();
this.isRoundModalActive = true;
},
});
} else {
// this.cancelAutoUpdate();
this.isRoundModalActive = true;
}
this.roundAnswers = _.shuffle(this.round.playersAnswers);
this.winner = '';
return false;
},
setWinner(playerIri) {
this.winner = playerIri;
},
winnerCardSelected(playerIri) {
if (this.winner == playerIri) {
return 'has-background-success';
} else {
return '';
}
},
async setRoundWinner() {
this.isRoundModalActive = false;
await this.finishRoundAction(this.winner);
Toast.open({
message: 'Round finished, how about starting another one?',
type: 'is-success',
});
this.startAutoUpdate();
},
roundCardsSelected() {
if (
!this.cardsSelected ||
(this.round.questionCard &&
this.round.questionCard.answerCount != this.cardsSelected.length)
) {
return 'has-background-danger';
}
return 'has-background-success';
},
winnerSelected() {
if (
!this.cardsSelected ||
(this.round.questionCard &&
this.round.questionCard.answerCount != this.cardsSelected.length)
) {
return 'has-background-danger';
}
return 'has-background-success';
},
async refresh() {
if (this.round['@id'] && !this.isRoundModalActive) {
console.log('round refresh');
await this.getRoundAction();
} else {
this.setRound(this.round);
}
if (this.round['@id'] != this.currentRoundIri) {
this.currentRoundIri = this.round['@id'];
this.isRoundModalActive = false;
this.startAutoUpdate();
}
},
cancelAutoUpdate() {
clearInterval(this.timer);
},
startAutoUpdate() {
this.cancelAutoUpdate();
this.timer = setInterval(this.refresh, 3000);
},
closeWinnerPopup() {
this.isRoundModalActive = false;
this.startAutoUpdate();
},
},
};
</script>