Jelajahi Sumber

Random Free match card setup

Dim Dim 6 tahun lalu
induk
melakukan
5f7c40e890
1 mengubah file dengan 47 tambahan dan 3 penghapusan
  1. 47 3
      services/matchmaking/randomFree.js

+ 47 - 3
services/matchmaking/randomFree.js

@@ -1,6 +1,10 @@
 const io  = require('socket.io')(3555);
 const nsp = io.of('/randomFree');
 
+const crypto = require('crypto-js');
+const cryptoSHA256= crypto.SHA256;
+
+
 class randomFree {
 
 	constructor() {
@@ -14,7 +18,7 @@ class randomFree {
 	match() {
 		nsp.in('queue').clients((error, clients) => {
 		  	if (error) throw error;
-		  
+
 			if(clients.length > 1) {
 				let p1 = nsp.sockets[clients[0]];
 				let p2 = nsp.sockets[clients[1]];
@@ -39,6 +43,10 @@ class randomFree {
 				nsp.in(channel).clients((err, clients) => {
 					console.log('Channel: ' + channel);
 					console.log(clients);
+                    let matchInfo = this.joinGame(p1, p2);
+					//possible change to 'joinFreeRandomGame' ?
+					p1.emit('joinGame', matchInfo.playerOneState);
+                    p2.emit('joinGame', matchInfo.playerTwoState);
 				});
 			}
 
@@ -49,9 +57,45 @@ class randomFree {
 		});
 	}
 
-    join(user){
-        //join randomFree queue room
+    joinGame(playerOne, playerTwo){
+	    let playerOneCards = this.createRandomCards();
+	    let playerTwoCards = this.createRandomCards();
+	    let setup = this.createSetupArray(playerOne, playerTwo, playerOneCards, playerTwoCards);
+	    let playerOneState = {cards: playerOneCards, setup: setup};
+        let playerTwoState = {cards: playerTwoCards, setup: setup};
+        return {playerOneState, playerTwoState};
+    }
+
+    createSetupArray(playerOne, playerTwo, playerOneCards, playerTwoCards){
+        let id = {
+	            playerOne: playerOne,
+                playerTwo: playerTwo,
+            };
+	    let cards = {
+	        playerOne: playerOneCards.hashedCardsArray,
+            playerTwo: 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);
+		}
+    return {playerCardsArray, saltArray, hashedCardsArray};
     }
+
 }
 
 module.exports = new randomFree();