Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

base fork: jmagnusson/the-olumpics
base: 9c9b402ee8
...
head fork: jmagnusson/the-olumpics
compare: cb43376e99
  • 2 commits
  • 1 file changed
  • 0 commit comments
  • 1 contributor
Showing with 24 additions and 14 deletions.
  1. +24 −14 api/app.js
38 api/app.js
View
@@ -6,8 +6,8 @@ var mysql = require('mysql');
var _ = require('lodash-node');
var gameData = {};
-var gameidCounter = 0;
-var useridCounter = 0;
+var gameidCounter = 1;
+var useridCounter = 1;
//Database connection credentials
var dbConnection = mysql.createConnection({
@@ -17,14 +17,18 @@ var dbConnection = mysql.createConnection({
password: 'root'
});
+dbConnection.connect();
+
server.listen(8080);
app.get('/game/challenge/:gameid', function(req, res) {
+ 'use strict';
//Here we send the user to a specific game.
res.sendfile(/*Some stuff here. Dom här kopplar till socket*/);
});
io.sockets.on('connection', function(socket) {
+ 'use strict';
var userid;
var gameid;
@@ -37,6 +41,7 @@ io.sockets.on('connection', function(socket) {
gameid = gameidCounter++;
gameData[gameid] = {};
gameData[gameid][userid] = {};
+ socket.join(gameid);
socket.emit('game.id', gameid);
})
.on('game.join', function(joinGameid) {
@@ -45,13 +50,14 @@ io.sockets.on('connection', function(socket) {
gameData[gameid] = {};
}
gameData[gameid][userid] = {};
+ socket.join(gameid);
- if (_.keys(gameData[gameid]).length == 2) {
- socket.broadcast.to(gameid).emit('fighter.select');
+ if (_.keys(gameData[gameid]).length === 2) {
+ io.sockets.in(gameid).emit('fighter.select');
}
})
.on('fighter.get_suggestions', function() {
- fighters = getRandomFighters(3, function(fighters) {
+ getRandomFighters(3, function(fighters) {
socket.emit('fighter.suggestions', fighters);
});
})
@@ -59,27 +65,33 @@ io.sockets.on('connection', function(socket) {
gameData[gameid][userid].fighter = fighter;
var selectedFighters = [];
- for (var userid in gameData[gameid]) {
- if (gameData[gameid][userid].fighter) {
+ for (var userid2 in gameData[gameid]) {
+ if (gameData[gameid][userid2].fighter) {
selectedFighters.push({
- userid: userid,
- fighter: gameData[gameid][userid].fighter
+ userid: userid2,
+ fighter: gameData[gameid][userid2].fighter
});
}
}
- if (selectedFighters.length == 2) {
+ if (selectedFighters.length === 2) {
socket.broadcast.to(gameid).emit('game.start', {
fighters: selectedFighters
});
}
+ })
+
+ .on('disconnect', function(){
+ if(userid && gameid && gameData[gameid][userid]) {
+ delete gameData[gameid][userid];
+ }
+
});
});
//Connect to db
function queryDB(query, callback) {
'use strict';
- dbConnection.connect();
dbConnection.query(query, function(err, rows, fields) {
if (err) throw err;
@@ -87,13 +99,11 @@ function queryDB(query, callback) {
//console.log('The solution is: ', rows[0]);
callback(rows);
});
-
- dbConnection.end();
}
function getRandomFighters(num, callback) {
//Get 3 random fighters from DB.
- queryDB('SELECT PERNR, INST FROM insark.test WHERE PERNR LIKE "70%" ORDER BY RAND() LIMIT '+num, function(data) {
+ queryDB('SELECT PERNR, STPI_STP1 AS logic, STPI_STP2 AS intelligence, STPI_STP3 AS shape, STPI_STP4 AS tech FROM insark.test WHERE PERNR LIKE "7%" AND STPI_STP4 != 0 ORDER BY RAND() LIMIT '+num, function(data) {
//queryDB('SELECT * FROM insark.test LIMIT 1', function(data){
'use strict';
callback(data);

No commit comments for this range

Something went wrong with that request. Please try again.