randomFree.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. const io = require('socket.io')(3555);
  2. const nsp = io.of('/randomFree');
  3. const crypto = require('crypto-js');
  4. const cryptoSHA256= crypto.SHA256;
  5. let Users = require('../../database').Repositories.Users;
  6. const gameService= {}; //require
  7. class randomFree {
  8. constructor() {
  9. nsp.on('connection', (socket) => {
  10. console.log('Someone connected');
  11. socket.join('queue');
  12. this.match();
  13. });
  14. }
  15. match() {
  16. nsp.in('queue').clients((error, clients) => {
  17. if (error) throw error;
  18. if(clients.length > 1) {
  19. let p1 = nsp.sockets[clients[0]];
  20. let p2 = nsp.sockets[clients[1]];
  21. p1.emit('test');
  22. p2.emit('test');
  23. const channel = clients[0] + clients[1];
  24. console.log(channel);
  25. let err = (e) => {
  26. if(e)throw e;
  27. return 0;
  28. };
  29. p1.leave('queue', err);
  30. p1.join(channel, err);
  31. p2.leave('queue', err);
  32. p2.join(channel, err);
  33. let token1 = p1.request._query['token'];
  34. let token2 = p2.request._query['token'];
  35. // maybe i need to change to username
  36. nsp.in(channel).clients((err, clients) => {
  37. console.log('Channel: ' + channel);
  38. console.log(clients);
  39. let matchInfo = this.joinGame(token1, token2);
  40. //possible change to 'joinFreeRandomGame' ?
  41. console.log("Joining Game");
  42. p1.emit('joinGame', matchInfo.playerOneState);
  43. p2.emit('joinGame', matchInfo.playerTwoState);
  44. const broadcast = (data) => {
  45. nsp.in(channel).emit('move', data);
  46. };
  47. p1.on('broadcast', broadcast);
  48. p2.on('broadcast', broadcast);
  49. const gameOver = (data) => {
  50. let result;
  51. // result = gameService.lastMove(data);
  52. nsp.in(channel).emit('result', result);
  53. };
  54. p1.once('gameOver', gameOver);
  55. p2.once('gameOver', gameOver);
  56. const dispute = (data) => {
  57. let result;
  58. // result. gameService.dispute(data);
  59. //may change to 'disputeResult'
  60. nsp.in(channel).emit('result', result);
  61. };
  62. p1.on('dispute', dispute);
  63. p2.on('dispute', dispute);
  64. });
  65. }
  66. nsp.in('queue').clients((err, clients) => {
  67. console.log('Queue');
  68. console.log(clients)
  69. });
  70. });
  71. }
  72. joinGame(playerOne, playerTwo){
  73. let playerOneCards = this.createRandomCards();
  74. let playerTwoCards = this.createRandomCards();
  75. let setup = this.createSetupArray(playerOne, playerTwo, playerOneCards, playerTwoCards);
  76. let playerOneState = {cards: playerOneCards, setup: setup};
  77. let playerTwoState = {cards: playerTwoCards, setup: setup};
  78. return {playerOneState, playerTwoState};
  79. }
  80. createSetupArray(playerOne, playerTwo, playerOneCards, playerTwoCards){
  81. let id = [
  82. playerOne,
  83. playerTwo
  84. ];
  85. let cards = [
  86. playerOneCards.hashedCardsArray,
  87. playerTwoCards.hashedCardsArray
  88. ];
  89. let setup = {id, cards };
  90. return setup;
  91. }
  92. createRandomCards(){
  93. let playerCardsArray = [];
  94. let saltArray = [];
  95. let hashedCardsArray= [];
  96. for (let i =0; i<5; i++){
  97. let playerCard = Math.floor((Math.random()*100 )% 100 );
  98. let salt = crypto.lib.WordArray.random(128 / 8).toString();
  99. let hashedCard = cryptoSHA256(playerCard.toString() + salt);
  100. playerCardsArray.push(playerCard);
  101. saltArray.push(salt);
  102. hashedCardsArray.push(hashedCard.toString());
  103. }
  104. return {playerCardsArray, saltArray, hashedCardsArray};
  105. }
  106. }
  107. module.exports = new randomFree();