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