Commit | Line | Data |
---|---|---|
85e5b5c1 | 1 | <template lang="pug"> |
7aa548e7 BA |
2 | div |
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 BA |
10 | tbody |
11 | tr(v-for="g in sortedGames" @click="$emit('show-game',g)" | |
12 | :class="{'my-turn': g.myTurn}") | |
bd76b456 BA |
13 | td {{ g.vname }} |
14 | td {{ player_s(g) }} | |
15 | td(v-if="showCadence") {{ g.cadence }} | |
16 | td(:class="{finished: g.score!='*'}" @click="deleteGame(g,$event)") | |
9ddaf8da | 17 | | {{ g.score }} |
85e5b5c1 BA |
18 | </template> |
19 | ||
20 | <script> | |
0b0dc040 | 21 | import { store } from "@/store"; |
9ddaf8da | 22 | import { GameStorage } from "@/utils/gameStorage"; |
85e5b5c1 BA |
23 | export default { |
24 | name: "my-game-list", | |
6808d7a1 | 25 | props: ["games", "showBoth"], |
0b0dc040 BA |
26 | data: function() { |
27 | return { | |
28 | st: store.state, | |
6808d7a1 | 29 | showCadence: true |
0b0dc040 BA |
30 | }; |
31 | }, | |
bd76b456 BA |
32 | mounted: function() { |
33 | // timeout to avoid calling too many time the adjust method | |
34 | let timeoutLaunched = false; | |
6808d7a1 BA |
35 | window.addEventListener("resize", () => { |
36 | if (!timeoutLaunched) { | |
bd76b456 | 37 | timeoutLaunched = true; |
6808d7a1 | 38 | setTimeout(() => { |
bd76b456 BA |
39 | this.showCadence = window.innerWidth >= 425; //TODO: arbitrary |
40 | timeoutLaunched = false; | |
41 | }, 500); | |
42 | } | |
43 | }); | |
44 | }, | |
dac39588 | 45 | computed: { |
0b0dc040 BA |
46 | sortedGames: function() { |
47 | // Show in order: games where it's my turn, my running games, my games, other games | |
6808d7a1 BA |
48 | let minCreated = Number.MAX_SAFE_INTEGER; |
49 | let maxCreated = 0; | |
0b0dc040 BA |
50 | let augmentedGames = this.games.map(g => { |
51 | let priority = 0; | |
6808d7a1 BA |
52 | if ( |
53 | g.players.some( | |
54 | p => p.uid == this.st.user.id || p.sid == this.st.user.sid | |
55 | ) | |
56 | ) { | |
0b0dc040 | 57 | priority++; |
6808d7a1 | 58 | if (g.score == "*") { |
0b0dc040 | 59 | priority++; |
6808d7a1 BA |
60 | const myColor = |
61 | g.players[0].uid == this.st.user.id || | |
62 | g.players[0].sid == this.st.user.sid | |
63 | ? "w" | |
64 | : "b"; | |
5fd5fb22 | 65 | // I play in this game, so g.fen will be defined |
6808d7a1 | 66 | if (g.fen.match(" " + myColor + " ")) priority++; |
0b0dc040 BA |
67 | } |
68 | } | |
6808d7a1 BA |
69 | if (g.created < minCreated) minCreated = g.created; |
70 | if (g.created > maxCreated) maxCreated = g.created; | |
71 | return Object.assign({}, g, { | |
72 | priority: priority, | |
73 | myTurn: priority == 3 | |
74 | }); | |
0b0dc040 | 75 | }); |
bd76b456 | 76 | const deltaCreated = maxCreated - minCreated; |
6808d7a1 BA |
77 | return augmentedGames.sort((g1, g2) => { |
78 | return ( | |
79 | g2.priority - g1.priority + (g2.created - g1.created) / deltaCreated | |
80 | ); | |
bd76b456 | 81 | }); |
6808d7a1 | 82 | } |
0b0dc040 | 83 | }, |
9ddaf8da | 84 | methods: { |
bd76b456 BA |
85 | player_s: function(g) { |
86 | if (this.showBoth) | |
6808d7a1 BA |
87 | return ( |
88 | (g.players[0].name || "@nonymous") + | |
89 | " - " + | |
90 | (g.players[1].name || "@nonymous") | |
91 | ); | |
92 | if ( | |
93 | this.st.user.sid == g.players[0].sid || | |
94 | this.st.user.id == g.players[0].uid | |
95 | ) | |
bd76b456 BA |
96 | return g.players[1].name || "@nonymous"; |
97 | return g.players[0].name || "@nonymous"; | |
98 | }, | |
9a3049f3 | 99 | deleteGame: function(game, e) { |
6808d7a1 BA |
100 | if (game.score != "*") { |
101 | if (confirm(this.st.tr["Remove game?"])) GameStorage.remove(game.id); | |
9a3049f3 BA |
102 | e.stopPropagation(); |
103 | } | |
6808d7a1 BA |
104 | } |
105 | } | |
85e5b5c1 BA |
106 | }; |
107 | </script> | |
0b0dc040 | 108 | |
41c80bb6 BA |
109 | <style lang="sass" scoped> |
110 | // TODO: understand why the style applied to <tr> element doesn't work | |
111 | tr.my-turn > td | |
112 | background-color: #fcd785 | |
9ddaf8da | 113 | tr td.finished |
9a3049f3 | 114 | background-color: #f5b7b1 |
0b0dc040 | 115 | </style> |