scroll to game beginning: better fix
[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)"
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 }}
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";
85e5b5c1
BA
31export default {
32 name: "my-game-list",
6808d7a1 33 props: ["games", "showBoth"],
0b0dc040
BA
34 data: function() {
35 return {
36 st: store.state,
8477e53d
BA
37 deleted: {}, //mark deleted games
38 showCadence: window.innerWidth >= 425 //TODO: arbitrary value
0b0dc040
BA
39 };
40 },
bd76b456
BA
41 mounted: function() {
42 // timeout to avoid calling too many time the adjust method
43 let timeoutLaunched = false;
6808d7a1
BA
44 window.addEventListener("resize", () => {
45 if (!timeoutLaunched) {
bd76b456 46 timeoutLaunched = true;
6808d7a1 47 setTimeout(() => {
8477e53d 48 this.showCadence = window.innerWidth >= 425;
bd76b456
BA
49 timeoutLaunched = false;
50 }, 500);
51 }
52 });
53 },
dac39588 54 computed: {
0b0dc040
BA
55 sortedGames: function() {
56 // Show in order: games where it's my turn, my running games, my games, other games
6808d7a1
BA
57 let minCreated = Number.MAX_SAFE_INTEGER;
58 let maxCreated = 0;
db1f1f9a
BA
59 const isMyTurn = (g, myColor) => {
60 const rem = g.movesCount % 2;
61 return (
62 (rem == 0 && myColor == "w") ||
63 (rem == 1 && myColor == "b")
64 );
65 };
8477e53d
BA
66 let augmentedGames = this.games
67 .filter(g => !this.deleted[g.id])
68 .map(g => {
69 let priority = 0;
70 let myColor = undefined;
71 if (
72 g.players.some(
73 p => p.uid == this.st.user.id || p.sid == this.st.user.sid
74 )
75 ) {
0b0dc040 76 priority++;
8477e53d
BA
77 myColor =
78 g.players[0].uid == this.st.user.id ||
79 g.players[0].sid == this.st.user.sid
80 ? "w"
81 : "b";
82 if (g.score == "*") {
83 priority++;
db1f1f9a 84 if (isMyTurn(g, myColor)) priority++;
8477e53d 85 }
0b0dc040 86 }
8477e53d
BA
87 if (g.created < minCreated) minCreated = g.created;
88 if (g.created > maxCreated) maxCreated = g.created;
89 return Object.assign({}, g, {
90 priority: priority,
91 myTurn: priority == 3,
92 myColor: myColor
93 });
6808d7a1 94 });
bd76b456 95 const deltaCreated = maxCreated - minCreated;
6808d7a1
BA
96 return augmentedGames.sort((g1, g2) => {
97 return (
98 g2.priority - g1.priority + (g2.created - g1.created) / deltaCreated
99 );
bd76b456 100 });
6808d7a1 101 }
0b0dc040 102 },
9ddaf8da 103 methods: {
bd76b456
BA
104 player_s: function(g) {
105 if (this.showBoth)
6808d7a1
BA
106 return (
107 (g.players[0].name || "@nonymous") +
108 " - " +
109 (g.players[1].name || "@nonymous")
110 );
111 if (
112 this.st.user.sid == g.players[0].sid ||
113 this.st.user.id == g.players[0].uid
114 )
bd76b456
BA
115 return g.players[1].name || "@nonymous";
116 return g.players[0].name || "@nonymous";
117 },
df647c70
BA
118 scoreClass: function(g) {
119 if (g.score == "*" || !g.myColor) return {};
120 // Ok it's my finished game: determine if I won, drew or lost.
121 let res = {};
122 switch (g.score) {
123 case "1-0":
124 res[g.myColor == "w" ? "won" : "lost"] = true;
125 break;
126 case "0-1":
127 res[g.myColor == "b" ? "won" : "lost"] = true;
128 break;
129 case "1/2":
130 res["draw"] = true;
131 break;
132 // default case: "?" for unknown finished
133 default:
134 res["unknown"] = true;
135 }
136 return res;
137 },
9a3049f3 138 deleteGame: function(game, e) {
f9c36b2d
BA
139 if (
140 game.score != "*" &&
141 game.players.some(p =>
142 p.sid == this.st.user.sid ||
143 p.uid == this.st.user.id
144 )
145 ) {
8477e53d
BA
146 if (confirm(this.st.tr["Remove game?"])) {
147 GameStorage.remove(
148 game.id,
149 () => {
150 this.$set(this.deleted, game.id, true);
151 }
152 );
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>