import { checkChallenge } from "@/data/challengeCheck";
import { ArrayFun } from "@/utils/array";
import { ajax } from "@/utils/ajax";
-import { genRandString } from "@/utils/alea";
+import { getRandString } from "@/utils/alea";
import GameList from "@/components/GameList.vue";
import ChallengeList from "@/components/ChallengeList.vue";
export default {
// 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 => {
-
- }
- );
+// 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 = () => {
- // 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)
{
if (!!error)
return alert(error);
// TODO: set FEN, set mainTime and increment ?!
-else //generate a FEN
- c.fen = V.GenRandInitFen();
+//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;
}
}
const finishAddChallenge = (cid) => {
- chall.id = cid || "c" + genRandString();
+ chall.id = cid || "c" + getRandString();
this.challenges.push(chall);
// Send challenge to peers
let challSock =
ajax(
"/challenges/" + this.newchallenge.vid,
"POST",
- ,
+ chall,
response => {
chall.id = response.cid;
finishAddChallenge();
vid integer,
nbPlayers integer,
fen varchar,
- mainTime integer,
- addTime integer,
+ timeControl varchar,
foreign key (uid) references Users(id),
foreign key (vid) references Variants(id)
);
--- Store informations about players who accept a challenge
+-- Store informations about players who (potentially) accept a challenge
create table WillPlay (
- cid integer,
+ yes boolean,
+ cid integer,
uid integer,
foreign key (cid) references Challenges(id),
foreign key (uid) references Users(id)
* vid: variant id (int)
* nbPlayers: integer
* fen: varchar (optional)
- * mainTime: integer
- * addTime: integer
+ * timeControl: string (3m+2s, 7d+1d ...)
*
* Structure table WillPlay:
* cid: ref challenge id
{
checkChallenge: function(c)
{
- const vid = parseInt(c.vid);
- if (isNaN(vid) || vid <= 0)
- return "Please select a variant";
+ if (!c.vid.match(/^[0-9]+$/))
+ return "Wrong variant ID";
- const mainTime = parseInt(c.mainTime);
- const increment = parseInt(c.increment);
- if (isNaN(mainTime) || mainTime <= 0)
- return "Main time should be strictly positive";
- if (isNaN(increment) || increment < 0)
- return "Increment must be positive";
+ if (!c.timeControl.match(/^[0-9dhms +]+$/))
+ return "Wrong characters in time control";
- // Basic alphanumeric check for players names
- let playerCount = 0;
- for (p of c.players)
- {
- if (p.name.length > 0)
- {
- if (!p.name.match(/^[\w]+$/))
- return "Wrong characters in players names";
- playerCount++;
- }
- }
+ if (!c.nbPlayers.match(/^[0-9]+$/))
+ return "Wrong number of players";
- if (playerCount > 0 && playerCount != c.nbPlayers-1)
- return "None, or all of the opponent names must be filled"
-
- // Just characters check on server:
- if (!c.fen.match(/^[a-zA-Z0-9, /-]*$/))
+ if (!c.fen.match(/^[a-zA-Z0-9, /-]+$/))
return "Bad FEN string";
},
- // fen cannot be undefined; TODO: generate fen on server instead
+ // fen cannot be undefined
create: function(c, cb)
{
db.serialize(function() {
let query =
"INSERT INTO Challenges " +
- "(added, uid, vid, nbPlayers, fen, mainTime, addTime) VALUES " +
+ "(added, uid, vid, nbPlayers, fen, timeControl) VALUES " +
"(" + Date.now() + "," + c.uid + "," + c.vid + "," + c.nbPlayers +
- ",'" + c.fen + "'," + c.mainTime + "," + c.increment + ")";
+ ",'" + c.fen + "'," + c.timeControl + ")";
db.run(query, err => {
if (!!err)
return cb(err);
db.get("SELECT last_insert_rowid() AS rowid", (err2,lastId) => {
- query =
+
+ // TODO: also insert "will play" "no" for other players ?
+ // willplay = "maybe" by default ?
+
+ query =
"INSERT INTO WillPlay VALUES " +
- "(" + lastId["rowid"] + "," + c.uid + ")";
+ "(true," + lastId["rowid"] + "," + c.uid + ")";
db.run(query, (err,ret) => {
cb(err, lastId); //all we need is the challenge ID
});
nbPlayers: challengeInfo.nbPlayers,
players: players, //currently in
fen: challengeInfo.fen,
- mainTime: challengeInfo.mainTime,
- increment: challengeInfo.addTime,
+ timeControl: challengeInfo.timeControl,
};
return cb(null, challenge);
});
let router = require("express").Router();
const access = require("../utils/access");
const ChallengeModel = require("../models/Challenge");
+const UserModel = require("../models/User"); //for name check
-router.post("/challenges/:vid([0-9]+)", access.logged, access.ajax, (req,res) => {
- const vid = req.params["vid"];
- // TODO: check data req.body.chall (
- const error = ChallengeModel.checkChallenge(chall);
- ChallengeModel.create(chall, (err,lastId) => {
+router.post("/challenges", access.logged, access.ajax, (req,res) => {
+ const error = ChallengeModel.checkChallenge(req.body.chall);
+ // TODO: treat "to" field separately (search users by name)
+ // --> replace "to" by an array of uid (in chall), then call:
+ ChallengeModel.create(req.body.chall, (err,lastId) => {
res.json(err || {cid: lastId["rowid"]});
});
});
wss.on("connection", (socket, req) => {
const query = getJsonFromUrl(req.url);
const sid = query["sid"];
- // Ignore duplicate connections (on the same live game that we play):
+ // Ignore duplicate connections (on the same live game that we play):
if (!!clients[sid])
return socket.send(JSON.stringify({code:"duplicate"}));
clients[sid] = socket;