randomFree.js 3.5 KB

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