ensl_gathers/lib/user/user.js

57 lines
1.3 KiB
JavaScript
Raw Normal View History

2015-07-22 23:30:14 +00:00
"use strict";
/*
* Implements User Model
*
*/
2015-08-28 14:00:14 +00:00
var client = require("../ensl/client")();
var mongoose = require("mongoose");
var Profile = mongoose.model("Profile");
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 = client.baseUrl + user['avatar'];
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-18 10:33:44 +00:00
if (user['steam']) {
this.steam = {
url: user['steam']['url'],
nickname: user['steam']['nickname']
};
} else {
this.steam = {
url: null,
nickname: null
};
}
2015-07-25 12:00:56 +00:00
this.ability = {
division: "Div " + (Math.floor(Math.random() * 4) + 1),
2015-07-26 11:54:35 +00:00
lifeforms: [["Lerk", "Onos", "Fade"][Math.floor(Math.random() * 3)]],
commander: true
2015-07-25 12:00:56 +00:00
}
2015-08-28 14:00:14 +00:00
this.profile = null;
}
2015-07-22 23:30:14 +00:00
2015-08-28 14:00:14 +00:00
User.find = (id, callback) => {
client.getUserById({
id: id
}, (error, response, body) => {
if (error) return callback(error);
if (response.statusCode !== 200) return callback(new Error("Unable to auth user against API"));
let u = new User(body);
Profile.findOrCreate(u, function (error, profile) {
if (error) return callback(error);
u.profile = profile;
return callback(null, u);
});
});
};
module.exports = User;