Commit 41ced83e authored by Dennis Willers's avatar Dennis Willers 🏀

Add getS2C API

parent b8d44a03
Pipeline #293 passed with stages
in 2 minutes and 3 seconds
......@@ -19,8 +19,10 @@ function createRouter(db) {
}
);
//Init new Gamestatus
var redTurn = Math.random();
redTurn = Math.round(redTurn);
db.query(
'INSERT INTO Gamestatus (SessionID, Red, Blue, RedTurn, ActiveExplainer, ActiveWatchdog) VALUES ((SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\"), 0, 0, 1, 0, 0);',
'INSERT INTO Gamestatus (SessionID, Red, Blue, RedTurn, ActiveExplainer, ActiveWatchdog) VALUES ((SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\"), 0, 0, '+redTurn+', 0, 0);',
error => {
if (error) {
console.error(error);
......@@ -103,6 +105,58 @@ function createRouter(db) {
}
});
//Return S2C
router.post('/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' +
'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' +
');',
(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(500).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: 'error'});
}
});
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