Better clocks management
[vchess.git] / client / src / views / MyGames.vue
CommitLineData
afd3240d
BA
1<template lang="pug">
2main
3 .row
4 .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
5 .button-group
910d631b
BA
6 button.tabbtn#liveGames(@click="setDisplay('live',$event)")
7 | {{ st.tr["Live games"] }}
8 button.tabbtn#corrGames(@click="setDisplay('corr',$event)")
9 | {{ st.tr["Correspondance games"] }}
10 GameList(
11 v-show="display=='live'"
12 :games="liveGames"
13 @show-game="showGame"
3b0f26c1 14 @abortgame="abortGame"
910d631b
BA
15 )
16 GameList(
17 v-show="display=='corr'"
18 :games="corrGames"
19 @show-game="showGame"
3b0f26c1 20 @abortgame="abortGame"
910d631b 21 )
afd3240d
BA
22</template>
23
24<script>
afd3240d
BA
25import { store } from "@/store";
26import { GameStorage } from "@/utils/gameStorage";
27import { ajax } from "@/utils/ajax";
aae89b49 28import { getScoreMessage } from "@/utils/scoring";
23ecf008
BA
29import params from "@/parameters";
30import { getRandString } from "@/utils/alea";
afd3240d
BA
31import GameList from "@/components/GameList.vue";
32export default {
89021f18 33 name: "my-my-games",
afd3240d 34 components: {
6808d7a1 35 GameList
afd3240d
BA
36 },
37 data: function() {
38 return {
39 st: store.state,
dac39588 40 display: "live",
2f258c37 41 liveGames: [],
db1f1f9a
BA
42 corrGames: [],
43 conn: null,
44 connexionString: ""
afd3240d
BA
45 };
46 },
47 created: function() {
db1f1f9a 48 GameStorage.getAll(true, localGames => {
aae89b49 49 localGames.forEach(g => g.type = "live");
2f258c37 50 this.liveGames = localGames;
afd3240d 51 });
6808d7a1 52 if (this.st.user.id > 0) {
aae89b49
BA
53 ajax(
54 "/games",
55 "GET",
56 { uid: this.st.user.id },
57 res => {
58 let serverGames = res.games.filter(g => {
59 const mySide =
60 g.players[0].uid == this.st.user.id
61 ? "White"
62 : "Black";
63 return !g["deletedBy" + mySide];
64 });
65 serverGames.forEach(g => g.type = "corr");
66 this.corrGames = serverGames;
afd3240d
BA
67 });
68 }
db1f1f9a
BA
69 // Initialize connection
70 this.connexionString =
71 params.socketUrl +
72 "/?sid=" +
73 this.st.user.sid +
74 "&tmpId=" +
75 getRandString() +
76 "&page=" +
77 encodeURIComponent(this.$route.path);
78 this.conn = new WebSocket(this.connexionString);
79 this.conn.onmessage = this.socketMessageListener;
80 this.conn.onclose = this.socketCloseListener;
afd3240d 81 },
2f258c37
BA
82 mounted: function() {
83 const showType = localStorage.getItem("type-myGames") || "live";
84 this.setDisplay(showType);
85 },
23ecf008
BA
86 beforeDestroy: function() {
87 this.conn.send(JSON.stringify({code: "disconnect"}));
88 },
afd3240d 89 methods: {
2f258c37
BA
90 setDisplay: function(type, e) {
91 this.display = type;
92 localStorage.setItem("type-myGames", type);
6808d7a1 93 let elt = e ? e.target : document.getElementById(type + "Games");
2f258c37 94 elt.classList.add("active");
23ecf008 95 elt.classList.remove("somethingnew"); //in case of
6808d7a1 96 if (elt.previousElementSibling)
2f258c37 97 elt.previousElementSibling.classList.remove("active");
6808d7a1 98 else elt.nextElementSibling.classList.remove("active");
2f258c37
BA
99 },
100 // TODO: classifyObject is redundant (see Hall.vue)
afd3240d 101 classifyObject: function(o) {
6808d7a1 102 return o.cadence.indexOf("d") === -1 ? "live" : "corr";
afd3240d 103 },
feaf1bf7
BA
104 showGame: function(game) {
105 // TODO: "isMyTurn" is duplicated (see GameList component). myColor also
106 const isMyTurn = (g) => {
dc821737 107 if (g.score != "*") return false;
feaf1bf7
BA
108 const myColor =
109 g.players[0].uid == this.st.user.id ||
110 g.players[0].sid == this.st.user.sid
111 ? "w"
112 : "b";
113 const rem = g.movesCount % 2;
114 return (
115 (rem == 0 && myColor == "w") ||
116 (rem == 1 && myColor == "b")
117 );
118 };
620a88ed 119 if (game.type == "live" || !isMyTurn(game)) {
feaf1bf7 120 this.$router.push("/game/" + game.id);
620a88ed
BA
121 return;
122 }
feaf1bf7
BA
123 // It's my turn in this game. Are there others?
124 let nextIds = "";
125 let otherCorrGamesMyTurn = this.corrGames.filter(
126 g => g.id != game.id && isMyTurn(g));
127 if (otherCorrGamesMyTurn.length > 0) {
128 nextIds += "/?next=[";
129 otherCorrGamesMyTurn.forEach(g => { nextIds += g.id + ","; });
130 // Remove last comma and close array:
131 nextIds = nextIds.slice(0, -1) + "]";
132 }
133 this.$router.push("/game/" + game.id + nextIds);
db1f1f9a 134 },
aae89b49
BA
135 abortGame: function(game) {
136 // Special "trans-pages" case: from MyGames to Game
137 // TODO: also for corr games? (It's less important)
138 if (game.type == "live") {
139 const oppsid =
140 game.players[0].sid == this.st.user.sid
141 ? game.players[1].sid
142 : game.players[0].sid;
143 this.conn.send(
144 JSON.stringify(
145 {
146 code: "mabort",
147 gid: game.id,
148 // NOTE: target might not be online
149 target: oppsid
150 }
151 )
152 );
153 }
154 else if (!game.deletedByWhite || !game.deletedByBlack) {
155 // Set score if game isn't deleted on server:
156 ajax(
157 "/games",
158 "PUT",
159 {
160 gid: game.id,
161 newObj: {
162 score: "?",
163 scoreMsg: getScoreMessage("?")
164 }
165 }
166 );
167 }
168 },
db1f1f9a
BA
169 socketMessageListener: function(msg) {
170 const data = JSON.parse(msg.data);
f9c36b2d 171 if (data.code == "changeturn") {
db1f1f9a
BA
172 let games = !!parseInt(data.gid)
173 ? this.corrGames
174 : this.liveGames;
175 // NOTE: new move itself is not received, because it wouldn't be used.
176 let g = games.find(g => g.id == data.gid);
177 this.$set(g, "movesCount", g.movesCount + 1);
23ecf008
BA
178 if (
179 (g.type == "live" && this.display == "corr") ||
180 (g.type == "corr" && this.display == "live")
181 ) {
182 document
183 .getElementById(g.type + "Games")
184 .classList.add("somethingnew");
185 }
db1f1f9a
BA
186 }
187 },
188 socketCloseListener: function() {
189 this.conn = new WebSocket(this.connexionString);
190 this.conn.addEventListener("message", this.socketMessageListener);
191 this.conn.addEventListener("close", this.socketCloseListener);
6808d7a1
BA
192 }
193 }
afd3240d
BA
194};
195</script>
2f258c37 196
e2590fa8 197<style lang="sass">
2f258c37
BA
198.active
199 color: #42a983
5fe7e71c
BA
200
201.tabbtn
202 background-color: #f9faee
e2590fa8
BA
203
204table.game-list
205 max-height: 100%
23ecf008
BA
206
207.somethingnew
208 background-color: #c5fefe !important
2f258c37 209</style>