randomFree.js 3.7 KB

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