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) { ...@@ -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; const spielname = req.body.spielname;
if (spielname.match(/^[0-9a-z]+$/) != null) { if (spielname.match(/^[0-9a-z]+$/) != null) {
// First Find next Card
let card; let card;
const sql = 'SELECT Card.CardID, Solution, Tabu1, Tabu2, Tabu3, Tabu4, Tabu5 \n' + const sql = 'SELECT Card.CardID, Solution, Tabu1, Tabu2, Tabu3, Tabu4, Tabu5 \n' +
'FROM Card \n' + 'FROM Card \n' +
...@@ -173,30 +174,58 @@ function createRouter(db) { ...@@ -173,30 +174,58 @@ function createRouter(db) {
random = Math.round(random); random = Math.round(random);
card = results[random]; card = results[random];
const sqlUpdate = 'UPDATE Gamestatus SET ActiveCard = ' + card.CardID + ' WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"' + spielname + '\")'; // Second Check if Nobody Already make a new Card. If not - Make a new Card
console.log('getS2C3: ', sql); const sqlInsert = 'INSERT INTO S2C (SessionID, CardID, Round, RedTurn, CardResultID, ActiveGame) \n' +
db.query(sqlUpdate); 'SELECT * FROM \n' +
'(SELECT \n' +
'(SELECT SessionID\n' +
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);'; 'FROM Session WHERE SessionName = \"'+spielname+'\"), \n' +
console.log('getS2C2: ', sql); '\"'+card.CardID+'\", \n' +
db.query(sqlInsert); '(SELECT Round FROM Gamestatus WHERE SessionID = (SELECT\n' +
res.header('Access-Control-Allow-Origin', "*").status(200).json( 'SessionID FROM Session WHERE SessionName = \"'+spielname+'\")), \n' +
{ '(SELECT RedTurn FROM Gamestatus WHERE SessionID =\n' +
status: 'true', '(SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")),\n' +
cardID: card.CardID, '0, \n' +
solution: card.Solution, '1\n' +
tabu1: card.Tabu1, ') AS tmp\n' +
tabu2: card.Tabu2, '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);' +
tabu3: card.Tabu3, 'SELECT ROW_COUNT() AS CheckUpdate;';
tabu4: card.Tabu4, console.log('getS2C Insert: ', sqlInsert);
tabu5: card.Tabu5, db.query(sqlInsert,
} (error, results) => {
); if (error) {
socket.emit('newCard', { console.log(error);
'sessionName': spielname, res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'false'});
'cardID': card.CardID } 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 { } else {
res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'error alle Karten durch'}); res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'error alle Karten durch'});
} }
......
This diff is collapsed.
...@@ -4,7 +4,9 @@ ...@@ -4,7 +4,9 @@
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"scripts": { "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": [], "keywords": [],
"author": "", "author": "",
...@@ -15,5 +17,8 @@ ...@@ -15,5 +17,8 @@
"mysql": "^2.18.1", "mysql": "^2.18.1",
"socket.io": "^3.0.4", "socket.io": "^3.0.4",
"socket.io-client": "^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