2019-06-06 19:22:43 +02:00
|
|
|
const ColorEncoder = require("./ColorEncoder.js");
|
|
|
|
class User {
|
|
|
|
constructor(cl) {
|
|
|
|
this.cl = cl;
|
|
|
|
this.userdb;
|
|
|
|
this.server = this.cl.server;
|
|
|
|
this.default_db = {}
|
|
|
|
}
|
|
|
|
async getUserData() {
|
|
|
|
if (!this.userdb) {
|
|
|
|
this.setUpDb();
|
|
|
|
}
|
|
|
|
let _id = createKeccakHash('keccak256').update((this.cl.server._id_Private_Key + this.cl.ip)).digest('hex').substr(0, 24);
|
|
|
|
if (!this.userdb.get(_id)) {
|
|
|
|
this.userdb.set(_id, {
|
|
|
|
"color": `#${ColorEncoder.intToRGB(ColorEncoder.hashCode(_id)).toLowerCase()}`,
|
|
|
|
"name": this.server.defaultUsername,
|
|
|
|
"_id": _id,
|
|
|
|
"ip": this.cl.ip
|
|
|
|
});
|
2019-06-12 19:28:15 +02:00
|
|
|
console.log("Set database", _id)
|
2019-06-06 19:22:43 +02:00
|
|
|
this.updatedb();
|
|
|
|
}
|
|
|
|
let user = this.userdb.get(_id);
|
|
|
|
return {
|
|
|
|
"color": user.color,
|
|
|
|
"name": user.name,
|
|
|
|
"_id": user._id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
updatedb() {
|
2019-06-11 23:01:44 +02:00
|
|
|
fs.writeFileSync('src/db/users.json', JSON.stringify(User.strMapToObj(this.userdb), null, 2), (err) => {
|
2019-06-06 19:22:43 +02:00
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
setUpDb() {
|
|
|
|
let files = fs.readdirSync("src/db/");
|
|
|
|
if (!files.includes("users.json")) {
|
|
|
|
fs.writeFileSync('src/db/users.json', JSON.stringify(this.default_db, null, 2), (err) => {
|
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.userdb = new Map(Object.entries(require("./db/users.json")));
|
|
|
|
} else {
|
|
|
|
this.userdb = new Map(Object.entries(require("./db/users.json")));
|
|
|
|
}
|
|
|
|
}
|
2019-06-11 23:01:44 +02:00
|
|
|
static strMapToObj(strMap) {
|
|
|
|
let obj = Object.create(null);
|
|
|
|
for (let [k, v] of strMap) {
|
|
|
|
obj[k] = v;
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
2019-06-06 19:22:43 +02:00
|
|
|
}
|
|
|
|
module.exports = User;
|