1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- const Queue = require('../Queue.js');
- const crypto = require('crypto-js');
- const cryptoSHA256= crypto.SHA256;
- class RandomFreeQueue extends Queue {
- constructor(){
- super("RandomFreeQueue");
- }
- joinGame(playerOne, playerTwo, channel){
- let playerOneCards = this.createRandomCards();
- let playerTwoCards = this.createRandomCards();
- let setup = this.createSetupArray(playerOne, playerTwo, playerOneCards, playerTwoCards);
- let playerOneState = { cards: playerOneCards, channel };
- let playerTwoState = { cards: playerTwoCards, channel };
- return { playerOneState, playerTwoState, setup};
- }
- createSetupArray(playerOne, playerTwo, playerOneCards, playerTwoCards){
- let id = [
- playerOne,
- playerTwo
- ];
- let cards = [
- playerOneCards.hashedCardsArray,
- playerTwoCards.hashedCardsArray
- ];
- let setup = {id, cards};
- return setup;
- }
- createRandomCards(){
- let playerCardsArray = [];
- let saltArray = [];
- let hashedCardsArray= [];
- for (let i =0; i<5; i++){
- let playerCard = Math.floor((Math.random()*100 ) % 100 );
- let salt = crypto.lib.WordArray.random(128 / 8).toString();
- let hashedCard = cryptoSHA256(playerCard.toString() + salt);
- playerCardsArray.push(playerCard);
- saltArray.push(salt);
- hashedCardsArray.push(hashedCard.toString());
- }
- return {playerCardsArray, saltArray, hashedCardsArray};
- }
- }
- module.exports = RandomFreeQueue;
|