-<!-- 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
// );
// 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;
- this.oldOnmessage = this.st.conn.onmessage || Function.prototype; //TODO: required here?
+ // 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 = () => {
switch (data.code)
{
// 0.2] Receive clients list (just socket IDs)
- case "clients":
+ 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}));
break;
+ case "askchallenge":
+ // Send my current live challenge
+ const cIdx = this.challenges
+ .findIndex(c => c.from.sid == this.st.user.sid && c.liveGame);
+ if (cIdx >= 0)
+ {
+ const c = this.challenges[cIdx];
+ const myChallenge =
+ {
+ // Minimal challenge informations: (from not required)
+ to: c.to,
+ fen: c.fen,
+ vid: c.vid,
+ timeControl: c.timeControl
+ };
+ this.st.conn.send(JSON.stringify({code:"challenge",
+ challenge:myChallenge, target:data.from})
+ }
+ break;
+ case "askgame":
+ // TODO: Send my current live game (if any): variant, players, movesCount
+ break;
case "identity":
if (data.user.id > 0) //otherwise "anonymous", nothing to retrieve
{
this.players[pIdx].name = data.user.name;
}
break;
+ case "challenge":
+ // Receive challenge from some player (+sid)
+ break;
+ case "game":
+ // Receive live game from some player (+sid)
+ 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
},
// 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");
- }
// 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
+ // checkChallenge side-effect = , 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 =
+ if (this.challenges.some(c => c.from.sid == this.st.user.sid && c.liveGame))
{
- id: 0, //unknown yet (no ID for live challenges)
- from: this.st.user,
- added: Date.now(),
- fen: this.newchallenge.fen,
+ document.getElementById("modalNewgame").checked = false;
+ return alert("You already have a pending live challenge");
+ // TODO: better to just replace current challenge
+ }
+ // Check that the players (if any indicated) are online
+ let chall = Object.Assign(
+ {},
+ this.newchallenge,
+ {
+ from: this.st.user,
+ added: Date.now(),
+ fen: this.newchallenge.fen || V.GenRandInitFen(),
variant: {id: this.newchallenge.vid, name: vname},
nbPlayers: this.newchallenge.nbPlayers,
to: [
{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)
{
}
document.getElementById("modalNewgame").checked = false;
};
- if (liveGame)
+ if (this.newchallenge.liveGame)
{
// Live challenges have cid = 0
finishAddChallenge();