Browse Source

Merge branch 'master' of http://git.onarbooks.com/LosPoulos/mafalda-api

Nikatlas 6 năm trước cách đây
mục cha
commit
cd0aaca8d5

+ 30 - 0
database/dal/decksDal.js

@@ -0,0 +1,30 @@
+const db = require("../db.js");
+
+class DecksDal {
+
+    getDecksByUser(userID){
+        return db.search(['user', userID]);
+    }
+
+    insertDeckByUser(userID, cards){
+        //TODO if 'userID' has all 'cards' continue, otherwise error
+        return db.put('deck:' + userID, cards);
+    }
+
+    deleteDeckByUser(userID, deckID) {
+        return db.del(deckID);
+    }
+
+    verify(data) {
+        let { user } = data;
+        if (!user) throw Error("You have to login before saving a deck!");
+
+        return { user };
+    }
+}
+
+let decksDal = null;
+function getSingleton() {
+    return decksDal = (decksDal === null ? new DecksDal(): decksDal);
+}
+module.exports = getSingleton();

+ 2 - 2
database/repositories/cards.js

@@ -2,7 +2,7 @@ const db = require("../db.js");
 const CryptoJS = require('crypto-js');
 
 const CARDS_SEARCH_CRITERIA = {
-	'lt': 'card;',
+	'lt': 'card;', //TODO card; h card:?
 	'gt': 'card:'
 };
 
@@ -35,6 +35,6 @@ class CardsRepository {
 
 let cardsRepository = null;
 function getSingleton() { 
-	return cardsRepository = (cardsRepository === null ? new CardsRepository(): userRepository);
+	return cardsRepository = (cardsRepository === null ? new CardsRepository(): cardsRepository);
 }
 module.exports = getSingleton();

+ 26 - 0
database/repositories/decks.js

@@ -0,0 +1,26 @@
+const decksDal = require("../dal/decksDal.js");
+
+class DecksRepository {
+//TODO continue
+// where UserID equals to logged in user's ID
+
+    getDecksByUser(userID) {
+        return decksDal.getDecksByUser(userID);
+    }
+
+    insertDeckByUser(userID, cards){
+        return decksDal.insertDeckByUser(userID, cards);
+    }
+
+    deleteDeckByUser(userID, deckID) {
+        return decksDal.deleteDeckByUser(userID, deckID)
+    }
+
+}
+
+let decksRepository = null;
+function getSingleton() {
+    return decksRepository = (decksRepository === null ? new DecksRepository(): decksRepository);
+}
+module.exports = getSingleton();
+

Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 214 - 214
package-lock.json


+ 2 - 1
routes/matchmaking/randomFree.js

@@ -10,6 +10,7 @@ router.get('/join', function(req, res, next) {
     randomFreeService.join(req.user).
     then((data) => {
         console.log(data);
+        console.log("Join Request!");
         res.send(data);
     }).
     catch((err) => {
@@ -18,4 +19,4 @@ router.get('/join', function(req, res, next) {
     });
 });
 
-module.exports = router;
+module.exports = router;

+ 39 - 11
services/matchmaking/randomFree.js

@@ -4,13 +4,16 @@ const nsp = io.of('/randomFree');
 const crypto = require('crypto-js');
 const cryptoSHA256= crypto.SHA256;
 
+let Users = require('../../database').Repositories.Users;
 
+const gameService= {}; //require
 class randomFree {
 
 	constructor() {
 		nsp.on('connection', (socket) => {
-		  console.log('someone connected');
+		  console.log('Someone connected');
 		  socket.join('queue');
+		  socket.emit('test');
 		  this.match();
 		});
 	}
@@ -22,9 +25,12 @@ class randomFree {
 			if(clients.length > 1) {
 				let p1 = nsp.sockets[clients[0]];
 				let p2 = nsp.sockets[clients[1]];
+
 				p1.emit('test');
 				p2.emit('test');
+				
 				const channel = clients[0] + clients[1];
+				console.log(channel);
 
 				let err = (e) => {
 					if(e)throw e;
@@ -36,10 +42,14 @@ class randomFree {
 				p2.leave('queue', err);
 				p2.join(channel, err);
 
+				let token1 = p1.request._query['token'];
+		  		let token2 = p2.request._query['token'];
+		  		// maybe i need to change to username
+
 				nsp.in(channel).clients((err, clients) => {
 					console.log('Channel: ' + channel);
 					console.log(clients);
-                    let matchInfo = this.joinGame(p1.id, p2.id);
+                    let matchInfo = this.joinGame(token1, token2);
 					//possible change to 'joinFreeRandomGame' ?
 					console.log("Joining Game");
 					p1.emit('joinGame', matchInfo.playerOneState);
@@ -50,6 +60,24 @@ class randomFree {
                     };
                     p1.on('broadcast', broadcast);
                     p2.on('broadcast', broadcast);
+
+                    const gameOver = (data) => {
+                        let result;
+                        // result = gameService.lastMove(data);
+                        nsp.in(channel).emit('result', result);
+                    };
+                    p1.once('gameOver', gameOver);
+                    p2.once('gameOver', gameOver);
+
+                    const dispute = (data) => {
+                        let result;
+                        // result. gameService.dispute(data);
+
+                        //may change to 'disputeResult'
+                        nsp.in(channel).emit('result', result);
+                    };
+                    p1.on('dispute', dispute);
+                    p2.on('dispute', dispute);
 				});
 			}
 
@@ -70,14 +98,14 @@ class randomFree {
     }
 
     createSetupArray(playerOne, playerTwo, playerOneCards, playerTwoCards){
-        let id = {
-            playerOne: playerOne,
-            playerTwo: playerTwo,
-        };
-	    let cards = {
-	        playerOne: playerOneCards.hashedCardsArray,
-            playerTwo: playerTwoCards.hashedCardsArray,
-        };
+        let id = [
+        	playerOne,
+            playerTwo
+        ];
+	    let cards = [
+	        playerOneCards.hashedCardsArray,
+            playerTwoCards.hashedCardsArray
+        ];
 
 	    let setup = {id, cards };
 	    return setup;
@@ -91,7 +119,7 @@ class randomFree {
 		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);
+            let hashedCard = cryptoSHA256(playerCard.toString() + salt);
 		    playerCardsArray.push(playerCard);
 			saltArray.push(salt);
 			hashedCardsArray.push(hashedCard.toString());

Một số tệp đã không được hiển thị bởi vì quá nhiều tập tin thay đổi trong này khác