123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- const io = require('socket.io')(3555);
- const nsp = io.of('/randomFree');
- const crypto = require('crypto-js');
- const cryptoSHA256= crypto.SHA256;
- let Users = require('../../database').Repositories.Users;
- const gameService= {}; //require
- class randomFree {
- constructor() {
- nsp.on('connection', (socket) => {
- console.log('Someone connected');
- socket.join('queue');
- socket.emit('test');
- this.match();
- });
- }
- match() {
- nsp.in('queue').clients((error, clients) => {
- if (error) throw error;
- if(clients.length > 1) {
- let p1 = nsp.sockets[clients[0]];
- let p2 = nsp.sockets[clients[1]];
- p1.emit('test');
- p2.emit('test');
-
- const channel = clients[0] + clients[1];
- console.log(channel);
- let err = (e) => {
- if(e)throw e;
- return 0;
- };
- p1.leave('queue', err);
- p1.join(channel, err);
- p2.leave('queue', 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) => {
- console.log('Channel: ' + channel);
- console.log(clients);
- let matchInfo = this.joinGame(token1, token2);
- //possible change to 'joinFreeRandomGame' ?
- console.log("Joining Game");
- p1.emit('joinGame', matchInfo.playerOneState);
- p2.emit('joinGame', matchInfo.playerTwoState);
- const broadcast = (data) => {
- data.timestamp = new Date().getTime();
- nsp.in(channel).emit('move', data);
- };
- p1.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);
- });
- }
- nsp.in('queue').clients((err, clients) => {
- console.log('Queue');
- console.log(clients)
- });
- });
- }
- joinGame(playerOne, playerTwo){
- let playerOneCards = this.createRandomCards();
- let playerTwoCards = this.createRandomCards();
- let setup = this.createSetupArray(playerOne, playerTwo, playerOneCards, playerTwoCards);
- let playerOneState = {cards: playerOneCards, setup: setup};
- let playerTwoState = {cards: playerTwoCards, setup: setup};
- return {playerOneState, playerTwoState};
- }
- createSetupArray(playerOne, playerTwo, playerOneCards, playerTwoCards){
- let id = [
- playerOne,
- playerTwo
- ];
- let cards = [
- playerOneCards.hashedCardsArray,
- playerTwoCards.hashedCardsArray
- ];
- let setup = {id, cards };
- return setup;
- }
- createRandomCards(){
- let playerCardsArray = [];
- let saltArray = [];
- let hashedCardsArray= [];
- for (let i =0; i<5; i++){
- let playerCard = Math.floor((Math.random()*100 ) % 100 );
- let salt = crypto.lib.WordArray.random(128 / 8).toString();
- let hashedCard = cryptoSHA256(playerCard.toString() + salt);
- playerCardsArray.push(playerCard);
- saltArray.push(salt);
- hashedCardsArray.push(hashedCard.toString());
- }
- return {playerCardsArray, saltArray, hashedCardsArray};
- }
- }
- module.exports = new randomFree();
|