Adjustments
[vchess.git] / client / src / components / ChallengeList.vue
1 <template lang="pug">
2 div
3 table(v-if="challenges.length > 0")
4 thead
5 tr
6 th {{ st.tr["Variant"] }}
7 th {{ st.tr["With"] }}
8 th {{ st.tr["Cadence"] }}
9 th {{ st.tr["Options"] }}
10 tbody
11 tr(
12 v-for="c in sortedChallenges"
13 :class="{toyou:c.priority==1,fromyou:c.priority==2}"
14 @click="$emit('click-challenge',c)"
15 )
16 td {{ c.vname }}
17 td {{ withWho(c) }}
18 td {{ c.cadence }}
19 td(:class="getRandomnessClass(c)") {{ c.options.abridged || '' }}
20 p(v-else)
21 | {{ st.tr["No challenges found :( Click on 'New game'!"] }}
22 </template>
23
24 <script>
25 import { store } from "@/store";
26 export default {
27 name: "my-challenge-list",
28 props: ["challenges"],
29 data: function() {
30 return {
31 st: store.state
32 };
33 },
34 computed: {
35 sortedChallenges: function() {
36 // Show in order:
37 // challenges I sent, challenges I received, other challenges
38 let minAdded = Number.MAX_SAFE_INTEGER;
39 let maxAdded = 0;
40 let augmentedChalls = this.challenges.map(c => {
41 let priority = 0;
42 if (!!c.to && c.to == this.st.user.name) priority = 1;
43 else if (
44 c.from.sid == this.st.user.sid ||
45 (c.from.id > 0 && c.from.id == this.st.user.id)
46 ) {
47 priority = 2;
48 }
49 if (c.added < minAdded) minAdded = c.added;
50 if (c.added > maxAdded) maxAdded = c.added;
51 return Object.assign({ priority: priority }, c);
52 });
53 const deltaAdded = maxAdded - minAdded;
54 return augmentedChalls.sort((c1, c2) => {
55 return c2.priority - c1.priority + (c2.added - c1.added) / deltaAdded;
56 });
57 }
58 },
59 methods: {
60 withWho: function(c) {
61 if (c.from.sid == this.st.user.sid || c.from.id == this.st.user.id)
62 return c.to || this.st.tr["Any player"];
63 return c.from.name || "@nonymous";
64 },
65 getRandomnessClass: function(c) {
66 if (
67 // TODO: one extra test here
68 !Number.isInteger(c.options.randomness) &&
69 !parseInt(c.options.randomness, 10)
70 ) {
71 return {};
72 }
73 return {
74 ["random-" + c.options.randomness]: true
75 };
76 }
77 }
78 };
79 </script>
80
81 <style lang="sass" scoped>
82 p
83 text-align: center
84 font-weight: bold
85
86 // NOTE: the style applied to <tr> element doesn't work
87 tr.fromyou > td
88 font-style: italic
89 tr.toyou > td
90 background-color: #fcd785
91
92 tr > td:last-child
93 &.random-0
94 background-color: #FEAF9E
95 &.random-1
96 background-color: #9EB2FE
97 &.random-2
98 background-color: #A5FE9E
99 </style>