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) { ...@@ -46,7 +46,7 @@ function createRouter(db) {
const getGamestatus = function(req, res) { const getGamestatus = function(req, res) {
const spielname = req.body.spielname; const spielname = req.body.spielname;
if (spielname.match(/^[0-9a-z]+$/) != null) { 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); console.log('getGamestatus: ',sql);
db.query( db.query(
sql, sql,
...@@ -63,7 +63,7 @@ function createRouter(db) { ...@@ -63,7 +63,7 @@ function createRouter(db) {
blue: results[0].Blue, blue: results[0].Blue,
redTurn: results[0].RedTurn, redTurn: results[0].RedTurn,
activeExplainer: results[0].ActiveExplainer, activeExplainer: results[0].ActiveExplainer,
activeWatchdog: results[0].ActiveWatchdog activeCard: results[0].ActiveCard
} }
); );
} }
...@@ -74,6 +74,25 @@ function createRouter(db) { ...@@ -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 addSession = function addSession(req, res) {
const spielname = req.body.spielname; const spielname = req.body.spielname;
if (spielname.match(/^[0-9a-z]+$/) != null) { if (spielname.match(/^[0-9a-z]+$/) != null) {
...@@ -89,7 +108,7 @@ function createRouter(db) { ...@@ -89,7 +108,7 @@ 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);'; 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); console.log('addSession2: ', sql);
db.query( db.query(
sql, sql,
...@@ -147,35 +166,36 @@ function createRouter(db) { ...@@ -147,35 +166,36 @@ function createRouter(db) {
res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'false'}); res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'false'});
} else { } else {
if (results.length > 0) { 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];
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); console.log('getS2C2: ', sql);
db.query( db.query(sqlInsert);
sql, res.header('Access-Control-Allow-Origin', "*").status(200).json(
(error) => { {
if (error) { status: 'true',
res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'false'}); cardID: card.CardID,
} else { solution: card.Solution,
res.header('Access-Control-Allow-Origin', "*").status(200).json( tabu1: card.Tabu1,
{ tabu2: card.Tabu2,
status: 'true', tabu3: card.Tabu3,
cardID: card.CardID, tabu4: card.Tabu4,
solution: card.Solution, tabu5: card.Tabu5,
tabu1: card.Tabu1,
tabu2: card.Tabu2,
tabu3: card.Tabu3,
tabu4: card.Tabu4,
tabu5: card.Tabu5,
}
);
socket.emit('newCard', {'sessionName': spielname, 'cardID': card.CardID})
}
} }
); );
socket.emit('newCard', {
'sessionName': spielname,
'cardID': card.CardID
})
} else { } 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) { ...@@ -212,7 +232,7 @@ function createRouter(db) {
const newRound = function newRound(req, res) { const newRound = function newRound(req, res) {
const spielname = req.body.spielname; const spielname = req.body.spielname;
if (spielname.match(/^[0-9a-z]+$/) != null) { 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); console.log('newRound: ', sql);
db.query( db.query(
sql, sql,
...@@ -238,7 +258,7 @@ function createRouter(db) { ...@@ -238,7 +258,7 @@ function createRouter(db) {
function endRound(req) { function endRound(req) {
const spielname = req.body.spielname; const spielname = req.body.spielname;
if (spielname.match(/^[0-9a-z]+$/) != null) { 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); console.log('endRound: ', sql);
db.query( db.query(
sql, sql,
...@@ -391,7 +411,7 @@ function createRouter(db) { ...@@ -391,7 +411,7 @@ function createRouter(db) {
const cardID = req.body.cardID; const cardID = req.body.cardID;
if (Number.isInteger(cardID)) { if (Number.isInteger(cardID)) {
const sql = 'SELECT CardID, Solution, Tabu1, Tabu2, Tabu3, Tabu4, Tabu5 FROM Card WHERE CardID = \"'+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( db.query(
sql, sql,
(error, results) => { (error, results) => {
...@@ -436,13 +456,13 @@ function createRouter(db) { ...@@ -436,13 +456,13 @@ function createRouter(db) {
router.post('/getS2C', [getS2C]); router.post('/getS2C', [getS2C]);
//Start a new Round and lock the game positions //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 //End a Round and unlock the game positions and invert RedTurn
router.post('/endRound', [endRound]); router.post('/endRound', [endRound]);
//Starts a new Game and resets red and blue and chooses a random value for RedTurn //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 //Add Point
router.post('/addPoint', [addPoint]); router.post('/addPoint', [addPoint]);
...@@ -450,7 +470,10 @@ function createRouter(db) { ...@@ -450,7 +470,10 @@ function createRouter(db) {
//Remove Point //Remove Point
router.post('/removePoint', [removePoint]); router.post('/removePoint', [removePoint]);
//Get CardID //Get ActiveCard
router.post('/getActiveCard', [isSession, getActiveCard]);
//Get Card
router.post('/getCard', [getCard]); router.post('/getCard', [getCard]);
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