Commit 748d2225 authored by Dennis Willers's avatar Dennis Willers 🏀

Implement ActiveCard

Change S2C
parent d05bcfd3
Pipeline #341 passed with stages
in 3 minutes and 11 seconds
......@@ -46,7 +46,7 @@ function createRouter(db) {
const getGamestatus = function(req, res) {
const spielname = req.body.spielname;
if (spielname.match(/^[0-9a-z]+$/) != null) {
const sql = 'SELECT SessionID, Red, Blue, RedTurn, ActiveExplainer, ActiveWatchdog FROM Gamestatus WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")';
const sql = 'SELECT SessionID, Red, Blue, RedTurn, ActiveExplainer, ActiveCard FROM Gamestatus WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")';
console.log('getGamestatus: ',sql);
db.query(
sql,
......@@ -63,7 +63,7 @@ function createRouter(db) {
blue: results[0].Blue,
redTurn: results[0].RedTurn,
activeExplainer: results[0].ActiveExplainer,
activeWatchdog: results[0].ActiveWatchdog
activeCard: results[0].ActiveCard
}
);
}
......@@ -74,6 +74,25 @@ function createRouter(db) {
}
};
const getActiveCard = function getActiveCard(req, res) {
const spielname = req.body.spielname;
if (spielname.match(/^[0-9a-z]+$/) != null) {
const sql = 'SELECT ActiveCard FROM Gamestatus WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\");';
console.log('getActiveCard: ', sql);
db.query(
sql,
(error, results) => {
if (error) {
console.log(error);
res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'error'});
} else {
console.log(results);
res.header('Access-Control-Allow-Origin', "*").status(200).json({cardID: results[0].ActiveCard});
}
})
}
};
const addSession = function addSession(req, res) {
const spielname = req.body.spielname;
if (spielname.match(/^[0-9a-z]+$/) != null) {
......@@ -89,7 +108,7 @@ 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);';
const sql = 'INSERT INTO Gamestatus (SessionID, Red, Blue, RedTurn, ActiveExplainer, ActiveCard) VALUES ((SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\"), 0, 0, '+redTurn+', 0, 0);';
console.log('addSession2: ', sql);
db.query(
sql,
......@@ -147,35 +166,36 @@ function createRouter(db) {
res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'false'});
} else {
if (results.length > 0) {
let random = Math.random() * (results.length-1);
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+');';
const sqlUpdate = 'UPDATE Gamestatus SET ActiveCard = ' + card.CardID + ' WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"' + spielname + '\")';
console.log('getS2C3: ', sql);
db.query(sqlUpdate);
const sqlInsert = '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.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'false'});
} else {
res.header('Access-Control-Allow-Origin', "*").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,
}
);
socket.emit('newCard', {'sessionName': spielname, 'cardID': card.CardID})
}
db.query(sqlInsert);
res.header('Access-Control-Allow-Origin', "*").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,
}
);
socket.emit('newCard', {
'sessionName': spielname,
'cardID': card.CardID
})
} else {
res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'false'});
res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'error alle Karten durch'});
}
}
});
......@@ -212,7 +232,7 @@ function createRouter(db) {
const newRound = function newRound(req, res) {
const spielname = req.body.spielname;
if (spielname.match(/^[0-9a-z]+$/) != null) {
const sql = 'UPDATE Gamestatus SET ActiveExplainer = 1, ActiveWatchdog=1 WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")';
const sql = 'UPDATE Gamestatus SET ActiveExplainer = 1 WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")';
console.log('newRound: ', sql);
db.query(
sql,
......@@ -238,7 +258,7 @@ function createRouter(db) {
function endRound(req) {
const spielname = req.body.spielname;
if (spielname.match(/^[0-9a-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+'\")';
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, ActiveCard =0 WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")';
console.log('endRound: ', sql);
db.query(
sql,
......@@ -391,7 +411,7 @@ function createRouter(db) {
const cardID = req.body.cardID;
if (Number.isInteger(cardID)) {
const sql = 'SELECT CardID, Solution, Tabu1, Tabu2, Tabu3, Tabu4, Tabu5 FROM Card WHERE CardID = \"'+cardID+'\"';
console.log('Session: ', sql);
console.log('getCard: ', sql);
db.query(
sql,
(error, results) => {
......@@ -436,13 +456,13 @@ function createRouter(db) {
router.post('/getS2C', [getS2C]);
//Start a new Round and lock the game positions
router.post('/newRound', [isActiveExplainer, newRound]);
router.post('/newRound', [isSession, isActiveExplainer, 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', [isActiveExplainer, newGame]);
router.post('/newGame', [isSession, isActiveExplainer, newGame]);
//Add Point
router.post('/addPoint', [addPoint]);
......@@ -450,7 +470,10 @@ function createRouter(db) {
//Remove Point
router.post('/removePoint', [removePoint]);
//Get CardID
//Get ActiveCard
router.post('/getActiveCard', [isSession, getActiveCard]);
//Get Card
router.post('/getCard', [getCard]);
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