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