Fix games ordering in MyGames, fix en-passant mistake in Rifle variant
[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
BA
11 tr(
12 v-for="g in sortedGames"
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 },
dac39588 55 computed: {
0b0dc040 56 sortedGames: function() {
e727fe31 57 // Show in order: it's my turn, running games, completed games
6808d7a1
BA
58 let minCreated = Number.MAX_SAFE_INTEGER;
59 let maxCreated = 0;
e727fe31
BA
60 this.games.forEach(g => {
61 if (g.created < minCreated) minCreated = g.created;
62 if (g.created > maxCreated) maxCreated = g.created;
63 });
bd76b456 64 const deltaCreated = maxCreated - minCreated;
e727fe31 65 return this.games.sort((g1, g2) => {
6808d7a1
BA
66 return (
67 g2.priority - g1.priority + (g2.created - g1.created) / deltaCreated
68 );
bd76b456 69 });
e727fe31 70 },
0b0dc040 71 },
9ddaf8da 72 methods: {
bd76b456
BA
73 player_s: function(g) {
74 if (this.showBoth)
6808d7a1
BA
75 return (
76 (g.players[0].name || "@nonymous") +
77 " - " +
78 (g.players[1].name || "@nonymous")
79 );
80 if (
81 this.st.user.sid == g.players[0].sid ||
82 this.st.user.id == g.players[0].uid
83 )
bd76b456
BA
84 return g.players[1].name || "@nonymous";
85 return g.players[0].name || "@nonymous";
86 },
df647c70
BA
87 scoreClass: function(g) {
88 if (g.score == "*" || !g.myColor) return {};
89 // Ok it's my finished game: determine if I won, drew or lost.
90 let res = {};
91 switch (g.score) {
92 case "1-0":
93 res[g.myColor == "w" ? "won" : "lost"] = true;
94 break;
95 case "0-1":
96 res[g.myColor == "b" ? "won" : "lost"] = true;
97 break;
98 case "1/2":
99 res["draw"] = true;
100 break;
101 // default case: "?" for unknown finished
102 default:
103 res["unknown"] = true;
104 }
105 return res;
106 },
9a3049f3 107 deleteGame: function(game, e) {
f9c36b2d 108 if (
aae89b49 109 // My game ?
f9c36b2d
BA
110 game.players.some(p =>
111 p.sid == this.st.user.sid ||
112 p.uid == this.st.user.id
113 )
114 ) {
aae89b49
BA
115 const message =
116 game.score != "*"
117 ? "Remove game?"
118 : "Abort and remove game?";
119 if (confirm(this.st.tr[message])) {
120 const afterDelete = () => {
3b0f26c1 121 if (game.score == "*") this.$emit("abortgame", game);
aae89b49
BA
122 this.$set(this.deleted, game.id, true);
123 };
124 if (game.type == "live")
125 // Effectively remove game:
126 GameStorage.remove(game.id, afterDelete);
127 else {
128 const mySide =
129 game.players[0].uid == this.st.user.id
130 ? "White"
131 : "Black";
132 game["deletedBy" + mySide] = true;
133 // Mark the game for deletion on server
134 // If both people mark it, it is deleted
135 ajax(
136 "/games",
137 "PUT",
138 {
e57c4de4
BA
139 data: {
140 gid: game.id,
141 newObj: { removeFlag: true }
142 },
143 success: afterDelete
144 }
aae89b49
BA
145 );
146 }
8477e53d 147 }
9a3049f3
BA
148 e.stopPropagation();
149 }
6808d7a1
BA
150 }
151 }
85e5b5c1
BA
152};
153</script>
0b0dc040 154
41c80bb6 155<style lang="sass" scoped>
1c9826a5
BA
156p
157 text-align: center
158 font-weight: bold
159
910d631b 160// NOTE: the style applied to <tr> element doesn't work
41c80bb6
BA
161tr.my-turn > td
162 background-color: #fcd785
df647c70
BA
163
164tr
165 td.lost
166 background-color: #f5b7b1
167 td.won
168 background-color: lightgreen
169 td.draw
170 background-color: lightblue
171 td.unknown
172 background-color: lightgrey
0b0dc040 173</style>