Fix user database, add nice async stuff

This commit is contained in:
BopItFreak 2019-06-14 12:30:28 -04:00
parent cd71430087
commit d32526d244
1 changed files with 22 additions and 28 deletions

View File

@ -1,59 +1,53 @@
const ColorEncoder = require("./ColorEncoder.js"); const ColorEncoder = require("./ColorEncoder.js");
const { promisify } = require('util');
let userdb;
class User { class User {
constructor(cl) { constructor(cl) {
this.cl = cl; this.cl = cl;
this.userdb;
this.server = this.cl.server; this.server = this.cl.server;
this.default_db = {} this.userdb = userdb;
this.default_db = {};
} }
async getUserData() { async getUserData() {
if (!this.userdb) { if (!userdb || (userdb instanceof Map && [...userdb.entries()] == [])) {
this.setUpDb(); await this.setUpDb();
} }
let _id = createKeccakHash('keccak256').update((this.cl.server._id_Private_Key + this.cl.ip)).digest('hex').substr(0, 24); let _id = createKeccakHash('keccak256').update((this.cl.server._id_Private_Key + this.cl.ip)).digest('hex').substr(0, 24);
if (!this.userdb.get(_id)) { let usertofind = userdb.get(_id);
this.userdb.set(_id, { if (!usertofind) {
if (typeof usertofind == 'object' && (usertofind.hasOwnProperty('name') && usertofind.name != this.server.defaultUsername)) return;
userdb.set(_id, {
"color": `#${ColorEncoder.intToRGB(ColorEncoder.hashCode(_id)).toLowerCase()}`, "color": `#${ColorEncoder.intToRGB(ColorEncoder.hashCode(_id)).toLowerCase()}`,
"name": this.server.defaultUsername, "name": this.server.defaultUsername,
"_id": _id, "_id": _id,
"ip": this.cl.ip "ip": this.cl.ip
}); });
console.log("Set database", _id)
this.updatedb(); this.updatedb();
} }
let user = this.userdb.get(_id); let user = userdb.get(_id);
return { return {
"color": user.color, "color": user.color,
"name": user.name, "name": user.name,
"_id": user._id, "_id": user._id,
} }
} }
updatedb() { async updatedb() {
fs.writeFileSync('src/db/users.json', JSON.stringify(User.strMapToObj(this.userdb), null, 2), (err) => { const writeFile = promisify(fs.writeFile);
if (err) { await writeFile('src/db/users.json', JSON.stringify(User.strMapToObj(userdb), null, 2));
throw err;
} }
}); async setUpDb() {
} const writeFile = promisify(fs.writeFile);
setUpDb() { const readdir = promisify(fs.readdir);
let files = fs.readdirSync("src/db/"); let files = await readdir("src/db/");
if (!files.includes("users.json")) { if (!files.includes("users.json")) {
fs.writeFileSync('src/db/users.json', JSON.stringify(this.default_db, null, 2), (err) => { await writeFile('src/db/users.json', JSON.stringify(this.default_db, null, 2))
if (err) { userdb = new Map(Object.entries(require("./db/users.json")));
throw err;
}
});
this.userdb = new Map(Object.entries(require("./db/users.json")));
} else { } else {
this.userdb = new Map(Object.entries(require("./db/users.json"))); userdb = new Map(Object.entries(require("./db/users.json")));
} }
} }
static strMapToObj(strMap) { static strMapToObj(strMap) {
let obj = Object.create(null); return [...strMap.entries()].reduce((obj, [key, value]) => (obj[key] = value, obj), {});
for (let [k, v] of strMap) {
obj[k] = v;
}
return obj;
} }
} }
module.exports = User; module.exports = User;