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

Fix Button Spam Bug / Add Debug Option

parent 23572366
Pipeline #390 passed with stage
in 1 minute and 46 seconds
......@@ -148,9 +148,10 @@ function createRouter(db) {
);
};
const getS2C = function getS2C(req, res) {
const getS2C = async function getS2C(req, res) {
const spielname = req.body.spielname;
if (spielname.match(/^[0-9a-z]+$/) != null) {
// First Find next Card
let card;
const sql = 'SELECT Card.CardID, Solution, Tabu1, Tabu2, Tabu3, Tabu4, Tabu5 \n' +
'FROM Card \n' +
......@@ -173,30 +174,58 @@ function createRouter(db) {
random = Math.round(random);
card = results[random];
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, 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(
{
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
})
// Second Check if Nobody Already make a new Card. If not - Make a new Card
const sqlInsert = 'INSERT INTO S2C (SessionID, CardID, Round, RedTurn, CardResultID, ActiveGame) \n' +
'SELECT * FROM \n' +
'(SELECT \n' +
'(SELECT SessionID\n' +
'FROM Session WHERE SessionName = \"'+spielname+'\"), \n' +
'\"'+card.CardID+'\", \n' +
'(SELECT Round FROM Gamestatus WHERE SessionID = (SELECT\n' +
'SessionID FROM Session WHERE SessionName = \"'+spielname+'\")), \n' +
'(SELECT RedTurn FROM Gamestatus WHERE SessionID =\n' +
'(SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")),\n' +
'0, \n' +
'1\n' +
') AS tmp\n' +
'WHERE 0 = (SELECT COUNT(CardResultID) AS ActiveCard FROM S2C WHERE ActiveGame = 1 AND Round = (SELECT Round FROM Gamestatus WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")) AND SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\") AND CardResultID = 0);' +
'SELECT ROW_COUNT() AS CheckUpdate;';
console.log('getS2C Insert: ', sqlInsert);
db.query(sqlInsert,
(error, results) => {
if (error) {
console.log(error);
res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'false'});
} else {
const checkUpdate = results[0].affectedRows;
console.log('CheckUpdate: ', results[0].affectedRows);
if (checkUpdate === 1) {
// Third Say everybody there is a new Card
const sqlUpdate = 'UPDATE Gamestatus SET ActiveCard = ' + card.CardID + ' WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"' + spielname + '\")';
console.log('getS2C Update: ', sqlUpdate);
db.query(sqlUpdate);
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 {
console.log('Check Update Failed: ', checkUpdate, error);
res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'false', reason: 'checkUpdate Failed with '+checkUpdate});
}
}
});
} else {
res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'error alle Karten durch'});
}
......
This diff is collapsed.
......@@ -4,7 +4,9 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon index.js",
"debug": "nodemon --inspect index.js"
},
"keywords": [],
"author": "",
......@@ -15,5 +17,8 @@
"mysql": "^2.18.1",
"socket.io": "^3.0.4",
"socket.io-client": "^3.0.4"
},
"devDependencies": {
"nodemon": "^2.0.7"
}
}
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