From: Benjamin Auder Date: Wed, 20 Feb 2019 13:37:13 +0000 (+0100) Subject: Separate timeControl logic into utils/timeControl X-Git-Url: https://git.auder.net/doc/%7B%7B%20asset%28%27mixstore/current/img/%3C?a=commitdiff_plain;h=dd75774d31a140cabf80790bdade9a40048c38d5;p=vchess.git Separate timeControl logic into utils/timeControl --- diff --git a/client/src/data/challengeCheck.js b/client/src/data/challengeCheck.js index f35eeb57..e0aa978c 100644 --- a/client/src/data/challengeCheck.js +++ b/client/src/data/challengeCheck.js @@ -1,52 +1,5 @@ -// 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); @@ -56,6 +9,8 @@ export function checkChallenge(c) 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; @@ -79,4 +34,6 @@ export function checkChallenge(c) if (!V.IsGoodFen(c.fen)) return "Bad FEN string"; } + else + c.fen = ""; } diff --git a/client/src/utils/timeControl.js b/client/src/utils/timeControl.js new file mode 100644 index 00000000..c4a22950 --- /dev/null +++ b/client/src/utils/timeControl.js @@ -0,0 +1,46 @@ +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}; +} diff --git a/client/src/views/Hall.vue b/client/src/views/Hall.vue index 85214d44..1dd72b41 100644 --- a/client/src/views/Hall.vue +++ b/client/src/views/Hall.vue @@ -165,25 +165,27 @@ export default { 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 @@ -193,11 +195,11 @@ export default { 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 @@ -326,24 +328,19 @@ export default { }, // 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( {}, @@ -351,7 +348,7 @@ export default { { 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: [ @@ -408,7 +405,7 @@ export default { } document.getElementById("modalNewgame").checked = false; }; - if (liveGame) + if (this.newchallenge.liveGame) { // Live challenges have cid = 0 finishAddChallenge(); diff --git a/server/sockets.js b/server/sockets.js index ae5cc7bd..09b21eaf 100644 --- a/server/sockets.js +++ b/server/sockets.js @@ -57,9 +57,9 @@ module.exports = function(wss) { 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( @@ -69,13 +69,14 @@ module.exports = function(wss) { 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;