Ver Fonte

Boosters coming

Nik Atlas há 6 anos atrás
pai
commit
8d5e0bd544

+ 0 - 1
,/database/mafaldadb/CURRENT

@@ -1 +0,0 @@
-MANIFEST-000002

+ 0 - 0
,/database/mafaldadb/LOCK


+ 0 - 1
,/database/mafaldadb/LOG

@@ -1 +0,0 @@
-2018/01/28-17:07:16.780 1550 Delete type=3 #1

BIN
,/database/mafaldadb/MANIFEST-000002


+ 38 - 5
database/db.js

@@ -4,12 +4,14 @@ const search = require('level-search');
 
 var path = require('path');
 var DIR = path.dirname(__filename);
-console.log(DIR);
 class Database {
 	constructor(){
 		console.log("Constructing database!")
 		this.db = sub(level(DIR + "/mafaldadb", {valueEncoding: 'json'}));
 		this.index = search(this.db, 'search');
+
+		// this.createSearchStream = this.createSearchStream.bind(this);
+
 	}
 	open(callback){
 		return this.db.open(callback);
@@ -47,8 +49,22 @@ class Database {
 	isClosed(){
 		return this.db.isClosed();
 	}
-	createReadStream(options){
-		return this.db.createReadStream(options);
+	createReadStream(options) {
+		const results = [];
+		return new Promise((res,rej) => {
+			this.db.createReadStream(options)
+			.on('data', function (data) {
+				results.push(data.value);
+			})
+			.on('error', function (err) {
+			    rej(err);
+			})
+			.on('close', function () {
+			})
+			.on('end', function () {
+			    res(results);
+			});
+		});
 	}
 	createKeyStream(options){
 		return this.db.createKeyStream(options);
@@ -56,8 +72,25 @@ class Database {
 	createValueStream(options){
 		return this.db.createValueStream(options);
 	}
-	createSearchStream(keys, options){
-		return this.index.createSearchStream(keys, options);	
+	createSearchStream(keys, options) {
+		const results = [];
+		return new Promise((res,rej) => {
+			this.index.createSearchStream(keys,options)
+			.on('data', function (data) {
+				results.push(data.value);
+			})
+			.on('error', function (err) {
+			    rej(err);
+			})
+			.on('close', function () {
+			})
+			.on('end', function () {
+			    res(results);
+			});
+		});
+	}
+	read(options) {
+		return this.createReadStream(options);
 	}
 	search(keys, options) {
 		return this.createSearchStream(keys, options);

+ 4 - 2
database/index.js

@@ -1,11 +1,13 @@
 const db = require('./db.js');
 const UserRepo = require('./repositories/users');
 const CardRepo = require('./repositories/cards');
+const BoostersRepo = require('./repositories/boosters');
 
 
 let Repositories = {
-	Users: UserRepo,
-	Cards: CardRepo
+	Boosters: BoostersRepo,
+	Cards: CardRepo,
+	Users: UserRepo
 };
 
 module.exports = {

+ 40 - 0
database/repositories/boosters.js

@@ -0,0 +1,40 @@
+const db = require("../db.js");
+var CryptoJS = require('crypto-js');
+
+const BOOSTERS_SEARCH_CRITERIA = {
+	'lt': 'booster;',
+	'gt': 'booster:'
+};
+
+class BoosterRepository {
+	getByID(id) {
+		return db.get('booster:' + id);
+	}
+
+	getAll() {
+		return db.read(BOOSTERS_SEARCH_CRITERIA);
+	}
+
+	getBoostersByUser(userid) {
+		return db.search(['user', userid],BOOSTERS_SEARCH_CRITERIA);
+	}
+
+	insert(id, data) {
+		data = this.verify(data);
+		return db.put('booster:' + id, data);
+	}
+
+	verify(data) {
+		let { treasure, user } = data;
+		if (!user) throw Error("This data are corrupted!");
+		if (!treasure) throw Error("This data are corrupted!");
+		return { treasure, user };
+	}
+}
+
+
+let boosterRepository = null;
+function getSingleton() { 
+	return boosterRepository = (boosterRepository === null ? new BoosterRepository(): boosterRepository);
+}
+module.exports = getSingleton();

+ 2 - 0
database/repositories/boosters.test.js

@@ -0,0 +1,2 @@
+var booster = require('./boosters.js');
+

+ 1 - 0
database/repositories/cards.js

@@ -20,6 +20,7 @@ class CardsRepository {
 	}
 
 	insert(id, data) {
+		data = this.verify(data);
 		return db.put('card:' + id, data);
 	}
 

+ 1 - 21
database/repositories/users.js

@@ -54,27 +54,7 @@ class UserRepository {
     	then(() => newuser);
 	}
 	getUser(token) {
-		return new Promise((res,rej) => {
-			db.search(['token', token]).
-			on('data', (data) => {
-				res(data);
-			}).
-			on('error', function (err) {
-				let error = new Error("DB error");
-				error.name = "InternalError";
-				rej(error);
-		  	}).
-		  	on('close', function () {
-		    	let error = new Error("Token invalid");
-				error.name = "TokenInvalid";
-				rej(error);
-		  	}).
-		  	on('end', function () {
-				let error = new Error("Token invalid");
-				error.name = "TokenInvalid";
-				rej(error);
-			});
-		});
+		return db.search(['token', token]).then((data) => data[0]);
 	}
 	getByID(id){
 		return db.get("user:" + id);

+ 2 - 2
database/repositories/users.test.js

@@ -3,8 +3,8 @@ var users = require('./users.js');
 
 
 let u = {
-	username: "test",
-	password: "test",
+	username: "nikatlas",
+	password: "mafalda",
 	email: "[email protected]"
 }
 

+ 0 - 0
,/database/mafaldadb/000003.log → game/index.js


+ 23 - 0
game/machine/Booster.js

@@ -0,0 +1,23 @@
+var CryptoJS = require('crypto-js');
+
+const SHA = CryptoJS.SHA256;
+
+// var salt = CryptoJS.lib.WordArray.random(128 / 8).toString();
+
+class Booster {
+	constructor(user) {
+		if(!user) throw Error("Booster must be owned by a user!");
+		this.id = this.randomID();
+		this.treasure = this.randomTreasure();
+		this.user = user;
+	}
+
+	randomID() {
+		return CryptoJS.lib.WordArray.random(128 / 8).toString();
+	}
+
+	randomTreasure() {
+		return CryptoJS.lib.WordArray.random(256 / 8).toString();
+	}
+}
+module.exports = Booster;

+ 23 - 0
game/machine/BoosterService.js

@@ -0,0 +1,23 @@
+let BoostersRepository = require('../../database').Repositories.Boosters;
+var Booster = require('./Booster');
+
+class BoosterService {
+	constructor() {
+	}
+
+	getUserBoosters(user) {
+		return BoostersRepository.getBoostersByUser(user);
+	}
+
+	givePack(user) {
+		let {id, ...boosterdata} = new Booster(user);
+		return BoostersRepository.insert(id, boosterdata);
+	}
+}
+
+
+let service = null; 
+function getSingleton() { 
+	return service = (service === null ? new BoosterService() : service);
+}
+module.exports = getSingleton();

+ 6 - 0
game/machine/Card.js

@@ -0,0 +1,6 @@
+class Card {
+	constructor() {
+	}
+
+}
+module.exports = Card;

+ 16 - 0
tests/booster.tests.js

@@ -0,0 +1,16 @@
+var BoosterService = require('../game/machine/BoosterService');
+var Users = require('../database/repositories/users.js');
+
+Users.login({
+	username: 'nikatlas',
+	password: 'mafalda'
+})
+.then((user) => Users.getUser(user.token))
+.then((user) => {
+	console.log(user);
+	//BoosterService.givePack(user.username);
+	return user;
+})
+.then((user) => {
+	BoosterService.getUserBoosters(user.username).then(console.log);
+});