-// TODO: rename file "timeControl.js" in utils/
-function timeUnitToSeconds(value, unit)
-{
- let seconds = value;
- switch (unit)
- {
- case 'd':
- seconds *= 24;
- case 'h':
- seconds *= 60;
- case 'm':
- seconds *= 60;
- }
- return seconds;
-}
-
-function isLargerUnit(unit1, unit2)
-{
- return (unit1 == 'd' && unit2 != 'd')
- || (unit1 == 'h' && ['s','m'].includes(unit2))
- || (unit1 == 'm' && unit2 == 's');
-}
-
-export function extractTime(timeControl)
-{
- const tcParts = timeControl.replace(/ /g,"").split('+');
- const mainTimeArray = tcParts[0].match(/([0-9]+)([smhd])/);
- if (!mainTimeArray)
- return null;
- const mainTimeValue = parseInt(mainTimeArray[1]);
- const mainTimeUnit = mainTimeArray[2];
- const mainTime = timeUnitToSeconds(mainTimeValue, mainTimeUnit);
- let increment = 0;
- if (tcParts.length >= 2)
- {
- const increment = tcParts[1].match(/([0-9]+)([smhd])/);
- if (!increment)
- return null;
- const incrementValue = parseInt(increment[1]);
- const incrementUnit = increment[2];
- // Increment unit cannot be larger than main unit:
- if (isLargerUnit(incrementUnit, mainTimeUnit))
- return null;
- increment = timeUnitToSeconds(incrementValue, incrementUnit);
- }
- return {mainTime:mainTime, increment:increment};
-}
+import { extractTime } from "@/utils/timeControl";
-// TODO: put this in Hall.vue
export function checkChallenge(c)
{
const vid = parseInt(c.vid);
const tc = extractTime(c.timeControl);
if (!tc)
return "Wrong time control";
+ // Less than 3 days ==> live game (TODO: heuristic... 40 moves also)
+ c.liveGame = (tc.mainTime + 40 * tc.increment < 3*24*60*60);
// Basic alphanumeric check for players names
let playerCount = 0;
if (!V.IsGoodFen(c.fen))
return "Bad FEN string";
}
+ else
+ c.fen = "";
}
--- /dev/null
+function timeUnitToSeconds(value, unit)
+{
+ let seconds = value;
+ switch (unit)
+ {
+ case 'd':
+ seconds *= 24;
+ case 'h':
+ seconds *= 60;
+ case 'm':
+ seconds *= 60;
+ }
+ return seconds;
+}
+
+function isLargerUnit(unit1, unit2)
+{
+ return (unit1 == 'd' && unit2 != 'd')
+ || (unit1 == 'h' && ['s','m'].includes(unit2))
+ || (unit1 == 'm' && unit2 == 's');
+}
+
+export function extractTime(timeControl)
+{
+ const tcParts = timeControl.replace(/ /g,"").split('+');
+ const mainTimeArray = tcParts[0].match(/([0-9]+)([smhd])/);
+ if (!mainTimeArray)
+ return null;
+ const mainTimeValue = parseInt(mainTimeArray[1]);
+ const mainTimeUnit = mainTimeArray[2];
+ const mainTime = timeUnitToSeconds(mainTimeValue, mainTimeUnit);
+ let increment = 0;
+ if (tcParts.length >= 2)
+ {
+ const increment = tcParts[1].match(/([0-9]+)([smhd])/);
+ if (!increment)
+ return null;
+ const incrementValue = parseInt(increment[1]);
+ const incrementUnit = increment[2];
+ // Increment unit cannot be larger than main unit:
+ if (isLargerUnit(incrementUnit, mainTimeUnit))
+ return null;
+ increment = timeUnitToSeconds(incrementValue, incrementUnit);
+ }
+ return {mainTime:mainTime, increment:increment};
+}
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 => {
+ 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
- });
- if (myChallenges.length > 0)
- {
- this.st.conn.send(JSON.stringify({code:"challenges",
- challenges:myChallenges, target:data.from})
+ };
+ this.st.conn.send(JSON.stringify({code:"challenge",
+ challenge:myChallenge, target:data.from})
}
break;
case "askgame":
- // TODO: Send my current live game (if any)
+ // 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 "challenges":
- // Receive challenges from some player
+ case "challenge":
+ // Receive challenge from some player (+sid)
break;
- case "games":
- // Receive live game from some player
+ 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
},
// 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;
+ if (this.challenges.some(c => c.from.sid == this.st.user.sid && c.liveGame))
+ {
+ 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(
{},
{
from: this.st.user,
added: Date.now(),
- fen: this.newchallenge.fen,
+ fen: this.newchallenge.fen || V.GenRandInitFen(),
variant: {id: this.newchallenge.vid, name: vname},
nbPlayers: this.newchallenge.nbPlayers,
to: [
}
document.getElementById("modalNewgame").checked = false;
};
- if (liveGame)
+ if (this.newchallenge.liveGame)
{
// Live challenges have cid = 0
finishAddChallenge();
clients[obj.target].send(
JSON.stringify({code:"askidentity",from:sid}));
break;
- case "askchallenges":
+ case "askchallenge":
clients[obj.target].send(
- JSON.stringify({code:"askchallenges",from:sid}));
+ JSON.stringify({code:"askchallenge",from:sid}));
break;
case "askgame":
clients[obj.target].send(
clients[obj.target].send(
JSON.stringify({code:"identity",user:obj.user}));
break;
- case "askchallenges":
+ case "challenge":
+ // Relay challenge to other player
+ break;
+ case "game":
+ // Relay (live) game to other player
break;
case "newchallenge":
clients[obj.target].send(JSON.stringify({code:"newchallenge",chall:obj.chall}));
- case "askgames":
- // TODO: ask directly to people (webRTC)
- break;
case "newchat":
clients[obj.target].send(JSON.stringify({code:"newchat",msg:obj.msg}));
break;