2020-05-19 17:14:33 +00:00
//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' ) ;
/ * c o n s t { P o o l } = r e q u i r e ( ' p g ' ) ;
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({
/ * c o n s t c o n n e c t i o n = n e w P o o l ( {
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 ) && ( k < n ) ) {
valide = valide && ( ( typeof data [ Object . keys ( variables ) [ k ] ] ) === typeof variables [ Object . keys ( variables ) [ k ] ] ) ;
if ( ( typeof data [ Object . keys ( variables ) [ k ] ] ) === 'string' ) {
data [ Object . keys ( variables ) [ k ] ] . substring ( 0 , parseInt ( variables [ Object . keys ( variables ) [ k ] ] ) ) ;
}
k += 1 ;
}
} else {
var valide = true ;
}
if ( valide ) {
var refresh = function ( ) {
this . emit ( 'refresh' ) ;
}
const query = {
2020-05-23 08:44:15 +00:00
text : 'SELECT id_page,noeud_courant,id_jeu_depart FROM page WHERE nom_page = $1;' ,
2020-05-19 17:14:33 +00:00
values : [ this . nomPlatoo ] ,
}
connection . query ( query , function ( err , resIP ) {
if ( err ) {
console . log ( 'erreur de ' + errMess + ' : ' + err . stack ) ;
} else {
if ( resIP . rows . length > 0 ) {
//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 ) && ( k < n ) ) {
valide = valide && ( ( typeof data [ Object . keys ( variables ) [ k ] ] ) === typeof variables [ Object . keys ( variables ) [ k ] ] ) ;
if ( ( typeof data [ Object . keys ( variables ) [ k ] ] ) === 'string' ) {
data [ Object . keys ( variables ) [ k ] ] . substring ( 0 , parseInt ( variables [ Object . keys ( variables ) [ k ] ] ) ) ;
}
k += 1 ;
}
} else {
var valide = true ;
}
return valide
}
/ * f u n c t i o n N o e u d s B D D ( r e s I P , e r r F u n , d a t a E r r F u n , q u e r y S t r i n g , F U N , e r r M e s s = ' E r r e u r i n N o e u d s B D D : ' ) {
const query = {
text : 'SELECT noeud_precedent,noeud_suivant FROM save_on_page WHERE id_page = $1;' ,
values : [ resIP [ 0 ] . id _page ] ,
}
connection . query ( query , function ( errlinks , reslinks ) {
if ( errlinks ) {
console . log ( errMess + errlinks . stack ) ;
//res.redirect('/accueil');//passer le nom en paramètre?
errFun ( dataErrFun ) ;
return false ;
} else {
var noeuds = [ resIP [ 0 ] . noeud _courant ] ;
var noeuds _suivants = [ ] ;
var noeuds _precedents = [ ] ;
for ( var i = 0 ; i < reslinks . rows . length ; i ++ ) {
noeuds _suivants . push ( reslinks . rows [ i ] . noeud _suivant )
}
for ( var i = 0 ; i < reslinks . rows . length ; i ++ ) {
noeuds _precedents . push ( reslinks . rows [ i ] . noeud _precedent )
}
//if(typeof noeuds_suivants == 'number'){
// noeuds_suivants=[noeuds_suivants];
//}
//if(typeof noeuds_precedents == 'number'){
// noeuds_precedents=[noeuds_precedents];
//}
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,/ , '' ) ;
//queryString.replace(HTnoeud,noeuds);
queryString = queryString . replace ( HTnoeud , noeuds ) ;
//console.log(queryString);
connection . query ( queryString , FUN ) ;
return true ;
}
} ) ;
} * /
function queryChainedClient ( client , query , callback , data , resTot ) {
client . query ( query , function ( err , results ) {
if ( err ) {
//err.message=err.stack;
callback ( err ) ;
} else {
resTot . push ( results . rows ) ;
callback ( null , data , resTot ) ;
}
} ) ;
} ;
connection . queryChained = function ( query , callback , data , resTot ) {
this . query ( query , function ( err , results ) {
if ( err ) {
//err.message=err.stack;
callback ( err ) ;
} else {
resTot . push ( results . rows ) ;
callback ( null , data , resTot ) ;
}
} ) ;
} ;
server . listen ( 8080 ) ;
app . use ( express . static ( _ _dirname + '/public' ) ) ;
//empêche le injection sql
//app.use(sqlinjection);
//on fait les routes avec express
app
. get ( '/' , function ( req , res ) {
res . redirect ( '/accueil' ) ;
} )
. get ( '/accueil/' , function ( req , res ) {
/ * c o n n e c t i o n . q u e r y ( ' s e l e c t * f r o m i n f o r m a t i o n _ s c h e m a . t a b l e s ' , f u n c t i o n ( e , r ) {
if ( e ) {
console . log ( e )
} else {
console . log ( r )
}
} ) * /
2020-05-23 08:44:15 +00:00
connection . query ( 'SELECT nom_jeu FROM jeu ORDER BY nom_jeu;' , function ( err , results ) {
2020-05-19 17:14:33 +00:00
if ( err ) {
console . log ( 'erreur de connexion : ' + err . stack ) ;
var parametresAccueil = {
url _defaut : '' ,
nomsJeux : [ ]
} ;
res . render ( 'accueil.ejs' , parametresAccueil ) ;
} else {
nomsJeux = [ ] ;
for ( var i = 0 ; i < results . rows . length ; i ++ ) {
nomsJeux . push ( results . rows [ i ] . nom _jeu ) ;
}
var parametresAccueil = {
url _defaut : '' ,
nomsJeux : nomsJeux
} ;
res . render ( 'accueil.ejs' , parametresAccueil ) ;
}
} ) ;
} )
. post ( '/accueil/' , urlencodedParser , function ( req , res ) {
//on vérifie l'entrée
//on essaie de se connecter à la bdd
//on regarde si le nom du jeux et le nom de la page sont dans la bdd
var valide _url = ( req . body . url && typeof req . body . url === 'string' && pattUrl . test ( req . body . url . substring ( 0 , 50 ) ) ) ;
var valide _jeu = req . body . jeu && typeof req . body . jeu === 'string' ;
var valide _hasard = ( req . body . hasard && req . body . hasard === 'on' ) && valide _jeu ;
if ( valide _url && ! valide _hasard ) {
req . body . url = req . body . url . substring ( 0 , 50 ) ;
}
if ( valide _hasard || valide _url ) {
async . waterfall ( [
function ( callback ) {
var jeu _ok = true ;
var page _dontexist = true ;
if ( valide _hasard ) {
2020-05-23 08:44:15 +00:00
var query = 'SELECT nom_page FROM page;' ;
2020-05-19 17:14:33 +00:00
} else {
var query = {
2020-05-23 08:44:15 +00:00
text : 'SELECT nom_page FROM page WHERE nom_page = $1;' ,
2020-05-19 17:14:33 +00:00
values : [ req . body . url ]
} ;
}
connection . query ( query , function ( err , results ) {
if ( err ) {
callback ( err ) ;
} else {
if ( valide _hasard ) {
page _dontexist = false ;
while ( ! page _dontexist ) {
page _dontexist = true ;
req . body . url = randomString ( 20 , 'aA' ) ;
var i = 0 ;
while ( ( page _dontexist ) && i < results . length ) {
if ( req . body . url === results . rows [ i ] . nom _page ) {
page _dontexist = false ;
}
i += 1 ;
}
}
} else {
if ( results . rows . length > 0 ) {
page _dontexist = false ;
}
}
}
callback ( null , jeu _ok , page _dontexist ) ;
} ) ;
} ,
function ( jeu _ok , page _dontexist , callback ) {
if ( page _dontexist && valide _jeu ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'SELECT nom_jeu FROM jeu WHERE nom_jeu = $1;' ,
2020-05-19 17:14:33 +00:00
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 ;
2020-05-23 08:44:15 +00:00
connection . query ( 'SELECT MAX(id_page)+1 AS id FROM page;' , function ( err , results ) {
2020-05-19 17:14:33 +00:00
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 = {
2020-05-23 08:44:15 +00:00
text : 'SELECT id_jeu AS id FROM jeu WHERE nom_jeu = $1;' ,
2020-05-19 17:14:33 +00:00
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 = {
2020-05-23 08:44:15 +00:00
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; ' ,
2020-05-19 17:14:33 +00:00
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 = {
2020-05-23 08:44:15 +00:00
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; ' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page , data . id _jeu ]
} ;
queryChainedClient ( client , query , callback3 , data , resTot ) ;
} ,
function ( data , resTot , callback3 ) {
const query = {
2020-05-23 08:44:15 +00:00
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;' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page ]
} ;
queryChainedClient ( client , query , callback3 , data , resTot ) ;
} ,
function ( data , resTot , callback3 ) {
const query = {
2020-05-23 08:44:15 +00:00
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; ' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page , data . id _jeu ]
} ;
queryChainedClient ( client , query , callback3 , data , resTot ) ;
} ,
function ( data , resTot , callback3 ) {
const query = {
2020-05-23 08:44:15 +00:00
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; ' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page , data . id _jeu ]
} ;
queryChainedClient ( client , query , callback3 , data , resTot ) ;
} ,
function ( data , resTot , callback3 ) {
const query = {
2020-05-23 08:44:15 +00:00
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; ' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page , data . id _jeu ]
} ;
queryChainedClient ( client , query , callback3 , data , resTot ) ;
} ,
function ( data , resTot , callback3 ) {
const query = {
2020-05-23 08:44:15 +00:00
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; ' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page , data . id _jeu ]
} ;
queryChainedClient ( client , query , callback3 , data , resTot ) ;
} ,
function ( data , resTot , callback3 ) {
const query = {
2020-05-23 08:44:15 +00:00
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; ' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page , data . id _jeu ]
} ;
queryChainedClient ( client , query , callback3 , data , resTot ) ;
} ,
function ( data , resTot , callback3 ) {
const query = {
2020-05-23 08:44:15 +00:00
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; ' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page , data . id _jeu ]
} ;
queryChainedClient ( client , query , callback3 , data , resTot ) ;
} ,
function ( data , resTot , callback3 ) {
const query = {
2020-05-23 08:44:15 +00:00
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; ' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page , data . id _jeu ]
} ;
queryChainedClient ( client , query , callback3 , data , resTot ) ;
} ,
function ( data , resTot , callback3 ) {
const query = {
2020-05-23 08:44:15 +00:00
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; ' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page , data . id _jeu ]
} ;
queryChainedClient ( client , query , callback3 , data , resTot ) ;
} ,
function ( data , resTot , callback3 ) {
const query = {
2020-05-23 08:44:15 +00:00
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; ' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page , data . id _jeu ]
} ;
queryChainedClient ( client , query , callback3 , data , resTot ) ;
} ,
function ( data , resTot , callback3 ) {
const query = {
2020-05-23 08:44:15 +00:00
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; ' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page , data . id _jeu ]
} ;
queryChainedClient ( client , query , callback3 , data , resTot ) ;
} ,
function ( data , resTot , callback3 ) {
const query = {
2020-05-23 08:44:15 +00:00
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; ' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page , data . id _jeu ]
} ;
queryChainedClient ( client , query , callback3 , data , resTot ) ;
} ,
function ( data , resTot , callback3 ) {
const query = {
2020-05-23 08:44:15 +00:00
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;' ,
2020-05-19 17:14:33 +00:00
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 = {
2020-05-23 08:44:15 +00:00
text : 'SELECT id_page,noeud_courant FROM page WHERE nom_page =$1;' ,
2020-05-19 17:14:33 +00:00
values : [ req . params . pageid ]
} ;
client . query ( query , function ( err , results _1 ) {
if ( err ) {
//console.log('erreur sql :'+err.stack);
2020-05-23 09:16:09 +00:00
//res.redirect('/accueil');//passer le nom en paramètre?
2020-05-19 17:14:33 +00:00
callback ( err ) ;
} else {
if ( results _1 . rows . length > 1 ) {
//console.log('plusieurs pages');
2020-05-23 09:16:09 +00:00
//res.redirect('/accueil');//supprimer les pages ?
2020-05-19 17:14:33 +00:00
callback ( 'plusieurs pages' ) ;
} else if ( results _1 . rows . length == 0 ) {
2020-05-23 09:16:09 +00:00
//res.redirect('/accueil');//passer le nom en paramètre?
2020-05-19 17:14:33 +00:00
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 = {
2020-05-23 08:44:15 +00:00
text : 'SELECT nombre_trashs,noeud_courant,creating_date FROM page WHERE id_page =$1; ' ,
2020-05-19 17:14:33 +00:00
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, ' +
2020-05-23 08:44:15 +00:00
'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 ' +
2020-05-19 17:14:33 +00:00
'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, ' +
2020-05-23 08:44:15 +00:00
'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 ' +
2020-05-19 17:14:33 +00:00
'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 = {
2020-05-23 08:44:15 +00:00
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' +
2020-05-19 17:14:33 +00:00
' 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 = {
2020-05-23 08:44:15 +00:00
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' +
2020-05-19 17:14:33 +00:00
' 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 = {
2020-05-23 08:44:15 +00:00
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' +
2020-05-19 17:14:33 +00:00
' 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 = {
2020-05-23 08:44:15 +00:00
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' +
2020-05-19 17:14:33 +00:00
' 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 = {
2020-05-23 08:44:15 +00:00
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' +
2020-05-19 17:14:33 +00:00
' 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 = {
2020-05-23 08:44:15 +00:00
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; ' ,
2020-05-19 17:14:33 +00:00
values : [ id _page ]
} ;
queryChainedClient ( client , query , callback , id _page , results _2 ) ;
} ,
function ( id _page , results _2 , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'SELECT DISTINCT id_board,nom_board FROM board ORDER BY nom_board; ' ,
2020-05-19 17:14:33 +00:00
values : [ ]
} ;
queryChainedClient ( client , query , callback , id _page , results _2 ) ;
} ,
function ( id _page , results _2 , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'SELECT DISTINCT id_pion,nom_pion FROM pion ORDER BY nom_pion; ' ,
2020-05-19 17:14:33 +00:00
values : [ ]
} ;
queryChainedClient ( client , query , callback , id _page , results _2 ) ;
} ,
function ( id _page , results _2 , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'SELECT time,message,auteur,couleur,id_message FROM chat_on_page WHERE id_page = $1 ORDER BY time DESC; ' ,
2020-05-19 17:14:33 +00:00
values : [ id _page ]
} ;
queryChainedClient ( client , query , callback , id _page , results _2 ) ;
} ,
function ( id _page , results _2 , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'SELECT j.wiki AS wiki FROM page p JOIN jeu j ON p.id_jeu_depart=j.id_jeu;' ,
2020-05-19 17:14:33 +00:00
values : [ ]
} ;
queryChainedClient ( client , query , callback , id _page , results _2 ) ;
} ,
function ( id _page , results _2 , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
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;' ,
2020-05-19 17:14:33 +00:00
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 ; i < results _2 [ 3 ] . length ; i ++ ) {
/ * v a r n o e u d s _ s t o p = r e s u l t s _ 2 [ 3 ] [ i ] . n o e u d _ s t o p . s p l i t ( ' , ' ) . m a p ( f u n c t i o n ( x ) {
return parseInt ( x , 10 ) ;
} ) ; * /
var noeuds _stop = results _2 [ 3 ] [ i ] . noeud _stop
//if( (noeuds.indexOf(results_2[3][i].noeud_start)!= -1) && (noeuds.indexOf(results_2[3][i].noeud_stop)==-1)) {
if ( ( noeuds . indexOf ( results _2 [ 3 ] [ i ] . noeud _start ) != - 1 ) && ( intersect ( noeuds , noeuds _stop ) . length === 0 ) ) {
display = 'actif' ;
} else {
display = 'inactif' ;
}
params . objBoxes . push ( {
nom : results _2 [ 3 ] [ i ] . nom _pion ,
svg : results _2 [ 3 ] [ i ] . svg _pion ,
diese : results _2 [ 3 ] [ i ] . diese _box _on _page ,
num : results _2 [ 3 ] [ i ] . num _box _on _page ,
directed : results _2 [ 3 ] [ i ] . directed ,
noeud _start : results _2 [ 3 ] [ i ] . noeud _start ,
noeud _stop : results _2 [ 3 ] [ i ] . noeud _stop ,
display : display
} ) ;
// if(results_2[3][i].diese_box_on_page>params.maxDieseBox){
// params.maxDieseBox = results_2[3][i].diese_box_on_page;
// }
}
params . objBacs = [ ] ;
var i = 0 ;
var c = 0 ;
while ( i < results _2 [ 4 ] . length ) {
var diese = results _2 [ 4 ] [ i ] . diese _bac _on _page ;
params . objBacs . push ( {
bacs : [ ] ,
groupes : [ ] ,
diese : results _2 [ 4 ] [ i ] . diese _bac _on _page
} ) ;
while ( i < results _2 [ 4 ] . length && diese === results _2 [ 4 ] [ i ] . diese _bac _on _page ) {
/ * v a r n o e u d s _ s t o p = r e s u l t s _ 2 [ 4 ] [ i ] . n o e u d _ s t o p . s p l i t ( ' , ' ) . m a p ( f u n c t i o n ( x ) {
return parseInt ( x , 10 ) ;
} ) ; * /
var noeuds _stop = results _2 [ 4 ] [ i ] . noeud _stop
if ( ( noeuds . indexOf ( results _2 [ 4 ] [ i ] . noeud _start ) != - 1 ) && ( intersect ( noeuds , noeuds _stop ) . length === 0 ) ) {
display = 'actif' ;
} else {
display = 'inactif' ;
}
params . objBacs [ c ] . bacs . push ( {
label : results _2 [ 4 ] [ i ] . label _bac ,
num : results _2 [ 4 ] [ i ] . num _bac _on _page ,
noeud _start : results _2 [ 4 ] [ i ] . noeud _start ,
noeud _stop : results _2 [ 4 ] [ i ] . noeud _stop ,
display : display
} ) ;
i = i + 1
}
c = c + 1 ;
}
var j = 0 ;
for ( var i = 0 ; i < results _2 [ 5 ] . length ; i ++ ) {
var n = 0 ;
var cont = true ;
while ( params . objBacs [ j ] . diese != results _2 [ 5 ] [ i ] . diese _bac _on _page || cont ) {
j ++ ;
if ( j >= params . objBacs . length ) {
n ++ ;
j = 0 ;
if ( n == 2 ) {
cont = false ;
}
}
}
/ * v a r n o e u d s _ s t o p = r e s u l t s _ 2 [ 5 ] [ i ] . n o e u d _ s t o p . s p l i t ( ' , ' ) . m a p ( f u n c t i o n ( 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 < results _2 [ 6 ] . length ) {
var diese = results _2 [ 6 ] [ i ] . diese _board _on _page ;
params . objBoards . push ( {
boards : [ ] ,
pions : [ ] ,
diese : results _2 [ 6 ] [ i ] . diese _board _on _page
} ) ;
while ( i < results _2 [ 6 ] . length && diese === results _2 [ 6 ] [ i ] . diese _board _on _page ) {
/ * v a r n o e u d s _ s t o p = r e s u l t s _ 2 [ 6 ] [ i ] . n o e u d _ s t o p . s p l i t ( ' , ' ) . m a p ( f u n c t i o n ( x ) {
return parseInt ( x , 10 ) ;
} ) ; * /
var noeuds _stop = results _2 [ 6 ] [ i ] . noeud _stop ;
if ( ( noeuds . indexOf ( results _2 [ 6 ] [ i ] . noeud _start ) != - 1 ) && ( intersect ( noeuds , noeuds _stop ) . length === 0 ) ) {
display = 'actif' ;
} else {
display = 'inactif' ;
}
params . objBoards [ c ] . boards . push ( {
svg : results _2 [ 6 ] [ i ] . svg _board ,
nom : results _2 [ 6 ] [ i ] . nom _board ,
num : results _2 [ 6 ] [ i ] . num _board _on _page ,
noeud _start : results _2 [ 6 ] [ i ] . noeud _start ,
noeud _stop : results _2 [ 6 ] [ i ] . noeud _stop ,
display : display
} ) ;
i = i + 1
}
c = c + 1 ;
}
var j = 0 ;
for ( var i = 0 ; i < results _2 [ 7 ] . length ; i ++ ) {
var n = 0 ;
var cont = true ;
while ( params . objBoards [ j ] . diese != results _2 [ 7 ] [ i ] . diese _board _on _page || cont ) {
j ++ ;
if ( j >= params . objBoards . length ) {
n ++ ;
j = 0 ;
if ( n == 2 ) {
cont = false ;
}
}
}
/ * v a r n o e u d s _ s t o p = r e s u l t s _ 2 [ 7 ] [ i ] . n o e u d _ s t o p . s p l i t ( ' , ' ) . m a p ( f u n c t i o n ( 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 < results _2 [ 8 ] . length ; i ++ ) {
params . Joueurs . push ( {
id : results _2 [ 8 ] [ i ] . id _joueur ,
nom : results _2 [ 8 ] [ i ] . label _joueur
} ) ;
}
for ( var i = 0 ; i < results _2 [ 9 ] . length ; i ++ ) {
params . Boards . push ( {
id : results _2 [ 9 ] [ i ] . id _board ,
nom : results _2 [ 9 ] [ i ] . nom _board
} ) ;
}
for ( var i = 0 ; i < results _2 [ 10 ] . length ; i ++ ) {
params . Boxes . push ( {
id : results _2 [ 10 ] [ i ] . id _pion ,
nom : results _2 [ 10 ] [ i ] . nom _pion
} ) ;
}
for ( var i = 0 ; i < results _2 [ 11 ] . length ; i ++ ) {
params . Chat . push ( {
id : results _2 [ 11 ] [ i ] . id _message ,
auteur : results _2 [ 11 ] [ i ] . auteur ,
message : results _2 [ 11 ] [ i ] . message ,
couleur : results _2 [ 11 ] [ i ] . couleur ,
time : affTime . exec ( results _2 [ 11 ] [ i ] . time )
} ) ;
}
params . wiki = results _2 [ 12 ] [ 0 ] . wiki ;
params . Save = [ {
noeud _precedent : 0 ,
noeud _suivant : 1 ,
date : results _2 [ 0 ] [ 0 ] . creating _date ,
auteur : "" ,
couleur : "black" ,
label _joueur : "" ,
x : 0 ,
y : 0
} ] ;
var noeuds _sui = [ 1 ] ;
var noeuds _pre = [ 0 ] ;
var y = [ 0 ] ;
var epaisseur = [ 1 ] ;
//on a déjà fait le premier noeud
for ( var i = 1 ; i < results _2 [ 13 ] . length ; i ++ ) {
y _pre _ind = params . Save [ noeuds _sui . indexOf ( results _2 [ 13 ] [ i ] . noeud _precedent ) ] . y ;
epais = epaisseur [ noeuds _sui . indexOf ( results _2 [ 13 ] [ i ] . noeud _precedent ) ] ;
//Save.y au debut indique l'indice dans le vecteur y
if ( noeuds _pre . indexOf ( results _2 [ 13 ] [ i ] . noeud _precedent ) === - 1 ) {
//il n'y a pas de nouveau embranchement
new _y _ind = y _pre _ind ;
} else {
//nouvel embranchement
//la valeur du nouveau y
new _y = y [ y _pre _ind ] + epais ;
//on corrige le vecteur y
for ( var j = 0 ; j < y . length ; j ++ ) {
if ( y [ j ] >= 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 ; i < params . Save . length ; i ++ ) {
if ( params . Save [ i ] . x > xmax ) {
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 = {
2020-05-23 08:44:15 +00:00
text : 'DELETE FROM stop_pion_on_bac_on_page WHERE id_page = $1 ; ' ,
2020-05-19 17:14:33 +00:00
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 = {
2020-05-23 08:44:15 +00:00
text : 'DELETE FROM stop_pion_on_board_on_page WHERE id_page = $1 ; ' ,
2020-05-19 17:14:33 +00:00
values : [ id _page ]
} ;
queryChainedClient ( client , query , callback , id _page , resTot ) ;
} ,
function ( id _page , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'DELETE FROM pion_on_bac_on_page WHERE id_page = $1 ; ' ,
2020-05-19 17:14:33 +00:00
values : [ id _page ]
} ;
queryChainedClient ( client , query , callback , id _page , resTot ) ;
} ,
function ( id _page , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'DELETE FROM carac_pions_joueur_on_page WHERE id_page = $1 ; ' ,
2020-05-19 17:14:33 +00:00
values : [ id _page ]
} ;
queryChainedClient ( client , query , callback , id _page , resTot ) ;
} ,
function ( id _page , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'DELETE FROM pion_on_board_on_page WHERE id_page = $1 ; ' ,
2020-05-19 17:14:33 +00:00
values : [ id _page ]
} ;
queryChainedClient ( client , query , callback , id _page , resTot ) ;
} ,
function ( id _page , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'DELETE FROM carac_pions_page WHERE id_page = $1 ; ' ,
2020-05-19 17:14:33 +00:00
values : [ id _page ]
} ;
queryChainedClient ( client , query , callback , id _page , resTot ) ;
} ,
function ( id _page , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'DELETE FROM stop_bac_on_page WHERE id_page = $1 ; ' ,
2020-05-19 17:14:33 +00:00
values : [ id _page ]
} ;
queryChainedClient ( client , query , callback , id _page , resTot ) ;
} ,
function ( id _page , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'DELETE FROM bac_on_page WHERE id_page = $1 ; ' ,
2020-05-19 17:14:33 +00:00
values : [ id _page ]
} ;
queryChainedClient ( client , query , callback , id _page , resTot ) ;
} ,
function ( id _page , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'DELETE FROM stop_box_on_page WHERE id_page = $1 ; ' ,
2020-05-19 17:14:33 +00:00
values : [ id _page ]
} ;
queryChainedClient ( client , query , callback , id _page , resTot ) ;
} ,
function ( id _page , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'DELETE FROM box_on_page WHERE id_page = $1 ; ' ,
2020-05-19 17:14:33 +00:00
values : [ id _page ]
} ;
queryChainedClient ( client , query , callback , id _page , resTot ) ;
} ,
function ( id _page , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'DELETE FROM stop_board_on_page WHERE id_page = $1 ; ' ,
2020-05-19 17:14:33 +00:00
values : [ id _page ]
} ;
queryChainedClient ( client , query , callback , id _page , resTot ) ;
} ,
function ( id _page , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'DELETE FROM board_on_page WHERE id_page = $1 ; ' ,
2020-05-19 17:14:33 +00:00
values : [ id _page ]
} ;
queryChainedClient ( client , query , callback , id _page , resTot ) ;
} ,
function ( id _page , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'DELETE FROM chat_on_page WHERE id_page = $1 ; ' ,
2020-05-19 17:14:33 +00:00
values : [ id _page ]
} ;
queryChainedClient ( client , query , callback , id _page , resTot ) ;
} ,
function ( id _page , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'DELETE FROM save_on_page WHERE id_page = $1 ; ' ,
2020-05-19 17:14:33 +00:00
values : [ id _page ]
} ;
queryChainedClient ( client , query , callback , id _page , resTot ) ;
} ,
function ( id _page , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'DELETE FROM joueur_on_page WHERE id_page = $1 ; ' ,
2020-05-19 17:14:33 +00:00
values : [ id _page ]
} ;
queryChainedClient ( client , query , callback , id _page , resTot ) ;
} ,
function ( id _page , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'DELETE FROM page WHERE id_page = $1 ; ' ,
2020-05-19 17:14:33 +00:00
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 = {
2020-05-23 08:44:15 +00:00
text : 'DELETE FROM stop_pion_on_bac_on_page WHERE id_page = $1 ; ' ,
2020-05-19 17:14:33 +00:00
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 = {
2020-05-23 08:44:15 +00:00
text : 'DELETE FROM pion_on_bac_on_page WHERE id_page = $1 ; ' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page ]
} ;
queryChainedClient ( client , query , callback , data , resTot ) ;
} ,
function ( data , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'DELETE FROM stop_pion_on_board_on_page WHERE id_page = $1 ; ' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page ]
} ;
queryChainedClient ( client , query , callback , data , resTot ) ;
} ,
function ( data , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'DELETE FROM pion_on_board_on_page WHERE id_page = $1 ; ' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page ]
} ;
queryChainedClient ( client , query , callback , data , resTot ) ;
} ,
function ( data , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'DELETE FROM carac_pions_joueur_on_page WHERE id_page = $1 ; ' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page ]
} ;
queryChainedClient ( client , query , callback , data , resTot ) ;
} ,
function ( data , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'DELETE FROM carac_pions_page WHERE id_page = $1 ; ' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page ]
} ;
queryChainedClient ( client , query , callback , data , resTot ) ;
} ,
function ( data , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'DELETE FROM stop_bac_on_page WHERE id_page = $1 ; ' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page ]
} ;
queryChainedClient ( client , query , callback , data , resTot ) ;
} ,
function ( data , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'DELETE FROM bac_on_page WHERE id_page = $1 ; ' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page ]
} ;
queryChainedClient ( client , query , callback , data , resTot ) ;
} ,
function ( data , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'DELETE FROM stop_box_on_page WHERE id_page = $1 ; ' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page ]
} ;
queryChainedClient ( client , query , callback , data , resTot ) ;
} ,
function ( data , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'DELETE FROM box_on_page WHERE id_page = $1 ; ' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page ]
} ;
queryChainedClient ( client , query , callback , data , resTot ) ;
} ,
function ( data , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'DELETE FROM stop_board_on_page WHERE id_page = $1 ; ' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page ]
} ;
queryChainedClient ( client , query , callback , data , resTot ) ;
} ,
function ( data , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'DELETE FROM board_on_page WHERE id_page = $1 ; ' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page ]
} ;
queryChainedClient ( client , query , callback , data , resTot ) ;
} ,
function ( data , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'DELETE FROM chat_on_page WHERE id_page = $1 ; ' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page ]
} ;
queryChainedClient ( client , query , callback , data , resTot ) ;
} ,
function ( data , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'DELETE FROM save_on_page WHERE id_page = $1 ; ' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page ]
} ;
queryChainedClient ( client , query , callback , data , resTot ) ;
} ,
function ( data , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'DELETE FROM joueur_on_page WHERE id_page = $1 ; ' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page ]
} ;
queryChainedClient ( client , query , callback , data , resTot ) ;
} ,
function ( data , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
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; ' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page , data . id _jeu _depart ]
} ;
queryChainedClient ( client , query , callback , data , resTot ) ;
} ,
function ( data , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
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;' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page ]
} ;
queryChainedClient ( client , query , callback , data , resTot ) ;
} ,
function ( data , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
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; ' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page , data . id _jeu _depart ]
} ;
queryChainedClient ( client , query , callback , data , resTot ) ;
} ,
function ( data , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
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; ' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page , data . id _jeu _depart ]
} ;
queryChainedClient ( client , query , callback , data , resTot ) ;
} ,
function ( data , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
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; ' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page , data . id _jeu _depart ]
} ;
queryChainedClient ( client , query , callback , data , resTot ) ;
} ,
function ( data , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
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; ' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page , data . id _jeu _depart ]
} ;
queryChainedClient ( client , query , callback , data , resTot ) ;
} ,
function ( data , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
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; ' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page , data . id _jeu _depart ]
} ;
queryChainedClient ( client , query , callback , data , resTot ) ;
} ,
function ( data , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
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; ' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page , data . id _jeu _depart ]
} ;
queryChainedClient ( client , query , callback , data , resTot ) ;
} ,
function ( data , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
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; ' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page , data . id _jeu _depart ]
} ;
queryChainedClient ( client , query , callback , data , resTot ) ;
} ,
function ( data , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
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; ' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page , data . id _jeu _depart ]
} ;
queryChainedClient ( client , query , callback , data , resTot ) ;
} ,
function ( data , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
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; ' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page , data . id _jeu _depart ]
} ;
queryChainedClient ( client , query , callback , data , resTot ) ;
} ,
function ( data , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
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; ' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page , data . id _jeu _depart ]
} ;
queryChainedClient ( client , query , callback , data , resTot ) ;
} ,
function ( data , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
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; ' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page , data . id _jeu _depart ]
} ;
queryChainedClient ( client , query , callback , data , resTot ) ;
} ,
function ( data , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
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;' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page , data . id _jeu _depart ]
} ;
queryChainedClient ( client , query , callback , data , resTot ) ;
} ,
function ( id _page , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'UPDATE page SET noeud_courant=1 WHERE id_page = $1; ' ,
2020-05-19 17:14:33 +00:00
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 ' +
2020-05-23 08:44:15 +00:00
'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 ' +
2020-05-19 17:14:33 +00:00
'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 = {
2020-05-23 08:44:15 +00:00
text : 'INSERT INTO chat_on_page (id_page,id_message,auteur,message,couleur,time) ' +
2020-05-19 17:14:33 +00:00
'SELECT $1,CASE WHEN MAX(id_message) IS NULL THEN 1 ELSE MAX(id_message)+1 END,' +
2020-05-23 08:44:15 +00:00
'$2,$3,$4,CURRENT_TIMESTAMP FROM chat_on_page WHERE id_page = $5; ' ,
2020-05-19 17:14:33 +00:00
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 = {
2020-05-23 08:44:15 +00:00
text : 'SELECT id_message,time FROM chat_on_page WHERE id_page = $1 ORDER BY id_message DESC LIMIT 1 ; ' ,
2020-05-19 17:14:33 +00:00
values : [ data . id _page ]
} ;
queryChainedClient ( client , query , callback , data , res2 ) ;
} ,
function ( id _page , res2 , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
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) ; ' ,
2020-05-19 17:14:33 +00:00
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' +
2020-05-23 08:44:15 +00:00
' 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' +
2020-05-19 17:14:33 +00:00
' 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 < res2 . rows . length ; i ++ ) {
tabPionListe . push ( res2 . rows [ i ] . id _pion ) ;
tabPionChange . push ( res2 . rows [ i ] . change _pion ) ;
tabPionMove . push ( res2 . rows [ i ] . diese _bac _on _page ) ;
tabPionTaille . push ( res2 . rows [ i ] . taille ) ;
tabPionSvg . push ( res2 . rows [ i ] . svg _pion ) ;
}
socket . emit ( 'setTabPion' , { tabPionListe : tabPionListe , tabPionChange : tabPionChange , tabPionMove : tabPionMove , tabPionTaille : tabPionTaille , tabPionSvg : tabPionSvg } ) ;
}
}
) ;
} ) ;
} ) ;
//ok
socket . on ( 'add_trash' , function ( ) {
socket . executeSocket ( variables = { } , needsData = false , errMess = 'add_trash' , data = { } , FUN = function ( resIP ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'UPDATE page SET nombre_trashs = (nombre_trashs + 1) WHERE id_page = $1 ;' ,
2020-05-19 17:14:33 +00:00
values : [ resIP [ 0 ] . id _page ]
}
connection . query ( query , function ( err ) {
if ( err ) {
console . log ( 'erreur de add_trash : ' + err . stack ) ;
socket . emit ( 'refresh' ) ;
} else {
socket . broadcast . to ( socket . nomPlatoo ) . emit ( 'add_trash' ) ;
}
} ) ;
} ) ;
} ) ;
//ok
socket . on ( 'remove_trash' , function ( ) {
socket . executeSocket ( variables = { } , needsData = false , errMess = 'remove_trash' , data = { } , FUN = function ( resIP ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'UPDATE page SET nombre_trashs = GREATEST(nombre_trashs -1,0) WHERE id_page = $1 ; ' ,
2020-05-19 17:14:33 +00:00
values : [ resIP [ 0 ] . id _page ]
}
connection . query ( query , function ( err ) {
if ( err ) {
console . log ( 'erreur de remove_trash : ' + err . stack ) ;
socket . emit ( 'refresh' ) ;
} else {
socket . broadcast . to ( socket . nomPlatoo ) . emit ( 'removeElement' , { id : 'trash' , noeud : null } ) ;
socket . emit ( 'removeElement' , { id : 'trash' , noeud : null } ) ;
}
} ) ;
} ) ;
} ) ;
//ok
socket . on ( 'changeNoeud' , function ( data ) {
socket . executeSocket ( variables = { noeud : 1 } , needsData = true , errMess = 'changeNoeud' , data = data , FUN = function ( resIP ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'SELECT noeud_suivant FROM save_on_page WHERE noeud_suivant = $1 AND id_page = $2 ;' ,
2020-05-19 17:14:33 +00:00
values : [ data . noeud , resIP [ 0 ] . id _page ]
}
connection . query ( query ,
function ( err , res1 ) {
if ( err ) {
console . log ( 'erreur de changeNoeud1 : ' + err . stack ) ;
socket . emit ( 'refresh' ) ;
} else if ( resIP . length === 0 ) {
console . log ( 'bad noeud de changeNoeud : ' + err . stack ) ;
socket . emit ( 'refresh' ) ;
} else {
const query = {
2020-05-23 08:44:15 +00:00
text : 'UPDATE page SET noeud_courant = $1 WHERE id_page = $2 ;' ,
2020-05-19 17:14:33 +00:00
values : [ data . noeud , resIP [ 0 ] . id _page ]
}
connection . query ( query ,
function ( err2 , res2 ) {
if ( err2 ) {
console . log ( 'erreur de changeNoeud2 : ' + err2 . stack ) ;
socket . emit ( 'refresh' ) ;
} else {
socket . broadcast . to ( socket . nomPlatoo ) . emit ( 'changeNoeud' , { noeud : data . noeud } ) ;
socket . emit ( 'changeNoeud' , { noeud : data . noeud } ) ;
}
}
) ;
}
}
) ;
} ) ;
} ) ;
socket . on ( 'ModifJeu' , function ( dataTot ) {
socket . executeSocket ( variables = { auteur : '20' ,
couleur : '20' ,
id _joueur : 1 ,
noeud _courant : 1 } , needsData = true , errMess = 'ModifJeu' , data = dataTot , FUN = function ( resIP ) {
//on vérifie le noeud courant
//console.log(dataTot);
//console.log(dataTot.noeud_courant);
//console.log(resIP[0].noeud_courant);
if ( dataTot . noeud _courant !== resIP [ 0 ] . noeud _courant ) {
//si ça n'est pas le même: il y a un decalage, on ne fait rien
console . log ( 'mauvais noeud' ) ;
socket . emit ( 'refresh' ) ;
} else {
connection . connect ( ( err , client , done ) => {
//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 = {
2020-05-23 08:44:15 +00:00
text : 'SELECT noeud_precedent,noeud_suivant FROM save_on_page WHERE id_page = $1;' ,
2020-05-19 17:14:33 +00:00
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 = {
2020-05-23 08:44:15 +00:00
text : 'INSERT INTO save_on_page (id_page,noeud_precedent,noeud_suivant,date,type_action,auteur,couleur,id_joueur) SELECT ' +
2020-05-19 17:14:33 +00:00
'$1,$2,CASE WHEN MAX(noeud_suivant) IS NULL THEN 1 ELSE MAX(noeud_suivant)+1 END,CURRENT_TIMESTAMP,\'add_groupe\',$3,' +
2020-05-23 08:44:15 +00:00
'$4,$5 FROM save_on_page WHERE id_page=$1;' ,
2020-05-19 17:14:33 +00:00
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 = {
2020-05-23 08:44:15 +00:00
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 ' +
2020-05-19 17:14:33 +00:00
' 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 = {
2020-05-23 08:44:15 +00:00
text : 'SELECT noeud_courant AS new_noeud,CURRENT_TIMESTAMP AS date FROM page WHERE id_page=$1;' ,
2020-05-19 17:14:33 +00:00
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 ) ;
}
/ * n o e u d s O b j = {
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 < dataTot . requetes . length ; i ++ ) {
var requete = dataTot . requetes [ i ] ;
requetes _tot . push ( requete ) ;
//action erase-------------------------------------------------------------------------
if ( requete . action === 'erase' ) { //ok
if ( ! checkValide ( variables = { } , needsData = false , errMess = 'erase' , data = requete . data ) ) {
console . log ( errMess + '.data incorrect dans erase' ) ;
Abort = true ;
} else {
//un waterfall pour faire toutes les requêtes
Array _waterfall = Array _waterfall . concat ( [
function ( resNoeuds , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'INSERT INTO stop_pion_on_bac_on_page (id_page,diese_bac_on_page,num_pion_on_bac_on_page,noeud_stop)' +
' SELECT id_page,diese_bac_on_page,num_pion_on_bac_on_page,$1 FROM pion_on_bac_on_page p' +
' LEFT JOIN (SELECT COUNT(noeud_stop) nbstop,MAX(id_page) id, MAX(num_pion_on_bac_on_page) num, MAX(diese_bac_on_page) diese FROM stop_pion_on_bac_on_page' +
2020-05-19 17:14:33 +00:00
' WHERE id_page = $2 AND ARRAY[noeud_stop] <@ $3 GROUP BY id_page,diese_bac_on_page,num_pion_on_bac_on_page) AS g' +
' ON p.id_page=g.id AND p.num_pion_on_bac_on_page = g.num AND p.diese_bac_on_page=g.diese' +
' WHERE id_page = $2 AND ARRAY[noeud_start] <@ $3 AND ( COALESCE(g.nbstop,0)=0 OR g.nbstop=0) ;' ,
values : [ resNoeuds [ 3 ] [ 0 ] . new _noeud , resIP [ 0 ] . id _page , resNoeuds [ 4 ] ]
} ;
client . query ( query , function ( err , results ) {
if ( err ) {
callback ( err ) ;
} else {
//resTot.push(results.rows);
resTot = [ results . rows ] ;
callback ( null , resNoeuds , resTot ) ;
}
} ) ;
} ,
function ( resNoeuds , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'INSERT INTO stop_pion_on_board_on_page (id_page,diese_board_on_page,num_pion_on_board_on_page,noeud_stop)' +
' SELECT id_page,diese_board_on_page,num_pion_on_board_on_page,$1 FROM pion_on_board_on_page p' +
' LEFT JOIN (SELECT COUNT(noeud_stop) nbstop,MAX(id_page) id, MAX(num_pion_on_board_on_page) num, MAX(diese_board_on_page) diese FROM stop_pion_on_board_on_page' +
2020-05-19 17:14:33 +00:00
' WHERE id_page = $2 AND ARRAY[noeud_stop] <@ $3 GROUP BY id_page,diese_board_on_page,num_pion_on_board_on_page) AS g' +
' ON p.id_page=g.id AND p.num_pion_on_board_on_page = g.num AND p.diese_board_on_page=g.diese' +
' WHERE id_page = $2 AND ARRAY[noeud_start] <@ $3 AND ( COALESCE(g.nbstop,0)=0 OR g.nbstop=0) ;' ,
values : [ resNoeuds [ 3 ] [ 0 ] . new _noeud , resIP [ 0 ] . id _page , resNoeuds [ 4 ] ]
} ;
client . query ( query , function ( err , results ) {
if ( err ) {
callback ( err ) ;
} else {
//resTot.push(results.rows);
resTot . push ( results . rows ) ;
callback ( null , resNoeuds , resTot ) ;
}
} ) ;
} ,
function ( resNoeuds , resTot , callback ) {
emit . push ( { text : 'erase' , data : { noeud : resNoeuds [ 3 ] [ 0 ] . new _noeud } } ) ;
emit _broad . push ( { text : 'erase' , data : { noeud : resNoeuds [ 3 ] [ 0 ] . new _noeud } } ) ;
nb _requete += 1 ;
callback ( null , resNoeuds ) ;
}
] ) ;
}
//action add_bac--------------------------------------------------------------
} else if ( requete . action === 'add_bac' ) { //ok
if ( ! checkValide ( variables = { label : '20' } , needsData = true , errMess = 'erase' , data = requete . data ) ) {
console . log ( errMess + '.data incorrect dans add_bac' ) ;
Abort = true ;
} else {
//un waterfall pour faire toutes les requêtes
Array _waterfall = Array _waterfall . concat ( [
function ( resNoeuds , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'INSERT INTO bac_on_page (id_page,diese_bac_on_page,num_bac_on_page,label_bac,noeud_start) SELECT $1' +
2020-05-19 17:14:33 +00:00
', CASE WHEN MAX(diese_bac_on_page) IS NULL THEN 1 ELSE MAX(diese_bac_on_page)+1 END, ' +
'CASE WHEN MAX(num_bac_on_page) IS NULL THEN 0 ELSE MAX(num_bac_on_page)+1 END,' +
2020-05-23 08:44:15 +00:00
'$2,$3 FROM bac_on_page WHERE id_page = $1 ;' ,
2020-05-19 17:14:33 +00:00
values : [ resIP [ 0 ] . id _page , requetes _tot [ nb _requete ] . data . label , resNoeuds [ 3 ] [ 0 ] . new _noeud ] ,
} ;
client . query ( query , function ( err , results ) {
if ( err ) {
callback ( err ) ;
} else {
//resTot.push(results.rows);
resTot = [ results . rows ] ;
callback ( null , resNoeuds , resTot ) ;
}
} ) ;
} ,
function ( resNoeuds , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'INSERT INTO stop_bac_on_page (id_page,num_bac_on_page,noeud_stop) SELECT $1' +
' ,MAX(num_bac_on_page),0 FROM bac_on_page WHERE id_page = $1 GROUP BY id_page;' ,
2020-05-19 17:14:33 +00:00
values : [ resIP [ 0 ] . id _page ]
} ;
queryChainedClient ( client , query , callback , resNoeuds , resTot ) ;
} ,
function ( resNoeuds , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'SELECT MAX(diese_bac_on_page) diese,MAX(num_bac_on_page) num FROM bac_on_page WHERE id_page = $1 GROUP BY id_page;' ,
2020-05-19 17:14:33 +00:00
values : [ resIP [ 0 ] . id _page ]
} ;
client . query ( query , function ( err , results ) {
if ( err ) {
callback ( err ) ;
} else {
//resTot.push(results.rows);
resTot . push ( results . rows ) ;
callback ( null , resNoeuds , resTot ) ;
}
} ) ;
} ,
function ( resNoeuds , resTot , callback ) {
emit . push ( { text : 'add_bac' , data : { diese : resTot [ 2 ] [ 0 ] . diese , label : requetes _tot [ nb _requete ] . data . label , num : resTot [ 2 ] [ 0 ] . num , noeud : resNoeuds [ 3 ] [ 0 ] . new _noeud } } ) ;
emit _broad . push ( { text : 'add_bac' , data : { diese : resTot [ 2 ] [ 0 ] . diese , label : requetes _tot [ nb _requete ] . data . label , num : resTot [ 2 ] [ 0 ] . num , noeud : resNoeuds [ 3 ] [ 0 ] . new _noeud } } ) ;
nb _requete += 1 ;
callback ( null , resNoeuds ) ;
}
] ) ;
}
//action add_box--------------------------------------------------------------
} else if ( requete . action === 'add_box' ) { //ok
if ( ! checkValide ( variables = { nom : '45' } , needsData = true , errMess = 'add_box' , data = requete . data ) ) {
console . log ( errMess + '.data incorrect dans add_box' ) ;
Abort = true ;
} else {
Array _waterfall = Array _waterfall . concat ( [
function ( resNoeuds , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'SELECT id_pion,taille,svg_pion FROM pion WHERE nom_pion = $1; ' ,
2020-05-19 17:14:33 +00:00
values : [ requetes _tot [ nb _requete ] . data . nom ]
}
client . query ( query , function ( err , results ) {
if ( err ) {
callback ( err ) ;
} else {
//resTot.push(results.rows);
resPion = [ results . rows ] ;
callback ( null , resNoeuds , resPion ) ;
}
} ) ;
} ,
function ( resNoeuds , resPion , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'SELECT id_pion FROM carac_pions_page c JOIN page p ON c.id_page = p.id_page WHERE p.id_page = $1;' ,
2020-05-19 17:14:33 +00:00
values : [ resIP [ 0 ] . id _page ]
}
client . query ( query , function ( err , results ) {
if ( err ) {
callback ( err ) ;
} else {
if ( resPion [ 0 ] . length != 1 ) {
//mauvais diese
console . log ( resPion ) ;
callback ( { stack : 'mauvais diese add_box' } ) ;
//return false;
} else {
resPion . push ( results . rows ) ;
callback ( null , resNoeuds , resPion ) ;
}
}
} ) ;
} ,
function ( resNoeuds , resPion , callback ) {
//test pour savoir si on doit ajouter des caractéristiques de pion à la page
var test = false ;
var j = 0 ;
while ( ! test && ( j < resPion [ 1 ] . length ) ) {
if ( resPion [ 1 ] [ j ] . id _pion === resPion [ 0 ] [ 0 ] . id _pion ) {
test = true ;
}
j ++ ;
}
//si test est true, alors il y a un commun entre ceux sur la carac_pion_page et le nouveau
test = ! test ;
//maintenant, test = true indique qu'on doit rajouter la caractéristique
if ( ! test ) {
var N = 2 ;
} else {
var N = 4 ;
}
var r = {
resPion : resPion ,
resNoeuds : resNoeuds ,
test : test ,
N : N
}
const query = {
2020-05-23 08:44:15 +00:00
text : 'INSERT INTO box_on_page (id_page,id_pion,diese_box_on_page,num_box_on_page,noeud_start) SELECT $1' +
',$2, new_diese, new_num, $3 FROM box_on_page b JOIN (SELECT ' +
2020-05-19 17:14:33 +00:00
'CASE WHEN MAX(diese_box_on_page) IS NULL THEN 1 ELSE MAX(diese_box_on_page)+1 END new_diese,' +
'CASE WHEN MAX(num_box_on_page) IS NULL THEN 1 ELSE MAX(num_box_on_page)+1 END new_num,MAX(id_page) id_pageb' +
2020-05-23 08:44:15 +00:00
' FROM box_on_page GROUP BY id_page) AS bb ON b.id_page=bb.id_pageb' +
2020-05-19 17:14:33 +00:00
' WHERE id_page = $1 LIMIT 1; ' ,
values : [ resIP [ 0 ] . id _page , r . resPion [ 0 ] [ 0 ] . id _pion , r . resNoeuds [ 3 ] [ 0 ] . new _noeud ] ,
} ;
client . query ( query , function ( err , results ) {
if ( err ) {
callback ( err ) ;
} else {
//resTot.push(results.rows);
resTot = [ results . rows ] ;
callback ( null , r , resTot ) ;
}
} ) ;
} ,
function ( r , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'INSERT INTO stop_box_on_page (id_page,num_box_on_page,noeud_stop) SELECT $1' +
' ,MAX(num_box_on_page),0 FROM box_on_page WHERE id_page = $1 GROUP BY id_page;' ,
2020-05-19 17:14:33 +00:00
values : [ resIP [ 0 ] . id _page ]
} ;
queryChainedClient ( client , query , callback , r , resTot ) ;
} ,
function ( r , resTot , callback ) {
if ( r . test ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'INSERT INTO carac_pions_page (id_page,id_pion,change_pion,diese_bac_on_page,taille) SELECT $1,$2,$2,1,$3 ; ' ,
2020-05-19 17:14:33 +00:00
values : [ resIP [ 0 ] . id _page , r . resPion [ 0 ] [ 0 ] . id _pion , r . resPion [ 0 ] [ 0 ] . taille ]
} ;
client . query ( query , function ( err , results ) {
if ( err ) {
callback ( err ) ;
} else {
//resTot.push(results.rows);
resTot . push ( results . rows ) ;
callback ( null , r , resTot ) ;
}
} ) ;
} else {
callback ( null , r , resTot ) ;
}
} ,
function ( r , resTot , callback ) {
if ( r . test ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'INSERT INTO carac_pions_joueur_on_page (id_joueur,id_page,id_pion,change_pion,diese_bac_on_page,taille) SELECT id_joueur,' +
'$1,$2,$2,1,$3 FROM carac_pions_joueur_on_page WHERE id_page = $1 LIMIT 1; ' ,
2020-05-19 17:14:33 +00:00
values : [ resIP [ 0 ] . id _page , r . resPion [ 0 ] [ 0 ] . id _pion , r . resPion [ 0 ] [ 0 ] . taille ]
} ;
client . query ( query , function ( err , results ) {
if ( err ) {
callback ( err ) ;
} else {
//resTot.push(results.rows);
resTot . push ( results . rows ) ;
callback ( null , r , resTot ) ;
}
} ) ;
} else {
callback ( null , r , resTot ) ;
}
} ,
function ( r , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'SELECT MAX(diese_box_on_page) diese,MAX(num_box_on_page) num FROM box_on_page WHERE id_page = $1; ' ,
2020-05-19 17:14:33 +00:00
values : [ resIP [ 0 ] . id _page ]
} ;
client . query ( query , function ( err , results ) {
if ( err ) {
callback ( err ) ;
} else {
//resTot.push(results.rows);
resTot . push ( results . rows ) ;
callback ( null , r , resTot ) ;
}
} ) ;
} ,
function ( r , resTot , callback ) {
if ( r . test ) {
emit . push ( { text : 'getTabPion' , data : null } ) ;
emit _broad . push ( { text : 'getTabPion' , data : null } ) ;
}
emit . push ( { text : 'add_box' , data : { nom : requetes _tot [ nb _requete ] . data . nom , diese : resTot [ r . N ] [ 0 ] . diese , svg : r . resPion [ 0 ] [ 0 ] . svg _pion , num : resTot [ r . N ] [ 0 ] . num , noeud : r . resNoeuds [ 3 ] [ 0 ] . new _noeud } } ) ;
emit _broad . push ( { text : 'add_box' , data : { nom : requetes _tot [ nb _requete ] . data . nom , diese : resTot [ r . N ] [ 0 ] . diese , svg : r . resPion [ 0 ] [ 0 ] . svg _pion , num : resTot [ r . N ] [ 0 ] . num , noeud : r . resNoeuds [ 3 ] [ 0 ] . new _noeud } } ) ;
nb _requete += 1 ;
callback ( null , r . resNoeuds ) ;
}
] ) ;
}
//action add_board--------------------------------------------------------------
} else if ( requete . action === 'add_board' ) { //ok
if ( ! checkValide ( variables = { nom : '45' } , needsData = true , errMess = 'add_board' , data = requete . data ) ) {
console . log ( errMess + '.data incorrect dans add_board' ) ;
Abort = true ;
} else {
Array _waterfall = Array _waterfall . concat ( [
function ( resNoeuds , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'SELECT id_board,svg_board FROM board WHERE nom_board = $1;' ,
2020-05-19 17:14:33 +00:00
values : [ requetes _tot [ nb _requete ] . data . nom ]
} ;
client . query ( query ,
function ( err2 , resPion ) {
if ( err2 ) {
console . log ( 'erreur de add_board : ' + err2 . stack ) ;
callback ( err2 ) ;
//return false;
} else {
//verif que le pion et la board existent
if ( resPion . rows . length != 1 ) {
//mauvais diese
console . log ( 'erreur de add_board: mauvais pion' ) ;
callback ( { stack : 'erreur de add_board: mauvais pion' } ) ;
//return false;
} else {
var r = {
resPion : resPion . rows ,
resNoeuds , resNoeuds
}
callback ( null , r ) ;
}
}
}
) ;
} ,
function ( r , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'INSERT INTO board_on_page (id_page,diese_board_on_page,num_board_on_page,id_board,noeud_start) SELECT ' +
2020-05-19 17:14:33 +00:00
'$1, new_diese, new_num, $2,$3' +
2020-05-23 08:44:15 +00:00
' FROM page b LEFT JOIN (SELECT ' +
2020-05-19 17:14:33 +00:00
'CASE WHEN MAX(diese_board_on_page) IS NULL THEN 1 ELSE MAX(diese_board_on_page)+1 END new_diese, ' +
'CASE WHEN MAX(num_board_on_page) IS NULL THEN 1 ELSE MAX(num_board_on_page)+1 END new_num,MAX(id_page) id_pageb' +
2020-05-23 08:44:15 +00:00
' FROM board_on_page GROUP BY id_page) AS bb ON b.id_page=bb.id_pageb' +
2020-05-19 17:14:33 +00:00
' WHERE id_page = $1 LIMIT 1; ' ,
values : [ resIP [ 0 ] . id _page , r . resPion [ 0 ] . id _board , r . resNoeuds [ 3 ] [ 0 ] . new _noeud ] ,
} ;
client . query ( query , function ( err , results ) {
if ( err ) {
callback ( err ) ;
} else {
//resTot.push(results.rows);
resTot = [ results . rows ] ;
callback ( null , r , resTot ) ;
}
} ) ;
} ,
function ( r , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'INSERT INTO stop_board_on_page (id_page,num_board_on_page,noeud_stop) SELECT $1' +
' ,MAX(num_board_on_page),0 FROM board_on_page WHERE id_page = $1 GROUP BY id_page;' ,
2020-05-19 17:14:33 +00:00
values : [ resIP [ 0 ] . id _page ]
} ;
queryChainedClient ( client , query , callback , r , resTot ) ;
} ,
function ( r , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : ' SELECT MAX(diese_board_on_page) diese,MAX(num_board_on_page) num FROM board_on_page WHERE id_page = $1 GROUP BY id_page;' ,
2020-05-19 17:14:33 +00:00
values : [ resIP [ 0 ] . id _page ]
} ;
client . query ( query , function ( err , results ) {
if ( err ) {
callback ( err ) ;
} else {
//resTot.push(results.rows);
resTot . push ( results . rows ) ;
callback ( null , r , resTot ) ;
}
} ) ;
} ,
function ( r , resTot , callback ) {
emit . push ( { text : 'add_board' , data : { nom : requetes _tot [ nb _requete ] . data . nom , diese : resTot [ 2 ] [ 0 ] . diese , svg : r . resPion [ 0 ] . svg _board , num : resTot [ 2 ] [ 0 ] . num , noeud : r . resNoeuds [ 3 ] [ 0 ] . new _noeud } } ) ;
emit _broad . push ( { text : 'add_board' , data : { nom : requetes _tot [ nb _requete ] . data . nom , diese : resTot [ 2 ] [ 0 ] . diese , svg : r . resPion [ 0 ] . svg _board , num : resTot [ 2 ] [ 0 ] . num , noeud : r . resNoeuds [ 3 ] [ 0 ] . new _noeud } } ) ;
nb _requete += 1 ;
callback ( null , r . resNoeuds )
}
] ) ;
}
//action removeElement--------------------------------------------------------------
} else if ( requete . action === 'removeElement' ) { //
if ( ! checkValide ( variables = { id : '45' } , needsData = true , errMess = 'removeElement' , data = requete . data ) ) {
console . log ( errMess + '.data incorrect dans removeElement' ) ;
Abort = true ;
} else {
var classe = /^[a-z]+/ . exec ( requete . data . id ) . toString ( ) ;
var id = parseInt ( /[0-9]+$/ . exec ( requete . data . id ) , 10 ) ;
if ( isNaN ( id ) && classe != 'trash' ) {
console . log ( 'mauvais diese1' ) ;
} else if ( classe === 'board' ) { //ok
Array _waterfall = Array _waterfall . concat ( [
function ( resNoeuds , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : ' SELECT id_page,num_board_on_page,$1 FROM board_on_page p' +
' LEFT JOIN (SELECT COUNT(noeud_stop) nbstop,MAX(id_page) id, MAX(num_board_on_page) num FROM stop_board_on_page' +
2020-05-19 17:14:33 +00:00
' WHERE id_page = $2 AND ARRAY[noeud_stop] <@ $3 GROUP BY id_page,num_board_on_page) AS g' +
' ON p.id_page=g.id AND p.num_board_on_page = g.num' +
' WHERE id_page = $2 AND diese_board_on_page = $4 AND ARRAY[noeud_start] <@ $3 AND ( COALESCE(g.nbstop,0)=0 OR g.nbstop=0) ;' ,
values : [ resIP [ 0 ] . noeud _courant , resIP [ 0 ] . id _page , resNoeuds [ 4 ] , id ]
}
client . query ( query ,
function ( err , res2 ) {
if ( err ) {
console . log ( 'erreur remove board: ' + err . stack ) ;
callback ( err ) ;
} else if ( res2 . rows . length != 1 ) {
//console.log('bad id remove board');
callback ( { stack : 'bad id remove board' } ) ;
} else {
callback ( null , resNoeuds ) ;
}
}
) ;
} ,
function ( resNoeuds , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'INSERT INTO stop_pion_on_board_on_page (id_page,diese_board_on_page,num_pion_on_board_on_page,noeud_stop)' +
' SELECT id_page,diese_board_on_page,num_pion_on_board_on_page,$1 FROM pion_on_board_on_page p' +
' LEFT JOIN (SELECT COUNT(noeud_stop) nbstop,MAX(id_page) id, MAX(num_pion_on_board_on_page) num, MAX(diese_board_on_page) diese FROM stop_pion_on_board_on_page' +
2020-05-19 17:14:33 +00:00
' WHERE id_page = $2 AND diese_board_on_page = $3 AND ARRAY[noeud_stop] <@ $4 GROUP BY id_page,diese_board_on_page,num_pion_on_board_on_page) AS g' +
' ON p.id_page=g.id AND p.num_pion_on_board_on_page = g.num AND p.diese_board_on_page=g.diese' +
' WHERE id_page = $2 AND diese_board_on_page = $3 AND ARRAY[noeud_start] <@ $4 AND ( COALESCE(g.nbstop,0)=0 OR g.nbstop=0) ;' ,
values : [ resNoeuds [ 3 ] [ 0 ] . new _noeud , resIP [ 0 ] . id _page , id , resNoeuds [ 4 ] ] ,
} ;
client . query ( query , function ( err , results ) {
if ( err ) {
callback ( err ) ;
} else {
//resTot.push(results.rows);
resTot = [ results . rows ] ;
callback ( null , resNoeuds , resTot ) ;
}
} ) ;
} ,
function ( resNoeuds , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'INSERT INTO stop_board_on_page (id_page,num_board_on_page,noeud_stop)' +
' SELECT id_page,num_board_on_page,$1 FROM board_on_page p' +
' LEFT JOIN (SELECT COUNT(noeud_stop) nbstop,MAX(id_page) id, MAX(num_board_on_page) num FROM stop_board_on_page' +
2020-05-19 17:14:33 +00:00
' WHERE id_page = $2 AND ARRAY[noeud_stop] <@ $3 GROUP BY id_page,num_board_on_page) AS g' +
' ON p.id_page=g.id AND p.num_board_on_page = g.num' +
' WHERE id_page = $2 AND diese_board_on_page = $4 AND ARRAY[noeud_start] <@ $3 AND ( COALESCE(g.nbstop,0)=0 OR g.nbstop=0) ;' ,
values : [ resNoeuds [ 3 ] [ 0 ] . new _noeud , resIP [ 0 ] . id _page , resNoeuds [ 4 ] , id ]
} ;
client . query ( query , function ( err , results ) {
if ( err ) {
callback ( err ) ;
} else {
//resTot.push(results.rows);
resTot . push ( results . rows ) ;
callback ( null , resNoeuds , resTot ) ;
}
} ) ;
} ,
function ( resNoeuds , resTot , callback ) {
emit . push ( { text : 'removeElement' , data : { id : classe + '-' + id , noeud : resNoeuds [ 3 ] [ 0 ] . new _noeud } } ) ;
emit _broad . push ( { text : 'removeElement' , data : { id : classe + '-' + id , noeud : resNoeuds [ 3 ] [ 0 ] . new _noeud } } ) ;
nb _requete += 1 ;
callback ( null , resNoeuds ) ;
}
] ) ;
} else if ( classe === 'bac' ) { //ok
Array _waterfall = Array _waterfall . concat ( [
function ( resNoeuds , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : ' SELECT num_bac_on_page FROM bac_on_page p' +
' LEFT JOIN (SELECT COUNT(noeud_stop) nbstop,MAX(id_page) id, MAX(num_bac_on_page) num FROM stop_bac_on_page' +
2020-05-19 17:14:33 +00:00
' WHERE id_page = $1 AND ARRAY[noeud_stop] <@ $2 GROUP BY id_page,num_bac_on_page) AS g' +
' ON p.id_page=g.id AND p.num_bac_on_page = g.num' +
' WHERE id_page = $1 AND diese_bac_on_page = $3 AND ARRAY[noeud_start] <@ $2 AND ( COALESCE(g.nbstop,0)=0 OR g.nbstop=0) ;' ,
values : [ resIP [ 0 ] . id _page , resNoeuds [ 4 ] , id ]
}
client . query ( query ,
function ( err , res2 ) {
if ( err ) {
console . log ( 'erreur remove bac: ' + err . stack ) ;
callback ( err )
} else if ( res2 . rows . length != 1 ) {
//console.log('bad id remove bac');
callback ( { stack : 'bad id remove bac' } ) ;
} else {
callback ( null , resNoeuds ) ;
}
}
) ;
} ,
function ( resNoeuds , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'UPDATE joueur_on_page SET click_board_move_bac = null WHERE id_page = $1 AND click_board_move_bac = $2 ; ' ,
2020-05-19 17:14:33 +00:00
values : [ resIP [ 0 ] . id _page , id ]
} ;
client . query ( query , function ( err , results ) {
if ( err ) {
callback ( err ) ;
} else {
//resTot.push(results.rows);
resTot = [ results . rows ] ;
callback ( null , resNoeuds , resTot ) ;
}
} ) ;
} ,
function ( resNoeuds , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'UPDATE carac_pions_page SET diese_bac_on_page = null WHERE id_page = $1 AND diese_bac_on_page = $2 ; ' ,
2020-05-19 17:14:33 +00:00
values : [ resIP [ 0 ] . id _page , id ]
} ;
queryChainedClient ( client , query , callback , resNoeuds , resTot ) ;
} ,
function ( resNoeuds , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'UPDATE carac_pions_joueur_on_page SET diese_bac_on_page = null WHERE id_page = $1 AND diese_bac_on_page = $2 ; ' ,
2020-05-19 17:14:33 +00:00
values : [ resIP [ 0 ] . id _page , id ]
} ;
queryChainedClient ( client , query , callback , resNoeuds , resTot ) ;
} ,
function ( resNoeuds , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'INSERT INTO stop_pion_on_bac_on_page (id_page,diese_bac_on_page,num_pion_on_bac_on_page,noeud_stop)' +
' SELECT id_page,diese_bac_on_page,num_pion_on_bac_on_page,$1 FROM pion_on_bac_on_page p' +
' LEFT JOIN (SELECT COUNT(noeud_stop) nbstop,MAX(id_page) id, MAX(num_pion_on_bac_on_page) num, MAX(diese_bac_on_page) diese FROM stop_pion_on_bac_on_page' +
2020-05-19 17:14:33 +00:00
' WHERE id_page = $2 AND diese_bac_on_page = $3 AND ARRAY[noeud_stop] <@ $4 GROUP BY id_page,diese_bac_on_page,num_pion_on_bac_on_page) AS g' +
' ON p.id_page=g.id AND p.num_pion_on_bac_on_page = g.num AND p.diese_bac_on_page=g.diese' +
' WHERE id_page = $2 AND diese_bac_on_page = $3 AND ARRAY[noeud_start] <@ $4 AND ( COALESCE(g.nbstop,0)=0 OR g.nbstop=0) ;' ,
values : [ resNoeuds [ 3 ] [ 0 ] . new _noeud , resIP [ 0 ] . id _page , id , resNoeuds [ 4 ] ]
} ;
queryChainedClient ( client , query , callback , resNoeuds , resTot ) ;
} ,
function ( resNoeuds , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'INSERT INTO stop_bac_on_page (id_page,num_bac_on_page,noeud_stop)' +
' SELECT id_page,num_bac_on_page,$1 FROM bac_on_page p' +
' LEFT JOIN (SELECT COUNT(noeud_stop) nbstop,MAX(id_page) id, MAX(num_bac_on_page) num FROM stop_bac_on_page' +
2020-05-19 17:14:33 +00:00
' WHERE id_page = $2 AND ARRAY[noeud_stop] <@ $3 GROUP BY id_page,num_bac_on_page) AS g' +
' ON p.id_page=g.id AND p.num_bac_on_page = g.num' +
' WHERE id_page = $2 AND diese_bac_on_page = $4 AND ARRAY[noeud_start] <@ $3 AND ( COALESCE(g.nbstop,0)=0 OR g.nbstop=0) ;' ,
values : [ resNoeuds [ 3 ] [ 0 ] . new _noeud , resIP [ 0 ] . id _page , resNoeuds [ 4 ] , id ]
} ;
client . query ( query , function ( err , results ) {
if ( err ) {
callback ( err ) ;
} else {
//resTot.push(results.rows);
resTot . push ( results . rows ) ;
callback ( null , resNoeuds , resTot ) ;
}
} ) ;
} ,
function ( resNoeuds , resTot , callback ) {
emit . push ( { text : 'removeElement' , data : { id : classe + '-' + id , noeud : resNoeuds [ 3 ] [ 0 ] . new _noeud } } ) ;
emit _broad . push ( { text : 'removeElement' , data : { id : classe + '-' + id , noeud : resNoeuds [ 3 ] [ 0 ] . new _noeud } } ) ;
nb _requete += 1 ;
callback ( null , resNoeuds ) ;
}
] ) ;
} else if ( classe === 'box' ) { //ok
Array _waterfall = Array _waterfall . concat ( [
function ( resNoeuds , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : ' SELECT num_box_on_page,$1 FROM box_on_page p' +
' LEFT JOIN (SELECT COUNT(noeud_stop) nbstop,MAX(id_page) id, MAX(num_box_on_page) num FROM stop_box_on_page' +
2020-05-19 17:14:33 +00:00
' WHERE id_page = $2 AND ARRAY[noeud_stop] <@ $3 GROUP BY id_page,num_box_on_page) AS g' +
' ON p.id_page=g.id AND p.num_box_on_page = g.num' +
' WHERE id_page = $2 AND diese_box_on_page = $4 AND ARRAY[noeud_start] <@ $3 AND ( COALESCE(g.nbstop,0)=0 OR g.nbstop=0) ;' ,
values : [ resIP [ 0 ] . noeud _courant , resIP [ 0 ] . id _page , resNoeuds [ 4 ] , id ]
}
client . query ( query ,
function ( err , res2 ) {
if ( err ) {
//console.log('erreur remove box: '+err.stack);
scallback ( err ) ;
} else if ( res2 . rows . length != 1 ) {
//console.log('bad id remove box');
//socket.emit('refresh');
callback ( { stack : 'bad id remove box' } ) ;
} else {
callback ( null , resNoeuds ) ;
}
}
) ;
} ,
function ( resNoeuds , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'INSERT INTO stop_box_on_page (id_page,num_box_on_page,noeud_stop)' +
' SELECT id_page,num_box_on_page,$1 FROM box_on_page p' +
' LEFT JOIN (SELECT COUNT(noeud_stop) nbstop,MAX(id_page) id, MAX(num_box_on_page) num FROM stop_box_on_page' +
2020-05-19 17:14:33 +00:00
' WHERE id_page = $2 AND ARRAY[noeud_stop] <@ $3 GROUP BY id_page,num_box_on_page) AS g' +
' ON p.id_page=g.id AND p.num_box_on_page = g.num' +
' WHERE id_page = $2 AND diese_box_on_page = $4 AND ARRAY[noeud_start] <@ $3 AND ( COALESCE(g.nbstop,0)=0 OR g.nbstop=0) ;' ,
values : [ resNoeuds [ 3 ] [ 0 ] . new _noeud , resIP [ 0 ] . id _page , resNoeuds [ 4 ] , id ]
}
client . query ( query ,
function ( err ) {
if ( err ) {
console . log ( 'erreure de remove : ' + err . stack ) ;
//socket.emit('refresh');
callback ( err ) ;
//return false;
} else {
emit . push ( { text : 'removeElement' , data : { id : classe + '-' + id , noeud : resNoeuds [ 3 ] [ 0 ] . new _noeud } } ) ;
emit _broad . push ( { text : 'removeElement' , data : { id : classe + '-' + id , noeud : resNoeuds [ 3 ] [ 0 ] . new _noeud } } ) ;
nb _requete += 1 ;
callback ( null , resNoeuds ) ;
}
}
) ;
}
] ) ;
} else if ( classe === 'pion' ) { //ok
Array _waterfall = Array _waterfall . concat ( [
function ( resNoeuds , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : ' SELECT num_pion_on_board_on_page FROM pion_on_board_on_page p' +
' LEFT JOIN (SELECT COUNT(noeud_stop) nbstop,MAX(id_page) id, MAX(num_pion_on_board_on_page) num, MAX(diese_board_on_page) diese FROM stop_pion_on_board_on_page' +
2020-05-19 17:14:33 +00:00
' WHERE id_page = $1 AND ARRAY[noeud_stop] <@ $2 GROUP BY id_page,diese_board_on_page,num_pion_on_board_on_page) AS g' +
' ON p.id_page=g.id AND p.num_pion_on_board_on_page = g.num AND p.diese_board_on_page=g.diese' +
' WHERE id_page = $1 AND num_pion_on_board_on_page = $3 AND ARRAY[noeud_start] <@ $2 AND ( COALESCE(g.nbstop,0)=0 OR g.nbstop=0) ;' ,
values : [ resIP [ 0 ] . id _page , resNoeuds [ 4 ] , id ]
}
client . query ( query ,
function ( err , res2 ) {
if ( err ) {
//console.log('erreur remove pion: '+err.stack);
//socket.emit('refresh');
callback ( err ) ;
} else if ( res2 . rows . length != 1 ) {
//console.log('bad id remove pion');
callback ( { stack : 'bad id remove pion' } ) ;
//socket.emit('refresh');
} else {
callback ( null , resNoeuds ) ;
}
}
) ;
} ,
function ( resNoeuds , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'INSERT INTO stop_pion_on_board_on_page (id_page,diese_board_on_page,num_pion_on_board_on_page,noeud_stop)' +
' SELECT id_page,diese_board_on_page,num_pion_on_board_on_page,$1 FROM pion_on_board_on_page p' +
' LEFT JOIN (SELECT COUNT(noeud_stop) nbstop,MAX(id_page) id, MAX(num_pion_on_board_on_page) num, MAX(diese_board_on_page) diese FROM stop_pion_on_board_on_page' +
2020-05-19 17:14:33 +00:00
' WHERE id_page = $2 AND ARRAY[noeud_stop] <@ $3 GROUP BY id_page,diese_board_on_page,num_pion_on_board_on_page) AS g' +
' ON p.id_page=g.id AND p.num_pion_on_board_on_page = g.num AND p.diese_board_on_page=g.diese' +
' WHERE id_page = $2 AND num_pion_on_board_on_page = $4 AND ARRAY[noeud_start] <@ $3 AND ( COALESCE(g.nbstop,0)=0 OR g.nbstop=0) ;' ,
values : [ resNoeuds [ 3 ] [ 0 ] . new _noeud , resIP [ 0 ] . id _page , resNoeuds [ 4 ] , id ]
}
client . query ( query ,
function ( err , res2 ) {
if ( err ) {
console . log ( 'erreure de remove : ' + err . stack ) ;
//socket.emit('refresh');
callback ( err ) ;
} else {
//console.log(res2)
emit . push ( { text : 'removeElement' , data : { id : classe + '-' + id , noeud : resNoeuds [ 3 ] [ 0 ] . new _noeud } } ) ;
emit _broad . push ( { text : 'removeElement' , data : { id : classe + '-' + id , noeud : resNoeuds [ 3 ] [ 0 ] . new _noeud } } ) ;
nb _requete += 1 ;
callback ( null , resNoeuds ) ;
}
}
) ;
}
] ) ;
} else if ( classe === 'groupe' ) { //ok
Array _waterfall = Array _waterfall . concat ( [
function ( resNoeuds , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'SELECT num_pion_on_bac_on_page FROM pion_on_bac_on_page p' +
' LEFT JOIN (SELECT COUNT(noeud_stop) nbstop,MAX(id_page) id, MAX(num_pion_on_bac_on_page) num, MAX(diese_bac_on_page) diese FROM stop_pion_on_bac_on_page' +
2020-05-19 17:14:33 +00:00
' WHERE id_page = $1 AND ARRAY[noeud_stop] <@ $2 GROUP BY id_page,diese_bac_on_page,num_pion_on_bac_on_page) AS g' +
' ON p.id_page=g.id AND p.num_pion_on_bac_on_page = g.num AND p.diese_bac_on_page=g.diese' +
' WHERE id_page = $1 AND num_pion_on_bac_on_page = $3 AND ARRAY[noeud_start] <@ $2 AND ( COALESCE(g.nbstop,0)=0 OR g.nbstop=0) ;' ,
values : [ resIP [ 0 ] . id _page , resNoeuds [ 4 ] , id ]
}
client . query ( query ,
function ( err , res2 ) {
if ( err ) {
//console.log('erreur remove groupe: '+err.stack);
//socket.emit('refresh');
callback ( err ) ;
} else if ( res2 . rows . length != 1 ) {
//console.log('bad id remove groupe');
//socket.emit('refresh');
callback ( { stack : 'bad id remove groupe' } ) ;
} else {
callback ( null , resNoeuds ) ;
}
}
) ;
} ,
function ( resNoeuds , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'INSERT INTO stop_pion_on_bac_on_page (id_page,diese_bac_on_page,num_pion_on_bac_on_page,noeud_stop)' +
' SELECT id_page,diese_bac_on_page,num_pion_on_bac_on_page,$1 FROM pion_on_bac_on_page p' +
' LEFT JOIN (SELECT COUNT(noeud_stop) nbstop,MAX(id_page) id, MAX(num_pion_on_bac_on_page) num, MAX(diese_bac_on_page) diese FROM stop_pion_on_bac_on_page' +
2020-05-19 17:14:33 +00:00
' WHERE id_page = $2 AND ARRAY[noeud_stop] <@ $3 GROUP BY id_page,diese_bac_on_page,num_pion_on_bac_on_page) AS g' +
' ON p.id_page=g.id AND p.num_pion_on_bac_on_page = g.num AND p.diese_bac_on_page=g.diese' +
' WHERE id_page = $2 AND num_pion_on_bac_on_page = $4 AND ARRAY[noeud_start] <@ $3 AND ( COALESCE(g.nbstop,0)=0 OR g.nbstop=0) ;' ,
values : [ resNoeuds [ 3 ] [ 0 ] . new _noeud , resIP [ 0 ] . id _page , resNoeuds [ 4 ] , id ]
}
client . query ( query ,
function ( err ) {
if ( err ) {
//console.log('erreure de remove : '+err.stack);
//socket.emit('refresh');
callback ( err ) ;
//return false;
} else {
emit . push ( { text : 'removeElement' , data : { id : classe + '-' + id , noeud : resNoeuds [ 3 ] [ 0 ] . new _noeud } } ) ;
emit _broad . push ( { text : 'removeElement' , data : { id : classe + '-' + id , noeud : resNoeuds [ 3 ] [ 0 ] . new _noeud } } ) ;
nb _requete += 1 ;
callback ( null , resNoeuds ) ;
//return true;
}
}
) ;
}
] ) ;
} else {
console . log ( 'erreur de remove : mauvaise classe' ) ;
Abort = true ;
//return false;
}
}
//action changePionOnboard--------------------------------------------------------------
} else if ( requete . action === 'changePionOnboard' ) { //ok
if ( ! checkValide ( variables = {
diese : 1 ,
type : '45' ,
top : 1 ,
left : 1 ,
height : 1 ,
board : 1 } , needsData = true , errMess = 'changePionOnboard' , data = requete . data ) ) {
console . log ( requete . data ) ;
console . log ( errMess + '.data incorrect dans changePionOnboard' ) ;
Abort = true
} else {
Array _waterfall = Array _waterfall . concat ( [
function ( resNoeuds , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'SELECT num_pion_on_board_on_page,diese_board_on_page FROM pion_on_board_on_page p' +
' LEFT JOIN (SELECT COUNT(noeud_stop) nbstop,MAX(id_page) id, MAX(num_pion_on_board_on_page) num, MAX(diese_board_on_page) diese FROM stop_pion_on_board_on_page' +
2020-05-19 17:14:33 +00:00
' WHERE id_page = $1 AND ARRAY[noeud_stop] <@ $2 GROUP BY id_page,diese_board_on_page,num_pion_on_board_on_page) AS g' +
' ON p.id_page=g.id AND p.num_pion_on_board_on_page = g.num AND p.diese_board_on_page=g.diese' +
' WHERE id_page = $1 AND diese_pion_on_board_on_page = $3' +
' AND ARRAY[noeud_start] <@ $2 AND ( COALESCE(g.nbstop,0)=0 OR g.nbstop=0) ;' ,
values : [ resIP [ 0 ] . id _page , resNoeuds [ 4 ] , requetes _tot [ nb _requete ] . data . diese ]
}
//console.log(noeuds)
client . query ( query , function ( err , results ) {
if ( err ) {
callback ( err ) ;
} else {
//resTot.push(results.rows);
results2 = [ results . rows ] ;
callback ( null , resNoeuds , results2 ) ;
}
} ) ;
} ,
function ( resNoeuds , results2 , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'SELECT id_pion,taille,svg_pion,directed FROM pion WHERE nom_pion = $1;' ,
2020-05-19 17:14:33 +00:00
values : [ requetes _tot [ nb _requete ] . data . type ]
}
queryChainedClient ( client , query , callback , resNoeuds , results2 ) ;
} ,
function ( resNoeuds , results2 , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'SELECT c.id_pion FROM carac_pions_page c JOIN pion p ON c.id_pion = p.id_pion WHERE p.nom_pion = $1 AND c.id_page = $2; ' ,
2020-05-19 17:14:33 +00:00
values : [ requetes _tot [ nb _requete ] . data . type , resIP [ 0 ] . id _page ]
}
queryChainedClient ( client , query , callback , resNoeuds , results2 ) ;
} ,
function ( resNoeuds , results2 , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'SELECT num_board_on_page FROM board_on_page b' +
' LEFT JOIN (SELECT COUNT(noeud_stop) nbstop,MAX(id_page) id, MAX(num_board_on_page) num FROM stop_board_on_page' +
2020-05-19 17:14:33 +00:00
' WHERE id_page = $1 AND ARRAY[noeud_stop] <@ $2 GROUP BY id_page,num_board_on_page) AS g' +
' ON b.id_page=g.id AND b.num_board_on_page = g.num' +
' WHERE b.id_page = $1 AND diese_board_on_page=$3' +
' AND ARRAY[b.noeud_start] <@ $2 AND ( COALESCE(g.nbstop,0)=0 OR g.nbstop=0) ;' ,
values : [ resIP [ 0 ] . id _page , resNoeuds [ 4 ] , requetes _tot [ nb _requete ] . data . board ]
}
client . query ( query , function ( err , results ) {
results2 . push ( results . rows ) ;
if ( err ) {
callback ( err ) ;
} else if ( results2 [ 0 ] . length != 1 || results2 [ 3 ] . length != 1 ) {
//mauvais diese
callback ( { stack : 'mauvais diese changePionOnBoard' } ) ;
} else if ( results2 [ 1 ] . length != 1 ) {
//pas le bon pion
callback ( { stack : 'mauvais pion changePionOnBoard' } ) ;
} else {
var r = {
resNoeuds : resNoeuds ,
results2 : results2
} ;
callback ( null , r ) ;
}
} ) ;
} ,
function ( r , callback ) {
//test pour savoir si on doit ajouter des caractéristiques de pion à la page
r . test = results2 [ 1 ] . length != 1 ;
const query = {
2020-05-23 08:44:15 +00:00
text : 'INSERT INTO pion_on_board_on_page (id_page,diese_pion_on_board_on_page,num_pion_on_board_on_page,id_pion,diese_board_on_page,position_x,position_y,taille,noeud_start) ' +
2020-05-19 17:14:33 +00:00
' SELECT $1,$2, pp.new_num,$3,$4,$5,$6,$7, new_noeud ' +
2020-05-23 08:44:15 +00:00
' FROM page p LEFT JOIN ' +
'(SELECT MAX(id_page) id_pageb,MAX(noeud_suivant) new_noeud FROM save_on_page GROUP BY id_page) AS s ON p.id_page=s.id_pageb' +
' LEFT JOIN (SELECT MAX(num_pion_on_board_on_page)+1 new_num, MAX(id_page) id_pagec FROM pion_on_board_on_page GROUP BY id_page) AS pp ON p.id_page=pp.id_pagec' +
2020-05-19 17:14:33 +00:00
' WHERE id_page = $1 LIMIT 1; ' ,
values : [ resIP [ 0 ] . id _page , requetes _tot [ nb _requete ] . data . diese , r . results2 [ 1 ] [ 0 ] . id _pion , requetes _tot [ nb _requete ] . data . board , requetes _tot [ nb _requete ] . data . left , requetes _tot [ nb _requete ] . data . top , requetes _tot [ nb _requete ] . data . height ]
} ;
client . query ( query , function ( err , results ) {
if ( err ) {
callback ( err ) ;
} else {
//resTot.push(results.rows);
result3 = [ results . rows ] ;
callback ( null , r , result3 ) ;
}
} ) ;
} ,
function ( r , result3 , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'INSERT INTO stop_pion_on_board_on_page (id_page,diese_board_on_page,num_pion_on_board_on_page,noeud_stop)' +
2020-05-19 17:14:33 +00:00
' SELECT $1,$2,$3,$4; ' ,
values : [ resIP [ 0 ] . id _page , r . results2 [ 0 ] [ 0 ] . diese _board _on _page , r . results2 [ 0 ] [ 0 ] . num _pion _on _board _on _page , r . resNoeuds [ 3 ] [ 0 ] . new _noeud ] ,
} ;
queryChainedClient ( client , query , callback , r , result3 ) ;
} ,
function ( r , result3 , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : ' SELECT MAX(num_pion_on_board_on_page) num FROM pion_on_board_on_page WHERE id_page = $1 GROUP BY id_page;' ,
2020-05-19 17:14:33 +00:00
values : [ resIP [ 0 ] . id _page ]
}
queryChainedClient ( client , query , callback , r , result3 ) ;
} ,
function ( r , result3 , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
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,$2 ,MAX(num_pion_on_board_on_page),0 FROM pion_on_board_on_page WHERE id_page = $1 GROUP BY id_page;' ,
2020-05-19 17:14:33 +00:00
values : [ resIP [ 0 ] . id _page , requetes _tot [ nb _requete ] . data . board ]
}
queryChainedClient ( client , query , callback , r , result3 ) ;
} ,
function ( r , result3 , callback ) {
if ( r . test ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'INSERT INTO carac_pions_page (id_page,id_pion,change_pion,diese_bac_on_page,taille) SELECT ($1,$2,$2,1,$3) ; ' ,
2020-05-19 17:14:33 +00:00
values : [ resIP [ 0 ] . id _page , r . results2 [ 1 ] [ 0 ] . id _pion , r . results2 [ 1 ] [ 0 ] . taille ]
} ;
client . query ( query , function ( err , results ) {
if ( err ) {
callback ( err ) ;
} else {
//resTot.push(results.rows);
result3 . push ( results . rows ) ;
callback ( null , r , result3 ) ;
}
} ) ;
} else {
callback ( null , r , result3 ) ;
}
} ,
function ( r , result3 , callback ) {
if ( r . test ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'INSERT INTO carac_pions_joueur_on_page (id_joueur,id_page,id_pion,change_pion,diese_bac_on_page,taille) SELECT id_joueur,$1,$2,$2,1,$3 FROM carac_pions_joueur_on_page WHERE id_page = $1 LIMIT 1; ' ,
2020-05-19 17:14:33 +00:00
values : [ resIP [ 0 ] . id _page , results2 [ 1 ] [ 0 ] . id _pion , results2 [ 1 ] [ 0 ] . taille ]
} ;
client . query ( query , function ( err , results ) {
if ( err ) {
callback ( err ) ;
} else {
//resTot.push(results.rows);
result3 . push ( results . rows ) ;
callback ( null , r , result3 ) ;
}
} ) ;
} else {
callback ( null , r , result3 ) ;
}
} ,
function ( r , result3 , callback ) {
if ( r . test ) {
emit . push ( { text : 'getTabPion' , data : null } ) ;
emit _broad . push ( { text : 'getTabPion' , data : null } ) ;
}
emit . push ( { text : 'removeElement' , data : { id : 'pion-' + r . results2 [ 0 ] [ 0 ] . num _pion _on _board _on _page , noeud : r . resNoeuds [ 3 ] [ 0 ] . new _noeud } } ) ;
emit _broad . push ( { text : 'removeElement' , data : { id : 'pion-' + r . results2 [ 0 ] [ 0 ] . num _pion _on _board _on _page , noeud : r . resNoeuds [ 3 ] [ 0 ] . new _noeud } } ) ;
requetes _tot [ nb _requete ] . data . svg = r . results2 [ 1 ] [ 0 ] . svg _pion ;
requetes _tot [ nb _requete ] . data . directed = r . results2 [ 1 ] [ 0 ] . directed ;
requetes _tot [ nb _requete ] . data . num = result3 [ 2 ] [ 0 ] . num ;
requetes _tot [ nb _requete ] . data . noeud = r . resNoeuds [ 3 ] [ 0 ] . new _noeud ;
emit . push ( { text : 'createPionOnboard' , data : requetes _tot [ nb _requete ] . data } ) ;
emit _broad . push ( { text : 'createPionOnboard' , data : requetes _tot [ nb _requete ] . data } ) ;
nb _requete += 1 ;
callback ( null , r . resNoeuds ) ;
}
] ) ;
}
//action modifyGroup--------------------------------------------------------------
} else if ( requete . action === 'modifyGroup' ) { //ok
if ( ! checkValide ( variables = {
diese : 1 ,
n : 1 ,
bac : 1 ,
type : '' } ,
needsData = true , errMess = 'modifyGroup' , data = requete . data ) ) {
console . log ( errMess + '.data incorrect dans modifyGroup' ) ;
Abort = true
} else {
Array _waterfall = Array _waterfall . concat ( [
function ( resNoeuds , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'SELECT num_pion_on_bac_on_page FROM pion_on_bac_on_page p' +
' LEFT JOIN (SELECT COUNT(noeud_stop) nbstop,MAX(id_page) id, MAX(num_pion_on_bac_on_page) num, MAX(diese_bac_on_page) diese FROM stop_pion_on_bac_on_page' +
2020-05-19 17:14:33 +00:00
' WHERE id_page = $1 AND ARRAY[noeud_stop] <@ $2 GROUP BY id_page,diese_bac_on_page,num_pion_on_bac_on_page) AS g' +
' ON p.id_page=g.id AND p.num_pion_on_bac_on_page = g.num AND p.diese_bac_on_page=g.diese' +
' WHERE id_page = $1 AND diese_pion_on_bac_on_page = $3' +
' AND ARRAY[noeud_start] <@ $2 AND ( COALESCE(g.nbstop,0)=0 OR g.nbstop=0) ;' ,
values : [ resIP [ 0 ] . id _page , resNoeuds [ 4 ] , requetes _tot [ nb _requete ] . data . diese ]
}
client . query ( query , function ( err , results ) {
if ( err ) {
callback ( err ) ;
} else {
//resTot.push(results.rows);
results2 = [ results . rows ] ;
callback ( null , resNoeuds , results2 ) ;
}
} ) ;
} ,
function ( resNoeuds , results2 , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'SELECT id_pion,taille,svg_pion,directed FROM pion WHERE nom_pion = $1;' ,
2020-05-19 17:14:33 +00:00
values : [ requetes _tot [ nb _requete ] . data . type ]
}
queryChainedClient ( client , query , callback , resNoeuds , results2 ) ;
} ,
function ( resNoeuds , results2 , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'SELECT c.id_pion FROM carac_pions_page c JOIN pion p ON c.id_pion = p.id_pion WHERE p.nom_pion = $1 AND c.id_page = $2; ' ,
2020-05-19 17:14:33 +00:00
values : [ requetes _tot [ nb _requete ] . data . type , resIP [ 0 ] . id _page ]
}
queryChainedClient ( client , query , callback , resNoeuds , results2 ) ;
} ,
function ( resNoeuds , results2 , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'SELECT num_bac_on_page FROM bac_on_page b' +
' LEFT JOIN (SELECT COUNT(noeud_stop) nbstop,MAX(id_page) id, MAX(num_bac_on_page) num FROM stop_bac_on_page' +
2020-05-19 17:14:33 +00:00
' WHERE id_page = $1 AND ARRAY[noeud_stop] <@ $2 GROUP BY id_page,num_bac_on_page) AS g' +
' ON b.id_page=g.id AND b.num_bac_on_page = g.num' +
' WHERE b.id_page = $1 AND diese_bac_on_page=$3' +
' AND ARRAY[b.noeud_start] <@ $2 AND ( COALESCE(g.nbstop,0)=0 OR g.nbstop=0) ;' ,
values : [ resIP [ 0 ] . id _page , resNoeuds [ 4 ] , requetes _tot [ nb _requete ] . data . bac ]
}
client . query ( query , function ( err , results ) {
results2 . push ( results . rows ) ;
if ( err ) {
callback ( err ) ;
//verif que le pion et la board existent
} else if ( results2 [ 0 ] . length != 1 || results2 [ 3 ] . length != 1 ) {
callback ( { stack : 'mauvais diese2' } ) ;
} else if ( results2 [ 1 ] . length != 1 ) {
//pas le bon pion
callback ( { stack : 'pion incorrect' } ) ;
} else {
r = {
resNoeuds : resNoeuds ,
results2 : results2
} ;
callback ( null , r ) ;
}
} ) ;
} ,
function ( r , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'INSERT INTO stop_pion_on_bac_on_page (id_page,diese_bac_on_page,num_pion_on_bac_on_page,noeud_stop)' +
2020-05-19 17:14:33 +00:00
' SELECT $1,$2,$3,$4; ' ,
values : [ resIP [ 0 ] . id _page , requetes _tot [ nb _requete ] . data . bac , r . results2 [ 0 ] [ 0 ] . num _pion _on _bac _on _page , r . resNoeuds [ 3 ] [ 0 ] . new _noeud ] ,
} ;
client . query ( query , function ( err , results ) {
if ( err ) {
callback ( err ) ;
} else {
//resTot.push(results.rows);
result3 = [ results . rows ] ;
callback ( null , r , result3 ) ;
}
} ) ;
} ,
function ( r , result3 , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
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,$2,$3,$4,$5, new_num, new_noeud FROM page p' +
' LEFT JOIN (SELECT MAX(id_page) id_pagea,MAX(num_pion_on_bac_on_page)+1 new_num FROM pion_on_bac_on_page GROUP BY id_page) AS n ' +
2020-05-19 17:14:33 +00:00
'ON p.id_page=n.id_pagea ' +
2020-05-23 08:44:15 +00:00
'LEFT JOIN (SELECT MAX(id_page) id_pageb,MAX(noeud_suivant) new_noeud FROM save_on_page GROUP BY id_page) AS s ' +
2020-05-19 17:14:33 +00:00
'ON n.id_pagea=s.id_pageb WHERE id_pagea=$1 LIMIT 1;' ,
values : [ resIP [ 0 ] . id _page , requetes _tot [ nb _requete ] . data . bac , r . results2 [ 1 ] [ 0 ] . id _pion , requetes _tot [ nb _requete ] . data . n , requetes _tot [ nb _requete ] . data . diese ]
} ;
queryChainedClient ( client , query , callback , r , result3 ) ;
} ,
function ( r , result3 , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
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,$2 ,MAX(num_pion_on_bac_on_page),0 FROM pion_on_bac_on_page WHERE id_page = $1 GROUP BY id_page;' ,
2020-05-19 17:14:33 +00:00
values : [ resIP [ 0 ] . id _page , requetes _tot [ nb _requete ] . data . bac ]
}
queryChainedClient ( client , query , callback , r , result3 ) ;
} ,
function ( r , result3 , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : ' SELECT MAX(num_pion_on_bac_on_page) num FROM pion_on_bac_on_page WHERE id_page = $1 GROUP BY id_page;' ,
2020-05-19 17:14:33 +00:00
values : [ resIP [ 0 ] . id _page ]
} ;
client . query ( query , function ( err , results ) {
if ( err ) {
callback ( err ) ;
} else {
//resTot.push(results.rows);
result3 . push ( results . rows ) ;
callback ( null , r , result3 ) ;
}
} ) ;
} , function ( r , result3 , callback ) {
emit . push ( { text : 'removeElement' , data : { id : 'groupe-' + r . results2 [ 0 ] [ 0 ] . num _pion _on _bac _on _page , noeud : r . resNoeuds [ 3 ] [ 0 ] . new _noeud } } ) ;
emit _broad . push ( { text : 'removeElement' , data : { id : 'groupe-' + r . results2 [ 0 ] [ 0 ] . num _pion _on _bac _on _page , noeud : r . resNoeuds [ 3 ] [ 0 ] . new _noeud } } ) ;
requetes _tot [ nb _requete ] . data . svg = r . results2 [ 1 ] [ 0 ] . svg _pion ;
requetes _tot [ nb _requete ] . data . directed = r . results2 [ 1 ] [ 0 ] . directed ;
requetes _tot [ nb _requete ] . data . num = result3 [ 3 ] [ 0 ] . num ;
requetes _tot [ nb _requete ] . data . noeud = r . resNoeuds [ 3 ] [ 0 ] . new _noeud ;
emit . push ( { text : 'createGroupeOnbac' , data : requetes _tot [ nb _requete ] . data } ) ;
emit _broad . push ( { text : 'createGroupeOnbac' , data : requetes _tot [ nb _requete ] . data } ) ;
nb _requete += 1 ;
callback ( null , r . resNoeuds ) ;
}
] ) ;
}
//action createPionOnboard--------------------------------------------------------------
} else if ( requete . action === 'createPionOnboard' ) { //ok
if ( ! checkValide ( variables = {
type : '45' ,
top : 1 ,
left : 1 ,
height : 1 ,
board : 1 } , needsData = true , errMess = 'createPionOnboard' , data = requete . data ) ) {
console . log ( errMess + '.data incorrect dans createPionOnboard' ) ;
Abort = true
//return false;
} else {
Array _waterfall = Array _waterfall . concat ( [
function ( resNoeuds , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'SELECT id_pion,taille,svg_pion,directed FROM pion WHERE nom_pion = $1;' ,
2020-05-19 17:14:33 +00:00
values : [ requetes _tot [ nb _requete ] . data . type ]
}
client . query ( query , function ( err , results ) {
if ( err ) {
callback ( err ) ;
} else {
results2 = [ results . rows ] ;
callback ( null , resNoeuds , results2 ) ;
}
} ) ;
} ,
function ( resNoeuds , results2 , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'SELECT c.id_pion FROM carac_pions_page c JOIN pion p ON c.id_pion=p.id_pion WHERE p.nom_pion = $1 AND c.id_page = $2; ' ,
2020-05-19 17:14:33 +00:00
values : [ requetes _tot [ nb _requete ] . data . type , resIP [ 0 ] . id _page ]
}
queryChainedClient ( client , query , callback , resNoeuds , results2 ) ;
} ,
function ( resNoeuds , results2 , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'SELECT num_board_on_page FROM board_on_page b' +
' LEFT JOIN (SELECT COUNT(noeud_stop) nbstop,MAX(id_page) id, MAX(num_board_on_page) num FROM stop_board_on_page' +
2020-05-19 17:14:33 +00:00
' WHERE id_page = $1 AND ARRAY[noeud_stop] <@ $2 GROUP BY id_page,num_board_on_page) AS g' +
' ON b.id_page=g.id AND b.num_board_on_page = g.num' +
' WHERE b.id_page = $1 AND diese_board_on_page=$3' +
' AND ARRAY[b.noeud_start] <@ $2 AND ( COALESCE(g.nbstop,0)=0 OR g.nbstop=0) ;' ,
values : [ resIP [ 0 ] . id _page , resNoeuds [ 4 ] , requetes _tot [ nb _requete ] . data . board ]
}
client . query ( query , function ( err , results ) {
results2 . push ( results . rows ) ;
if ( err ) {
callback ( err ) ;
} else if ( results2 [ 2 ] . length != 1 ) {
//verif que le pion et la board existent
//mauvais diese
callback ( { stack : 'mauvais diese createPionOnBoard' } ) ;
} else if ( results2 [ 0 ] . length != 1 ) {
//pas le bon pion
callback ( { stack : 'mauvais pion createPionOnBoard' } ) ;
} else {
var r = {
results2 : results2 ,
resNoeuds : resNoeuds
}
callback ( null , r ) ;
}
} ) ;
} ,
function ( r , callback ) {
//test pour savoir si on doit ajouter des caractéristiques de pion à la page
r . test = r . results2 [ 1 ] . length != 1 ;
const query = {
2020-05-23 08:44:15 +00:00
text : 'INSERT INTO pion_on_board_on_page (id_page,diese_pion_on_board_on_page,num_pion_on_board_on_page,id_pion,diese_board_on_page,position_x,position_y,taille,noeud_start) ' +
2020-05-19 17:14:33 +00:00
' SELECT $1,CASE WHEN pp.new_diese IS NULL THEN 1 ELSE new_diese END, ' +
2020-05-23 08:44:15 +00:00
' CASE WHEN pp.new_num IS NULL THEN 0 ELSE new_num END,$2,$3, $4,$5 , $6,$7 FROM page p' +
' LEFT JOIN (SELECT MAX(num_pion_on_board_on_page)+1 new_num, MAX(diese_pion_on_board_on_page)+1 new_diese, MAX(id_page) id_pagec FROM pion_on_board_on_page GROUP BY id_page) AS pp ON p.id_page=pp.id_pagec' +
2020-05-19 17:14:33 +00:00
' WHERE id_page = $1 LIMIT 1;' ,
values : [ resIP [ 0 ] . id _page , r . results2 [ 0 ] [ 0 ] . id _pion , requetes _tot [ nb _requete ] . data . board , requetes _tot [ nb _requete ] . data . left , requetes _tot [ nb _requete ] . data . top , requetes _tot [ nb _requete ] . data . height , r . resNoeuds [ 3 ] [ 0 ] . new _noeud ] ,
} ;
client . query ( query , function ( err , results ) {
if ( err ) {
callback ( err ) ;
} else {
//resTot.push(results.rows);
result3 = [ results . rows ] ;
callback ( null , r , result3 ) ;
}
} ) ;
} ,
function ( r , result3 , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
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,$2 ,MAX(num_pion_on_board_on_page),0 FROM pion_on_board_on_page WHERE id_page = $1 GROUP BY id_page;' ,
2020-05-19 17:14:33 +00:00
values : [ resIP [ 0 ] . id _page , requetes _tot [ nb _requete ] . data . board ]
} ;
queryChainedClient ( client , query , callback , r , result3 ) ;
} ,
function ( r , result3 , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : ' SELECT MAX(diese_pion_on_board_on_page) diese,MAX(num_pion_on_board_on_page) num FROM pion_on_board_on_page WHERE id_page = $1 GROUP BY id_page;' ,
2020-05-19 17:14:33 +00:00
values : [ resIP [ 0 ] . id _page ]
}
queryChainedClient ( client , query , callback , r , result3 ) ;
} ,
function ( r , result3 , callback ) {
if ( r . test ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'INSERT INTO carac_pions_page (id_page,id_pion,change_pion,diese_bac_on_page,taille) SELECT ($1,$2,$2,1,$3) ; ' ,
2020-05-19 17:14:33 +00:00
values : [ resIP [ 0 ] . id _page , r . results2 [ 1 ] [ 0 ] . id _pion , r . results2 [ 1 ] [ 0 ] . taille ]
} ;
client . query ( query , function ( err , results ) {
if ( err ) {
callback ( err ) ;
} else {
//resTot.push(results.rows);
result3 . push ( results . rows ) ;
callback ( null , result3 ) ;
}
} ) ;
} else {
callback ( null , r , result3 ) ;
}
} ,
function ( r , result3 , callback ) {
if ( r . test ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'INSERT INTO carac_pions_joueur_on_page (id_joueur,id_page,id_pion,change_pion,diese_bac_on_page,taille) SELECT id_joueur,$1,$2,$2,1,$3 FROM carac_pions_joueur_on_page WHERE id_page = $1 LIMIT 1; ' ,
2020-05-19 17:14:33 +00:00
values : [ resIP [ 0 ] . id _page , r . results2 [ 1 ] [ 0 ] . id _pion , r . results2 [ 1 ] [ 0 ] . taille ]
} ;
client . query ( query , function ( err , results ) {
if ( err ) {
callback ( err ) ;
} else {
//resTot.push(results.rows);
result3 . push ( results . rows ) ;
callback ( null , result3 ) ;
}
} ) ;
} else {
callback ( null , r , result3 ) ;
}
} ,
function ( r , result3 , callback ) {
if ( r . test ) {
emit . push ( { text : 'getTabPion' , data : null } ) ;
emit _broad . push ( { text : 'getTabPion' , data : null } ) ;
}
requetes _tot [ nb _requete ] . data . diese = result3 [ 2 ] [ 0 ] . diese ;
requetes _tot [ nb _requete ] . data . svg = r . results2 [ 0 ] [ 0 ] . svg _pion ;
requetes _tot [ nb _requete ] . data . directed = r . results2 [ 0 ] [ 0 ] . directed ;
requetes _tot [ nb _requete ] . data . num = result3 [ 2 ] [ 0 ] . num ;
requetes _tot [ nb _requete ] . data . noeud = r . resNoeuds [ 3 ] [ 0 ] . new _noeud ;
emit . push ( { text : 'createPionOnboard' , data : requetes _tot [ nb _requete ] . data } ) ;
emit _broad . push ( { text : 'createPionOnboard' , data : requetes _tot [ nb _requete ] . data } ) ;
nb _requete += 1 ;
callback ( null , r . resNoeuds )
}
] ) ;
}
//action createGroupeOnbac--------------------------------------------------------------
} else if ( requete . action === 'createGroupeOnbac' ) { //ok
if ( ! checkValide ( variables = {
type : '45' ,
n : 1 ,
bac : 1 } , needsData = false , errMess = 'createGroupeOnbac' , data = requete . data ) ) {
console . log ( errMess + '.data incorrect dans createGroupeOnbac' ) ;
Abort = true ;
} else {
Array _waterfall = Array _waterfall . concat ( [
function ( resNoeuds , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'SELECT id_pion,taille,svg_pion FROM pion WHERE nom_pion = $1;' ,
2020-05-19 17:14:33 +00:00
values : [ requetes _tot [ nb _requete ] . data . type ]
}
client . query ( query , function ( err , results ) {
if ( err ) {
callback ( err ) ;
} else {
//resTot.push(results.rows);
results2 = [ results . rows ] ;
callback ( null , resNoeuds , results2 ) ;
}
} ) ;
} ,
function ( resNoeuds , results2 , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'SELECT c.id_pion FROM carac_pions_page c JOIN pion p ON c.id_pion=p.id_pion WHERE p.nom_pion = $1 AND c.id_page = $2; ' ,
2020-05-19 17:14:33 +00:00
values : [ requetes _tot [ nb _requete ] . data . type , resIP [ 0 ] . id _page ]
}
queryChainedClient ( client , query , callback , resNoeuds , results2 ) ;
} ,
function ( resNoeuds , results2 , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'SELECT num_bac_on_page FROM bac_on_page b' +
' LEFT JOIN (SELECT COUNT(noeud_stop) nbstop,MAX(id_page) id, MAX(num_bac_on_page) num FROM stop_bac_on_page' +
2020-05-19 17:14:33 +00:00
' WHERE id_page = $1 AND ARRAY[noeud_stop] <@ $2 GROUP BY id_page,num_bac_on_page) AS g' +
' ON b.id_page=g.id AND b.num_bac_on_page = g.num' +
' WHERE b.id_page = $1 AND diese_bac_on_page=$3' +
' AND ARRAY[b.noeud_start] <@ $2 AND ( COALESCE(g.nbstop,0)=0 OR g.nbstop=0) ;' ,
values : [ resIP [ 0 ] . id _page , resNoeuds [ 4 ] , requetes _tot [ nb _requete ] . data . bac ]
}
client . query ( query , function ( err , results ) {
results2 . push ( results . rows ) ;
if ( err ) {
callback ( err ) ;
} else if ( results2 [ 0 ] . length != 1 || results2 [ 2 ] . length != 1 ) {
//mauvais diese
callback ( { stack : 'mauvais diese createGroupeOnbac' } ) ;
} else if ( results2 [ 1 ] . length != 1 ) {
//pas le bon pion
callback ( { stack : 'mauvais pion createGroupeOnbac' } ) ;
} else {
var r = {
resNoeuds : resNoeuds ,
results2 , results2
}
callback ( null , r ) ;
}
} ) ;
} ,
function ( r , callback ) {
//test pour savoir si on doit ajouter des caractéristiques de pion à la page
r . test = r . results2 [ 1 ] . length != 1 ;
const query = {
2020-05-23 08:44:15 +00:00
text : 'INSERT INTO pion_on_bac_on_page (id_page,diese_pion_on_bac_on_page,num_pion_on_bac_on_page,id_pion,diese_bac_on_page,nombre_pions,noeud_start) ' +
2020-05-19 17:14:33 +00:00
' SELECT $1' +
', CASE WHEN new_diese IS NULL THEN 1 ELSE new_diese END,' +
' CASE WHEN new_num IS NULL THEN 0 ELSE new_num END, ' +
'$2,$3, $4, new_noeud ' +
2020-05-23 08:44:15 +00:00
' FROM page p LEFT JOIN ' +
'(SELECT MAX(id_page) id_pageb,MAX(noeud_suivant) new_noeud FROM save_on_page GROUP BY id_page) AS s ON p.id_page=s.id_pageb' +
' LEFT JOIN (SELECT MAX(num_pion_on_bac_on_page)+1 new_num, MAX(diese_pion_on_bac_on_page)+1 new_diese, MAX(id_page) id_pagec FROM pion_on_bac_on_page GROUP BY id_page) AS pp ON p.id_page=pp.id_pagec' +
2020-05-19 17:14:33 +00:00
' WHERE id_page = $1 LIMIT 1;' ,
values : [ resIP [ 0 ] . id _page , r . results2 [ 0 ] [ 0 ] . id _pion , requetes _tot [ nb _requete ] . data . bac , requetes _tot [ nb _requete ] . data . n ] ,
} ;
client . query ( query , function ( err , results ) {
if ( err ) {
callback ( err ) ;
} else {
//resTot.push(results.rows);
result3 = [ results . rows ] ;
callback ( null , r , result3 ) ;
}
} ) ;
} ,
function ( r , result3 , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
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,$2 ,MAX(num_pion_on_bac_on_page),0 FROM pion_on_bac_on_page WHERE id_page = $1 GROUP BY id_page;' ,
2020-05-19 17:14:33 +00:00
values : [ resIP [ 0 ] . id _page , requetes _tot [ nb _requete ] . data . bac ]
} ;
queryChainedClient ( client , query , callback , r , result3 ) ;
} ,
function ( r , result3 , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : ' SELECT MAX(diese_pion_on_bac_on_page) diese,MAX(num_pion_on_bac_on_page) num FROM pion_on_bac_on_page WHERE id_page = $1 GROUP BY id_page; ' ,
2020-05-19 17:14:33 +00:00
values : [ resIP [ 0 ] . id _page ]
}
queryChainedClient ( client , query , callback , r , result3 ) ;
} ,
function ( r , result3 , callback ) {
if ( r . test ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'INSERT INTO carac_pions_page (id_page,id_pion,change_pion,diese_bac_on_page,taille) SELECT ($1,$2,$2,1,$3) ; ' ,
2020-05-19 17:14:33 +00:00
values : [ resIP [ 0 ] . id _page , r . results2 [ 1 ] [ 0 ] . id _pion , r . results2 [ 1 ] [ 0 ] . taille ]
} ;
client . query ( query , function ( err , results ) {
if ( err ) {
callback ( err ) ;
} else {
//resTot.push(results.rows);
result3 . push ( results . rows ) ;
callback ( null , r , result3 ) ;
}
} ) ;
} else {
callback ( null , r , result3 ) ;
}
} ,
function ( r , result3 , callback ) {
if ( r . test ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'INSERT INTO carac_pions_joueur_on_page (id_joueur,id_page,id_pion,change_pion,diese_bac_on_page,taille) SELECT id_joueur,$1,$2,$2,1,$3 FROM carac_pions_joueur_on_page WHERE id_page = $1 LIMIT 1; ' ,
2020-05-19 17:14:33 +00:00
values : [ resIP [ 0 ] . id _page , r . results2 [ 1 ] [ 0 ] . id _pion , r . results2 [ 1 ] [ 0 ] . taille ]
} ;
client . query ( query , function ( err , results ) {
if ( err ) {
callback ( err ) ;
} else {
//resTot.push(results.rows);
result3 . push ( results . rows ) ;
callback ( null , r , result3 ) ;
}
} ) ;
} else {
callback ( null , r , result3 ) ;
}
} , function ( r , result3 , callback ) {
if ( r . test ) {
emit . push ( { text : 'getTabPion' , data : null } ) ;
emit _broad . push ( { text : 'getTabPion' , data : null } ) ;
}
requetes _tot [ nb _requete ] . data . diese = result3 [ 2 ] [ 0 ] . diese ;
requetes _tot [ nb _requete ] . data . svg = r . results2 [ 0 ] [ 0 ] . svg _pion ;
requetes _tot [ nb _requete ] . data . num = result3 [ 2 ] [ 0 ] . num ;
requetes _tot [ nb _requete ] . data . noeud = r . resNoeuds [ 3 ] [ 0 ] . new _noeud ;
emit . push ( { text : 'createGroupeOnbac' , data : requetes _tot [ nb _requete ] . data } ) ;
emit _broad . push ( { text : 'createGroupeOnbac' , data : requetes _tot [ nb _requete ] . data } ) ;
nb _requete += 1 ;
callback ( null , r . resNoeuds ) ;
}
] ) ;
}
//action changeLabelBac--------------------------------------------------------------
} else if ( requete . action === 'changeLabelBac' ) { //
if ( ! checkValide ( variables = { diese : 1 , label : '20' } , needsData = true , errMess = 'changeLabelBac' , data = requete . data ) ) {
console . log ( errMess + '.data incorrect dans changeLabelBac' ) ;
Abort = true ;
} else {
Array _waterfall = Array _waterfall . concat ( [
function ( resNoeuds , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'SELECT num_bac_on_page FROM bac_on_page b' +
' LEFT JOIN (SELECT COUNT(noeud_stop) nbstop,MAX(id_page) id, MAX(num_bac_on_page) num FROM stop_bac_on_page' +
2020-05-19 17:14:33 +00:00
' WHERE id_page = $1 AND ARRAY[noeud_stop] <@ $2 GROUP BY id_page,num_bac_on_page) AS g' +
' ON b.id_page=g.id AND b.num_bac_on_page = g.num' +
' WHERE b.id_page = $1 AND diese_bac_on_page=$3' +
' AND ARRAY[b.noeud_start] <@ $2 AND ( COALESCE(g.nbstop,0)=0 OR g.nbstop=0) ;' ,
values : [ resIP [ 0 ] . id _page , resNoeuds [ 4 ] , requetes _tot [ nb _requete ] . data . diese ]
} ;
client . query ( query ,
function ( err2 , results2 ) {
if ( err2 ) {
console . log ( 'erreur de changeLabelBac : ' + err2 . stack ) ;
callback ( err2 ) ;
//return false;
} else {
//verif que le bac existe
if ( results2 . rows . length != 1 ) {
//console.log('mauvais diese3');
//socket.emit('refresh');
//return false;
callback ( { stak : 'mauvais diese3' } ) ;
} else {
r = {
resNoeuds : resNoeuds ,
results2 : results2 . rows
} ;
callback ( null , r ) ;
}
}
}
) ;
} ,
function ( r , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'INSERT INTO stop_bac_on_page (id_page,num_bac_on_page,noeud_stop)' +
2020-05-19 17:14:33 +00:00
' SELECT $1,$2,$3;' ,
values : [ resIP [ 0 ] . id _page , r . results2 [ 0 ] . num _bac _on _page , r . resNoeuds [ 3 ] [ 0 ] . new _noeud ] ,
} ;
client . query ( query , function ( err , results ) {
if ( err ) {
callback ( err ) ;
} else {
//resTot.push(results.rows);
resTot = [ results . rows ] ;
callback ( null , r , resTot ) ;
}
} ) ;
} ,
function ( r , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'INSERT INTO bac_on_page (id_page,diese_bac_on_page,num_bac_on_page,label_bac,noeud_start) ' +
' SELECT $1,$2, new_num, $3, new_noeud FROM page p LEFT JOIN ' +
'(SELECT MAX(id_page) id_pageb,MAX(noeud_suivant) new_noeud FROM save_on_page GROUP BY id_page) AS s ON p.id_page=s.id_pageb' +
' LEFT JOIN (SELECT MAX(num_bac_on_page)+1 new_num,MAX(id_page) id_pagec FROM bac_on_page GROUP BY id_page) AS b ON p.id_page=b.id_pagec' +
2020-05-19 17:14:33 +00:00
' WHERE id_page = $1 LIMIT 1;' ,
values : [ resIP [ 0 ] . id _page , requetes _tot [ nb _requete ] . data . diese , requetes _tot [ nb _requete ] . data . label ]
} ;
queryChainedClient ( client , query , callback , r , resTot ) ;
} ,
function ( r , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'INSERT INTO stop_bac_on_page (id_page,num_bac_on_page,noeud_stop) SELECT ' +
'$1,MAX(num_bac_on_page),0 FROM bac_on_page WHERE id_page = $1 GROUP BY id_page;' ,
2020-05-19 17:14:33 +00:00
values : [ resIP [ 0 ] . id _page ]
} ;
queryChainedClient ( client , query , callback , r , resTot ) ;
} ,
function ( r , resTot , callback ) {
const query = {
2020-05-23 08:44:15 +00:00
text : 'SELECT MAX(num_bac_on_page) num FROM bac_on_page WHERE id_page = $1 GROUP BY id_page; ' ,
2020-05-19 17:14:33 +00:00
values : [ resIP [ 0 ] . id _page ]
} ;
client . query ( query , function ( err , results ) {
if ( err ) {
callback ( err ) ;
} else {
//resTot.push(results.rows);
resTot . push ( results . rows ) ;
callback ( null , r , resTot ) ;
}
} ) ;
} ,
function ( r , resTot , callback ) {
requetes _tot [ nb _requete ] . data . num = resTot [ 3 ] [ 0 ] . num ;
requetes _tot [ nb _requete ] . data . oldnum = r . results2 [ 0 ] . num _bac _on _page ;
//requetes_tot[nb_requete].data.noeud=resIP[0].noeud_courant;
requetes _tot [ nb _requete ] . data . noeud = resNoeuds [ 3 ] [ 0 ] . new _noeud ;
emit . push ( { text : 'changeLabelBac' , data : requetes _tot [ nb _requete ] . data } ) ;
emit _broad . push ( { text : 'changeLabelBac' , data : requetes _tot [ nb _requete ] . data } ) ;
nb _requete += 1 ;
callback ( null , r . resNoeuds ) ;
}
] ) ;
}
}
}
if ( shouldAbort ( Abort ) ) {
return
} else {
async . waterfall ( Array _waterfall ,
function ( err , result ) {
if ( shouldAbort ( err ) ) {
return
} else {
client . query ( 'COMMIT' , function ( err ) {
if ( shouldAbort ( err ) ) return
for ( var i = 0 ; i < emit . length ; i ++ ) {
if ( emit [ i ] . data === null ) {
socket . emit ( emit [ i ] . text ) ;
} else {
socket . emit ( emit [ i ] . text , emit [ i ] . data ) ;
}
}
for ( var i = 0 ; i < emit _broad . length ; i ++ ) {
if ( emit _broad [ i ] . data === null ) {
socket . emit ( emit _broad [ i ] . text ) ;
} else {
socket . broadcast . to ( socket . nomPlatoo ) . emit ( emit _broad [ i ] . text , emit _broad [ i ] . data ) ;
}
}
done ( ) ;
} ) ;
}
}
) ;
}
} ) ;
}
} ) ;
} ) ;
} ) ;
//http://stackoverflow.com/questions/19035373/how-do-i-redirect-in-expressjs-while-passing-some-context
//http://stackoverflow.com/questions/5710358/how-to-retrieve-post-query-parameters
//http://naholyr.fr/2011/06/bonnes-pratiques-asynchrone-javascript-nodejs/
//promises