RandomFreeQueue.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. const Queue = require('../Queue.js');
  2. const crypto = require('crypto-js');
  3. const cryptoSHA256= crypto.SHA256;
  4. class RandomFreeQueue extends Queue {
  5. constructor(){
  6. super("RandomFreeQueue");
  7. }
  8. joinGame(playerOne, playerTwo, channel){
  9. let playerOneCards = this.createRandomCards();
  10. let playerTwoCards = this.createRandomCards();
  11. let setup = this.createSetupArray(playerOne, playerTwo, playerOneCards, playerTwoCards);
  12. let playerOneState = { cards: playerOneCards, channel };
  13. let playerTwoState = { cards: playerTwoCards, channel };
  14. return { playerOneState, playerTwoState, setup};
  15. }
  16. createSetupArray(playerOne, playerTwo, playerOneCards, playerTwoCards){
  17. let id = [
  18. playerOne,
  19. playerTwo
  20. ];
  21. let cards = [
  22. playerOneCards.hashedCardsArray,
  23. playerTwoCards.hashedCardsArray
  24. ];
  25. let setup = {id, cards};
  26. return setup;
  27. }
  28. createRandomCards(){
  29. let playerCardsArray = [];
  30. let saltArray = [];
  31. let hashedCardsArray= [];
  32. for (let i =0; i<5; i++){
  33. let playerCard = Math.floor((Math.random()*100 ) % 100 );
  34. let salt = crypto.lib.WordArray.random(128 / 8).toString();
  35. let hashedCard = cryptoSHA256(playerCard.toString() + salt);
  36. playerCardsArray.push(playerCard);
  37. saltArray.push(salt);
  38. hashedCardsArray.push(hashedCard.toString());
  39. }
  40. return {playerCardsArray, saltArray, hashedCardsArray};
  41. }
  42. }
  43. module.exports = RandomFreeQueue;