ensl_gathers/lib/user/user.js

112 lines
3.1 KiB
JavaScript
Raw Normal View History

2015-07-22 23:30:14 +00:00
"use strict";
/*
* Implements User Model
*
*/
2015-08-31 14:46:35 +00:00
var _ = require("lodash");
var async = require("async");
2015-08-28 14:00:14 +00:00
var mongoose = require("mongoose");
var Profile = mongoose.model("Profile");
var steam = require('steamidconvert')();
var enslClient = require("../ensl/client")();
var hiveClient = require("../hive/client")();
2015-07-22 23:30:14 +00:00
2015-08-28 14:00:14 +00:00
function User (user) {
this.id = user['id'];
this.online = true;
2015-07-22 23:30:14 +00:00
this.username = user['username'];
this.country = user['country'];
this.time_zone = user['time_zone'];
this.avatar = enslClient.baseUrl + user['avatar'];
2015-07-22 23:30:14 +00:00
this.admin = user['admin'];
2015-08-05 00:06:09 +00:00
this.team = user['team'];
2015-08-27 12:06:54 +00:00
this.bans = user['bans'];
2015-08-28 20:28:50 +00:00
this.steam = {
2015-08-31 15:50:56 +00:00
id: user['steam']['id'] || null,
2015-08-28 20:28:50 +00:00
url: user['steam']['url'] || null,
nickname: user['steam']['nickname'] || null
};
2015-08-28 14:00:14 +00:00
this.profile = null;
this.hive = {
id: null
};
if (this.steam.url) {
this.hive.id = this.getHiveId();
}
2015-08-28 14:00:14 +00:00
}
2015-07-22 23:30:14 +00:00
2015-09-25 21:23:30 +00:00
User.prototype.getSteamId = function () {
if (this.steam.url === null) return null;
var urlId = this.steam.url.match(/\d*$/);
2015-08-31 14:46:35 +00:00
if (!urlId || urlId[0].length === 0) return null;
return steam.convertToText(urlId[0]);
};
2015-09-25 21:23:30 +00:00
User.prototype.getHiveId = function () {
2015-08-31 15:50:56 +00:00
var steamId = this.steam.id;
if (!steamId) return null;
var index = steamId.match(/:0:/) ? 0 : 1;
var tailId = parseInt(steamId.match(/\d*$/), 10);
return index === 1 ? (tailId * 2) + 1 : tailId * 2;
};
2015-09-18 11:53:01 +00:00
var allowedAttributes = ["enslo", "division", "skill", "gatherMusic"];
2015-09-17 19:59:05 +00:00
var allowedAbilities = ["skulk", "lerk", "fade", "gorge", "onos", "commander"];
2015-09-25 21:23:30 +00:00
User.prototype.updateProfile = function (data, callback) {
2015-08-28 20:28:50 +00:00
let self = this;
2015-09-17 19:59:05 +00:00
Profile.findOne({userId: self.id}, (error, profile) => {
2015-08-28 20:28:50 +00:00
if (error) return callback(error);
2015-09-17 19:59:05 +00:00
allowedAttributes.forEach(function (attr) {
if (data[attr] !== undefined) profile[attr] = data[attr];
});
if (data.abilities) {
allowedAbilities.forEach(function (attr) {
let newAbility = data.abilities[attr];
let abilities = profile.abilities;
if (newAbility !== undefined) abilities[attr] = newAbility;
});
}
profile.save(function (error, profile) {
2015-08-28 20:28:50 +00:00
if (error) return callback(error);
self.profile = profile.toJson();
return callback(error, profile);
});
});
};
2015-09-25 21:23:30 +00:00
User.find = function (id, callback) {
enslClient.getUserById({
2015-08-28 14:00:14 +00:00
id: id
}, (error, response, body) => {
if (error) return callback(error);
if (response.statusCode !== 200) return callback(new Error("Unable to auth user against API"));
2015-08-28 14:17:08 +00:00
let user = new User(body);
2015-08-31 14:46:35 +00:00
async.parallel([
// Retrieve or create user profile from local store
callback => {
Profile.findOrCreate(user, (error, profile) => {
if (error) return callback(error);
user.profile = profile.toJson();
return callback(null, profile);
});
},
// Retrive Hive Stats
callback => {
hiveClient.getUserStats(user, (error, response, body) => {
if (error || response.statusCode !== 200 || body.id === null) return callback();
_.assign(user.hive, body);
return callback(null, body);
});
}
], function (error, result) {
2015-08-28 14:00:14 +00:00
if (error) return callback(error);
2015-08-28 14:17:08 +00:00
return callback(null, user);
2015-08-31 14:46:35 +00:00
})
2015-08-28 14:00:14 +00:00
});
};
module.exports = User;