Commit 6a1bcda4 authored by Dennis Willers's avatar Dennis Willers 🏀

New architecture Design. Use of 'next' for checking if session exist

parent ca70d09a
Pipeline #312 passed with stages
in 2 minutes and 8 seconds
......@@ -3,18 +3,24 @@ const express = require('express');
function createRouter(db) {
const router = express.Router();
function isSession(req, res) {
var isTrue = function(req, res) {
res.status(200).json({status: 'true'});
};
var isSession = function isSession(req, res, next) {
const spielname = req.body.spielname;
if (spielname.match(/^[0-9a-zA-Z]+$/) != null) {
const sql = '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);
return res.status(500).json({status: 'error'});
} else {
if (results.length > 0) {
return res.status(200).json({status: 'true'});
next();
} else {
return res.status(200).json({status: 'false'});
}
......@@ -24,16 +30,46 @@ function createRouter(db) {
} else {
return res.status(500).json({status: 'error'});
}
}
};
// the routes are defined here
var getGamestatus = function(req, res, next) {
const spielname = req.body.spielname;
if (spielname.match(/^[0-9a-zA-Z]+$/) != null) {
const sql = '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) => {
var addSession = function addSession(req, res) {
const spielname = req.body.spielname;
if (spielname.match(/^[0-9a-zA-Z]+$/) != null) {
const sql = '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
var redTurn = Math.random();
redTurn = Math.round(redTurn);
const sql = '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) {
var session = function session(req, res) {
const sql = 'SELECT * FROM Session';
console.log('Session: ', sql);
db.query(
'SELECT * FROM Session',
sql,
(error, results) => {
if (error) {
console.log(error);
......@@ -79,97 +113,73 @@ function createRouter(db) {
}
}
);
});
//Return GameStatus
router.post('/getGamestatus', (req, res) => {
const spielname = 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+'\")',
(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'});
}
});
};
//Return S2C
router.post('/getS2C', (req,res) => {
var getS2C = function getS2C(req, res) {
const spielname = req.body.spielname;
if (spielname.match(/^[0-9a-zA-Z]+$/) != null) {
let card;
db.query(
'SELECT Card.CardID, Solution, Tabu1, Tabu2, Tabu3, Tabu4, Tabu5 \n' +
const sql = 'SELECT Card.CardID, Solution, Tabu1, Tabu2, Tabu3, Tabu4, Tabu5 \n' +
'FROM Card \n' +
'WHERE Card.CardID NOT IN \n' +
'(\n' +
' SELECT S2C.CardID\n' +
' FROM S2C\n' +
' 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);
let random = Math.random() * (results.length-1);
random = Math.round(random)
card = results[random];
console.log(card);
db.query(
'INSERT INTO S2C (SessionID, CardID) VALUES ((SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\"), '+card.CardID+');',
(error, results) => {
if (error) {
res.status(500).json({status: 'false'});
} else {
res.status(200).json(
{
status: 'true',
cardID: card.CardID,
solution: card.Solution,
tabu1: card.Tabu1,
tabu2: card.Tabu2,
tabu3: card.Tabu3,
tabu4: card.Tabu4,
tabu5: card.Tabu5,
}
);
if (results.length > 0) {
let random = Math.random() * (results.length-1);
random = Math.round(random);
card = results[random];
const sql = 'INSERT INTO S2C (SessionID, CardID) VALUES ((SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\"), '+card.CardID+');';
console.log('getS2C2: ', sql);
db.query(
sql,
(error) => {
if (error) {
res.status(500).json({status: 'false'});
} else {
res.status(200).json(
{
status: 'true',
cardID: card.CardID,
solution: card.Solution,
tabu1: card.Tabu1,
tabu2: card.Tabu2,
tabu3: card.Tabu3,
tabu4: card.Tabu4,
tabu5: card.Tabu5,
}
);
}
}
}
);
);
} 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) => {
var newRound = function newRound(req, res) {
const spielname = req.body.spielname;
if (spielname.match(/^[0-9a-zA-Z]+$/) != null) {
const sql = '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) => {
var endRound = function endRound(req, res) {
const spielname = req.body.spielname;
if (spielname.match(/^[0-9a-zA-Z]+$/) != null) {
const sql = '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) => {
var newGame = function newGame(req, res) {
const spielname = req.body.spielname;
if (spielname.match(/^[0-9a-zA-Z]+$/) != null) {
const sql = '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+'\")',
(error, results) => {
sql,
(error) => {
if (error) {
console.log(error);
res.status(500).json({status: 'false'});
......@@ -221,17 +233,18 @@ function createRouter(db) {
} else {
res.status(500).json({status: 'false'});
}
});
};
//Add Point
router.post('/addPoint', (req, res) => {
var addPoint = function addPoint(req, res) {
console.log(req.body);
const spielname = req.body.spielname;
const team = req.body.team;
if (spielname.match(/^[0-9a-zA-Z]+$/) != null && team.match(/^[0-9a-zA-Z]+$/) != null) {
if (team === 'red') {
const sql = '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) {
}
);
} else if (team === 'blue') {
const sql = '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+'\")',
sql,
(error) => {
if (error) {
console.log(error);
......@@ -269,17 +284,18 @@ function createRouter(db) {
} else {
res.status(500).json({status: 'false'});
}
});
};
//Remove Point
router.post('/removePoint', (req, res) => {
var removePoint = function removePoint(req, res) {
console.log(req.body);
const spielname = req.body.spielname;
const team = req.body.team;
if (spielname.match(/^[0-9a-zA-Z]+$/) != null && team.match(/^[0-9a-zA-Z]+$/) != null) {
if (team === 'red') {
const sql = '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) {
}
);
} else if (team === 'blue') {
const sql = '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+'\")',
sql,
(error) => {
if (error) {
console.log(error);
......@@ -317,7 +335,39 @@ function createRouter(db) {
} else {
res.status(500).json({status: 'false'});
}
});
};
// the routes are defined here
//Generate new Session
router.post('/addSession', [addSession]);
//Check if Session exist
router.post('/isSession', [isSession, isTrue]);
//Return all Sessions (Not neccesary)
router.get('/Session', [session]);
//Return GameStatus
router.post('/getGamestatus', ([isSession, getGamestatus]));
//Return S2C
router.post('/getS2C', [getS2C]);
//Start a new Round and lock the game positions
router.post('/newRound', [newRound]);
//End a Round and unlock the game positions and invert RedTurn
router.post('/endRound', [endRound]);
//Starts a new Game and resets red and blue and chooses a random value for RedTurn
router.post('/newGame', [newGame]);
//Add Point
router.post('/addPoint', [addPoint]);
//Remove Point
router.post('/removePoint', [removePoint]);
return router;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment