import { checkChallenge } from "@/data/challengeCheck";
import { ArrayFun } from "@/utils/array";
import { ajax } from "@/utils/ajax";
+import { genRandString } 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"}));
};
const data = JSON.parse(msg.data);
switch (data.code)
{
+ // 0.2] Receive clients list (just socket IDs)
case "clients":
data.sockIds.forEach(sid => {
this.players.push({sid:sid, id:0, name:""});
// TODO: also receive "askchallenges", "askgames"
case "identify":
// 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 "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;
// 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;
p.sid = this.players[pIdx].sid;
}
}
- const finishAddChallenge = () => {
+ const finishAddChallenge = (cid) => {
+ chall.id = cid || "c" + genRandString();
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,
"POST",
- chall,
+ ,
response => {
chall.id = response.cid;
finishAddChallenge();