//on charge les modules et on initialise les paramètres par défaut const express = require('express'); var app = express(); const server = require('http').Server(app);//https? const io = require('socket.io')(server); const bodyParser = require('body-parser');//changer payant ? var urlencodedParser = bodyParser.urlencoded({ extended: false, parameterLimit:10 });//changer const async = require('async'); /*const { Pool } = require('pg'); var connection = new Pool({ user: 'admin', host: '127.0.0.1', database: 'platoo', password: 'm1y2s3q4l5', port: 5432, })*/ // notice here I'm requiring my database adapter file // and not requiring node-postgres directly const connection = require('./db') //var sqlinjection = require('sql-injection');//necessaire ? var pattUrl =/^[0-9A-Za-z_-àéèêîïëù]+$/; var affTime=/[0-9]{2} [0-9]{4} [0-9]{2}:[0-9]{2}:[0-9]{2}/; //var HTnoeud =/^(?:[^']|(?:\\'))*(?:(?:[^\\]'(?:[^']|(?:\\'))*){2})*#NOEUDS(?:[^']|(?:\\'))*(?:(?:[^\\]'(?:[^']|(?:\\'))*){2})*$/g var HTnoeud =/(?<=^([^']|\\')*(('([^']|\\')*){2})*)#NOEUDS(?=([^']|\\')*(('([^']|\\')*){2})*$)/g /* /^ (?:[^']|(?=\\)')* (?: (?: (?!\\)' (?:[^']|(?=\\)')* ){2} )* #NOEUD (?:[^']|(?=\\)')* (?: (?: (?!\\)' (?:[^']|(?=\\)')* ){2} )* $/ */ /* / (?<=^ ([^']|\\')* ( ( ' ([^']|\\')* ){2} )* ) #NOEUD (?= ([^']|\\')* ( ( ' ([^']|\\')* ){2} )* $) /g */ //aync //ejs //icone //ent //socketio //const pool = new Pool({ /*const connection = new Pool({ user: 'admin', host: '127.0.0.1', database: 'platoopg', password: 'm1y2s3q4l5', port: 5432, })*/ var NBNOEUD=1024; function randomString(length, chars) { var mask = ''; if (chars.indexOf('a') > -1) mask += 'abcdefghijklmnopqrstuvwxyz'; if (chars.indexOf('A') > -1) mask += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; if (chars.indexOf('#') > -1) mask += '0123456789'; if (chars.indexOf('!') > -1) mask += '~`!@#$%^&*()_+-={}[]:";\'<>?,./|\\'; var result = ''; for (var i = length; i > 0; --i) result += mask[Math.floor(Math.random() * mask.length)]; return result; } //intersection of two arrys //stack overflow function intersect(a, b) { var t; if (b.length > a.length) t = b, b = a, a = t; // indexOf to loop over shorter return a.filter(function (e) { return b.indexOf(e) > -1; }); } //il s'agit d'une nouvelle méthode de socket. les socket ont été remplacé par this function executeSocket(variables={},needsData=true,errMess='',data={},FUN=function(resIP){}){ //console.log(data) if(this.nomPlatoo && typeof this.nomPlatoo==='string'){ this.nomPlatoo=this.nomPlatoo.substring(0,50); if(needsData){ var n=Object.keys(variables).length; var k = 0; var valide = (typeof data ==='object'); while((valide)&&(k0){ //results[0].length>0 & results[1].length>0 & results[2].length>0 FUN(resIP.rows); }else{ console.log('erreur de '+errMess+' : bad id_page'); } } }); }else{ console.log(errMess+'.data incorrect dans executeSocket'); this.emit('refresh'); } }else { this.emit('noname'); } } function checkValide(variables={},needsData=true,errMess='',data={}){ if(needsData){ var n=Object.keys(variables).length; var k = 0; var valide = (typeof data ==='object'); while((valide)&&(k0){ page_dontexist=false; } } } callback(null,jeu_ok,page_dontexist); }); }, function(jeu_ok,page_dontexist,callback){ if(page_dontexist && valide_jeu){ const query = { text:'SELECT nom_jeu FROM jeu WHERE nom_jeu = $1;', values:[req.body.jeu] }; connection.query(query,function(err,results){ if(err){ callback(err); }else{ if(results.rows.length===0){ jeu_ok=false; } callback(null,jeu_ok,page_dontexist); } }); }else if(!page_dontexist){ res.redirect('/p/'+req.body.url); callback(false); }else{ //texte à afficher sur la page: jeu pas valide //res.redirect('/accueil'); callback({stack:'jeu non valide'}); } }, function(jeu_ok,page_dontexist,callback){ //si ok: if(jeu_ok){ //on crée une page async.waterfall([ function(callback2){ var id_page=0; var id_jeu=0; connection.query('SELECT MAX(id_page)+1 AS id FROM page;',function(err,results){ if(err){ //console.log('ERROR :'+err.stack);//ATT //err.message=err.stack callback2(err); }else{ if(results.rows.length===0){ id_page=1; }else{ id_page=results.rows[0].id||1;//si null->id=1 } callback2(null,id_page,id_jeu); } }); }, function(id_page,id_jeu,callback2){ const query = { text:'SELECT id_jeu AS id FROM jeu WHERE nom_jeu = $1;', values:[req.body.jeu] }; connection.query(query,function(err,results){ if(err){ //console.log('ERROR :'+err.stack);//ATT //err.message=err.stack callback2(err); }else{ id_jeu=results.rows[0].id; callback2(null,id_page,id_jeu); } }); }, //On démarre une transaction, pour que tout soit ajouté d'un coup ou annulé ensemble function(id_page,id_jeu,callback2){ //https://node-postgres.com/features/transactions connection.connect((err, client, done) => { //gere l'erreur et le rollback const shouldAbort = err => { if (err) { console.error('Error in transaction', err.stack) client.query('ROLLBACK', err => { // release the client back to the pool done(); if (err) { console.error('Error rolling back client', err.stack) callback2(err); } }) } return !!err } //les instructions: on démarre l transaction client.query('BEGIN', err => { if (shouldAbort(err)) return //un waterfall pour faire toutes les requêtes async.waterfall([ function(callback3){ const query={ text:'INSERT INTO page (id_page,nom_page,id_jeu_depart,nombre_trashs,creating_date,noeud_courant) SELECT $1,$2,id_jeu,nombre_trashs,CURRENT_TIMESTAMP,1 FROM jeu WHERE id_jeu = $3; ', values:[id_page,req.body.url,id_jeu] }; client.query(query,function(err,results){ if(err){ callback3(err); }else{ //resTot.push(results); resTot=[results.rows]; data={id_page:id_page,id_jeu:id_jeu}; callback3(null,data,resTot); } }); }, function(data,resTot,callback3){ const query={ text:'INSERT INTO joueur_on_page (id_page,id_joueur,label_joueur,action_clic_onboard,action_scroll_onboard,clickable_board,click_board_create_type,click_board_move_type,click_board_move_bac) SELECT $1,'+'id_joueur,label_joueur,action_clic_onboard,action_scroll_onboard,clickable_board,click_board_create_type,click_board_move_type,click_board_move_bac FROM joueur_on_jeu WHERE id_jeu = $2; ', values:[data.id_page,data.id_jeu] }; queryChainedClient(client,query,callback3,data,resTot); }, function(data,resTot,callback3){ const query={ text:'INSERT INTO save_on_page (id_page,noeud_precedent,noeud_suivant,date,type_action,auteur,couleur,id_joueur) SELECT $1,0,1,CURRENT_TIMESTAMP,\'create\',NULL,NULL,1;', values:[data.id_page] }; queryChainedClient(client,query,callback3,data,resTot); }, function(data,resTot,callback3){ const query={ text:'INSERT INTO box_on_page (id_page,diese_box_on_page,num_box_on_page,id_pion,noeud_start) SELECT $1,diese_box_on_jeu,num_box_on_jeu,id_pion,1 FROM box_on_jeu WHERE id_jeu = $2; ', values:[data.id_page,data.id_jeu] }; queryChainedClient(client,query,callback3,data,resTot); }, function(data,resTot,callback3){ const query={ text:'INSERT INTO stop_box_on_page (id_page,num_box_on_page,noeud_stop) SELECT $1,num_box_on_jeu,0 FROM box_on_jeu WHERE id_jeu = $2; ', values:[data.id_page,data.id_jeu] }; queryChainedClient(client,query,callback3,data,resTot); }, function(data,resTot,callback3){ const query={ text:'INSERT INTO bac_on_page (id_page,diese_bac_on_page,num_bac_on_page,label_bac,noeud_start) SELECT $1,diese_bac_on_jeu,num_bac_on_jeu,label_bac,1 FROM bac_on_jeu WHERE id_jeu = $2; ', values:[data.id_page,data.id_jeu] }; queryChainedClient(client,query,callback3,data,resTot); }, function(data,resTot,callback3){ const query={ text:'INSERT INTO stop_bac_on_page (id_page,num_bac_on_page,noeud_stop) SELECT $1,num_bac_on_jeu,0 FROM bac_on_jeu WHERE id_jeu = $2; ', values:[data.id_page,data.id_jeu] }; queryChainedClient(client,query,callback3,data,resTot); }, function(data,resTot,callback3){ const query={ text:'INSERT INTO board_on_page (id_page,diese_board_on_page,num_board_on_page,id_board,noeud_start) SELECT $1,diese_board_on_jeu,num_board_on_jeu,id_board,1 FROM board_on_jeu WHERE id_jeu = $2; ', values:[data.id_page,data.id_jeu] }; queryChainedClient(client,query,callback3,data,resTot); }, function(data,resTot,callback3){ const query={ text:'INSERT INTO stop_board_on_page (id_page,num_board_on_page,noeud_stop) SELECT $1,num_board_on_jeu,0 FROM board_on_jeu WHERE id_jeu = $2; ', values:[data.id_page,data.id_jeu] }; queryChainedClient(client,query,callback3,data,resTot); }, function(data,resTot,callback3){ const query={ text:'INSERT INTO pion_on_bac_on_page (id_page,diese_bac_on_page,id_pion,nombre_pions,diese_pion_on_bac_on_page,num_pion_on_bac_on_page,noeud_start) SELECT $1,'+'diese_bac_on_jeu,id_pion,nombre_pions,diese_pion_on_bac_on_jeu,num_pion_on_bac_on_jeu,1 FROM pion_on_bac_on_jeu WHERE id_jeu = $2; ', values:[data.id_page,data.id_jeu] }; queryChainedClient(client,query,callback3,data,resTot); }, function(data,resTot,callback3){ const query={ text:'INSERT INTO stop_pion_on_bac_on_page (id_page,diese_bac_on_page,num_pion_on_bac_on_page,noeud_stop) SELECT $1,diese_bac_on_jeu,num_pion_on_bac_on_jeu,0 FROM pion_on_bac_on_jeu WHERE id_jeu = $2; ', values:[data.id_page,data.id_jeu] }; queryChainedClient(client,query,callback3,data,resTot); }, function(data,resTot,callback3){ const query={ text:'INSERT INTO pion_on_board_on_page (id_page,diese_board_on_page,id_pion,position_x,position_y,taille,diese_pion_on_board_on_page,num_pion_on_board_on_page,noeud_start) SELECT $1,'+'diese_board_on_jeu,id_pion,position_x,position_y,taille,diese_pion_on_board_on_jeu,num_pion_on_board_on_jeu,1 FROM pion_on_board_on_jeu WHERE id_jeu = $2; ', values:[data.id_page,data.id_jeu] }; queryChainedClient(client,query,callback3,data,resTot); }, function(data,resTot,callback3){ const query={ text:'INSERT INTO stop_pion_on_board_on_page (id_page,diese_board_on_page,num_pion_on_board_on_page,noeud_stop) SELECT $1,'+'diese_board_on_jeu,num_pion_on_board_on_jeu,0 FROM pion_on_board_on_jeu WHERE id_jeu = $2; ', values:[data.id_page,data.id_jeu] }; queryChainedClient(client,query,callback3,data,resTot); }, function(data,resTot,callback3){ const query={ text:'INSERT INTO carac_pions_page (id_page,id_pion,change_pion,diese_bac_on_page,taille) SELECT $1,'+'id_pion,change_pion,diese_bac_on_jeu,taille FROM carac_pions_jeu WHERE id_jeu = $2; ', values:[data.id_page,data.id_jeu] }; queryChainedClient(client,query,callback3,data,resTot); }, function(data,resTot,callback3){ const query={ text:'INSERT INTO carac_pions_joueur_on_page (id_page,id_joueur,id_pion,change_pion,diese_bac_on_page,taille) SELECT $1,id_joueur,id_pion,change_pion,diese_bac_on_jeu,taille FROM carac_pions_joueur_on_jeu WHERE id_jeu = $2;', values:[data.id_page,data.id_jeu] }; client.query(query,function(err,results){ if(err){ callback3(err); }else{ //resTot.push(results.rows); resTot.push(results.rows); callback3(null,resTot); } }); } ],function(err){ if(shouldAbort(err)){ return false; }else{ client.query('COMMIT', err => { if (err) { console.error('Error committing transaction', err.stack); callback2(err); }else{ res.redirect('/p/'+req.body.url); callback2(null); } done(); }); } }); }); }); } ], function(err) { if(err){ console.log('erreur dans la création de la page : ' + err.stack); //res.redirect('/accueil'); callback(err); }else{ callback(null); } } ); }else{ //texte à afficher sur la page : jeu n'existe pas //res.redirect('/accueil'); callback({stack:'le jeu n existe pas'}); } } ], function(err) { if(err){ console.log('erreur lors de la demande de connection: ' + err.stack); res.redirect('/accueil'); } } ); }else{ res.redirect('/accueil'); //message : pas bon formulaire (nom url ou check) } }) .get('/p/:pageid/',function(req,res){ //vérifier id -> necesaire ? remplacer " " par _ ? if(!pattUrl.test(req.params.pageid)){ res.writeHead(403,{'Content-Type': 'text/plain'}); res.write('Forbidden characters in URL'); res.end(); }else{ connection.connect((err, client, done) => { //gere l'erreur et le rollback const shouldAbort = err => { if (err) { client.query('ROLLBACK', err => { if (err) { console.log('erreur de suppression de la page : '+err.stack); } // release the client back to the pool done(); }) } return !!err } async.waterfall([ function(callback){ client.query('BEGIN',function(err){ if(err){ callback(err); }else{ callback(null); } }); }, function(callback){ const query={ text:'SELECT id_page,noeud_courant FROM page WHERE nom_page =$1;', values:[req.params.pageid] }; client.query(query,function(err,results_1){ if(err){ //console.log('erreur sql :'+err.stack); //res.redirect('/accueil');//passer le nom en paramètre? callback(err); }else{ if(results_1.rows.length>1){ //console.log('plusieurs pages'); //res.redirect('/accueil');//supprimer les pages ? callback('plusieurs pages'); }else if(results_1.rows.length==0){ //res.redirect('/accueil');//passer le nom en paramètre? callback('page absente'); }else{ //renvoyer la page à partir de la bdd //NoeudsBDD(results,errFun=function(res){res.redirect('/accueil')},dataErrFun=res, var id_page = results_1.rows[0].id_page; callback(null,id_page); } } }); }, function(id_page,callback){ const query={ text:'SELECT nombre_trashs,noeud_courant,creating_date FROM page WHERE id_page =$1; ', values:[id_page] }; client.query(query,function(err,results){ if(err){ callback(err); }else{ //resTot.push(results.rows); results_2=[results.rows]; callback(null,id_page,results_2); } }); }, function(id_page,results_2,callback){ const query={ text:'SELECT fa.label_fonction AS action_clic_onboard, '+ 'fb.label_fonction AS action_scroll_onboard, '+ 'fc.label_fonction AS clickable_board, '+ 'pa.nom_pion AS click_board_create_type, '+ 'pb.nom_pion AS click_board_move_type, '+ 'click_board_move_bac FROM joueur_on_page '+ 'JOIN bdd_fonctions fa ON action_clic_onboard = fa.id_fonction '+ 'JOIN bdd_fonctions fb ON action_scroll_onboard = fb.id_fonction '+ 'JOIN bdd_fonctions fc ON clickable_board = fc.id_fonction '+ 'JOIN pion pa ON click_board_create_type = pa.id_pion '+ 'JOIN pion pb ON click_board_move_type = pb.id_pion '+ 'WHERE id_joueur = 1 AND id_page =$1; ', values:[id_page] }; queryChainedClient(client,query,callback,id_page,results_2); }, function(id_page,results_2,callback){ const query={ text:'SELECT pa.nom_pion AS id_pion, pb.nom_pion AS change_pion, pa.svg_pion AS svg_pion, '+ 'c.diese_bac_on_page,c.taille FROM carac_pions_joueur_on_page c '+ 'JOIN pion pa ON c.id_pion = pa.id_pion '+ 'JOIN pion pb ON c.change_pion = pb.id_pion '+ 'WHERE c.id_joueur = 1 AND c.id_page =$1; ', values:[id_page] }; queryChainedClient(client,query,callback,id_page,results_2); }, function(id_page,results_2,callback){ const query={ text:'SELECT p.svg_pion,p.nom_pion,b.diese_box_on_page,p.directed,b.num_box_on_page,b.noeud_start,s.nstop noeud_stop FROM box_on_page b'+ ' JOIN pion p ON b.id_pion = p.id_pion'+ ' JOIN (SELECT ARRAY_AGG(noeud_stop) nstop,MAX(num_box_on_page) num,MAX(id_page) id FROM stop_box_on_page WHERE id_page =$1 GROUP BY num_box_on_page) AS s'+ ' ON b.id_page=s.id AND b.num_box_on_page=s.num'+ ' WHERE b.id_page =$1 ORDER BY b.diese_box_on_page; ', values:[id_page] }; queryChainedClient(client,query,callback,id_page,results_2); }, function(id_page,results_2,callback){ const query={ text:'SELECT diese_bac_on_page,num_bac_on_page,label_bac,noeud_start,s.nstop noeud_stop FROM bac_on_page'+ ' JOIN (SELECT ARRAY_AGG(noeud_stop) nstop,MAX(num_bac_on_page) num,MAX(id_page) id FROM stop_bac_on_page WHERE id_page =$1 GROUP BY num_bac_on_page) AS s'+ ' ON id_page=s.id AND num_bac_on_page=s.num'+ ' WHERE id_page =$1 ORDER BY diese_bac_on_page;', values:[id_page] }; queryChainedClient(client,query,callback,id_page,results_2); }, function(id_page,results_2,callback){ const query={ text:'SELECT pp.diese_bac_on_page,p.svg_pion,p.nom_pion,pp.nombre_pions,pp.diese_pion_on_bac_on_page,p.directed,pp.num_pion_on_bac_on_page,pp.noeud_start,s.nstop noeud_stop FROM pion_on_bac_on_page pp'+ ' JOIN pion p ON pp.id_pion = p.id_pion'+ ' JOIN (SELECT ARRAY_AGG(noeud_stop) nstop,MAX(num_pion_on_bac_on_page) num,MAX(diese_bac_on_page) diese,MAX(id_page) id FROM stop_pion_on_bac_on_page WHERE id_page =$1 GROUP BY num_pion_on_bac_on_page) AS s'+ ' ON pp.id_page=s.id AND pp.num_pion_on_bac_on_page=s.num AND pp.diese_bac_on_page=s.diese'+ ' WHERE pp.id_page = $1 ORDER BY pp.num_pion_on_bac_on_page; ', values:[id_page] }; queryChainedClient(client,query,callback,id_page,results_2); }, function(id_page,results_2,callback){ const query={ text:'SELECT bp.num_board_on_page,bp.diese_board_on_page,b.svg_board,b.nom_board,bp.noeud_start,s.nstop noeud_stop FROM board_on_page bp'+ ' JOIN board b ON bp.id_board = b.id_board'+ ' JOIN (SELECT ARRAY_AGG(noeud_stop) nstop,MAX(num_board_on_page) num,MAX(id_page) id FROM stop_board_on_page WHERE id_page =$1 GROUP BY num_board_on_page) AS s'+ ' ON bp.id_page=s.id AND bp.num_board_on_page=s.num'+ ' WHERE bp.id_page = $1 ORDER BY diese_board_on_page; ', values:[id_page] }; queryChainedClient(client,query,callback,id_page,results_2); }, function(id_page,results_2,callback){ const query={ text:'SELECT pp.diese_board_on_page,pp.diese_pion_on_board_on_page,pp.num_pion_on_board_on_page,p.nom_pion,p.svg_pion,pp.position_x,pp.position_y,pp.taille,p.directed,pp.noeud_start,s.nstop noeud_stop FROM pion_on_board_on_page pp'+ ' JOIN pion p ON pp.id_pion = p.id_pion'+ ' JOIN (SELECT ARRAY_AGG(noeud_stop) nstop,MAX(num_pion_on_board_on_page) num,MAX(diese_board_on_page) diese,MAX(id_page) id FROM stop_pion_on_board_on_page WHERE id_page = $1 GROUP BY num_pion_on_board_on_page) AS s'+ ' ON pp.id_page=s.id AND pp.num_pion_on_board_on_page=s.num AND pp.diese_board_on_page=s.diese'+ ' WHERE pp.id_page =$1 ORDER BY num_pion_on_board_on_page;', values:[id_page] }; queryChainedClient(client,query,callback,id_page,results_2); }, function(id_page,results_2,callback){ const query={ text:'SELECT id_joueur,label_joueur FROM joueur_on_jeu j JOIN page p ON j.id_jeu=p.id_jeu_depart WHERE id_page =$1 ORDER BY label_joueur; ', values:[id_page] }; queryChainedClient(client,query,callback,id_page,results_2); }, function(id_page,results_2,callback){ const query={ text:'SELECT DISTINCT id_board,nom_board FROM board ORDER BY nom_board; ', values:[] }; queryChainedClient(client,query,callback,id_page,results_2); }, function(id_page,results_2,callback){ const query={ text:'SELECT DISTINCT id_pion,nom_pion FROM pion ORDER BY nom_pion; ', values:[] }; queryChainedClient(client,query,callback,id_page,results_2); }, function(id_page,results_2,callback){ const query={ text:'SELECT time,message,auteur,couleur,id_message FROM chat_on_page WHERE id_page = $1 ORDER BY time DESC; ', values:[id_page] }; queryChainedClient(client,query,callback,id_page,results_2); }, function(id_page,results_2,callback){ const query={ text:'SELECT j.wiki AS wiki FROM page p JOIN jeu j ON p.id_jeu_depart=j.id_jeu;', values:[] }; queryChainedClient(client,query,callback,id_page,results_2); }, function(id_page,results_2,callback){ const query={ text:'SELECT s.noeud_precedent, s.noeud_suivant, s.date, s.auteur, s.couleur, j.label_joueur FROM save_on_page s JOIN page p ON s.id_page=p.id_page JOIN joueur_on_jeu j ON s.id_joueur = j.id_joueur AND p.id_jeu_depart=j.id_jeu WHERE s.id_page =$1;', values:[id_page] }; client.query(query,function(err,results){ if(err){ err.message=err.stack; callback(err); }else{ results_2.push(results.rows); callback(null,results_2); } }); }, function(results_2,callback){ client.query('COMMIT',function(err,results){ if(err){ err.message=err.stack; callback(err); }else{ callback(null,results_2); } }); } ], function(err,results_2){ if(shouldAbort(err)){ console.log('Erreur de chargement de la page depuis la BDD : '+err.stack); res.redirect('/accueil');//passer le nom en paramètre? }else{ done(); var noeuds=[results_2[0][0].noeud_courant]; var noeuds_suivants=[]; var noeuds_precedents=[]; for(var i=0; i< results_2[13].length;i++){ noeuds_suivants.push(results_2[13][i].noeud_suivant) noeuds_precedents.push(results_2[13][i].noeud_precedent) } var new_noeud=0; while((noeuds.indexOf(0)==-1) && (noeuds_suivants.indexOf(noeuds[noeuds.length-1])!=-1)){ new_noeud=noeuds_precedents[noeuds_suivants.indexOf(noeuds[noeuds.length-1])]; noeuds.push(new_noeud); } if(noeuds[noeuds.length-1]==0){ noeuds.pop(); } var params ={ objBoxes : [], objBoards : [], objBacs : [], NTrashs : 0, action_clic_onboardStr : '', action_scroll_onboardStr : '', clickable_boardStr : '', click_board_create_type : 0, click_board_move_type : 0, click_board_move_bac : '', tabPionListe : [], tabPionChange : [], tabPionMove : [], tabPionTaille : [], tabPionSvg : [], nomPlatoo : req.params.pageid, Joueurs : [], Boxes :[], Boards : [], Chat : [], Save : [], wiki:''//, // maxDiesePion : 0, // maxDieseBoard : 0, // maxDieseBac : 0, // maxDieseGroupe : 0, // maxDieseBox : 0 } params.NTrashs = results_2[0][0].nombre_trashs; params.noeud_courant = results_2[0][0].noeud_courant; params.action_clic_onboardStr = results_2[1][0].action_clic_onboard; params.action_scroll_onboardStr = results_2[1][0].action_scroll_onboard; params.clickable_boardStr = results_2[1][0].clickable_board; params.click_board_create_type = results_2[1][0].click_board_create_type; if(params.click_board_create_type === null){ params.click_board_create_type=-1; } params.click_board_move_type = results_2[1][0].click_board_move_type; if(params.click_board_move_type === null){ params.click_board_move_type=-1; } params.click_board_move_bac = results_2[1][0].click_board_move_bac; if(params.click_board_move_bac === null){ params.click_board_move_bac=-1; } params.tabPionListe = []; params.tabPionChange = []; params.tabPionMove = []; params.tabPionTaille = []; params.tabPionSvg = []; for(var i = 0; i < results_2[2].length; i++){ params.tabPionListe.push(results_2[2][i].id_pion); if(results_2[2][i].change_pion === null){ results_2[2][i].change_pion=-1; } params.tabPionChange.push(results_2[2][i].change_pion); if(results_2[2][i].diese_bac_on_page === null){ results_2[2][i].diese_bac_on_page=-1; } params.tabPionMove.push(results_2[2][i].diese_bac_on_page); params.tabPionTaille.push(results_2[2][i].taille); params.tabPionSvg.push(results_2[2][i].svg_pion); } params.objBoxes = []; var display =''; for(var i = 0; iparams.maxDieseBox){ // params.maxDieseBox = results_2[3][i].diese_box_on_page; // } } params.objBacs = []; var i=0; var c=0; while(i=params.objBacs.length){ n++; j = 0; if(n==2){ cont=false; } } } /*var noeuds_stop = results_2[5][i].noeud_stop.split(',').map(function (x) { return parseInt(x, 10); });*/ var noeuds_stop = results_2[5][i].noeud_stop; if( (noeuds.indexOf(results_2[5][i].noeud_start)!= -1) && (intersect(noeuds,noeuds_stop).length===0) ){ display='actif'; }else{ display='inactif'; } params.objBacs[j].groupes.push({ nom : results_2[5][i].nom_pion, svg : results_2[5][i].svg_pion, N : results_2[5][i].nombre_pions, diese : results_2[5][i].diese_pion_on_bac_on_page, num : results_2[5][i].num_pion_on_bac_on_page, directed : results_2[5][i].directed, noeud_start : results_2[5][i].noeud_start, noeud_stop : results_2[5][i].noeud_stop, display : display }); // if(results_2[5][i].diese_pion_on_bac_on_page>params.maxDieseGroupe){ // params.maxDieseGroupe = results_2[5][i].diese_pion_on_bac_on_page; // } } params.objBoards = []; var i=0; var c=0; while(i=params.objBoards.length){ n++; j = 0; if(n==2){ cont=false; } } } /*var noeuds_stop = results_2[7][i].noeud_stop.split(',').map(function (x) { return parseInt(x, 10); });*/ var noeuds_stop = results_2[7][i].noeud_stop; if( (noeuds.indexOf(results_2[7][i].noeud_start)!= -1) && (intersect(noeuds,noeuds_stop).length===0) ){ display='actif'; }else{ display='inactif'; } params.objBoards[j].pions.push({ nom : results_2[7][i].nom_pion, svg : results_2[7][i].svg_pion, x : results_2[7][i].position_x, y : results_2[7][i].position_y, height: results_2[7][i].taille, diese : results_2[7][i].diese_pion_on_board_on_page, num : results_2[7][i].num_pion_on_board_on_page, directed : results_2[7][i].directed, noeud_start : results_2[7][i].noeud_start, noeud_stop : results_2[7][i].noeud_stop, display : display }); } for(var i=0;i=new_y){ y[j]=y[j]+1; } } //on ajoute le nouveau y y.push(new_y) //nouvel indice new_y_ind=y.length-1; //on corrige l'epaisseur var noe = results_2[13][i].noeud_precedent; while(noe>0){ epaisseur[noeuds_sui.indexOf(noe)]+=1; noe=params.Save[noeuds_sui.indexOf(noe)].noeud_precedent; } epais=1; } params.Save.push({ noeud_precedent : results_2[13][i].noeud_precedent, noeud_suivant : results_2[13][i].noeud_suivant, date : results_2[13][i].date, auteur : results_2[13][i].auteur, couleur : results_2[13][i].couleur, label_joueur : results_2[13][i].label_joueur, x:params.Save[noeuds_sui.indexOf(results_2[13][i].noeud_precedent)].x+1, y:new_y_ind, ypre:y_pre_ind, xpre:params.Save[noeuds_sui.indexOf(results_2[13][i].noeud_precedent)].x }); noeuds_sui.push(results_2[13][i].noeud_suivant); noeuds_pre.push(results_2[13][i].noeud_precedent); epaisseur.push(epais); } params.SaveYmax=Math.max(...y); //on cherche xmax et on remplace les indices de y par les vraies valeurs xmax=0; for(var i = 0; ixmax){ xmax=params.Save[i].x } params.Save[i].y = y[params.Save[i].y] params.Save[i].ypre = y[params.Save[i].ypre] } params.SaveXmax=xmax; res.render('platoo.ejs',params); } }); }); } }) .use(function(req, res, next){ res.writeHead(404,{'Content-Type': 'text/plain'}); res.write('Page not found'); res.end(); }); //gérer les autres erreurs que 404 //faire une page 404 //on lance le WebSocket io.on('connection',function(socket){ //on ajoute la méthode executeSocket socket.executeSocket=executeSocket; //socket.runModifJeu=runModifJeu; //socket.runModifJeuWrap=runModifJeuWrap; socket.on('newplatoo',function(nomPlatoo){ //securiser nomPlatoo if(typeof nomPlatoo==='string' && pattUrl.test(nomPlatoo)){ nomPlatoo = nomPlatoo.substring(0,50); if(socket.nomPlatoo){ socket.leave(socket.nomPlatoo); } socket.nomPlatoo=nomPlatoo; socket.join(nomPlatoo); }else{ console.log("nomPlatoo incorrect"); socket.emit('refresh'); } }); //supprime la page //ok socket.on('delete',function(){ socket.executeSocket(variables={},needsData=false,errMess='delete',data={},FUN=function(resIP){ connection.connect((err, client, done) => { //gere l'erreur et le rollback const shouldAbort = err => { if (err) { console.error('erreur de suppression de la page', err.stack); client.query('ROLLBACK', err => { if (err) { console.log('erreur de suppression de la page : '+err.stack); } socket.emit('refresh'); // release the client back to the pool done(); }) } return !!err } //les instructions: on démarre l transaction client.query('BEGIN', err => { if (shouldAbort(err)) return //un waterfall pour faire toutes les requêtes async.waterfall([ function(callback){ const query={ text:'DELETE FROM stop_pion_on_bac_on_page WHERE id_page = $1 ; ', values:[resIP[0].id_page] }; client.query(query,function(err,results){ if(err){ callback(err); }else{ //resTot.push(results); resTot=[results.rows]; id_page=resIP[0].id_page; callback(null,id_page,resTot); } }); }, function(id_page,resTot,callback){ const query={ text:'DELETE FROM stop_pion_on_board_on_page WHERE id_page = $1 ; ', values:[id_page] }; queryChainedClient(client,query,callback,id_page,resTot); }, function(id_page,resTot,callback){ const query={ text:'DELETE FROM pion_on_bac_on_page WHERE id_page = $1 ; ', values:[id_page] }; queryChainedClient(client,query,callback,id_page,resTot); }, function(id_page,resTot,callback){ const query={ text:'DELETE FROM carac_pions_joueur_on_page WHERE id_page = $1 ; ', values:[id_page] }; queryChainedClient(client,query,callback,id_page,resTot); }, function(id_page,resTot,callback){ const query={ text:'DELETE FROM pion_on_board_on_page WHERE id_page = $1 ; ', values:[id_page] }; queryChainedClient(client,query,callback,id_page,resTot); }, function(id_page,resTot,callback){ const query={ text:'DELETE FROM carac_pions_page WHERE id_page = $1 ; ', values:[id_page] }; queryChainedClient(client,query,callback,id_page,resTot); }, function(id_page,resTot,callback){ const query={ text:'DELETE FROM stop_bac_on_page WHERE id_page = $1 ; ', values:[id_page] }; queryChainedClient(client,query,callback,id_page,resTot); }, function(id_page,resTot,callback){ const query={ text:'DELETE FROM bac_on_page WHERE id_page = $1 ; ', values:[id_page] }; queryChainedClient(client,query,callback,id_page,resTot); }, function(id_page,resTot,callback){ const query={ text:'DELETE FROM stop_box_on_page WHERE id_page = $1 ; ', values:[id_page] }; queryChainedClient(client,query,callback,id_page,resTot); }, function(id_page,resTot,callback){ const query={ text:'DELETE FROM box_on_page WHERE id_page = $1 ; ', values:[id_page] }; queryChainedClient(client,query,callback,id_page,resTot); }, function(id_page,resTot,callback){ const query={ text:'DELETE FROM stop_board_on_page WHERE id_page = $1 ; ', values:[id_page] }; queryChainedClient(client,query,callback,id_page,resTot); }, function(id_page,resTot,callback){ const query={ text:'DELETE FROM board_on_page WHERE id_page = $1 ; ', values:[id_page] }; queryChainedClient(client,query,callback,id_page,resTot); }, function(id_page,resTot,callback){ const query={ text:'DELETE FROM chat_on_page WHERE id_page = $1 ; ', values:[id_page] }; queryChainedClient(client,query,callback,id_page,resTot); }, function(id_page,resTot,callback){ const query={ text:'DELETE FROM save_on_page WHERE id_page = $1 ; ', values:[id_page] }; queryChainedClient(client,query,callback,id_page,resTot); }, function(id_page,resTot,callback){ const query={ text:'DELETE FROM joueur_on_page WHERE id_page = $1 ; ', values:[id_page] }; queryChainedClient(client,query,callback,id_page,resTot); }, function(id_page,resTot,callback){ const query={ text:'DELETE FROM page WHERE id_page = $1 ; ', values:[id_page] }; client.query(query,function(err,results){ if(err){ callback(err); }else{ //resTot.push(results.rows); resTot.push(results.rows); id_page=id_page; callback(null,resTot); } }); } ],function(err,resTot){ if(shouldAbort(err)){ //socket.emit('refresh'); }else{ client.query('COMMIT', err => { if (err) { console.log('erreur de suppression de la page : '+err.stack); socket.emit('refresh'); }else{ socket.broadcast.to(socket.nomPlatoo).emit('refresh'); socket.emit('refresh'); } done(); }); } }); }); }); }); }); //remise à zéro de la page //ok socket.on('raz',function(){ socket.executeSocket(variables={},needsData=false,errMess='RAZ',data={},FUN=function(resIP){ connection.connect((err, client, done) => { //gere l'erreur et le rollback const shouldAbort = err => { if (err) { console.error('erreur de remise à zéro ', err.stack) client.query('ROLLBACK', err => { if (err) { console.log('erreur de remise à zéro : '+err.stack); //socket.emit('refresh'); } socket.emit('refresh'); // release the client back to the pool done(); }) } return !!err } //les instructions: on démarre l transaction client.query('BEGIN', err => { if (shouldAbort(err)) return //un waterfall pour faire toutes les requêtes async.waterfall([ function(callback){ const query={ text:'DELETE FROM stop_pion_on_bac_on_page WHERE id_page = $1 ; ', values:[resIP[0].id_page] }; client.query(query,function(err,results){ if(err){ callback(err); }else{ //resTot.push(results.rows); resTot=[results.rows]; data={id_page:resIP[0].id_page,id_jeu_depart:resIP[0].id_jeu_depart} callback(null,data,resTot); } }); }, function(data,resTot,callback){ const query={ text:'DELETE FROM pion_on_bac_on_page WHERE id_page = $1 ; ', values:[data.id_page] }; queryChainedClient(client,query,callback,data,resTot); }, function(data,resTot,callback){ const query={ text:'DELETE FROM stop_pion_on_board_on_page WHERE id_page = $1 ; ', values:[data.id_page] }; queryChainedClient(client,query,callback,data,resTot); }, function(data,resTot,callback){ const query={ text:'DELETE FROM pion_on_board_on_page WHERE id_page = $1 ; ', values:[data.id_page] }; queryChainedClient(client,query,callback,data,resTot); }, function(data,resTot,callback){ const query={ text:'DELETE FROM carac_pions_joueur_on_page WHERE id_page = $1 ; ', values:[data.id_page] }; queryChainedClient(client,query,callback,data,resTot); }, function(data,resTot,callback){ const query={ text:'DELETE FROM carac_pions_page WHERE id_page = $1 ; ', values:[data.id_page] }; queryChainedClient(client,query,callback,data,resTot); }, function(data,resTot,callback){ const query={ text:'DELETE FROM stop_bac_on_page WHERE id_page = $1 ; ', values:[data.id_page] }; queryChainedClient(client,query,callback,data,resTot); }, function(data,resTot,callback){ const query={ text:'DELETE FROM bac_on_page WHERE id_page = $1 ; ', values:[data.id_page] }; queryChainedClient(client,query,callback,data,resTot); }, function(data,resTot,callback){ const query={ text:'DELETE FROM stop_box_on_page WHERE id_page = $1 ; ', values:[data.id_page] }; queryChainedClient(client,query,callback,data,resTot); }, function(data,resTot,callback){ const query={ text:'DELETE FROM box_on_page WHERE id_page = $1 ; ', values:[data.id_page] }; queryChainedClient(client,query,callback,data,resTot); }, function(data,resTot,callback){ const query={ text:'DELETE FROM stop_board_on_page WHERE id_page = $1 ; ', values:[data.id_page] }; queryChainedClient(client,query,callback,data,resTot); }, function(data,resTot,callback){ const query={ text:'DELETE FROM board_on_page WHERE id_page = $1 ; ', values:[data.id_page] }; queryChainedClient(client,query,callback,data,resTot); }, function(data,resTot,callback){ const query={ text:'DELETE FROM chat_on_page WHERE id_page = $1 ; ', values:[data.id_page] }; queryChainedClient(client,query,callback,data,resTot); }, function(data,resTot,callback){ const query={ text:'DELETE FROM save_on_page WHERE id_page = $1 ; ', values:[data.id_page] }; queryChainedClient(client,query,callback,data,resTot); }, function(data,resTot,callback){ const query={ text:'DELETE FROM joueur_on_page WHERE id_page = $1 ; ', values:[data.id_page] }; queryChainedClient(client,query,callback,data,resTot); }, function(data,resTot,callback){ const query={ text:'INSERT INTO joueur_on_page (id_page,id_joueur,label_joueur,action_clic_onboard,action_scroll_onboard,clickable_board,click_board_create_type,click_board_move_type,click_board_move_bac) SELECT $1,id_joueur,label_joueur,action_clic_onboard,action_scroll_onboard,clickable_board,click_board_create_type,click_board_move_type,click_board_move_bac FROM joueur_on_jeu WHERE id_jeu = $2; ', values:[data.id_page,data.id_jeu_depart] }; queryChainedClient(client,query,callback,data,resTot); }, function(data,resTot,callback){ const query={ text:'INSERT INTO save_on_page (id_page,noeud_precedent,noeud_suivant,date,type_action,auteur,couleur,id_joueur) SELECT $1,0,1,CURRENT_TIMESTAMP,\'create\',NULL,NULL,1;', values:[data.id_page] }; queryChainedClient(client,query,callback,data,resTot); }, function(data,resTot,callback){ const query={ text:'INSERT INTO box_on_page (id_page,diese_box_on_page,num_box_on_page,id_pion,noeud_start) SELECT $1,'+'diese_box_on_jeu,num_box_on_jeu,id_pion,1 FROM box_on_jeu WHERE id_jeu = $2; ', values:[data.id_page,data.id_jeu_depart] }; queryChainedClient(client,query,callback,data,resTot); }, function(data,resTot,callback){ const query={ text:'INSERT INTO stop_box_on_page (id_page,num_box_on_page,noeud_stop) SELECT $1,num_box_on_jeu,0 FROM box_on_jeu WHERE id_jeu = $2; ', values:[data.id_page,data.id_jeu_depart] }; queryChainedClient(client,query,callback,data,resTot); }, function(data,resTot,callback){ const query={ text:'INSERT INTO bac_on_page (id_page,diese_bac_on_page,num_bac_on_page,label_bac,noeud_start) SELECT $1,'+'diese_bac_on_jeu,num_bac_on_jeu,label_bac,1 FROM bac_on_jeu WHERE id_jeu = $2; ', values:[data.id_page,data.id_jeu_depart] }; queryChainedClient(client,query,callback,data,resTot); }, function(data,resTot,callback){ const query={ text:'INSERT INTO stop_bac_on_page (id_page,num_bac_on_page,noeud_stop) SELECT $1,num_bac_on_jeu,0 FROM bac_on_jeu WHERE id_jeu = $2; ', values:[data.id_page,data.id_jeu_depart] }; queryChainedClient(client,query,callback,data,resTot); }, function(data,resTot,callback){ const query={ text:'INSERT INTO board_on_page (id_page,diese_board_on_page,num_board_on_page,id_board,noeud_start) SELECT $1,diese_board_on_jeu,num_board_on_jeu,id_board,1 FROM board_on_jeu WHERE id_jeu = $2; ', values:[data.id_page,data.id_jeu_depart] }; queryChainedClient(client,query,callback,data,resTot); }, function(data,resTot,callback){ const query={ text:'INSERT INTO stop_board_on_page (id_page,num_board_on_page,noeud_stop) SELECT $1,num_board_on_jeu,0 FROM board_on_jeu WHERE id_jeu = $2; ', values:[data.id_page,data.id_jeu_depart] }; queryChainedClient(client,query,callback,data,resTot); }, function(data,resTot,callback){ const query={ text:'INSERT INTO pion_on_bac_on_page (id_page,diese_bac_on_page,id_pion,nombre_pions,diese_pion_on_bac_on_page,num_pion_on_bac_on_page,noeud_start) SELECT $1,'+'diese_bac_on_jeu,id_pion,nombre_pions,diese_pion_on_bac_on_jeu,num_pion_on_bac_on_jeu,1 FROM pion_on_bac_on_jeu WHERE id_jeu = $2; ', values:[data.id_page,data.id_jeu_depart] }; queryChainedClient(client,query,callback,data,resTot); }, function(data,resTot,callback){ const query={ text:'INSERT INTO stop_pion_on_bac_on_page (id_page,diese_bac_on_page,num_pion_on_bac_on_page,noeud_stop) SELECT $1,diese_bac_on_jeu,num_pion_on_bac_on_jeu,0 FROM pion_on_bac_on_jeu WHERE id_jeu = $2; ', values:[data.id_page,data.id_jeu_depart] }; queryChainedClient(client,query,callback,data,resTot); }, function(data,resTot,callback){ const query={ text:'INSERT INTO pion_on_board_on_page (id_page,diese_board_on_page,id_pion,position_x,position_y,taille,diese_pion_on_board_on_page,num_pion_on_board_on_page,noeud_start) SELECT $1,'+'diese_board_on_jeu,id_pion,position_x,position_y,taille,diese_pion_on_board_on_jeu,num_pion_on_board_on_jeu,1 FROM pion_on_board_on_jeu WHERE id_jeu = $2; ', values:[data.id_page,data.id_jeu_depart] }; queryChainedClient(client,query,callback,data,resTot); }, function(data,resTot,callback){ const query={ text:'INSERT INTO stop_pion_on_board_on_page (id_page,diese_board_on_page,num_pion_on_board_on_page,noeud_stop) SELECT $1,diese_board_on_jeu,num_pion_on_board_on_jeu,0 FROM pion_on_board_on_jeu WHERE id_jeu = $2; ', values:[data.id_page,data.id_jeu_depart] }; queryChainedClient(client,query,callback,data,resTot); }, function(data,resTot,callback){ const query={ text:'INSERT INTO carac_pions_page (id_page,id_pion,change_pion,diese_bac_on_page,taille) SELECT $1,id_pion,change_pion,diese_bac_on_jeu,taille FROM carac_pions_jeu WHERE id_jeu = $2; ', values:[data.id_page,data.id_jeu_depart] }; queryChainedClient(client,query,callback,data,resTot); }, function(data,resTot,callback){ const query={ text:'INSERT INTO carac_pions_joueur_on_page (id_page,id_joueur,id_pion,change_pion,diese_bac_on_page,taille) SELECT $1,id_joueur,id_pion,change_pion,diese_bac_on_jeu,taille FROM carac_pions_joueur_on_jeu WHERE id_jeu = $2;', values:[data.id_page,data.id_jeu_depart] }; queryChainedClient(client,query,callback,data,resTot); }, function(id_page,resTot,callback){ const query={ text:'UPDATE page SET noeud_courant=1 WHERE id_page = $1; ', values:[data.id_page] }; client.query(query,function(err,results){ if(err){ callback(err); }else{ //resTot.push(results.rows); resTot.push(results.rows); id_page=id_page; callback(null,resTot); } }); } ],function(err,resTot){ if(shouldAbort(err)){ //socket.emit('refresh'); }else{ client.query('COMMIT', err => { if (err) { console.log('erreur de remise à zéro : '+err.stack); socket.emit('refresh'); }else{ socket.broadcast.to(socket.nomPlatoo).emit('refresh'); socket.emit('refresh'); } done(); }); } }); }); }); }); }); //ok socket.on('getAction',function(data){ socket.executeSocket(variables={id_joueur:1},needsData=true,errMess='getAction',data=data,FUN=function(resIP){ var query ={ text: 'SELECT fa.label_fonction AS action_clic_onboard, fb.label_fonction AS action_scroll_onboard, fc.label_fonction AS clickable_board, '+ 'pa.nom_pion AS click_board_create_type, pb.nom_pion AS click_board_move_type, j.click_board_move_bac '+ 'FROM joueur_on_page j JOIN pion pa ON j.click_board_create_type=pa.id_pion JOIN pion pb ON j.click_board_move_type=pb.id_pion '+ 'JOIN bdd_fonctions fa ON j.action_clic_onboard = fa.id_fonction JOIN bdd_fonctions fb ON j.action_scroll_onboard = fb.id_fonction JOIN bdd_fonctions fc ON j.clickable_board = fc.id_fonction '+ 'WHERE id_page = $1 AND id_joueur = $2; ', values:[resIP[0].id_page,data.id_joueur] } connection.query(query,function(err,res2){ if(err){ console.log('erreur de getAction : '+err.stack); socket.emit('refresh'); }else{ var action_clic_onboard =res2.rows[0].action_clic_onboard; var action_scroll_onboard =res2.rows[0].action_scroll_onboard; var clickable_board =res2.rows[0].clickable_board; var click_board_create_type =res2.rows[0].click_board_create_type; var click_board_move_type =res2.rows[0].click_board_move_type; var click_board_move_bac =res2.rows[0].click_board_move_bac; socket.emit('setAction',{ action_clic_onboard:action_clic_onboard, action_scroll_onboard:action_scroll_onboard, clickable_board:clickable_board, click_board_create_type:click_board_create_type, click_board_move_type:click_board_move_type, click_board_move_bac:click_board_move_bac }); } }); }); }); //ok socket.on('chatNew',function(data){ socket.executeSocket(variables={message:'200', auteur:'20', couleur:'20'}, needsData=true,errMess='chat_new',data=data,FUN=function(resIP){ connection.connect((err, client, done) => { //gere l'erreur et le rollback const shouldAbort = err => { if (err) { console.log('erreur de chatNew : '+err.stack); socket.emit('refresh'); client.query('ROLLBACK', err => { if (err) { console.log('erreur de chatNew : '+err.stack); } // release the client back to the pool done(); }) } return !!err } //les instructions: on démarre l transaction client.query('BEGIN', err => { if (shouldAbort(err)) return //un waterfall pour faire toutes les requêtes async.waterfall([ function(callback){ const query={ text:'INSERT INTO chat_on_page (id_page,id_message,auteur,message,couleur,time) '+ 'SELECT $1,CASE WHEN MAX(id_message) IS NULL THEN 1 ELSE MAX(id_message)+1 END,'+ '$2,$3,$4,CURRENT_TIMESTAMP FROM chat_on_page WHERE id_page = $5; ', values:[resIP[0].id_page,data.auteur,data.message,data.couleur,resIP[0].id_page] }; client.query(query,function(err,results){ if(err){ callback(err); }else{ //resTot.push(results.rows); res2=[results.rows]; data={ auteur:data.auteur, couleur:data.couleur, message:data.message, id_page:resIP[0].id_page } callback(null,data,res2); } }); }, function(id_page,res2,callback){ const query={ text:'SELECT id_message,time FROM chat_on_page WHERE id_page = $1 ORDER BY id_message DESC LIMIT 1 ; ', values:[data.id_page] }; queryChainedClient(client,query,callback,data,res2); }, function(id_page,res2,callback){ const query={ text:'DELETE FROM chat_on_page WHERE id_page = $1 AND time <'+ '(SELECT c.time FROM (SELECT time FROM chat_on_page WHERE id_page = $2 ORDER BY time DESC LIMIT 1 OFFSET 19 ) AS c) ; ', values:[data.id_page,data.id_page] }; client.query(query,function(err,results){ if(err){ callback(err); }else{ res2.push(results.rows); //resTot=[results.rows]; callback(null,res2); } }); }, function(res2,callback){ client.query('COMMIT',function(err){ if(shouldAbort(err)) return callback(null,res2); }); } ],function(err,res2){ if(shouldAbort(err)){ //console.log('erreur cat on page'); //socket.emit('refresh'); }else if(res2[1].length>0){ var time = affTime.exec(res2[1][0].time); socket.emit('chatNew',{ message:data.message, id:res2[1][0].id_message, auteur:data.auteur, couleur:data.couleur, time:time }); socket.broadcast.to(socket.nomPlatoo).emit('chatNew',{ message:data.message, id:res2[1][0].id_message, auteur:data.auteur, couleur:data.couleur, time:time }); done(); }else{ console.log('erreur cat on page'); socket.emit('refresh'); done(); } }); }); }); }); }); //ok socket.on('getTabPion',function(data){ socket.executeSocket(variables={id_joueur:1},needsData=true,errMess='getTabPion',data=data,FUN=function(resIP){ const query={ text:'SELECT pa.nom_pion AS id_pion,pa.svg_pion AS svg_pion,pb.nom_pion AS change_pion,diese_bac_on_page,c.taille'+ ' AS taille FROM carac_pions_joueur_on_page c JOIN pion pa ON c.id_pion=pa.id_pion JOIN pion pb ON c.change_pion=pb.id_pion WHERE id_page = $1'+ ' AND id_joueur = $2;', values:[resIP[0].id_page,data.id_joueur] } connection.query(query, function(err,res2){ if(err){ console.log('erreur de getTabPion : '+err.stack); socket.emit('refresh'); }else{ var tabPionListe =[]; var tabPionChange =[]; var tabPionMove =[]; var tabPionTaille=[]; var tabPionSvg=[]; for(var i=0;i { //gere l'erreur et le rollback const shouldAbort = err => { if (err) { console.log('erreur de ModifJeu1 : '+err.stack); client.query('ROLLBACK', err => { if (err) { console.log('erreur de ModifJeu2 : '+err.stack); }else{ //console.log('rollbackok'); } socket.emit('refresh'); // release the client back to the pool done(); }) } return !!err } //un waterfall pour faire toutes les requêtes var Array_waterfall = [ function(callback){ //les instructions: on démarre l transaction client.query('BEGIN',function(err,results){ if(err){ callback(err); }else{ callback(null); } }); }, function(callback){ const query={ text:'SELECT noeud_precedent,noeud_suivant FROM save_on_page WHERE id_page = $1;', values:[resIP[0].id_page] }; client.query(query,function(err,results){ if(err){ callback(err); }else{ //resTot.push(results.rows); resNoeuds=[results.rows]; data={ auteur:dataTot.auteur, couleur:dataTot.couleur, message:dataTot.message, noeud_courant:dataTot.noeud_courant, id_joueur:dataTot.id_joueur, id_page:resIP[0].id_page } callback(null,data,resNoeuds); } }); }, function(data,resNoeuds,callback){ const query={ text:'INSERT INTO save_on_page (id_page,noeud_precedent,noeud_suivant,date,type_action,auteur,couleur,id_joueur) SELECT '+ '$1,$2,CASE WHEN MAX(noeud_suivant) IS NULL THEN 1 ELSE MAX(noeud_suivant)+1 END,CURRENT_TIMESTAMP,\'add_groupe\',$3,'+ '$4,$5 FROM save_on_page WHERE id_page=$1;', values:[data.id_page,data.noeud_courant,data.auteur,data.couleur,data.id_joueur] }; queryChainedClient(client,query,callback,data,resNoeuds); }, function(data,resNoeuds,callback){ const query={ text:'UPDATE page AS p SET noeud_courant = s.new_noeud'+ ' FROM (SELECT MAX(id_page) id_p,MAX(noeud_suivant) new_noeud FROM save_on_page GROUP BY id_page) AS s '+ ' WHERE p.id_page=s.id_p AND p.id_page = $1;', values:[data.id_page] }; queryChainedClient(client,query,callback,data,resNoeuds); }, function(data,resNoeuds,callback){ const query={ text:'SELECT noeud_courant AS new_noeud,CURRENT_TIMESTAMP AS date FROM page WHERE id_page=$1;', values:[data.id_page] }; client.query(query,function(err,results){ if(err){ callback(err); }else{ resNoeuds.push(results.rows); callback(null,resNoeuds); } }); }, function(resNoeuds,callback){ emit_broad.push({text:'newNoeud',data:{noeud:resNoeuds[3][0].new_noeud,save:{ noeud_precedent : resIP[0].noeud_courant, noeud_suivant : resNoeuds[3][0].new_noeud, date : resNoeuds[3][0].date, auteur : resIP[0].auteur, couleur : resIP[0].couleur, label_joueur : resIP[0].label_joueur, x:-1, y:-1, xpre:-1, ypre:-1 }}}); emit.push({text:'newNoeud',data:{noeud:resNoeuds[3][0].new_noeud,save:{ noeud_precedent : resIP[0].noeud_courant, noeud_suivant : resNoeuds[3][0].new_noeud, date : resNoeuds[3][0].date, auteur : resIP[0].auteur, couleur : resIP[0].couleur, label_joueur : resIP[0].label_joueur, x:-1, y:-1, xpre:-1, ypre:-1 }}}); //on calcule les noeuds à ajouter var noeuds=[resIP[0].noeud_courant]; var noeuds_suivants=[]; var noeuds_precedents=[]; for(var i=0; i< resNoeuds[0].length;i++){ noeuds_suivants.push(resNoeuds[0][i].noeud_suivant) } for(var i=0; i< resNoeuds[0].length;i++){ noeuds_precedents.push(resNoeuds[0][i].noeud_precedent) } while((noeuds.indexOf(0)==-1) && (noeuds_suivants.indexOf(noeuds[noeuds.length-1])!=-1)){ noeuds.push(noeuds_precedents[noeuds_suivants.indexOf(noeuds[noeuds.length-1])]); } //noeuds=noeuds.toString().replace(/,0/,''); //noeuds=noeuds.toString().replace(/^0,/,''); const index = noeuds.indexOf(0); if (index > -1) { noeuds.splice(index, 1); } /*noeudsObj = { noeuds:noeuds, toPostgres:function(value){ return noeuds.toString() } };*/ resNoeuds.push(noeuds); callback(null,resNoeuds); }]; //on parcours les différentes requetes qui sont englobées dans le mouvement var Abort = false; var emit = []; var emit_broad = []; var requetes_tot= []; var nb_requete = 0; for(var i=0;i