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

Implement Socket for Multiplayer Gaming

parent 6a1bcda4
Pipeline #316 passed with stages
in 3 minutes and 10 seconds
...@@ -42,6 +42,6 @@ docker-deploy: ...@@ -42,6 +42,6 @@ docker-deploy:
script: script:
- ssh root@willers.digital -p 2233 "/usr/local/bin/docker login hub.willers.digital -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD" - ssh root@willers.digital -p 2233 "/usr/local/bin/docker login hub.willers.digital -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD"
- ssh root@willers.digital -p 2233 "/usr/local/bin/docker pull hub.willers.digital/dennis.willers/tabu-middleware:latest" - ssh root@willers.digital -p 2233 "/usr/local/bin/docker pull hub.willers.digital/dennis.willers/tabu-middleware:latest"
- ssh root@willers.digital -p 2233 "/usr/local/bin/docker run -d -p 3150:8080 --restart=on-failure:5 --mount type=bind,src=/volume1/docker/tmp6,dst=/tmp --name tabu-middleware hub.willers.digital/dennis.willers/tabu-middleware:latest" - ssh root@willers.digital -p 2233 "/usr/local/bin/docker run -d -p 3150:8080 -p 3000:3000 --restart=on-failure:5 --mount type=bind,src=/volume1/docker/tmp6,dst=/tmp --name tabu-middleware hub.willers.digital/dennis.willers/tabu-middleware:latest"
only: only:
- master - master
FROM node:12.18.1 FROM node:12.18.1
ENV NODE_ENV=production ENV NODE_ENV=production
EXPOSE 8080 EXPOSE 8080
EXPOSE 3000
WORKDIR /app WORKDIR /app
COPY ["package.json", "package-lock.json*", "./"] COPY ["package.json", "package-lock.json*", "./"]
RUN npm install --production RUN npm install --production
......
const express = require('express'); const express = require('express');
const io = require("socket.io-client");
const socket = io('http://localhost:3000');
function createRouter(db) { function createRouter(db) {
const router = express.Router(); const router = express.Router();
var isTrue = function(req, res) { const isTrue = function(req, res) {
res.status(200).json({status: 'true'}); res.status(200).json({status: 'true'});
}; };
var isSession = function isSession(req, res, next) { const isSession = function isSession(req, res, next) {
const spielname = req.body.spielname; const spielname = req.body.spielname;
if (spielname.match(/^[0-9a-zA-Z]+$/) != null) { if (spielname.match(/^[0-9a-zA-Z]+$/) != null) {
const sql = 'SELECT SessionName FROM Session WHERE SessionName = \''+spielname+'\';'; const sql = 'SELECT SessionName FROM Session WHERE SessionName = \''+spielname+'\';';
...@@ -32,7 +34,7 @@ function createRouter(db) { ...@@ -32,7 +34,7 @@ function createRouter(db) {
} }
}; };
var getGamestatus = function(req, res, next) { const getGamestatus = function(req, res) {
const spielname = req.body.spielname; const spielname = req.body.spielname;
if (spielname.match(/^[0-9a-zA-Z]+$/) != null) { if (spielname.match(/^[0-9a-zA-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, ActiveWatchdog FROM Gamestatus WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")';
...@@ -63,7 +65,7 @@ function createRouter(db) { ...@@ -63,7 +65,7 @@ function createRouter(db) {
} }
}; };
var 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-zA-Z]+$/) != null) { if (spielname.match(/^[0-9a-zA-Z]+$/) != null) {
const sql = 'INSERT INTO Session (SessionName) VALUES (\"'+spielname+'\");'; const sql = 'INSERT INTO Session (SessionName) VALUES (\"'+spielname+'\");';
...@@ -99,7 +101,7 @@ function createRouter(db) { ...@@ -99,7 +101,7 @@ function createRouter(db) {
} }
}; };
var session = function session(req, res) { const session = function session(req, res) {
const sql = 'SELECT * FROM Session'; const sql = 'SELECT * FROM Session';
console.log('Session: ', sql); console.log('Session: ', sql);
db.query( db.query(
...@@ -115,7 +117,7 @@ function createRouter(db) { ...@@ -115,7 +117,7 @@ function createRouter(db) {
); );
}; };
var getS2C = function getS2C(req, res) { const getS2C = function getS2C(req, res) {
const spielname = req.body.spielname; const spielname = req.body.spielname;
if (spielname.match(/^[0-9a-zA-Z]+$/) != null) { if (spielname.match(/^[0-9a-zA-Z]+$/) != null) {
let card; let card;
...@@ -159,6 +161,7 @@ function createRouter(db) { ...@@ -159,6 +161,7 @@ function createRouter(db) {
tabu5: card.Tabu5, tabu5: card.Tabu5,
} }
); );
socket.emit('newCard', {'sessionName': spielname, 'cardID': card.CardID})
} }
} }
); );
...@@ -172,7 +175,7 @@ function createRouter(db) { ...@@ -172,7 +175,7 @@ function createRouter(db) {
} }
}; };
var 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-zA-Z]+$/) != null) { if (spielname.match(/^[0-9a-zA-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, ActiveWatchdog=1 WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")';
...@@ -193,7 +196,7 @@ function createRouter(db) { ...@@ -193,7 +196,7 @@ function createRouter(db) {
} }
}; };
var endRound = function endRound(req, res) { const endRound = function endRound(req, res) {
const spielname = req.body.spielname; const spielname = req.body.spielname;
if (spielname.match(/^[0-9a-zA-Z]+$/) != null) { if (spielname.match(/^[0-9a-zA-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, ActiveWatchdog=0 WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")';
...@@ -214,7 +217,7 @@ function createRouter(db) { ...@@ -214,7 +217,7 @@ function createRouter(db) {
} }
}; };
var newGame = function newGame(req, res) { const newGame = function newGame(req, res) {
const spielname = req.body.spielname; const spielname = req.body.spielname;
if (spielname.match(/^[0-9a-zA-Z]+$/) != null) { if (spielname.match(/^[0-9a-zA-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 Red = 0, Blue = 0, RedTurn = '+Math.round(Math.random())+' WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")';
...@@ -235,7 +238,7 @@ function createRouter(db) { ...@@ -235,7 +238,7 @@ function createRouter(db) {
} }
}; };
var addPoint = function addPoint(req, res) { const addPoint = function addPoint(req, res) {
console.log(req.body); console.log(req.body);
const spielname = req.body.spielname; const spielname = req.body.spielname;
const team = req.body.team; const team = req.body.team;
...@@ -286,7 +289,7 @@ function createRouter(db) { ...@@ -286,7 +289,7 @@ function createRouter(db) {
} }
}; };
var removePoint = function removePoint(req, res) { const removePoint = function removePoint(req, res) {
console.log(req.body); console.log(req.body);
const spielname = req.body.spielname; const spielname = req.body.spielname;
const team = req.body.team; const team = req.body.team;
...@@ -337,6 +340,37 @@ function createRouter(db) { ...@@ -337,6 +340,37 @@ function createRouter(db) {
} }
}; };
const getCard = function getCard(req, res) {
const cardID = req.body.cardID;
if (Number.isInteger(cardID)) {
const sql = 'SELECT CardID, Solution, Tabu1, Tabu2, Tabu3, Tabu4, Tabu5 FROM Card WHERE CardID = \"'+cardID+'\"';
console.log('Session: ', sql);
db.query(
sql,
(error, results) => {
if (error) {
console.log(error);
res.status(500).json({status: 'error'});
} else {
if (results.length > 0) {
res.status(200).json({
cardID: results[0].CardID,
solution: results[0].Solution,
tabu1: results[0].Tabu1,
tabu2: results[0].Tabu2,
tabu3: results[0].Tabu3,
tabu4: results[0].Tabu4,
tabu5: results[0].Tabu5
});
} else {
res.status(500).json({status: 'false'});
}
}
}
);
}
};
// the routes are defined here // the routes are defined here
//Generate new Session //Generate new Session
...@@ -369,6 +403,9 @@ function createRouter(db) { ...@@ -369,6 +403,9 @@ function createRouter(db) {
//Remove Point //Remove Point
router.post('/removePoint', [removePoint]); router.post('/removePoint', [removePoint]);
//Get CardID
router.post('/getCard', [getCard]);
return router; return router;
} }
......
This diff is collapsed.
...@@ -12,6 +12,8 @@ ...@@ -12,6 +12,8 @@
"dependencies": { "dependencies": {
"cors": "^2.8.5", "cors": "^2.8.5",
"express": "^4.17.1", "express": "^4.17.1",
"mysql": "^2.18.1" "mysql": "^2.18.1",
"socket.io": "^3.0.4",
"socket.io-client": "^3.0.4"
} }
} }
const Express = require("express")();
const Http = require("http").Server(Express);
const Socketio = require("socket.io")(Http);
Http.listen(3000, () => {
console.log("Listening at :3000...");
});
var position = {
x: 200,
y: 200
};
var currentCard = {};
Socketio.on("connection", socket => {
console.log('new Socket connect');
socket.emit("position", position);
socket.on("move", data => {
console.log('Data: ', data);
switch(data) {
case "left":
console.log('left');
position.x -= 5;
Socketio.emit("position", position);
break;
case "right":
position.x += 5;
Socketio.emit("position", position);
break;
case "up":
position.y -= 5;
Socketio.emit("position", position);
break;
case "down":
position.y += 5;
Socketio.emit("position", position);
break;
}
});
for (var key in currentCard) {
socket.emit(key, currentCard[key])
}
socket.on("newCard", data => {
console.log('New Card From: ', data, ':newCard');
currentCard[data.sessionName+":newCard"] = data.cardID;
Socketio.emit(data.sessionName+":newCard", data.cardID)
})
});
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