| 1 | <template lang="pug"> |
| 2 | main |
| 3 | input#modalNewgame.modal(type="checkbox") |
| 4 | div(role="dialog" aria-labelledby="titleFenedit") |
| 5 | .card.smallpad |
| 6 | label#closeNewgame.modal-close(for="modalNewgame") |
| 7 | fieldset |
| 8 | label(for="selectVariant") {{ st.tr["Variant"] }} |
| 9 | select#selectVariant(v-model="newchallenge.vid") |
| 10 | option(v-for="v in st.variants" :value="v.id") {{ v.name }} |
| 11 | fieldset |
| 12 | label(for="timeControl") {{ st.tr["Time control"] }} |
| 13 | input#timeControl(type="text" v-model="newchallenge.timeControl" |
| 14 | placeholder="3m+2s, 1h+30s, 7d+1d ...") |
| 15 | fieldset(v-if="st.user.id > 0") |
| 16 | label(for="selectPlayers") {{ st.tr["Play with? (optional)"] }} |
| 17 | input#selectPlayers(type="text" v-model="newchallenge.to") |
| 18 | fieldset(v-if="st.user.id > 0") |
| 19 | label(for="inputFen") {{ st.tr["FEN (optional)"] }} |
| 20 | input#inputFen(type="text" v-model="newchallenge.fen") |
| 21 | button(@click="newChallenge") {{ st.tr["Send challenge"] }} |
| 22 | .row |
| 23 | .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2 |
| 24 | button(onClick="doClick('modalNewgame')") New game |
| 25 | .row |
| 26 | .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2 |
| 27 | .collapse |
| 28 | input#challengeSection(type="radio" checked aria-hidden="true" name="accordion") |
| 29 | label(for="challengeSection" aria-hidden="true") Challenges |
| 30 | div |
| 31 | .button-group |
| 32 | button(@click="cdisplay='live'") Live Challenges |
| 33 | button(@click="cdisplay='corr'") Correspondance challenges |
| 34 | ChallengeList(v-show="cdisplay=='live'" |
| 35 | :challenges="filterChallenges('live')" @click-challenge="clickChallenge") |
| 36 | ChallengeList(v-show="cdisplay=='corr'" |
| 37 | :challenges="filterChallenges('corr')" @click-challenge="clickChallenge") |
| 38 | input#peopleSection(type="radio" aria-hidden="true" name="accordion") |
| 39 | label(for="peopleSection" aria-hidden="true") People |
| 40 | div |
| 41 | .button-group |
| 42 | button(@click="pdisplay='players'") Players |
| 43 | button(@click="pdisplay='chat'") Chat |
| 44 | #players(v-show="pdisplay=='players'") |
| 45 | h3 Online players |
| 46 | .player(v-for="p in uniquePlayers" @click="tryChallenge(p)" |
| 47 | :class="{anonymous: !!p.count}" |
| 48 | ) |
| 49 | | {{ p.name + (!!p.count ? " ("+p.count+")" : "") }} |
| 50 | #chat(v-show="pdisplay=='chat'") |
| 51 | h3 Chat (TODO) |
| 52 | input#gameSection(type="radio" aria-hidden="true" name="accordion") |
| 53 | label(for="gameSection" aria-hidden="true") Games |
| 54 | div |
| 55 | .button-group |
| 56 | button(@click="gdisplay='live'") Live games |
| 57 | button(@click="gdisplay='corr'") Correspondance games |
| 58 | GameList(v-show="gdisplay=='live'" :games="filterGames('live')" |
| 59 | @show-game="showGame") |
| 60 | GameList(v-show="gdisplay=='corr'" :games="filterGames('corr')" |
| 61 | @show-game="showGame") |
| 62 | </template> |
| 63 | |
| 64 | <script> |
| 65 | import { store } from "@/store"; |
| 66 | import { checkChallenge } from "@/data/challengeCheck"; |
| 67 | import { ArrayFun } from "@/utils/array"; |
| 68 | import { ajax } from "@/utils/ajax"; |
| 69 | import { getRandString, shuffle } from "@/utils/alea"; |
| 70 | import GameList from "@/components/GameList.vue"; |
| 71 | import ChallengeList from "@/components/ChallengeList.vue"; |
| 72 | import { GameStorage } from "@/utils/gameStorage"; |
| 73 | import { extractTime } from "@/utils/timeControl"; |
| 74 | export default { |
| 75 | name: "my-hall", |
| 76 | components: { |
| 77 | GameList, |
| 78 | ChallengeList, |
| 79 | }, |
| 80 | data: function () { |
| 81 | return { |
| 82 | st: store.state, |
| 83 | cdisplay: "live", //or corr |
| 84 | pdisplay: "players", //or chat |
| 85 | gdisplay: "live", |
| 86 | games: [], |
| 87 | challenges: [], |
| 88 | people: [], //(all) online players |
| 89 | newchallenge: { |
| 90 | fen: "", |
| 91 | vid: 0, |
| 92 | to: "", //name of challenged player (if any) |
| 93 | timeControl: "", //"2m+2s" ...etc |
| 94 | }, |
| 95 | }; |
| 96 | }, |
| 97 | computed: { |
| 98 | uniquePlayers: function() { |
| 99 | // Show e.g. "@nonymous (5)", and do nothing on click on anonymous |
| 100 | let anonymous = {id:0, name:"@nonymous", count:0}; |
| 101 | let playerList = []; |
| 102 | this.people.forEach(p => { |
| 103 | if (p.id > 0) |
| 104 | playerList.push(p); |
| 105 | else |
| 106 | anonymous.count++; |
| 107 | }); |
| 108 | if (anonymous.count > 0) |
| 109 | playerList.push(anonymous); |
| 110 | return playerList; |
| 111 | }, |
| 112 | }, |
| 113 | created: function() { |
| 114 | // Always add myself to players' list |
| 115 | this.people.push(this.st.user); |
| 116 | // Retrieve live challenge (not older than 30 minute) if any: |
| 117 | const chall = JSON.parse(localStorage.getItem("challenge") || "false"); |
| 118 | if (!!chall) |
| 119 | { |
| 120 | if ((Date.now() - chall.added)/1000 <= 30*60) |
| 121 | this.challenges.push(chall); |
| 122 | else |
| 123 | localStorage.removeItem("challenge"); |
| 124 | } |
| 125 | if (this.st.user.id > 0) |
| 126 | { |
| 127 | // Ask server for current corr games (all but mines) |
| 128 | ajax( |
| 129 | "/games", |
| 130 | "GET", |
| 131 | {uid: this.st.user.id, excluded: true}, |
| 132 | response => { |
| 133 | this.games = this.games.concat(response.games.map(g => { |
| 134 | const tc = |
| 135 | return Object.assign({}, g, {mainT |
| 136 | }); |
| 137 | } |
| 138 | ); |
| 139 | // Also ask for corr challenges (open + sent to me) |
| 140 | ajax( |
| 141 | "/challenges", |
| 142 | "GET", |
| 143 | {uid: this.st.user.id}, |
| 144 | response => { |
| 145 | console.log(response.challenges); |
| 146 | // TODO: post-treatment on challenges ? |
| 147 | Array.prototype.push.apply(this.challenges, response.challenges); |
| 148 | } |
| 149 | ); |
| 150 | } |
| 151 | // 0.1] Ask server for room composition: |
| 152 | const funcPollClients = () => { |
| 153 | this.st.conn.send(JSON.stringify({code:"pollclients"})); |
| 154 | }; |
| 155 | if (!!this.st.conn && this.st.conn.readyState == 1) //1 == OPEN state |
| 156 | funcPollClients(); |
| 157 | else //socket not ready yet (initial loading) |
| 158 | this.st.conn.onopen = funcPollClients; |
| 159 | this.st.conn.onmessage = this.socketMessageListener; |
| 160 | const socketCloseListener = () => { |
| 161 | store.socketCloseListener(); //reinitialize connexion (in store.js) |
| 162 | this.st.conn.addEventListener('message', this.socketMessageListener); |
| 163 | this.st.conn.addEventListener('close', socketCloseListener); |
| 164 | }; |
| 165 | this.st.conn.onclose = socketCloseListener; |
| 166 | }, |
| 167 | methods: { |
| 168 | // Helpers: |
| 169 | filterChallenges: function(type) { |
| 170 | return this.challenges.filter(c => c.type == type); |
| 171 | }, |
| 172 | filterGames: function(type) { |
| 173 | return this.games.filter(c => c.type == type); |
| 174 | }, |
| 175 | classifyObject: function(o) { //challenge or game |
| 176 | // Heuristic: should work for most cases... (TODO) |
| 177 | return (o.timeControl.indexOf('d') === -1 ? "live" : "corr"); |
| 178 | }, |
| 179 | showGame: function(g) { |
| 180 | // NOTE: we are an observer, since only games I don't play are shown here |
| 181 | // ==> Moves sent by connected remote player(s) if live game |
| 182 | let url = "/game/" + g.id; |
| 183 | if (g.type == "live") |
| 184 | { |
| 185 | const remotes = g.players.filter(p => this.people.some(pl => pl.sid == p.sid)); |
| 186 | const rIdx = (remotes.length == 1 ? 0 : Math.floor(Math.random()*2)); |
| 187 | url += "?rid=" + remotes[rIdx].sid; |
| 188 | } |
| 189 | this.$router.push(url); |
| 190 | }, |
| 191 | // TODO: ...filter(...)[0].name, one-line, just remove this function |
| 192 | getVname: function(vid) { |
| 193 | const vIdx = this.st.variants.findIndex(v => v.id == vid); |
| 194 | return this.st.variants[vIdx].name; |
| 195 | }, |
| 196 | getSid: function(pname) { |
| 197 | const pIdx = this.people.findIndex(pl => pl.name == pname); |
| 198 | return (pIdx === -1 ? null : this.people[pIdx].sid); |
| 199 | }, |
| 200 | getPname: function(sid) { |
| 201 | const pIdx = this.people.findIndex(pl => pl.sid == sid); |
| 202 | return (pIdx === -1 ? null : this.people[pIdx].name); |
| 203 | }, |
| 204 | sendSomethingTo: function(to, code, obj, warnDisconnected) { |
| 205 | const doSend = (code, obj, sid) => { |
| 206 | this.st.conn.send(JSON.stringify(Object.assign( |
| 207 | {}, |
| 208 | {code: code}, |
| 209 | obj, |
| 210 | {target: sid} |
| 211 | ))); |
| 212 | }; |
| 213 | if (!!to) |
| 214 | { |
| 215 | // Challenge with targeted players |
| 216 | const targetSid = this.getSid(to); |
| 217 | if (!targetSid) |
| 218 | { |
| 219 | if (!!warnDisconnected) |
| 220 | alert("Warning: " + pname + " is not connected"); |
| 221 | } |
| 222 | else |
| 223 | doSend(code, obj, targetSid); |
| 224 | } |
| 225 | else |
| 226 | { |
| 227 | // Open challenge: send to all connected players (except us) |
| 228 | this.people.forEach(p => { |
| 229 | if (p.sid != this.st.user.sid) //only sid is always set |
| 230 | doSend(code, obj, p.sid); |
| 231 | }); |
| 232 | } |
| 233 | }, |
| 234 | // Messaging center: |
| 235 | socketMessageListener: function(msg) { |
| 236 | const data = JSON.parse(msg.data); |
| 237 | switch (data.code) |
| 238 | { |
| 239 | // 0.2] Receive clients list (just socket IDs) |
| 240 | case "pollclients": |
| 241 | { |
| 242 | data.sockIds.forEach(sid => { |
| 243 | this.people.push({sid:sid, id:0, name:""}); |
| 244 | // Ask identity, challenges and game(s) |
| 245 | this.st.conn.send(JSON.stringify({code:"askidentity", target:sid})); |
| 246 | this.st.conn.send(JSON.stringify({code:"askchallenge", target:sid})); |
| 247 | this.st.conn.send(JSON.stringify({code:"askgame", target:sid})); |
| 248 | }); |
| 249 | break; |
| 250 | } |
| 251 | case "askidentity": |
| 252 | { |
| 253 | // Request for identification: reply if I'm not anonymous |
| 254 | if (this.st.user.id > 0) |
| 255 | { |
| 256 | this.st.conn.send(JSON.stringify( |
| 257 | {code:"identity", user:this.st.user, target:data.from})); |
| 258 | } |
| 259 | break; |
| 260 | } |
| 261 | case "askchallenge": |
| 262 | { |
| 263 | // Send my current live challenge (if any) |
| 264 | const cIdx = this.challenges |
| 265 | .findIndex(c => c.from.sid == this.st.user.sid && c.type == "live"); |
| 266 | if (cIdx >= 0) |
| 267 | { |
| 268 | const c = this.challenges[cIdx]; |
| 269 | const myChallenge = |
| 270 | { |
| 271 | // Minimal challenge informations: (from not required) |
| 272 | id: c.id, |
| 273 | to: c.to, |
| 274 | fen: c.fen, |
| 275 | vid: c.vid, |
| 276 | timeControl: c.timeControl |
| 277 | }; |
| 278 | this.st.conn.send(JSON.stringify({code:"challenge", |
| 279 | chall:myChallenge, target:data.from})); |
| 280 | } |
| 281 | break; |
| 282 | } |
| 283 | case "askgame": |
| 284 | { |
| 285 | // Send my current live game (if any) |
| 286 | GameStorage.getCurrent((game) => { |
| 287 | if (!!game) |
| 288 | { |
| 289 | const myGame = |
| 290 | { |
| 291 | // Minimal game informations: |
| 292 | id: game.id, |
| 293 | players: game.players.map(p => p.name), |
| 294 | vname: game.vname, |
| 295 | timeControl: game.timeControl, |
| 296 | }; |
| 297 | this.st.conn.send(JSON.stringify({code:"game", |
| 298 | game:myGame, target:data.from})); |
| 299 | } |
| 300 | }); |
| 301 | break; |
| 302 | } |
| 303 | case "identity": |
| 304 | { |
| 305 | const pIdx = this.people.findIndex(p => p.sid == data.user.sid); |
| 306 | this.people[pIdx].id = data.user.id; |
| 307 | this.people[pIdx].name = data.user.name; |
| 308 | break; |
| 309 | } |
| 310 | case "challenge": |
| 311 | { |
| 312 | // Receive challenge from some player (+sid) |
| 313 | let newChall = data.chall; |
| 314 | newChall.type = this.classifyObject(data.chall); |
| 315 | const pIdx = this.people.findIndex(p => p.sid == data.from); |
| 316 | newChall.from = this.people[pIdx]; //may be anonymous |
| 317 | newChall.added = Date.now(); //TODO: this is reception timestamp, not creation |
| 318 | newChall.vname = this.getVname(newChall.vid); |
| 319 | this.challenges.push(newChall); |
| 320 | break; |
| 321 | } |
| 322 | case "game": |
| 323 | { |
| 324 | // Receive game from some player (+sid) |
| 325 | // NOTE: it may be correspondance (if newgame while we are connected) |
| 326 | if (!this.games.some(g => g.id == data.game.id)) //ignore duplicates |
| 327 | { |
| 328 | let newGame = data.game; |
| 329 | newGame.type = this.classifyObject(data.game); |
| 330 | newGame.rid = data.from; |
| 331 | newGame.score = "*"; |
| 332 | this.games.push(newGame); |
| 333 | } |
| 334 | break; |
| 335 | } |
| 336 | case "newgame": |
| 337 | { |
| 338 | // New game just started: data contain all information |
| 339 | if (data.gameInfo.type == "live") |
| 340 | { |
| 341 | this.startNewGame(data.gameInfo); |
| 342 | // TODO: redirect to game |
| 343 | } |
| 344 | else |
| 345 | { |
| 346 | // TODO: notify with game link but do not redirect |
| 347 | } |
| 348 | break; |
| 349 | } |
| 350 | case "refusechallenge": |
| 351 | { |
| 352 | alert(this.getPname(data.from) + " declined your challenge"); |
| 353 | ArrayFun.remove(this.challenges, c => c.id == data.cid); |
| 354 | break; |
| 355 | } |
| 356 | case "deletechallenge": |
| 357 | { |
| 358 | // NOTE: the challenge may be already removed |
| 359 | ArrayFun.remove(this.challenges, c => c.id == data.cid); |
| 360 | break; |
| 361 | } |
| 362 | case "connect": |
| 363 | { |
| 364 | this.people.push({name:"", id:0, sid:data.sid}); |
| 365 | this.st.conn.send(JSON.stringify({code:"askidentity", target:data.sid})); |
| 366 | this.st.conn.send(JSON.stringify({code:"askchallenge", target:data.sid})); |
| 367 | this.st.conn.send(JSON.stringify({code:"askgame", target:data.sid})); |
| 368 | break; |
| 369 | } |
| 370 | case "disconnect": |
| 371 | { |
| 372 | ArrayFun.remove(this.people, p => p.sid == data.sid); |
| 373 | // Also remove all challenges sent by this player: |
| 374 | ArrayFun.remove(this.challenges, c => c.from.sid == data.sid); |
| 375 | // And all live games where he plays and no other opponent is online |
| 376 | ArrayFun.remove(this.games, g => |
| 377 | g.type == "live" && (g.players.every(p => p.sid == data.sid |
| 378 | || !this.people.some(pl => pl.sid == p.sid))), "all"); |
| 379 | break; |
| 380 | } |
| 381 | } |
| 382 | }, |
| 383 | // Challenge lifecycle: |
| 384 | tryChallenge: function(player) { |
| 385 | if (player.id == 0) |
| 386 | return; //anonymous players cannot be challenged |
| 387 | this.newchallenge.to = player.name; |
| 388 | doClick("modalNewgame"); |
| 389 | }, |
| 390 | newChallenge: async function() { |
| 391 | const vname = this.getVname(this.newchallenge.vid); |
| 392 | const vModule = await import("@/variants/" + vname + ".js"); |
| 393 | window.V = vModule.VariantRules; |
| 394 | const error = checkChallenge(this.newchallenge); |
| 395 | if (!!error) |
| 396 | return alert(error); |
| 397 | const ctype = this.classifyObject(this.newchallenge); |
| 398 | // NOTE: "from" information is not required here |
| 399 | let chall = Object.assign({}, this.newchallenge); |
| 400 | const finishAddChallenge = (cid,warnDisconnected) => { |
| 401 | chall.id = cid || "c" + getRandString(); |
| 402 | // Send challenge to peers (if connected) |
| 403 | this.sendSomethingTo(chall.to, "challenge", {chall:chall}, !!warnDisconnected); |
| 404 | chall.added = Date.now(); |
| 405 | chall.type = ctype; |
| 406 | chall.vname = vname; |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | // TODO: vname and type are redundant (can be deduced from timeControl + vid) |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | chall.from = this.st.user; |
| 417 | this.challenges.push(chall); |
| 418 | localStorage.setItem("challenge", JSON.stringify(chall)); |
| 419 | document.getElementById("modalNewgame").checked = false; |
| 420 | }; |
| 421 | const cIdx = this.challenges.findIndex( |
| 422 | c => c.from.sid == this.st.user.sid && c.type == ctype); |
| 423 | if (cIdx >= 0) |
| 424 | { |
| 425 | // Delete current challenge (will be replaced now) |
| 426 | this.sendSomethingTo(this.challenges[cIdx].to, |
| 427 | "deletechallenge", {cid:this.challenges[cIdx].id}); |
| 428 | if (ctype == "corr") |
| 429 | { |
| 430 | ajax( |
| 431 | "/challenges", |
| 432 | "DELETE", |
| 433 | {id: this.challenges[cIdx].id} |
| 434 | ); |
| 435 | } |
| 436 | this.challenges.splice(cIdx, 1); |
| 437 | } |
| 438 | if (ctype == "live") |
| 439 | { |
| 440 | // Live challenges have a random ID |
| 441 | finishAddChallenge(null, "warnDisconnected"); |
| 442 | } |
| 443 | else |
| 444 | { |
| 445 | // Correspondance game: send challenge to server |
| 446 | ajax( |
| 447 | "/challenges", |
| 448 | "POST", |
| 449 | chall, |
| 450 | response => { finishAddChallenge(response.cid); } |
| 451 | ); |
| 452 | } |
| 453 | }, |
| 454 | clickChallenge: function(c) { |
| 455 | const myChallenge = (c.from.sid == this.st.user.sid //live |
| 456 | || (this.st.user.id > 0 && c.from.id == this.st.user.id)); //corr |
| 457 | if (!myChallenge) |
| 458 | { |
| 459 | c.accepted = true; |
| 460 | if (!!c.to) //c.to == this.st.user.name (connected) |
| 461 | { |
| 462 | // TODO: if special FEN, show diagram after loading variant |
| 463 | c.accepted = confirm("Accept challenge?"); |
| 464 | } |
| 465 | if (c.accepted) |
| 466 | { |
| 467 | c.seat = this.st.user; |
| 468 | this.launchGame(c); |
| 469 | } |
| 470 | else |
| 471 | { |
| 472 | this.st.conn.send(JSON.stringify({ |
| 473 | code: "refusechallenge", |
| 474 | cid: c.id, target: c.from.sid})); |
| 475 | } |
| 476 | } |
| 477 | else |
| 478 | localStorage.removeItem("challenge"); |
| 479 | // In all cases, the challenge is consumed: |
| 480 | ArrayFun.remove(this.challenges, ch => ch.id == c.id); |
| 481 | // NOTE: deletechallenge event might be redundant (but it's easier this way) |
| 482 | this.sendSomethingTo(c.to, "deletechallenge", {cid:c.id}); |
| 483 | if (c.type == "corr") |
| 484 | { |
| 485 | ajax( |
| 486 | "/challenges", |
| 487 | "DELETE", |
| 488 | {id: this.challenges[cIdx].id} |
| 489 | ); |
| 490 | } |
| 491 | }, |
| 492 | // NOTE: when launching game, the challenge is already deleted |
| 493 | launchGame: async function(c) { |
| 494 | const vname = this.getVname(c.vid); |
| 495 | const vModule = await import("@/variants/" + vname + ".js"); |
| 496 | window.V = vModule.VariantRules; |
| 497 | // These game informations will be sent to other players |
| 498 | const gameInfo = |
| 499 | { |
| 500 | gameId: getRandString(), |
| 501 | fen: c.fen || V.GenRandInitFen(), |
| 502 | players: shuffle([c.from, c.seat]), //white then black |
| 503 | vid: c.vid, |
| 504 | timeControl: tc.timeControl, |
| 505 | }; |
| 506 | this.st.conn.send(JSON.stringify({code:"newgame", |
| 507 | gameInfo:gameInfo, target:c.seat.sid})); |
| 508 | if (c.type == "live") |
| 509 | this.startNewGame(gameInfo); |
| 510 | else //corr: game only on server |
| 511 | { |
| 512 | ajax( |
| 513 | "/games", |
| 514 | "POST", |
| 515 | {gameInfo: gameInfo} |
| 516 | ); |
| 517 | } |
| 518 | }, |
| 519 | // NOTE: for live games only (corr games are launched on server) |
| 520 | startNewGame: function(gameInfo) { |
| 521 | // Extract times (in [milli]seconds), set clocks |
| 522 | const tc = extractTime(c.timeControl); |
| 523 | const game = Object.assign({}, gameInfo, { |
| 524 | // (other) Game infos: constant |
| 525 | fenStart: gameInfo.fen, |
| 526 | // Game state (including FEN): will be updated |
| 527 | moves: [], |
| 528 | clocks: [tc.mainTime, tc.mainTime], |
| 529 | initime: [Date.now(), 0], |
| 530 | score: "*", |
| 531 | }); |
| 532 | GameStorage.add(game); |
| 533 | if (this.st.settings.sound >= 1) |
| 534 | new Audio("/sounds/newgame.mp3").play().catch(err => {}); |
| 535 | // TODO: redirect to game |
| 536 | }, |
| 537 | }, |
| 538 | }; |
| 539 | </script> |
| 540 | |
| 541 | <style lang="sass"> |
| 542 | // TODO |
| 543 | </style> |