From 87e0ad99b4ee0646d30f96b462d30e624fefebd2 Mon Sep 17 00:00:00 2001 From: Chris Blanchard Date: Mon, 31 Aug 2015 15:29:37 +0100 Subject: [PATCH] Add method to extract steam and hive id --- lib/user/user.js | 42 +++++++++++++++++++++++++++++------ spec/user.js | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 7 deletions(-) create mode 100644 spec/user.js diff --git a/lib/user/user.js b/lib/user/user.js index dae6082..e892acf 100644 --- a/lib/user/user.js +++ b/lib/user/user.js @@ -5,9 +5,12 @@ * */ -var client = require("../ensl/client")(); +var async = require("async"); var mongoose = require("mongoose"); var Profile = mongoose.model("Profile"); +var steam = require('steamidconvert')(); +var enslClient = require("../ensl/client")(); +var hiveClient = require("../hive/client")(); function User (user) { this.id = user['id']; @@ -15,24 +18,49 @@ function User (user) { this.username = user['username']; this.country = user['country']; this.time_zone = user['time_zone']; - this.avatar = client.baseUrl + user['avatar']; + this.avatar = enslClient.baseUrl + user['avatar']; this.admin = user['admin']; this.team = user['team']; this.bans = user['bans']; this.steam = { + id: null, url: user['steam']['url'] || null, nickname: user['steam']['nickname'] || null }; this.profile = null; + this.hive = { + id: null + }; + + if (this.steam.url) { + this.steam.id = this.getSteamId(); + this.hive.id = this.getHiveId(); + } + } -User.prototype.updateProfile = function (data, callback) { +User.prototype.getSteamId = () => { + if (this.steam.url === null) return null; + var urlId = this.steam.url.match(/\d*$/); + if (!urlId) return null; + return steam.convertToText(urlId[0]); +}; + +User.prototype.getHiveId = () => { + var steamId = this.getSteamId(); + 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; +}; + +User.prototype.updateProfile = (data, callback) => { let self = this; Profile.update({ userId: self.id - }, data, function (error) { + }, data, error => { if (error) return callback(error); - Profile.findOne({userId: self.id}, function (error, profile) { + Profile.findOne({userId: self.id}, (error, profile) => { if (error) return callback(error); self.profile = profile.toJson(); return callback(error, profile); @@ -41,13 +69,13 @@ User.prototype.updateProfile = function (data, callback) { }; User.find = (id, callback) => { - client.getUserById({ + enslClient.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 user = new User(body); - Profile.findOrCreate(user, function (error, profile) { + Profile.findOrCreate(user, (error, profile) => { if (error) return callback(error); user.profile = profile.toJson(); return callback(null, user); diff --git a/spec/user.js b/spec/user.js new file mode 100644 index 0000000..9e1b5fd --- /dev/null +++ b/spec/user.js @@ -0,0 +1,58 @@ +"use strict"; + +var helper = require("./helpers/index.js"); +var User = helper.User; +var assert = require("chai").assert; +var async = require("async"); +var userCount = 0; + +describe("User", () => { + var user, userAttributes; + + before(() => { + userCount++; + userAttributes = { + id: userCount, + username: "FearLess90", + country: "CA", + time_zone: "Eastern Time (US & Canada)", + avatar: "/images/icons/" + userCount + ".png", + admin: false, + steam: { + url: "http://steamcommunity.com/profiles/76561198076460617", + nickname: "FearLess90" + }, + bans: { + gather: false, + mute: false, + site: false + }, + team: { + id: 622, + name: "National Gamers" + } + } + }); + + describe("#getSteamId", () => { + beforeEach(() => { + user = new User(userAttributes); + }); + it ("returns steamid", () => { + assert.equal(user.getSteamId(), "STEAM_0:1:58097444"); + }); + it ("returns null if no steamid", () => { + user.steam.url = null; + assert.isNull(user.getSteamId()); + }); + }); + + describe("#getHiveId", () => { + beforeEach(() => { + user = new User(userAttributes); + }); + it ("returns hive id", () => { + console.log(user.getHiveId()); + }); + }); +}); \ No newline at end of file