X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Fviews%2FHall.vue;h=0c991781df747d1b2d6cebfdcb04ac569eb1a7f7;hb=052d17ea6e199533cefb11f1ef51020b55cb1382;hp=2e17ceb0665de1a7a9b28f6e881444c6e670abb5;hpb=5578a7bfe37908a6c020f1c394504062e0ad72b5;p=vchess.git diff --git a/client/src/views/Hall.vue b/client/src/views/Hall.vue index 2e17ceb0..0c991781 100644 --- a/client/src/views/Hall.vue +++ b/client/src/views/Hall.vue @@ -63,6 +63,7 @@ import { NbPlayers } from "@/data/nbPlayers"; import { checkChallenge } from "@/data/challengeCheck"; import { ArrayFun } from "@/utils/array"; import { ajax } from "@/utils/ajax"; +import { getRandString } from "@/utils/alea"; import GameList from "@/components/GameList.vue"; import ChallengeList from "@/components/ChallengeList.vue"; export default { @@ -92,46 +93,83 @@ export default { computed: { uniquePlayers: function() { // Show e.g. "5 @nonymous", and do nothing on click on anonymous - let playerList = [{id:0, name:"@nonymous", count:0}]; + let anonymous = {id:0, name:"@nonymous", count:0}; + let playerList = []; this.players.forEach(p => { if (p.id > 0) playerList.push(p); else - playerList[0].count++; + anonymous.count++; }); + if (anonymous.count > 0) + playerList.push(anonymous); return playerList; }, }, - // TODO: this looks ugly... (use VueX ?!) - watch: { - "st.conn": function() { - this.st.conn.onmessage = this.socketMessageListener; - this.st.conn.onclose = this.socketCloseListener; - // Ask server for for room composition: - this.st.conn.send(JSON.stringify({code:"askplayers"})); - }, - }, created: function() { - // TODO: ask server for current corr games (all but mines: names, ID, time control) - // also ask for corr challenges - if (!!this.st.conn) - { - this.st.conn.onmessage = this.socketMessageListener; - this.st.conn.onclose = this.socketCloseListener; - // Ask server for for room composition: - this.st.conn.send(JSON.stringify({code:"askplayers"})); - } + // Always add myself to players' list + this.players.push(this.st.user); + // Ask server for current corr games (all but mines) +// ajax( +// "", +// "GET", +// response => { +// +// } +// ); +// // Also ask for corr challenges (all) +// ajax( +// "", +// "GET", +// response => { +// +// } +// ); + // 0.1] Ask server for for room composition: + const socketOpenListener = () => { + this.st.conn.send(JSON.stringify({code:"askclients"})); + }; + this.st.conn.onopen = socketOpenListener; + this.oldOnmessage = this.st.conn.onmessage || Function.prototype; //TODO: required here? + this.st.conn.onmessage = this.socketMessageListener; + const oldOnclose = this.st.conn.onclose; + const socketCloseListener = () => { + oldOnclose(); //reinitialize connexion (in store.js) + this.st.conn.addEventListener('message', this.socketMessageListener); + this.st.conn.addEventListener('close', socketCloseListener); + }; + this.st.conn.onclose = socketCloseListener; }, methods: { socketMessageListener: function(msg) { + // Save and call current st.conn.onmessage if one was already defined + // --> also needed in future Game.vue (also in Chat.vue component) + // TODO: merge Game.vue and MoveList.vue (one logic entity, no ?) + this.oldOnmessage(msg); const data = JSON.parse(msg.data); switch (data.code) { - case "room": - // TODO: receive room composition (sids at least, id + names if registered) - // Add myself to players - this.players.push(this.st.user); - // TODO: also receive "askchallenges", "askgames" + // 0.2] Receive clients list (just socket IDs) + case "clients": + data.sockIds.forEach(sid => { + this.players.push({sid:sid, id:0, name:""}); + this.st.conn.send(JSON.stringify({code:"askidentity", target:sid})); + }); + break; + // TODO: also receive "askchallenges", "askgames" + case "identify": + // Request for identification + this.st.conn.send(JSON.stringify( + {code:"identity", user:this.st.user, target:data.from})); + break; + case "identity": + if (data.user.id > 0) //otherwise "anonymous", nothing to retrieve + { + const pIdx = this.players.findIndex(p => p.sid == data.user.sid); + this.players[pIdx].id = data.user.id; + this.players[pIdx].name = data.user.name; + } + break; // * - receive "new game": if live, store locally + redirect to game // * If corr: notify "new game has started", give link, but do not redirect case "newgame": @@ -154,6 +192,7 @@ export default { // * - receive new challenge: if targeted, replace our name with sender name case "newchallenge": // receive live or corr challenge + this.challenges.push(data.chall); break; // * - receive "accept/withdraw/cancel challenge": apply action to challenges list case "acceptchallenge": @@ -175,23 +214,19 @@ export default { // * - receive "player connect": send all our current challenges (to him or global) // * Also send all our games (live - max 1 - and corr) [in web worker ?] // * + all our sent challenges. - this.players.push({name:data.name, id:data.uid}); + this.players.push({name:"", id:0, sid:data.sid}); + this.st.conn.send(JSON.stringify({code:"askidentity", target:data.sid})); // TODO: si on est en train de jouer une partie, le notifier au nouveau connecté // envoyer aussi nos défis break; // * - receive "player disconnect": remove from players list case "disconnect": - ArrayFun.remove(this.players, p => p.id == data.uid); + ArrayFun.remove(this.players, p => p.sid == data.sid); // TODO: also remove all challenges sent by this player, // and all live games where he plays and no other opponent is online break; } }, - socketCloseListener: function() { - // connexion is reinitialized in store.js - this.st.conn.addEventListener('message', this.socketMessageListener); - this.st.conn.addEventListener('close', this.socketCloseListener); - }, showGame: function(game) { // NOTE: if we are an observer, the game will be found in main games list // (sent by connected remote players) @@ -256,6 +291,14 @@ export default { // if (this.settings.sound >= 1) // new Audio("/sounds/newgame.mp3").play().catch(err => {}); // }, + // Load a variant file (TODO: should probably be global) + loadVariant: async function(vid, variantArray) { + const idxInVariants = variantArray.findIndex(v => v.id == vid); + const vname = variantArray[idxInVariants].name; + const vModule = await import("@/variants/" + vname + ".js"); + window.V = vModule.VariantRules; + return vname; + }, // Send new challenge (corr or live, cf. time control), with button or click on player newChallenge: async function() { if (this.challenges.some(c => c.from.sid == this.st.user.sid)) @@ -263,15 +306,16 @@ export default { document.getElementById("modalNewgame").checked = false; return alert("You already have a pending challenge"); } - const idxInVariants = - this.st.variants.findIndex(v => v.id == this.newchallenge.vid); - const vname = this.st.variants[idxInVariants].name; - const vModule = await import("@/variants/" + vname + ".js"); - window.V = vModule.VariantRules; + // TODO: put this "load variant" block elsewhere + const vname = this.loadVariant(this.newchallenge.vid, this.st.variants); // checkChallenge side-effect = set FEN, and mainTime + increment in seconds + // TODO: should not be a side-effect but set here ; for received server challenges we do not have mainTime+increment const error = checkChallenge(this.newchallenge); if (!!error) return alert(error); +// TODO: set FEN, set mainTime and increment ?! +//else //generate a FEN +// c.fen = V.GenRandInitFen(); // Less than 3 days ==> live game (TODO: heuristic... 40 moves also) const liveGame = this.newchallenge.mainTime + 40 * this.newchallenge.increment < 3*24*60*60; @@ -308,35 +352,34 @@ export default { p.sid = this.players[pIdx].sid; } } - const finishAddChallenge = () => { + const finishAddChallenge = (cid) => { + chall.id = cid || "c" + getRandString(); this.challenges.push(chall); // Send challenge to peers - const challSock = + let challSock = { code: "newchallenge", chall: chall, + target: "", + }; + const sendChallengeTo = (sid) => { + challSock.target = sid; + this.st.conn.send(JSON.stringify(challSock)); }; if (chall.to[0].id > 0) { // Challenge with targeted players chall.to.forEach(p => { if (p.id > 0) - { - this.st.conn.send(JSON.stringify(Object.assign( - {}, - challSock, - {receiver: p.sid} - ))); - } + sendChallengeTo(p.sid); }); } else { // Open challenge: send to all connected players (except us) - const strChallSock = JSON.stringify(challSock); this.players.forEach(p => { if (p.sid != this.st.user.sid) //only sid is always set - this.st.conn.send(strChallSock); + sendChallengeTo(p.sid); }); } document.getElementById("modalNewgame").checked = false; @@ -348,6 +391,14 @@ export default { } else { + const chall = { + uid: req.body["from"], + vid: req.body["vid"], + fen: req.body["fen"], + timeControl: req.body["timeControl"], + nbPlayers: req.body["nbPlayers"], + to: req.body["to"], //array of IDs + }; // Correspondance game: send challenge to server ajax( "/challenges/" + this.newchallenge.vid,