Improvements - untested
[vchess.git] / client / src / components / GameList.vue
CommitLineData
85e5b5c1 1<template lang="pug">
7aa548e7 2div
e2590fa8 3 table.game-list
4f887105
BA
4 thead
5 tr
602d6bef 6 th {{ st.tr["Variant"] }}
bd76b456
BA
7 th {{ st.tr[showBoth ? "Players" : "Versus"] }}
8 th(v-if="showCadence") {{ st.tr["Cadence"] }}
26f3a887 9 th {{ st.tr["Result"] }}
4f887105 10 tbody
910d631b
BA
11 tr(
12 v-for="g in sortedGames"
13 @click="$emit('show-game',g)"
14 :class="{'my-turn': g.myTurn}"
15 )
bd76b456
BA
16 td {{ g.vname }}
17 td {{ player_s(g) }}
18 td(v-if="showCadence") {{ g.cadence }}
910d631b 19 td(
df647c70 20 :class="scoreClass(g)"
910d631b
BA
21 @click="deleteGame(g,$event)"
22 )
9ddaf8da 23 | {{ g.score }}
85e5b5c1
BA
24</template>
25
26<script>
0b0dc040 27import { store } from "@/store";
9ddaf8da 28import { GameStorage } from "@/utils/gameStorage";
85e5b5c1
BA
29export default {
30 name: "my-game-list",
6808d7a1 31 props: ["games", "showBoth"],
0b0dc040
BA
32 data: function() {
33 return {
34 st: store.state,
8477e53d
BA
35 deleted: {}, //mark deleted games
36 showCadence: window.innerWidth >= 425 //TODO: arbitrary value
0b0dc040
BA
37 };
38 },
bd76b456
BA
39 mounted: function() {
40 // timeout to avoid calling too many time the adjust method
41 let timeoutLaunched = false;
6808d7a1
BA
42 window.addEventListener("resize", () => {
43 if (!timeoutLaunched) {
bd76b456 44 timeoutLaunched = true;
6808d7a1 45 setTimeout(() => {
8477e53d 46 this.showCadence = window.innerWidth >= 425;
bd76b456
BA
47 timeoutLaunched = false;
48 }, 500);
49 }
50 });
51 },
dac39588 52 computed: {
0b0dc040
BA
53 sortedGames: function() {
54 // Show in order: games where it's my turn, my running games, my games, other games
6808d7a1
BA
55 let minCreated = Number.MAX_SAFE_INTEGER;
56 let maxCreated = 0;
db1f1f9a
BA
57 const isMyTurn = (g, myColor) => {
58 const rem = g.movesCount % 2;
59 return (
60 (rem == 0 && myColor == "w") ||
61 (rem == 1 && myColor == "b")
62 );
63 };
8477e53d
BA
64 let augmentedGames = this.games
65 .filter(g => !this.deleted[g.id])
66 .map(g => {
67 let priority = 0;
68 let myColor = undefined;
69 if (
70 g.players.some(
71 p => p.uid == this.st.user.id || p.sid == this.st.user.sid
72 )
73 ) {
0b0dc040 74 priority++;
8477e53d
BA
75 myColor =
76 g.players[0].uid == this.st.user.id ||
77 g.players[0].sid == this.st.user.sid
78 ? "w"
79 : "b";
80 if (g.score == "*") {
81 priority++;
db1f1f9a 82 if (isMyTurn(g, myColor)) priority++;
8477e53d 83 }
0b0dc040 84 }
8477e53d
BA
85 if (g.created < minCreated) minCreated = g.created;
86 if (g.created > maxCreated) maxCreated = g.created;
87 return Object.assign({}, g, {
88 priority: priority,
89 myTurn: priority == 3,
90 myColor: myColor
91 });
6808d7a1 92 });
bd76b456 93 const deltaCreated = maxCreated - minCreated;
6808d7a1
BA
94 return augmentedGames.sort((g1, g2) => {
95 return (
96 g2.priority - g1.priority + (g2.created - g1.created) / deltaCreated
97 );
bd76b456 98 });
6808d7a1 99 }
0b0dc040 100 },
9ddaf8da 101 methods: {
bd76b456
BA
102 player_s: function(g) {
103 if (this.showBoth)
6808d7a1
BA
104 return (
105 (g.players[0].name || "@nonymous") +
106 " - " +
107 (g.players[1].name || "@nonymous")
108 );
109 if (
110 this.st.user.sid == g.players[0].sid ||
111 this.st.user.id == g.players[0].uid
112 )
bd76b456
BA
113 return g.players[1].name || "@nonymous";
114 return g.players[0].name || "@nonymous";
115 },
df647c70
BA
116 scoreClass: function(g) {
117 if (g.score == "*" || !g.myColor) return {};
118 // Ok it's my finished game: determine if I won, drew or lost.
119 let res = {};
120 switch (g.score) {
121 case "1-0":
122 res[g.myColor == "w" ? "won" : "lost"] = true;
123 break;
124 case "0-1":
125 res[g.myColor == "b" ? "won" : "lost"] = true;
126 break;
127 case "1/2":
128 res["draw"] = true;
129 break;
130 // default case: "?" for unknown finished
131 default:
132 res["unknown"] = true;
133 }
134 return res;
135 },
9a3049f3 136 deleteGame: function(game, e) {
6808d7a1 137 if (game.score != "*") {
8477e53d
BA
138 if (confirm(this.st.tr["Remove game?"])) {
139 GameStorage.remove(
140 game.id,
141 () => {
142 this.$set(this.deleted, game.id, true);
143 }
144 );
145 }
9a3049f3
BA
146 e.stopPropagation();
147 }
6808d7a1
BA
148 }
149 }
85e5b5c1
BA
150};
151</script>
0b0dc040 152
41c80bb6 153<style lang="sass" scoped>
910d631b 154// NOTE: the style applied to <tr> element doesn't work
41c80bb6
BA
155tr.my-turn > td
156 background-color: #fcd785
df647c70
BA
157
158tr
159 td.lost
160 background-color: #f5b7b1
161 td.won
162 background-color: lightgreen
163 td.draw
164 background-color: lightblue
165 td.unknown
166 background-color: lightgrey
0b0dc040 167</style>