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

Implement History Feature

parent edbefb0f
Pipeline #378 passed with stages
in 3 minutes and 15 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, ActiveCard FROM Gamestatus WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")';
const sql = 'SELECT SessionID, Round, Red, Blue, RedTurn, ActiveExplainer, ActiveCard, isTabooh, lastPlay FROM Gamestatus WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")';
console.log('getGamestatus: ',sql);
db.query(
sql,
......@@ -58,12 +58,15 @@ function createRouter(db) {
res.header('Access-Control-Allow-Origin', "*").status(200).json(
{
status: 'true',
round: results[0].Round,
sessionID: results[0].SessionID,
red: results[0].Red,
blue: results[0].Blue,
redTurn: results[0].RedTurn,
activeExplainer: results[0].ActiveExplainer,
activeCard: results[0].ActiveCard
activeCard: results[0].ActiveCard,
isTabooh: results[0].isTabooh,
lastPlay: results[0].lastPlay
}
);
}
......@@ -108,7 +111,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, ActiveCard, isTabooh) VALUES ((SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\"), 0, 0, '+redTurn+', 0, 0, 0);';
const sql = 'INSERT INTO Gamestatus (SessionID, Round, Red, Blue, RedTurn, ActiveExplainer, ActiveCard, isTabooh, lastPlay) VALUES ((SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\"), 0, 0, 0, '+redTurn+', 0, 0, 0, now());';
console.log('addSession2: ', sql);
db.query(
sql,
......@@ -175,7 +178,7 @@ function createRouter(db) {
db.query(sqlUpdate);
const sqlInsert = 'INSERT INTO S2C (SessionID, CardID) VALUES ((SELECT SessionID FROM Session WHERE SessionName = \"' + spielname + '\"), ' + card.CardID + ');';
const sqlInsert = 'INSERT INTO S2C (SessionID, CardID, Round, RedTurn, CardResultID, ActiveGame) VALUES ((SELECT SessionID FROM Session WHERE SessionName = \"' + spielname + '\"), ' + card.CardID + ', (SELECT Round FROM Gamestatus WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"' + spielname + '\")), (SELECT RedTurn FROM Gamestatus WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"' + spielname + '\")), 0, 1);';
console.log('getS2C2: ', sql);
db.query(sqlInsert);
res.header('Access-Control-Allow-Origin', "*").status(200).json(
......@@ -232,7 +235,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 WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")';
const sql = 'UPDATE Gamestatus SET ActiveExplainer = 1, lastPlay = now(), Round = Round + 1, isTabooh = 0 WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")';
console.log('newRound: ', sql);
db.query(
sql,
......@@ -258,7 +261,8 @@ 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, ActiveCard =0 WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")';
const sql = 'UPDATE S2C SET CardResultID = 1 WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\") AND CardID = (SELECT ActiveCard FROM Gamestatus WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\"));' +
'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,
......@@ -284,7 +288,8 @@ function createRouter(db) {
const newGame = function newGame(req, res) {
const spielname = req.body.spielname;
if (spielname.match(/^[0-9a-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+'\")';
const sql = 'UPDATE Gamestatus SET Round = 0, Red = 0, Blue = 0, RedTurn = '+Math.round(Math.random())+' WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\");' +
'UPDATE S2C SET ActiveGame = 0 WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\");';
console.log('newGame: ', sql);
db.query(
sql,
......@@ -421,10 +426,12 @@ function createRouter(db) {
const addPoint = function addPoint(req, res) {
const spielname = req.body.spielname;
const cardID = req.body.cardID;
const team = req.body.team;
if (spielname.match(/^[0-9a-z]+$/) != null && team.match(/^[0-9a-z]+$/) != null) {
if (spielname.match(/^[0-9a-z]+$/) != null && team.match(/^[0-9a-z]+$/) != null && !isNaN(cardID)) {
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+'\")';
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+'\");' +
'UPDATE S2C SET CardResultID = 2 WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\") AND CardID = '+cardID+';';
console.log('addPoint Red:', sql);
db.query(
sql,
......@@ -444,7 +451,8 @@ function createRouter(db) {
}
);
} 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+'\")';
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+'\");' +
'UPDATE S2C SET CardResultID = 2 WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\") AND CardID = '+cardID+';';
console.log('addPoint Blue:', sql);
db.query(
sql,
......@@ -471,10 +479,12 @@ function createRouter(db) {
const removePoint = function removePoint(req, res, next) {
const spielname = req.body.spielname;
const cardID = req.body.cardID;
const team = req.body.team;
if (spielname.match(/^[0-9a-z]+$/) != null && team.match(/^[0-9a-z]+$/) != null) {
if (spielname.match(/^[0-9a-z]+$/) != null && team.match(/^[0-9a-z]+$/) != null && !isNaN(cardID)) {
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+'\")';
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+'\");' +
'UPDATE S2C SET CardResultID = 4 WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\") AND CardID = '+cardID+';';
console.log('removePoint Red: ', sql);
db.query(
sql,
......@@ -488,7 +498,8 @@ function createRouter(db) {
}
);
} 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+'\")';
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+'\");' +
'UPDATE S2C SET CardResultID = 4 WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\") AND CardID = '+cardID+';';
console.log('removePoint Blue: ', sql);
db.query(
sql,
......@@ -507,6 +518,30 @@ function createRouter(db) {
}
};
const skipCard = function skipCard(req, res) {
const spielname = req.body.spielname;
const cardID = req.body.cardID;
if (spielname.match(/^[0-9a-z]+$/) != null && !isNaN(cardID)) {
const sql = 'UPDATE S2C SET CardResultID = 3 WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\") AND CardID = '+cardID+';';
console.log('skipCard: ', sql);
db.query(
sql,
(error) => {
if (error) {
console.log(error);
res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'false'});
} else {
res.header('Access-Control-Allow-Origin', "*").status(200).json(
{
status: 'true',
}
);
}
}
);
}
};
const getCard = function getCard(req, res) {
const cardID = req.body.cardID;
if (Number.isInteger(cardID)) {
......@@ -538,6 +573,69 @@ function createRouter(db) {
}
};
const getSessionHistory = function getSessionHistory(req, res) {
const spielname = req.body.spielname;
if (spielname.match(/^[0-9a-z]+$/)) {
const sql = 'SELECT s.S2CID, c.Solution, c.Tabu1, c.Tabu2, c.Tabu3, c.Tabu4, c.Tabu5, s.Round, s.RedTurn, r.CardResultID, r.CardResultName\n' +
'FROM S2C s LEFT JOIN Card c ON s.CardID = c.CardID LEFT JOIN CardResult r ON s.CardResultID = r.CardResultID\n' +
'WHERE s.ActiveGame = 1 AND s.SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")' +
'ORDER BY s.S2CID DESC';
console.log('getSessionHistory: ', sql);
db.query(
sql,
(error, results) => {
if (error) {
console.log(error);
res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'error'});
} else {
res.header('Access-Control-Allow-Origin', "*").status(200).send({status: true, history: results})
}
})
}
};
const historyChangePoint = function historyChangePoint(req, res, next) {
const spielname = req.body.spielname;
const s2cID = req.body.s2cID;
const cardResultID = req.body.cardResultID;
const redTurn = req.body.redTurn;
const point = req.body.point;
const timestamp = req.body.time;
console.log(spielname, redTurn, point, s2cID, cardResultID);
if (spielname.match(/^[0-9a-z]+$/) != null && !isNaN(redTurn) && !isNaN(s2cID) && !isNaN(cardResultID) && !isNaN(point) && -2 <= Number(point) <= 2) {
//Get Team
const redTurnNumber = Number(redTurn);
let team = '';
if (redTurnNumber === 0) {
team = 'Blue'
} else {
team = 'Red'
}
//Create SQL
const sql = 'UPDATE S2C SET CardResultID = '+ cardResultID + ' WHERE S2CID = '+s2cID+';' +
'UPDATE Gamestatus SET ' + team +' = ' + point + ' +(SELECT ' + team + ' FROM Gamestatus WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")) WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\");';
console.log('historyChangePoint: ', sql);
db.query(
sql,
(error) => {
if (error) {
console.log(error);
res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'false'});
} else {
next();
socket.emit('historyChanged', {
sessionName: spielname,
time: timestamp
})
}
}
);
} else {
res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'false'});
}
};
// the routes are defined here
//Generate new Session
......@@ -570,12 +668,21 @@ function createRouter(db) {
//Remove Point
router.post('/removePoint', [isPoint4Card, canPressTabooh, setIsTaboohTo1, removePoint, acceptRemovePoint]);
//Skip Card
router.post('/skipCard', [isPoint4Card, skipCard]);
//Get ActiveCard
router.post('/getActiveCard', [isSession, getActiveCard]);
//Get Card
router.post('/getCard', [getCard]);
//Get Session History
router.post('/getSessionHistory', [getSessionHistory]);
//Change Point or Points in History
router.post('/historyChangePoint', [historyChangePoint, getGamestatus]);
return router;
}
......
......@@ -10,7 +10,8 @@ const connection = mysql.createConnection({
port : '3307',
user : 'tabuApp',
password : 'Mannheim123!',
database : 'tabu'
database : 'tabu',
multipleStatements: true,
});
connection.connect();
......
......@@ -49,5 +49,10 @@ Socketio.on("connection", socket => {
Socketio.emit(data.sessionName+":canPressTabooh", data.canPressTabooh);
socket.emit(data.sessionName+":canPressTabooh", data.canPressTabooh);
});
socket.on("historyChanged", data => {
console.log('historyChanged from: ', data.sessionName);
Socketio.emit(data.sessionName+":historyChanged", data);
socket.emit(data.sessionName+":historyChanged", data);
});
});
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