Commit b8d44a03 authored by Dennis Willers's avatar Dennis Willers 🏀

Implement getGamestatus API

parent 9200cc5f
Pipeline #289 passed with stages
in 2 minutes and 9 seconds
...@@ -4,27 +4,39 @@ function createRouter(db) { ...@@ -4,27 +4,39 @@ function createRouter(db) {
const router = express.Router(); const router = express.Router();
// the routes are defined here // the routes are defined here
//Generate new Session
router.post('/addSession', (req, res) => { router.post('/addSession', (req, res) => {
var 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) {
db.query( db.query(
'INSERT INTO Session (SessionName) VALUES (\"'+spielname+'\");', 'INSERT INTO Session (SessionName) VALUES (\"'+spielname+'\");',
(error) => { error => {
if (error) { if (error) {
console.error(error); console.error(error);
res.status(500).json({status: 'false'}); res.status(500).json({status: 'false'});
} else {
res.status(200).json({status: 'true'});
} }
} }
); );
//Init new Gamestatus
db.query(
'INSERT INTO Gamestatus (SessionID, Red, Blue, RedTurn, ActiveExplainer, ActiveWatchdog) VALUES ((SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\"), 0, 0, 1, 0, 0);',
error => {
if (error) {
console.error(error);
res.status(500).json({status: 'false'});
} else {
res.status(200).json({status: 'true'});
}
});
} else { } else {
res.status(500).json({status: 'error'}); res.status(500).json({status: 'error'});
} }
}); });
//Check if Session exist
router.post('/isSession', (req, res) => { router.post('/isSession', (req, res) => {
var 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) {
db.query( db.query(
'SELECT SessionName FROM Session WHERE SessionName = \''+spielname+'\';', 'SELECT SessionName FROM Session WHERE SessionName = \''+spielname+'\';',
...@@ -46,6 +58,7 @@ function createRouter(db) { ...@@ -46,6 +58,7 @@ function createRouter(db) {
} }
}); });
//Return all Sessions (Not neccesary)
router.get('/Session', function (req, res) { router.get('/Session', function (req, res) {
db.query( db.query(
'SELECT * FROM Session', 'SELECT * FROM Session',
...@@ -60,6 +73,36 @@ function createRouter(db) { ...@@ -60,6 +73,36 @@ 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 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