constsql='SELECT SessionName FROM Session WHERE SessionName = \''+spielname+'\';';
console.log('isSession:',sql);
db.query(
db.query(
'SELECT SessionName FROM Session WHERE SessionName = \''+spielname+'\';',
sql,
(error,results)=>{
(error,results)=>{
if(error){
if(error){
console.log(error);
console.log(error);
returnres.status(500).json({status:'error'});
returnres.status(500).json({status:'error'});
}else{
}else{
if(results.length>0){
if(results.length>0){
returnres.status(200).json({status:'true'});
next();
}else{
}else{
returnres.status(200).json({status:'false'});
returnres.status(200).json({status:'false'});
}
}
...
@@ -24,16 +30,46 @@ function createRouter(db) {
...
@@ -24,16 +30,46 @@ function createRouter(db) {
}else{
}else{
returnres.status(500).json({status:'error'});
returnres.status(500).json({status:'error'});
}
}
}
};
// the routes are defined here
vargetGamestatus=function(req,res,next){
constspielname=req.body.spielname;
if(spielname.match(/^[0-9a-zA-Z]+$/)!=null){
constsql='SELECT SessionID, Red, Blue, RedTurn, ActiveExplainer, ActiveWatchdog FROM Gamestatus WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")';
console.log('getGamestatus: ',sql);
db.query(
sql,
(error,results)=>{
if(error){
console.log(error);
res.status(500).json({status:'false'});
}else{
res.status(200).json(
{
status:'true',
sessionID:results[0].SessionID,
red:results[0].Red,
blue:results[0].Blue,
redTurn:results[0].RedTurn,
activeExplainer:results[0].ActiveExplainer,
activeWatchdog:results[0].ActiveWatchdog
}
);
}
}
);
}else{
res.status(500).json({status:'error'});
}
};
//Generate new Session
varaddSession=functionaddSession(req,res){
router.post('/addSession',(req,res)=>{
constspielname=req.body.spielname;
constspielname=req.body.spielname;
if(spielname.match(/^[0-9a-zA-Z]+$/)!=null){
if(spielname.match(/^[0-9a-zA-Z]+$/)!=null){
constsql='INSERT INTO Session (SessionName) VALUES (\"'+spielname+'\");';
console.log('addSession1: ',sql);
db.query(
db.query(
'INSERT INTO Session (SessionName) VALUES (\"'+spielname+'\");',
sql,
error=>{
error=>{
if(error){
if(error){
console.error(error);
console.error(error);
...
@@ -42,8 +78,10 @@ function createRouter(db) {
...
@@ -42,8 +78,10 @@ function createRouter(db) {
//Init new Gamestatus
//Init new Gamestatus
varredTurn=Math.random();
varredTurn=Math.random();
redTurn=Math.round(redTurn);
redTurn=Math.round(redTurn);
constsql='INSERT INTO Gamestatus (SessionID, Red, Blue, RedTurn, ActiveExplainer, ActiveWatchdog) VALUES ((SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\"), 0, 0, '+redTurn+', 0, 0);';
console.log('addSession2: ',sql);
db.query(
db.query(
'INSERT INTO Gamestatus (SessionID, Red, Blue, RedTurn, ActiveExplainer, ActiveWatchdog) VALUES ((SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\"), 0, 0, '+redTurn+', 0, 0);',
sql,
error=>{
error=>{
if(error){
if(error){
console.error(error);
console.error(error);
...
@@ -59,17 +97,13 @@ function createRouter(db) {
...
@@ -59,17 +97,13 @@ function createRouter(db) {
}else{
}else{
res.status(500).json({status:'error'});
res.status(500).json({status:'error'});
}
}
});
};
//Check if Session exist
varsession=functionsession(req,res){
router.post('/isSession',(req,res)=>{
constsql='SELECT * FROM Session';
isSession(req,res)
console.log('Session: ',sql);
});
//Return all Sessions (Not neccesary)
router.get('/Session',function(req,res){
db.query(
db.query(
'SELECT * FROM Session',
sql,
(error,results)=>{
(error,results)=>{
if(error){
if(error){
console.log(error);
console.log(error);
...
@@ -79,65 +113,37 @@ function createRouter(db) {
...
@@ -79,65 +113,37 @@ function createRouter(db) {
}
}
}
}
);
);
});
};
//Return GameStatus
router.post('/getGamestatus',(req,res)=>{
constspielname=req.body.spielname;
if(spielname.match(/^[0-9a-zA-Z]+$/)!=null){
db.query(
'SELECT SessionID, Red, Blue, RedTurn, ActiveExplainer, ActiveWatchdog FROM Gamestatus WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")',
' WHERE S2C.CardID IS NOT NULL AND S2C.SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")\n'+
' WHERE S2C.CardID IS NOT NULL AND S2C.SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")\n'+
');',
');';
console.log('getS2C1: ',sql);
db.query(
sql,
(error,results)=>{
(error,results)=>{
if(error){
if(error){
console.log(error);
console.log(error);
res.status(500).json({status:'false'});
res.status(500).json({status:'false'});
}else{
}else{
console.log(results);
if(results.length>0){
letrandom=Math.random()*(results.length-1);
letrandom=Math.random()*(results.length-1);
random=Math.round(random)
random=Math.round(random);
card=results[random];
card=results[random];
console.log(card);
constsql='INSERT INTO S2C (SessionID, CardID) VALUES ((SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\"), '+card.CardID+');';
console.log('getS2C2: ',sql);
db.query(
db.query(
'INSERT INTO S2C (SessionID, CardID) VALUES ((SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\"), '+card.CardID+');',
sql,
(error,results)=>{
(error)=>{
if(error){
if(error){
res.status(500).json({status:'false'});
res.status(500).json({status:'false'});
}else{
}else{
...
@@ -156,20 +162,24 @@ function createRouter(db) {
...
@@ -156,20 +162,24 @@ function createRouter(db) {
}
}
}
}
);
);
}else{
res.status(500).json({status:'false'});
}
}
}
});
});
}else{
}else{
res.status(500).json({status:'error'});
res.status(500).json({status:'error'});
}
}
});
};
//Start a new Round and lock the game positions
varnewRound=functionnewRound(req,res){
router.post('/newRound',(req,res)=>{
constspielname=req.body.spielname;
constspielname=req.body.spielname;
if(spielname.match(/^[0-9a-zA-Z]+$/)!=null){
if(spielname.match(/^[0-9a-zA-Z]+$/)!=null){
constsql='UPDATE Gamestatus SET ActiveExplainer = 1, ActiveWatchdog=1 WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")';
console.log('newRound: ',sql);
db.query(
db.query(
'UPDATE Gamestatus SET ActiveExplainer = 1, ActiveWatchdog=1 WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")',
sql,
(error,results)=>{
(error)=>{
if(error){
if(error){
console.log(error);
console.log(error);
res.status(500).json({status:'false'});
res.status(500).json({status:'false'});
...
@@ -181,15 +191,16 @@ function createRouter(db) {
...
@@ -181,15 +191,16 @@ function createRouter(db) {
}else{
}else{
res.status(500).json({status:'false'});
res.status(500).json({status:'false'});
}
}
});
};
//End a Round and unlock the game positions and invert RedTurn
varendRound=functionendRound(req,res){
router.post('/endRound',(req,res)=>{
constspielname=req.body.spielname;
constspielname=req.body.spielname;
if(spielname.match(/^[0-9a-zA-Z]+$/)!=null){
if(spielname.match(/^[0-9a-zA-Z]+$/)!=null){
constsql='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+'\")';
console.log('endRound: ',sql);
db.query(
db.query(
'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+'\")',
sql,
(error,results)=>{
(error)=>{
if(error){
if(error){
console.log(error);
console.log(error);
res.status(500).json({status:'false'});
res.status(500).json({status:'false'});
...
@@ -201,15 +212,16 @@ function createRouter(db) {
...
@@ -201,15 +212,16 @@ function createRouter(db) {
}else{
}else{
res.status(500).json({status:'false'});
res.status(500).json({status:'false'});
}
}
});
};
//Starts a new Game and resets red and blue and chooses a random value for RedTurn
varnewGame=functionnewGame(req,res){
router.post('/newGame',(req,res)=>{
constspielname=req.body.spielname;
constspielname=req.body.spielname;
if(spielname.match(/^[0-9a-zA-Z]+$/)!=null){
if(spielname.match(/^[0-9a-zA-Z]+$/)!=null){
constsql='UPDATE Gamestatus SET Red = 0, Blue = 0, RedTurn = '+Math.round(Math.random())+' WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")';
console.log('newGame: ',sql);
db.query(
db.query(
'UPDATE Gamestatus SET Red = 0, Blue = 0, RedTurn = '+Math.round(Math.random())+' WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")',
constsql='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+'\")';
console.log('addPoint Red:',sql);
db.query(
db.query(
'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+'\")',
sql,
(error)=>{
(error)=>{
if(error){
if(error){
console.log(error);
console.log(error);
...
@@ -248,8 +261,10 @@ function createRouter(db) {
...
@@ -248,8 +261,10 @@ function createRouter(db) {
}
}
);
);
}elseif(team==='blue'){
}elseif(team==='blue'){
constsql='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+'\")';
console.log('addPoint Blue:',sql);
db.query(
db.query(
'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+'\")',
constsql='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+'\")';
console.log('removePoint Red: ',sql);
db.query(
db.query(
'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+'\")',
sql,
(error)=>{
(error)=>{
if(error){
if(error){
console.log(error);
console.log(error);
...
@@ -296,8 +312,10 @@ function createRouter(db) {
...
@@ -296,8 +312,10 @@ function createRouter(db) {
}
}
);
);
}elseif(team==='blue'){
}elseif(team==='blue'){
constsql='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+'\")';
console.log('removePoint Blue: ',sql);
db.query(
db.query(
'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+'\")',