randomFree.js 2.7 KB

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