randomFree.js 2.8 KB

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