constsql='SELECT SessionName FROM Session WHERE SessionName = \''+spielname+'\';';
console.log('isSession:',sql);
db.query(
'SELECT SessionName FROM Session WHERE SessionName = \''+spielname+'\';',
sql,
(error,results)=>{
if(error){
console.log(error);
returnres.status(500).json({status:'error'});
}else{
if(results.length>0){
returnres.status(200).json({status:'true'});
next();
}else{
returnres.status(200).json({status:'false'});
}
...
...
@@ -24,16 +30,46 @@ function createRouter(db) {
}else{
returnres.status(500).json({status:'error'});
}
}
};
// the routes are defined here
vargetGamestatus=function(req,res,next){
constspielname=req.body.spielname;
if(spielname.match(/^[0-9a-zA-Z]+$/)!=null){
constsql='SELECT SessionID, Red, Blue, RedTurn, ActiveExplainer, ActiveWatchdog FROM Gamestatus WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")';
console.log('getGamestatus: ',sql);
db.query(
sql,
(error,results)=>{
if(error){
console.log(error);
res.status(500).json({status:'false'});
}else{
res.status(200).json(
{
status:'true',
sessionID:results[0].SessionID,
red:results[0].Red,
blue:results[0].Blue,
redTurn:results[0].RedTurn,
activeExplainer:results[0].ActiveExplainer,
activeWatchdog:results[0].ActiveWatchdog
}
);
}
}
);
}else{
res.status(500).json({status:'error'});
}
};
//Generate new Session
router.post('/addSession',(req,res)=>{
varaddSession=functionaddSession(req,res){
constspielname=req.body.spielname;
if(spielname.match(/^[0-9a-zA-Z]+$/)!=null){
constsql='INSERT INTO Session (SessionName) VALUES (\"'+spielname+'\");';
console.log('addSession1: ',sql);
db.query(
'INSERT INTO Session (SessionName) VALUES (\"'+spielname+'\");',
sql,
error=>{
if(error){
console.error(error);
...
...
@@ -42,8 +78,10 @@ function createRouter(db) {
//Init new Gamestatus
varredTurn=Math.random();
redTurn=Math.round(redTurn);
constsql='INSERT INTO Gamestatus (SessionID, Red, Blue, RedTurn, ActiveExplainer, ActiveWatchdog) VALUES ((SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\"), 0, 0, '+redTurn+', 0, 0);';
console.log('addSession2: ',sql);
db.query(
'INSERT INTO Gamestatus (SessionID, Red, Blue, RedTurn, ActiveExplainer, ActiveWatchdog) VALUES ((SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\"), 0, 0, '+redTurn+', 0, 0);',
sql,
error=>{
if(error){
console.error(error);
...
...
@@ -59,17 +97,13 @@ function createRouter(db) {
}else{
res.status(500).json({status:'error'});
}
});
};
//Check if Session exist
router.post('/isSession',(req,res)=>{
isSession(req,res)
});
//Return all Sessions (Not neccesary)
router.get('/Session',function(req,res){
varsession=functionsession(req,res){
constsql='SELECT * FROM Session';
console.log('Session: ',sql);
db.query(
'SELECT * FROM Session',
sql,
(error,results)=>{
if(error){
console.log(error);
...
...
@@ -79,65 +113,37 @@ function createRouter(db) {
}
}
);
});
//Return GameStatus
router.post('/getGamestatus',(req,res)=>{
constspielname=req.body.spielname;
if(spielname.match(/^[0-9a-zA-Z]+$/)!=null){
db.query(
'SELECT SessionID, Red, Blue, RedTurn, ActiveExplainer, ActiveWatchdog FROM Gamestatus WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")',
' WHERE S2C.CardID IS NOT NULL AND S2C.SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")\n'+
');',
');';
console.log('getS2C1: ',sql);
db.query(
sql,
(error,results)=>{
if(error){
console.log(error);
res.status(500).json({status:'false'});
}else{
console.log(results);
if(results.length>0){
letrandom=Math.random()*(results.length-1);
random=Math.round(random)
random=Math.round(random);
card=results[random];
console.log(card);
constsql='INSERT INTO S2C (SessionID, CardID) VALUES ((SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\"), '+card.CardID+');';
console.log('getS2C2: ',sql);
db.query(
'INSERT INTO S2C (SessionID, CardID) VALUES ((SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\"), '+card.CardID+');',
(error,results)=>{
sql,
(error)=>{
if(error){
res.status(500).json({status:'false'});
}else{
...
...
@@ -156,20 +162,24 @@ function createRouter(db) {
}
}
);
}else{
res.status(500).json({status:'false'});
}
}
});
}else{
res.status(500).json({status:'error'});
}
});
};
//Start a new Round and lock the game positions
router.post('/newRound',(req,res)=>{
varnewRound=functionnewRound(req,res){
constspielname=req.body.spielname;
if(spielname.match(/^[0-9a-zA-Z]+$/)!=null){
constsql='UPDATE Gamestatus SET ActiveExplainer = 1, ActiveWatchdog=1 WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")';
console.log('newRound: ',sql);
db.query(
'UPDATE Gamestatus SET ActiveExplainer = 1, ActiveWatchdog=1 WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")',
(error,results)=>{
sql,
(error)=>{
if(error){
console.log(error);
res.status(500).json({status:'false'});
...
...
@@ -181,15 +191,16 @@ function createRouter(db) {
}else{
res.status(500).json({status:'false'});
}
});
};
//End a Round and unlock the game positions and invert RedTurn
router.post('/endRound',(req,res)=>{
varendRound=functionendRound(req,res){
constspielname=req.body.spielname;
if(spielname.match(/^[0-9a-zA-Z]+$/)!=null){
constsql='UPDATE Gamestatus SET RedTurn = (SELECT 1-Gamestatus.RedTurn FROM Gamestatus INNER JOIN Session ON Session.SessionID=Gamestatus.SessionID WHERE Session.SessionName = \"'+spielname+'\"), ActiveExplainer = 0, ActiveWatchdog=0 WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")';
console.log('endRound: ',sql);
db.query(
'UPDATE Gamestatus SET RedTurn = (SELECT 1-Gamestatus.RedTurn FROM Gamestatus INNER JOIN Session ON Session.SessionID=Gamestatus.SessionID WHERE Session.SessionName = \"'+spielname+'\"), ActiveExplainer = 0, ActiveWatchdog=0 WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")',
(error,results)=>{
sql,
(error)=>{
if(error){
console.log(error);
res.status(500).json({status:'false'});
...
...
@@ -201,15 +212,16 @@ function createRouter(db) {
}else{
res.status(500).json({status:'false'});
}
});
};
//Starts a new Game and resets red and blue and chooses a random value for RedTurn
router.post('/newGame',(req,res)=>{
varnewGame=functionnewGame(req,res){
constspielname=req.body.spielname;
if(spielname.match(/^[0-9a-zA-Z]+$/)!=null){
constsql='UPDATE Gamestatus SET Red = 0, Blue = 0, RedTurn = '+Math.round(Math.random())+' WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")';
console.log('newGame: ',sql);
db.query(
'UPDATE Gamestatus SET Red = 0, Blue = 0, RedTurn = '+Math.round(Math.random())+' WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")',
constsql='UPDATE Gamestatus SET Red = 1+(SELECT Red FROM Gamestatus WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")) WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")';
console.log('addPoint Red:',sql);
db.query(
'UPDATE Gamestatus SET Red = 1+(SELECT Red FROM Gamestatus WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")) WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")',
sql,
(error)=>{
if(error){
console.log(error);
...
...
@@ -248,8 +261,10 @@ function createRouter(db) {
}
);
}elseif(team==='blue'){
constsql='UPDATE Gamestatus SET Blue = 1+(SELECT Blue FROM Gamestatus WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")) WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")';
console.log('addPoint Blue:',sql);
db.query(
'UPDATE Gamestatus SET Blue = 1+(SELECT Blue FROM Gamestatus WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")) WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")',
constsql='UPDATE Gamestatus SET Red = -1+(SELECT Red FROM Gamestatus WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")) WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")';
console.log('removePoint Red: ',sql);
db.query(
'UPDATE Gamestatus SET Red = -1+(SELECT Red FROM Gamestatus WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")) WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")',
sql,
(error)=>{
if(error){
console.log(error);
...
...
@@ -296,8 +312,10 @@ function createRouter(db) {
}
);
}elseif(team==='blue'){
constsql='UPDATE Gamestatus SET Blue = -1+(SELECT Blue FROM Gamestatus WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")) WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")';
console.log('removePoint Blue: ',sql);
db.query(
'UPDATE Gamestatus SET Blue = -1+(SELECT Blue FROM Gamestatus WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")) WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")',