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