randomFree.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. const GameMachine= require('../../game').GameMachine;
  6. let Users = require('../../database').Repositories.Users;
  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. data.timestamp = new Date().getTime();
  47. nsp.in(channel).emit('move', data);
  48. };
  49. p1.on('broadcast', broadcast);
  50. p2.on('broadcast', broadcast);
  51. const gameOver = (stack) => {
  52. // TODO this should be emitted ONLY if all 9 moves have been played
  53. //TODO verify move stack (compare with hashed cards)
  54. let gameMachine = new GameMachine(stack.setup);
  55. let winner = gameMachine.runMatch(stack.moves);
  56. nsp.in(channel).emit('winner', winner);
  57. };
  58. p1.once('gameOver', gameOver);
  59. p2.once('gameOver', gameOver);
  60. const dispute = (data) => {
  61. let result;
  62. // result. gameService.dispute(data);
  63. //may change to 'disputeResult'
  64. nsp.in(channel).emit('result', result);
  65. };
  66. p1.on('dispute', dispute);
  67. p2.on('dispute', dispute);
  68. });
  69. }
  70. nsp.in('queue').clients((err, clients) => {
  71. console.log('Queue');
  72. console.log(clients)
  73. });
  74. });
  75. }
  76. joinGame(playerOne, playerTwo){
  77. let playerOneCards = this.createRandomCards();
  78. let playerTwoCards = this.createRandomCards();
  79. let setup = this.createSetupArray(playerOne, playerTwo, playerOneCards, playerTwoCards);
  80. let playerOneState = {cards: playerOneCards, setup: setup};
  81. let playerTwoState = {cards: playerTwoCards, setup: setup};
  82. return {playerOneState, playerTwoState};
  83. }
  84. createSetupArray(playerOne, playerTwo, playerOneCards, playerTwoCards){
  85. let id = [
  86. playerOne,
  87. playerTwo
  88. ];
  89. let cards = [
  90. playerOneCards.hashedCardsArray,
  91. playerTwoCards.hashedCardsArray
  92. ];
  93. let setup = {id, cards };
  94. return setup;
  95. }
  96. createRandomCards(){
  97. let playerCardsArray = [];
  98. let saltArray = [];
  99. let hashedCardsArray= [];
  100. for (let i =0; i<5; i++){
  101. let playerCard = Math.floor((Math.random()*100 ) % 100 );
  102. let salt = crypto.lib.WordArray.random(128 / 8).toString();
  103. let hashedCard = cryptoSHA256(playerCard.toString() + salt);
  104. playerCardsArray.push(playerCard);
  105. saltArray.push(salt);
  106. hashedCardsArray.push(hashedCard.toString());
  107. }
  108. return {playerCardsArray, saltArray, hashedCardsArray};
  109. }
  110. }
  111. module.exports = new randomFree();