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