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'); ...@@ -3,18 +3,24 @@ const express = require('express');
function createRouter(db) { function createRouter(db) {
const router = express.Router(); 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; const spielname = req.body.spielname;
if (spielname.match(/^[0-9a-zA-Z]+$/) != null) { if (spielname.match(/^[0-9a-zA-Z]+$/) != null) {
const sql = 'SELECT SessionName FROM Session WHERE SessionName = \''+spielname+'\';';
console.log('isSession:' ,sql);
db.query( db.query(
'SELECT SessionName FROM Session WHERE SessionName = \''+spielname+'\';', sql,
(error, results) => { (error, results) => {
if (error) { if (error) {
console.log(error); console.log(error);
return res.status(500).json({status: 'error'}); return res.status(500).json({status: 'error'});
} else { } else {
if (results.length > 0) { if (results.length > 0) {
return res.status(200).json({status: 'true'}); next();
} else { } else {
return res.status(200).json({status: 'false'}); return res.status(200).json({status: 'false'});
} }
...@@ -24,16 +30,46 @@ function createRouter(db) { ...@@ -24,16 +30,46 @@ function createRouter(db) {
} else { } else {
return res.status(500).json({status: 'error'}); 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 var addSession = function addSession(req, res) {
router.post('/addSession', (req, res) => {
const spielname = req.body.spielname; const spielname = req.body.spielname;
if (spielname.match(/^[0-9a-zA-Z]+$/) != null) { if (spielname.match(/^[0-9a-zA-Z]+$/) != null) {
const sql = 'INSERT INTO Session (SessionName) VALUES (\"'+spielname+'\");';
console.log('addSession1: ', sql);
db.query( db.query(
'INSERT INTO Session (SessionName) VALUES (\"'+spielname+'\");', sql,
error => { error => {
if (error) { if (error) {
console.error(error); console.error(error);
...@@ -42,8 +78,10 @@ function createRouter(db) { ...@@ -42,8 +78,10 @@ function createRouter(db) {
//Init new Gamestatus //Init new Gamestatus
var redTurn = Math.random(); var redTurn = Math.random();
redTurn = Math.round(redTurn); 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( 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 => { error => {
if (error) { if (error) {
console.error(error); console.error(error);
...@@ -59,17 +97,13 @@ function createRouter(db) { ...@@ -59,17 +97,13 @@ function createRouter(db) {
} else { } else {
res.status(500).json({status: 'error'}); res.status(500).json({status: 'error'});
} }
}); };
//Check if Session exist
router.post('/isSession', (req, res) => {
isSession(req, res)
});
//Return all Sessions (Not neccesary) var session = function session(req, res) {
router.get('/Session', function (req, res) { const sql = 'SELECT * FROM Session';
console.log('Session: ', sql);
db.query( db.query(
'SELECT * FROM Session', sql,
(error, results) => { (error, results) => {
if (error) { if (error) {
console.log(error); console.log(error);
...@@ -79,97 +113,73 @@ function createRouter(db) { ...@@ -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 var getS2C = function getS2C(req, res) {
router.post('/getS2C', (req,res) => {
const spielname = req.body.spielname; const spielname = req.body.spielname;
if (spielname.match(/^[0-9a-zA-Z]+$/) != null) { if (spielname.match(/^[0-9a-zA-Z]+$/) != null) {
let card; let card;
db.query( const sql = 'SELECT Card.CardID, Solution, Tabu1, Tabu2, Tabu3, Tabu4, Tabu5 \n' +
'SELECT Card.CardID, Solution, Tabu1, Tabu2, Tabu3, Tabu4, Tabu5 \n' +
'FROM Card \n' + 'FROM Card \n' +
'WHERE Card.CardID NOT IN \n' + 'WHERE Card.CardID NOT IN \n' +
'(\n' + '(\n' +
' SELECT S2C.CardID\n' + ' SELECT S2C.CardID\n' +
' FROM S2C\n' + ' FROM S2C\n' +
' WHERE S2C.CardID IS NOT NULL AND S2C.SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")\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) => { (error, results) => {
if (error) { if (error) {
console.log(error); console.log(error);
res.status(500).json({status: 'false'}); res.status(500).json({status: 'false'});
} else { } else {
console.log(results); if (results.length > 0) {
let random = Math.random() * (results.length-1); let random = Math.random() * (results.length-1);
random = Math.round(random) random = Math.round(random);
card = results[random]; card = results[random];
console.log(card); const sql = 'INSERT INTO S2C (SessionID, CardID) VALUES ((SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\"), '+card.CardID+');';
db.query( console.log('getS2C2: ', sql);
'INSERT INTO S2C (SessionID, CardID) VALUES ((SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\"), '+card.CardID+');', db.query(
(error, results) => { sql,
if (error) { (error) => {
res.status(500).json({status: 'false'}); if (error) {
} else { res.status(500).json({status: 'false'});
res.status(200).json( } else {
{ res.status(200).json(
status: 'true', {
cardID: card.CardID, status: 'true',
solution: card.Solution, cardID: card.CardID,
tabu1: card.Tabu1, solution: card.Solution,
tabu2: card.Tabu2, tabu1: card.Tabu1,
tabu3: card.Tabu3, tabu2: card.Tabu2,
tabu4: card.Tabu4, tabu3: card.Tabu3,
tabu5: card.Tabu5, tabu4: card.Tabu4,
} tabu5: card.Tabu5,
); }
);
}
} }
} );
); } else {
res.status(500).json({status: 'false'});
}
} }
}); });
} else { } else {
res.status(500).json({status: 'error'}); res.status(500).json({status: 'error'});
} }
}); };
//Start a new Round and lock the game positions var newRound = function newRound(req, res) {
router.post('/newRound', (req, res) => {
const spielname = req.body.spielname; const spielname = req.body.spielname;
if (spielname.match(/^[0-9a-zA-Z]+$/) != null) { 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( db.query(
'UPDATE Gamestatus SET ActiveExplainer = 1, ActiveWatchdog=1 WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")', sql,
(error, results) => { (error) => {
if (error) { if (error) {
console.log(error); console.log(error);
res.status(500).json({status: 'false'}); res.status(500).json({status: 'false'});
...@@ -181,15 +191,16 @@ function createRouter(db) { ...@@ -181,15 +191,16 @@ function createRouter(db) {
} else { } else {
res.status(500).json({status: 'false'}); res.status(500).json({status: 'false'});
} }
}); };
//End a Round and unlock the game positions and invert RedTurn var endRound = function endRound(req, res) {
router.post('/endRound', (req, res) => {
const spielname = req.body.spielname; const spielname = req.body.spielname;
if (spielname.match(/^[0-9a-zA-Z]+$/) != null) { 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( 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+'\")', sql,
(error, results) => { (error) => {
if (error) { if (error) {
console.log(error); console.log(error);
res.status(500).json({status: 'false'}); res.status(500).json({status: 'false'});
...@@ -201,15 +212,16 @@ function createRouter(db) { ...@@ -201,15 +212,16 @@ function createRouter(db) {
} else { } else {
res.status(500).json({status: 'false'}); res.status(500).json({status: 'false'});
} }
}); };
//Starts a new Game and resets red and blue and chooses a random value for RedTurn var newGame = function newGame(req, res) {
router.post('/newGame', (req, res) => {
const spielname = req.body.spielname; const spielname = req.body.spielname;
if (spielname.match(/^[0-9a-zA-Z]+$/) != null) { 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( db.query(
'UPDATE Gamestatus SET Red = 0, Blue = 0, RedTurn = '+Math.round(Math.random())+' WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")', sql,
(error, results) => { (error) => {
if (error) { if (error) {
console.log(error); console.log(error);
res.status(500).json({status: 'false'}); res.status(500).json({status: 'false'});
...@@ -221,17 +233,18 @@ function createRouter(db) { ...@@ -221,17 +233,18 @@ function createRouter(db) {
} else { } else {
res.status(500).json({status: 'false'}); res.status(500).json({status: 'false'});
} }
}); };
//Add Point var addPoint = function addPoint(req, res) {
router.post('/addPoint', (req, res) => {
console.log(req.body); console.log(req.body);
const spielname = req.body.spielname; const spielname = req.body.spielname;
const team = req.body.team; const team = req.body.team;
if (spielname.match(/^[0-9a-zA-Z]+$/) != null && team.match(/^[0-9a-zA-Z]+$/) != null) { if (spielname.match(/^[0-9a-zA-Z]+$/) != null && team.match(/^[0-9a-zA-Z]+$/) != null) {
if (team === 'red') { 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( 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) => { (error) => {
if (error) { if (error) {
console.log(error); console.log(error);
...@@ -248,8 +261,10 @@ function createRouter(db) { ...@@ -248,8 +261,10 @@ function createRouter(db) {
} }
); );
} else if (team === 'blue') { } 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( 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) => { (error) => {
if (error) { if (error) {
console.log(error); console.log(error);
...@@ -269,17 +284,18 @@ function createRouter(db) { ...@@ -269,17 +284,18 @@ function createRouter(db) {
} else { } else {
res.status(500).json({status: 'false'}); res.status(500).json({status: 'false'});
} }
}); };
//Remove Point var removePoint = function removePoint(req, res) {
router.post('/removePoint', (req, res) => {
console.log(req.body); console.log(req.body);
const spielname = req.body.spielname; const spielname = req.body.spielname;
const team = req.body.team; const team = req.body.team;
if (spielname.match(/^[0-9a-zA-Z]+$/) != null && team.match(/^[0-9a-zA-Z]+$/) != null) { if (spielname.match(/^[0-9a-zA-Z]+$/) != null && team.match(/^[0-9a-zA-Z]+$/) != null) {
if (team === 'red') { 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( 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) => { (error) => {
if (error) { if (error) {
console.log(error); console.log(error);
...@@ -296,8 +312,10 @@ function createRouter(db) { ...@@ -296,8 +312,10 @@ function createRouter(db) {
} }
); );
} else if (team === 'blue') { } 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( 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) => { (error) => {
if (error) { if (error) {
console.log(error); console.log(error);
...@@ -317,7 +335,39 @@ function createRouter(db) { ...@@ -317,7 +335,39 @@ function createRouter(db) {
} else { } else {
res.status(500).json({status: 'false'}); 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; 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