-<!-- Main playing hall: online players + current challenges + button "new game" -->
-
<template lang="pug">
main
input#modalNewgame.modal(type="checkbox")
fieldset
label(for="selectNbPlayers") {{ st.tr["Number of players"] }}
select#selectNbPlayers(v-model="newchallenge.nbPlayers")
- option(v-show="possibleNbplayers(2)" value="2") 2
+ option(v-show="possibleNbplayers(2)" value="2" selected) 2
option(v-show="possibleNbplayers(3)" value="3") 3
option(v-show="possibleNbplayers(4)" value="4") 4
fieldset
fieldset(v-if="st.user.id > 0")
label(for="selectPlayers") {{ st.tr["Play with? (optional)"] }}
#selectPlayers
- input(type="text" v-model="newchallenge.to[0].name")
+ input(type="text" v-model="newchallenge.to[0]")
input(v-show="newchallenge.nbPlayers>=3" type="text"
- v-model="newchallenge.to[1].name")
+ v-model="newchallenge.to[1]")
input(v-show="newchallenge.nbPlayers==4" type="text"
- v-model="newchallenge.to[2].name")
+ v-model="newchallenge.to[2]")
fieldset(v-if="st.user.id > 0")
label(for="inputFen") {{ st.tr["FEN (optional)"] }}
input#inputFen(type="text" v-model="newchallenge.fen")
:challenges="challenges" @click-challenge="clickChallenge")
#players(v-show="cpdisplay=='players'")
h3 Online players
- div(v-for="p in uniquePlayers" @click="tryChallenge(p)")
+ .player(v-for="p in uniquePlayers" @click="tryChallenge(p)"
+ :class="{anonymous: !!p.count}"
+ )
| {{ p.name + (!!p.count ? " ("+p.count+")" : "") }}
.row
.col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
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 {
created: function() {
// Always add myself to players' list
this.players.push(this.st.user);
- // TODO: ask server for current corr games (all but mines: names, ID, time control)
- // also ask for corr challenges
- // Ask server for for room composition:
+ // 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.send(JSON.stringify({code:"pollclients"}));
};
this.st.conn.onopen = socketOpenListener;
+ // TODO: is this required here?
+ this.oldOnmessage = this.st.conn.onmessage || Function.prototype;
this.st.conn.onmessage = this.socketMessageListener;
+ const oldOnclose = this.st.conn.onclose;
const socketCloseListener = () => {
- // connexion is reinitialized in store.js
+ oldOnclose(); //reinitialize connexion (in store.js)
this.st.conn.addEventListener('message', this.socketMessageListener);
this.st.conn.addEventListener('close', 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 "clients":
+ // 0.2] Receive clients list (just socket IDs)
+ case "pollclients":
data.sockIds.forEach(sid => {
this.players.push({sid:sid, id:0, name:""});
+ // Ask identity, challenges and game(s)
this.st.conn.send(JSON.stringify({code:"askidentity", target:sid}));
+ this.st.conn.send(JSON.stringify({code:"askchallenges", target:sid}));
+ this.st.conn.send(JSON.stringify({code:"askgame", target:sid}));
});
break;
- // TODO: also receive "askchallenges", "askgames"
- case "identify":
+ case "askidentity":
// Request for identification
- this.st.conn.send(JSON.stringify({code:"identity", user:this.st.user, target:data.from}));
+ this.st.conn.send(JSON.stringify(
+ {code:"identity", user:this.st.user, target:data.from}));
+ break;
+ case "askchallenges":
+ // Send my current challenges
+ const myChallenges = this.challenges
+ .filter(c => c.from.sid == this.st.user.sid)
+ .map(c => {
+ // Minimal challenge informations: (from not required)
+ to: c.to,
+ fen: c.fen,
+ vid: c.vid,
+ timeControl: c.timeControl
+ });
+ if (myChallenges.length > 0)
+ {
+ this.st.conn.send(JSON.stringify({code:"challenges",
+ challenges:myChallenges, target:data.from})
+ }
+ break;
+ case "askgame":
+ // TODO: Send my current live game (if any)
break;
case "identity":
- if (data.user.id > 0)
+ 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;
+ case "challenges":
+ // Receive challenges from some player
+ break;
+ case "games":
+ // Receive live game from some player
+ 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":
tryChallenge: function(player) {
if (player.id == 0)
return; //anonymous players cannot be challenged
- this.newchallenge.players[0] = {
- name: player.name,
- id: player.id,
- sid: player.sid,
- };
+ this.newchallenge.to[0] = player.name;
doClick("modalNewgame");
},
// * - accept challenge (corr or live) --> send info to all concerned players
// * --> include challenge ID (so that opponents can delete the challenge too)
// * Also send to all connected players (only from me)
clickChallenge: function(challenge) {
+ // TODO: also correspondance case (send to server)
const index = this.challenges.findIndex(c => c.id == challenge.id);
- const toIdx = challenge.to.findIndex(p => p.id == user.id);
- const me = {name:user.name,id:user.id};
+ const toIdx = challenge.to.findIndex(name => name == this.st.user.name);
if (toIdx >= 0)
{
// It's a multiplayer challenge I accepted: withdraw
this.st.conn.send(JSON.stringify({code:"withdrawchallenge",
- cid:challenge.id, user:me}));
+ cid:challenge.id, user:this.st.user.sid}));
this.challenges.to.splice(toIdx, 1);
}
else if (challenge.from.id == user.id) //it's my challenge: cancel it
// 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))
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;
// Check that the players (if any indicated) are online
- let chall =
- {
- id: 0, //unknown yet (no ID for live challenges)
- from: this.st.user,
- added: Date.now(),
+ let chall = Object.Assign(
+ {},
+ this.newchallenge,
+ {
+ from: this.st.user,
+ added: Date.now(),
fen: this.newchallenge.fen,
variant: {id: this.newchallenge.vid, name: vname},
nbPlayers: this.newchallenge.nbPlayers,
{id: 0, name: this.newchallenge.to[2], sid: ""},
],
timeControl: this.newchallenge.timeControl,
- mainTime: this.newchallenge.mainTime,
- increment: this.newchallenge.increment,
};
for (let p of chall.to)
{
p.sid = this.players[pIdx].sid;
}
}
- const finishAddChallenge = () => {
+ const finishAddChallenge = (cid) => {
+ chall.id = cid || "c" + getRandString();
this.challenges.push(chall);
// Send challenge to peers
let challSock =
}
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,