Skip to content

Commit

Permalink
Some advances. TODO: test board.js, and then game.js, and then implem…
Browse files Browse the repository at this point in the history
…ent room.js
  • Loading branch information
Benjamin Auder committed Jan 12, 2019
1 parent 2305d34 commit fd08ab2
Show file tree
Hide file tree
Showing 17 changed files with 89 additions and 94 deletions.
5 changes: 5 additions & 0 deletions TODO
@@ -1,3 +1,8 @@
Sur index, introduction menu remplacé par "mes parties", montrant parties (corr) en cours toutes variantes confondues
Dans variant page, "mes parties" peut toujours contenir corr + importées (deux onglets)
En fin de partie (observée ou non), bouton "import game" en + de "download game" ==> directement dans indexedDB
--> sursis de 7 jours pour les parties par correspondance, qui sont encore chargées depuis le serveur

mat en 2 échiqueté : brnkr3/pppp1p1p/4ps2/8/2P2P2/P1qP4/2c1s1PP/R1K5
(Bb3+ Kb1 Ba2#)

Expand Down
8 changes: 3 additions & 5 deletions config/parameters.js.dist
@@ -1,4 +1,4 @@
const Parameters =
module.exports =
{
// For mail sending. NOTE: *no trailing slash*
siteURL: "http://localhost:3000",
Expand All @@ -7,12 +7,12 @@ const Parameters =
env: process.env.NODE_ENV || 'development',

// Lifespan of a (login) cookie
cookieExpire: 183*24*3600*1000, //6 months in milliseconds
cookieExpire: 183*24*60*60*1000, //6 months in milliseconds

// Characters in a login token, and period of validity (in milliseconds)
token: {
length: 16,
expire: 1000*60*30, //30 minutes in milliseconds
expire: 30*60*1000, //30 minutes in milliseconds
},

// Email settings
Expand All @@ -26,5 +26,3 @@ const Parameters =
contact: "some_contact_email",
},
};

module.exports = Parameters;
6 changes: 3 additions & 3 deletions db/create.sql
Expand Up @@ -59,11 +59,11 @@ create table Games (

-- Store informations about players in a corr game
create table Players (
gid integer,
uid integer,
color character,
gid integer,
foreign key (uid) references Users(id),
foreign key (gid) references Games(id)
foreign key (gid) references Games(id),
foreign key (uid) references Users(id)
);

create table Moves (
Expand Down
2 changes: 1 addition & 1 deletion models/Game.js
@@ -1 +1 @@
//TODO: at least this model (maybe MoveModel ?!)
//TODO:
2 changes: 1 addition & 1 deletion models/Problem.js
Expand Up @@ -65,7 +65,7 @@ exports.fetchN = function(vname, uid, type, directionStr, lastDt, MaxNbProblems,
"WHERE vid = (SELECT id FROM Variants WHERE name = '" + vname + "') " +
" AND added " + directionStr + " " + lastDt + " " + typeLine + " " +
"ORDER BY added " + (directionStr=="<" ? "DESC " : "") +
"LIMIT " + MaxNbProblems,
"LIMIT " + MaxNbProblems;
db.all(query, callback);
});
}
Expand Down
7 changes: 6 additions & 1 deletion public/javascripts/components/game.js
Expand Up @@ -4,7 +4,7 @@
// Game logic on a variant page: 3 modes, analyze, computer or human
Vue.component('my-game', {
// gameId: to find the game in storage (assumption: it exists)
props: ["gameId","mode","allowChat","allowMovelist"],
props: ["gameId","fen","mode","allowChat","allowMovelist"],
data: function() {
return {
// if oppid == "computer" then mode = "computer" (otherwise human)
Expand All @@ -24,6 +24,11 @@ Vue.component('my-game', {
vr: null, //VariantRules object, describing the game state + rules
};
},
watch: {
fen: function(newFen) {
this.vr = new VariantRules(newFen);
},
},
computed: {
showChat: function() {
return this.allowChat && this.mode=='human' && this.score != '*';
Expand Down
2 changes: 1 addition & 1 deletion public/javascripts/components/problems.js
Expand Up @@ -38,7 +38,7 @@ Vue.component('my-problems', {
{{ curProb.instructions }}
</p>
</div>
<my-board :fen="curProb.fen" :analyze:"true" .................> //TODO: use my-game in analyze mode ?
<my-game :fen="curProb.fen" :mode="analyze" :allowMovelist="true">
</my-board>
<div id="solution-div" class="section-content">
<h3 class="clickable" @click="showSolution = !showSolution">
Expand Down
4 changes: 2 additions & 2 deletions public/javascripts/index.js
Expand Up @@ -5,10 +5,10 @@ new Vue({
counts: {},
curPrefix: "",
conn: null,
display: "variants",
},
computed: {
sortedCounts: function () {
// TODO: priorité aux parties corr où c'est à nous de jouer !
const capitalizedPrefix = this.curPrefix.replace(/^\w/, c => c.toUpperCase());
const variantsCounts = variantArray
.filter( v => {
Expand Down Expand Up @@ -50,7 +50,7 @@ new Vue({
this.conn.onmessage = socketMessageListener;
this.conn.onclose = socketCloseListener;

// TODO:
// TODO: AJAX call get corr games (all variants)
// si dernier lastMove sur serveur n'est pas le mien et nextColor == moi, alors background orange
// ==> background orange si à moi de jouer par corr (sur main index)
// (helper: static fonction "GetNextCol()" dans base_rules.js)
Expand Down
1 change: 1 addition & 0 deletions public/javascripts/layout.js
Expand Up @@ -2,3 +2,4 @@
//à l'arrivée sur le site : set peerID (un identifiant unique en tout cas...) si pas trouvé
//
//TODO: si une partie en cours dans storage, rediriger vers cette partie
//(à condition que l'URL n'y corresponde pas déjà !)
7 changes: 0 additions & 7 deletions public/stylesheets/index.sass
Expand Up @@ -40,13 +40,6 @@
margin-top: 10px
font-size: 1em

#introductionMenu, #userMenu
float: right
@media screen and (max-width: 767px)
.info-container
p
margin-right: 5px

#flagMenu
float: right
margin-right: 10px
Expand Down
7 changes: 7 additions & 0 deletions public/stylesheets/layout.sass
Expand Up @@ -35,6 +35,13 @@ body
border-left: 1px solid var(--button-group-border-color)
border-top: 0

.right-menu
float: right
@media screen and (max-width: 767px)
.info-container
p
margin-right: 5px

#settings, #contactForm
max-width: 767px
@media screen and (max-width: 767px)
Expand Down
5 changes: 5 additions & 0 deletions reflexions
@@ -0,0 +1,5 @@
tell opponent that I got the move, for him to start timer (and lose...)
board2, board3, board4
VariantRules2, 3 et 4 aussi
fetch challenges and corr games from server at startup (room)
but forbid anonymous to start corr games or accept challenges
18 changes: 1 addition & 17 deletions routes/playing.js
Expand Up @@ -90,12 +90,8 @@ router.get("/gamesbyplayer", access.logged, access.ajax, (req,res) => {
});
});

// Load a rules page
router.get("/rules/:variant([a-zA-Z0-9]+)", access.ajax, (req,res) => {
res.render("rules/" + req.params["variant"]);
});

// TODO: if newmove fail, takeback in GUI // TODO: check move structure
// TODO: for corr games, move should contain an optional "message" field ("corr chat" !)
router.post("/moves", access.logged, access.ajax, (req,res) => {
let gid = ObjectId(req.body.gid);
let fen = req.body.fen;
Expand All @@ -111,16 +107,4 @@ router.post("/moves", access.logged, access.ajax, (req,res) => {
});
});

//TODO: if new chat fails, do not show chat message locally
router.post("/chats", access.logged, access.ajax, (req,res) => {
let gid = ObjectId(req.body.gid);
let uid = ObjectId(req.body.uid);
let msg = req.body.msg; //TODO: sanitize HTML (strip all tags...)
GameModel.addChat(gid, uid, msg, (err,game) => {
access.checkRequest(res, err, game, "Cannot find game", () => {
res.json({});
});
});
});

module.exports = router;
67 changes: 31 additions & 36 deletions utils/access.js
@@ -1,41 +1,36 @@
var Access = {};

// Prevent access to "users pages"
Access.logged = function(req, res, next)
module.exports =
{
if (req.userId == 0)
return res.redirect("/");
next();
};
// Prevent access to "users pages"
logged: function(req, res, next) {
if (req.userId == 0)
return res.redirect("/");
next();
},

// Prevent access to "anonymous pages"
Access.unlogged = function(req, res, next)
{
if (req.userId > 0)
return res.redirect("/");
next();
};
// Prevent access to "anonymous pages"
unlogged: function(req, res, next) {
if (req.userId > 0)
return res.redirect("/");
next();
},

// Prevent direct access to AJAX results
Access.ajax = function(req, res, next)
{
if (!req.xhr)
return res.json({errmsg: "Unauthorized access"});
next();
}
// Prevent direct access to AJAX results
ajax: function(req, res, next) {
if (!req.xhr)
return res.json({errmsg: "Unauthorized access"});
next();
},

// Check for errors before callback (continue page loading). TODO: better name.
Access.checkRequest = function(res, err, out, msg, cb)
{
if (!!err)
return res.json({errmsg: err.errmsg || err.toString()});
if (!out
|| (Array.isArray(out) && out.length == 0)
|| (typeof out === "object" && Object.keys(out).length == 0))
{
return res.json({errmsg: msg});
}
cb();
// Check for errors before callback (continue page loading). TODO: better name.
checkRequest: function(res, err, out, msg, cb) {
if (!!err)
return res.json({errmsg: err.errmsg || err.toString()});
if (!out
|| (Array.isArray(out) && out.length == 0)
|| (typeof out === "object" && Object.keys(out).length == 0))
{
return res.json({errmsg: msg});
}
cb();
},
}

module.exports = Access;
25 changes: 11 additions & 14 deletions utils/tokenGenerator.js
@@ -1,17 +1,14 @@
var TokenGen = {};

TokenGen.rand = function()
module.exports =
{
return Math.random().toString(36).substr(2); // remove `0.`
};
rand: function() {
return Math.random().toString(36).substr(2); // remove `0.`
},

TokenGen.generate = function(tlen)
{
var res = "";
var nbRands = Math.ceil(tlen/10); //10 = min length of a rand() string
for (var i = 0; i < nbRands; i++)
res += TokenGen.rand();
return res.substr(0, tlen);
generate: function(tlen) {
var res = "";
var nbRands = Math.ceil(tlen/10); //10 = min length of a rand() string
for (var i = 0; i < nbRands; i++)
res += TokenGen.rand();
return res.substr(0, tlen);
},
}

module.exports = TokenGen;
15 changes: 10 additions & 5 deletions views/index.pug
Expand Up @@ -14,24 +14,29 @@ block content
include welcome/fr
.row
#header.col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
#mainTitle
#mainTitle.clickable(onClick="doClick('modalWelcome')")
img(src="/images/index/unicorn.svg")
.info-container
p vchess.club
img(src="/images/index/wildebeest.svg")
#flagMenu.clickable(onClick="doClick('modalLang')")
img(src="/images/flags/" + lang + ".svg")
include userMenu
#introductionMenu.clickable(onClick="doClick('modalWelcome')")
#mygamesMenu.clickable.right-menu(v-show="display=='variants'" @click="display='games'")
.info-container
p Introduction
.row
p My games
#variantsMenu.clickable.right-menu(v-show="display=='games'" @click="display='variants'")
.info-container
p Variants
.row(v-show="display=='variants'")
.col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
label(for="prefixFilter") Type first letters...
input#prefixFilter(v-model="curPrefix")
.row
my-variant-summary(v-for="(v,idx) in sortedCounts"
v-bind:vobj="v" v-bind:index="idx" v-bind:key="v.name")
.row(v-show="display=='games'")
.col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
p TODO: load from server, show timeControl + players + link "play"

block javascripts
script.
Expand Down
2 changes: 1 addition & 1 deletion views/userMenu.pug
@@ -1,4 +1,4 @@
#userMenu.clickable(onClick="doClick('modalUser')")
#userMenu.clickable.right-menu(onClick="doClick('modalUser')")
.info-container
if !user.email
p
Expand Down

0 comments on commit fd08ab2

Please sign in to comment.