Refactor Games structure on server: no longer use an extra 'Players' table
[vchess.git] / client / src / components / GameList.vue
CommitLineData
85e5b5c1 1<template lang="pug">
7aa548e7 2div
1c9826a5 3 table.game-list(v-if="games.length > 0")
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 11 tr(
585d0955 12 v-for="g in sortedGames()"
910d631b 13 @click="$emit('show-game',g)"
e727fe31 14 :class="{'my-turn': !!g.myTurn}"
910d631b 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 }}
1c9826a5
BA
24 p(v-else)
25 | {{ st.tr["No games found :( Send a challenge!"] }}
85e5b5c1
BA
26</template>
27
28<script>
0b0dc040 29import { store } from "@/store";
9ddaf8da 30import { GameStorage } from "@/utils/gameStorage";
aae89b49 31import { ajax } from "@/utils/ajax";
85e5b5c1
BA
32export default {
33 name: "my-game-list",
6808d7a1 34 props: ["games", "showBoth"],
0b0dc040
BA
35 data: function() {
36 return {
37 st: store.state,
8477e53d
BA
38 deleted: {}, //mark deleted games
39 showCadence: window.innerWidth >= 425 //TODO: arbitrary value
0b0dc040
BA
40 };
41 },
bd76b456
BA
42 mounted: function() {
43 // timeout to avoid calling too many time the adjust method
44 let timeoutLaunched = false;
6808d7a1
BA
45 window.addEventListener("resize", () => {
46 if (!timeoutLaunched) {
bd76b456 47 timeoutLaunched = true;
6808d7a1 48 setTimeout(() => {
8477e53d 49 this.showCadence = window.innerWidth >= 425;
bd76b456
BA
50 timeoutLaunched = false;
51 }, 500);
52 }
53 });
54 },
585d0955
BA
55 methods: {
56 player_s: function(g) {
57 if (this.showBoth)
58 return (
59 (g.players[0].name || "@nonymous") +
60 " - " +
61 (g.players[1].name || "@nonymous")
62 );
63 if (
64 this.st.user.sid == g.players[0].sid ||
0234201f 65 this.st.user.id == g.players[0].id
585d0955
BA
66 )
67 return g.players[1].name || "@nonymous";
68 return g.players[0].name || "@nonymous";
69 },
0b0dc040 70 sortedGames: function() {
e727fe31 71 // Show in order: it's my turn, running games, completed games
6808d7a1
BA
72 let minCreated = Number.MAX_SAFE_INTEGER;
73 let maxCreated = 0;
b265313e
BA
74 let remGames = this.games.filter(g => !this.deleted[g.id]);
75 remGames.forEach(g => {
e727fe31
BA
76 if (g.created < minCreated) minCreated = g.created;
77 if (g.created > maxCreated) maxCreated = g.created;
28b32b4f
BA
78 g.priority = 0;
79 if (g.score == "*") {
80 g.priority++;
81 if (!!g.myColor) g.priority++;
82 if (!!g.myTurn) g.priority++;
83 }
e727fe31 84 });
bd76b456 85 const deltaCreated = maxCreated - minCreated;
b265313e 86 return remGames.sort((g1, g2) => {
6808d7a1 87 return (
f14572c4
BA
88 g2.priority - g1.priority +
89 // Modulate with creation time (value in ]0,1[)
90 (g2.created - g1.created) / (deltaCreated + 1)
6808d7a1 91 );
bd76b456 92 });
e727fe31 93 },
df647c70
BA
94 scoreClass: function(g) {
95 if (g.score == "*" || !g.myColor) return {};
96 // Ok it's my finished game: determine if I won, drew or lost.
97 let res = {};
98 switch (g.score) {
99 case "1-0":
100 res[g.myColor == "w" ? "won" : "lost"] = true;
101 break;
102 case "0-1":
103 res[g.myColor == "b" ? "won" : "lost"] = true;
104 break;
105 case "1/2":
106 res["draw"] = true;
107 break;
108 // default case: "?" for unknown finished
109 default:
110 res["unknown"] = true;
111 }
112 return res;
113 },
9a3049f3 114 deleteGame: function(game, e) {
f9c36b2d 115 if (
aae89b49 116 // My game ?
f9c36b2d 117 game.players.some(p =>
0234201f 118 p.sid == this.st.user.sid || p.id == this.st.user.id
f9c36b2d
BA
119 )
120 ) {
aae89b49
BA
121 const message =
122 game.score != "*"
123 ? "Remove game?"
124 : "Abort and remove game?";
125 if (confirm(this.st.tr[message])) {
126 const afterDelete = () => {
3b0f26c1 127 if (game.score == "*") this.$emit("abortgame", game);
aae89b49
BA
128 this.$set(this.deleted, game.id, true);
129 };
130 if (game.type == "live")
131 // Effectively remove game:
132 GameStorage.remove(game.id, afterDelete);
133 else {
134 const mySide =
0234201f 135 game.players[0].id == this.st.user.id
aae89b49
BA
136 ? "White"
137 : "Black";
138 game["deletedBy" + mySide] = true;
139 // Mark the game for deletion on server
140 // If both people mark it, it is deleted
141 ajax(
142 "/games",
143 "PUT",
144 {
e57c4de4
BA
145 data: {
146 gid: game.id,
147 newObj: { removeFlag: true }
148 },
149 success: afterDelete
150 }
aae89b49
BA
151 );
152 }
8477e53d 153 }
9a3049f3
BA
154 e.stopPropagation();
155 }
6808d7a1
BA
156 }
157 }
85e5b5c1
BA
158};
159</script>
0b0dc040 160
41c80bb6 161<style lang="sass" scoped>
1c9826a5
BA
162p
163 text-align: center
164 font-weight: bold
165
910d631b 166// NOTE: the style applied to <tr> element doesn't work
41c80bb6
BA
167tr.my-turn > td
168 background-color: #fcd785
df647c70
BA
169
170tr
171 td.lost
172 background-color: #f5b7b1
173 td.won
174 background-color: lightgreen
175 td.draw
176 background-color: lightblue
177 td.unknown
178 background-color: lightgrey
0b0dc040 179</style>