|
@@ -4,13 +4,16 @@ const nsp = io.of('/randomFree');
|
|
const crypto = require('crypto-js');
|
|
const crypto = require('crypto-js');
|
|
const cryptoSHA256= crypto.SHA256;
|
|
const cryptoSHA256= crypto.SHA256;
|
|
|
|
|
|
|
|
+let Users = require('../../database').Repositories.Users;
|
|
|
|
|
|
|
|
+const gameService= {}; //require
|
|
class randomFree {
|
|
class randomFree {
|
|
|
|
|
|
constructor() {
|
|
constructor() {
|
|
nsp.on('connection', (socket) => {
|
|
nsp.on('connection', (socket) => {
|
|
- console.log('someone connected');
|
|
|
|
|
|
+ console.log('Someone connected');
|
|
socket.join('queue');
|
|
socket.join('queue');
|
|
|
|
+ socket.emit('test');
|
|
this.match();
|
|
this.match();
|
|
});
|
|
});
|
|
}
|
|
}
|
|
@@ -22,9 +25,12 @@ class randomFree {
|
|
if(clients.length > 1) {
|
|
if(clients.length > 1) {
|
|
let p1 = nsp.sockets[clients[0]];
|
|
let p1 = nsp.sockets[clients[0]];
|
|
let p2 = nsp.sockets[clients[1]];
|
|
let p2 = nsp.sockets[clients[1]];
|
|
|
|
+
|
|
p1.emit('test');
|
|
p1.emit('test');
|
|
p2.emit('test');
|
|
p2.emit('test');
|
|
|
|
+
|
|
const channel = clients[0] + clients[1];
|
|
const channel = clients[0] + clients[1];
|
|
|
|
+ console.log(channel);
|
|
|
|
|
|
let err = (e) => {
|
|
let err = (e) => {
|
|
if(e)throw e;
|
|
if(e)throw e;
|
|
@@ -36,10 +42,14 @@ class randomFree {
|
|
p2.leave('queue', err);
|
|
p2.leave('queue', err);
|
|
p2.join(channel, err);
|
|
p2.join(channel, err);
|
|
|
|
|
|
|
|
+ let token1 = p1.request._query['token'];
|
|
|
|
+ let token2 = p2.request._query['token'];
|
|
|
|
+ // maybe i need to change to username
|
|
|
|
+
|
|
nsp.in(channel).clients((err, clients) => {
|
|
nsp.in(channel).clients((err, clients) => {
|
|
console.log('Channel: ' + channel);
|
|
console.log('Channel: ' + channel);
|
|
console.log(clients);
|
|
console.log(clients);
|
|
- let matchInfo = this.joinGame(p1.id, p2.id);
|
|
|
|
|
|
+ let matchInfo = this.joinGame(token1, token2);
|
|
//possible change to 'joinFreeRandomGame' ?
|
|
//possible change to 'joinFreeRandomGame' ?
|
|
console.log("Joining Game");
|
|
console.log("Joining Game");
|
|
p1.emit('joinGame', matchInfo.playerOneState);
|
|
p1.emit('joinGame', matchInfo.playerOneState);
|
|
@@ -50,6 +60,24 @@ class randomFree {
|
|
};
|
|
};
|
|
p1.on('broadcast', broadcast);
|
|
p1.on('broadcast', broadcast);
|
|
p2.on('broadcast', broadcast);
|
|
p2.on('broadcast', broadcast);
|
|
|
|
+
|
|
|
|
+ const gameOver = (data) => {
|
|
|
|
+ let result;
|
|
|
|
+ // result = gameService.lastMove(data);
|
|
|
|
+ nsp.in(channel).emit('result', result);
|
|
|
|
+ };
|
|
|
|
+ p1.once('gameOver', gameOver);
|
|
|
|
+ p2.once('gameOver', gameOver);
|
|
|
|
+
|
|
|
|
+ const dispute = (data) => {
|
|
|
|
+ let result;
|
|
|
|
+ // result. gameService.dispute(data);
|
|
|
|
+
|
|
|
|
+ //may change to 'disputeResult'
|
|
|
|
+ nsp.in(channel).emit('result', result);
|
|
|
|
+ };
|
|
|
|
+ p1.on('dispute', dispute);
|
|
|
|
+ p2.on('dispute', dispute);
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
@@ -70,14 +98,14 @@ class randomFree {
|
|
}
|
|
}
|
|
|
|
|
|
createSetupArray(playerOne, playerTwo, playerOneCards, playerTwoCards){
|
|
createSetupArray(playerOne, playerTwo, playerOneCards, playerTwoCards){
|
|
- let id = {
|
|
|
|
- playerOne: playerOne,
|
|
|
|
- playerTwo: playerTwo,
|
|
|
|
- };
|
|
|
|
- let cards = {
|
|
|
|
- playerOne: playerOneCards.hashedCardsArray,
|
|
|
|
- playerTwo: playerTwoCards.hashedCardsArray,
|
|
|
|
- };
|
|
|
|
|
|
+ let id = [
|
|
|
|
+ playerOne,
|
|
|
|
+ playerTwo
|
|
|
|
+ ];
|
|
|
|
+ let cards = [
|
|
|
|
+ playerOneCards.hashedCardsArray,
|
|
|
|
+ playerTwoCards.hashedCardsArray
|
|
|
|
+ ];
|
|
|
|
|
|
let setup = {id, cards };
|
|
let setup = {id, cards };
|
|
return setup;
|
|
return setup;
|
|
@@ -91,7 +119,7 @@ class randomFree {
|
|
for (let i =0; i<5; i++){
|
|
for (let i =0; i<5; i++){
|
|
let playerCard = Math.floor((Math.random()*100 ) % 100 );
|
|
let playerCard = Math.floor((Math.random()*100 ) % 100 );
|
|
let salt = crypto.lib.WordArray.random(128 / 8).toString();
|
|
let salt = crypto.lib.WordArray.random(128 / 8).toString();
|
|
- let hashedCard = cryptoSHA256(playerCard.toString + salt);
|
|
|
|
|
|
+ let hashedCard = cryptoSHA256(playerCard.toString() + salt);
|
|
playerCardsArray.push(playerCard);
|
|
playerCardsArray.push(playerCard);
|
|
saltArray.push(salt);
|
|
saltArray.push(salt);
|
|
hashedCardsArray.push(hashedCard.toString());
|
|
hashedCardsArray.push(hashedCard.toString());
|