Fixes
[vchess.git] / client / src / components / GameList.vue
CommitLineData
85e5b5c1 1<template lang="pug">
7aa548e7
BA
2div
3 table
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;
8477e53d
BA
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 ) {
0b0dc040 67 priority++;
8477e53d
BA
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 }
0b0dc040 81 }
8477e53d
BA
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 });
6808d7a1 89 });
bd76b456 90 const deltaCreated = maxCreated - minCreated;
6808d7a1
BA
91 return augmentedGames.sort((g1, g2) => {
92 return (
93 g2.priority - g1.priority + (g2.created - g1.created) / deltaCreated
94 );
bd76b456 95 });
6808d7a1 96 }
0b0dc040 97 },
9ddaf8da 98 methods: {
bd76b456
BA
99 player_s: function(g) {
100 if (this.showBoth)
6808d7a1
BA
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 )
bd76b456
BA
110 return g.players[1].name || "@nonymous";
111 return g.players[0].name || "@nonymous";
112 },
df647c70
BA
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 },
9a3049f3 133 deleteGame: function(game, e) {
6808d7a1 134 if (game.score != "*") {
8477e53d
BA
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 }
9a3049f3
BA
143 e.stopPropagation();
144 }
6808d7a1
BA
145 }
146 }
85e5b5c1
BA
147};
148</script>
0b0dc040 149
41c80bb6 150<style lang="sass" scoped>
910d631b 151// NOTE: the style applied to <tr> element doesn't work
41c80bb6
BA
152tr.my-turn > td
153 background-color: #fcd785
df647c70
BA
154
155tr
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
0b0dc040 164</style>