Experimental symmetric randomness + deterministic option
[vchess.git] / client / src / components / ChallengeList.vue
1 <template lang="pug">
2 div
3 table
4 thead
5 tr
6 th {{ st.tr["Variant"] }}
7 th {{ st.tr["With"] }}
8 th {{ st.tr["Cadence"] }}
9 th {{ st.tr["Random?"] }}
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)")
20 </template>
21
22 <script>
23 import { store } from "@/store";
24 export default {
25 name: "my-challenge-list",
26 props: ["challenges"],
27 data: function() {
28 return {
29 st: store.state
30 };
31 },
32 computed: {
33 sortedChallenges: function() {
34 // Show in order: challenges I sent, challenges I received, other challenges
35 let minAdded = Number.MAX_SAFE_INTEGER;
36 let maxAdded = 0;
37 let augmentedChalls = this.challenges.map(c => {
38 let priority = 0;
39 if (!!c.to && c.to == this.st.user.name) priority = 1;
40 else if (c.from.sid == this.st.user.sid || c.from.id == this.st.user.id)
41 priority = 2;
42 if (c.added < minAdded) minAdded = c.added;
43 if (c.added > maxAdded) maxAdded = c.added;
44 return Object.assign({}, c, { priority: priority });
45 });
46 const deltaAdded = maxAdded - minAdded;
47 return augmentedChalls.sort((c1, c2) => {
48 return c2.priority - c1.priority + (c2.added - c1.added) / deltaAdded;
49 });
50 }
51 },
52 methods: {
53 withWho: function(c) {
54 if (c.from.sid == this.st.user.sid || c.from.id == this.st.user.id)
55 return c.to || this.st.tr["Any player"];
56 return c.from.name || "@nonymous";
57 },
58 getRandomnessClass: function(c) {
59 return {
60 ["random-" + c.randomness]: true
61 };
62 }
63 }
64 };
65 </script>
66
67 <style lang="sass" scoped>
68 // NOTE: the style applied to <tr> element doesn't work
69 tr.fromyou > td
70 font-style: italic
71 tr.toyou > td
72 background-color: #fcd785
73
74 tr > td:last-child
75 &.random-0
76 background-color: #FF5733
77 &.random-1
78 background-color: #2B63B4
79 &.random-2
80 background-color: #33B42B
81 </style>