* getAchievementsEarnedByUser() exported as standard method using global inst

* Added timestamp info
This commit is contained in:
Bryan Ashby 2019-01-22 21:54:37 -07:00
parent 4b763cc369
commit 4f0ade6ce1
1 changed files with 56 additions and 49 deletions

View File

@ -37,6 +37,8 @@ const async = require('async');
const moment = require('moment'); const moment = require('moment');
const paths = require('path'); const paths = require('path');
exports.getAchievementsEarnedByUser = getAchievementsEarnedByUser;
class Achievement { class Achievement {
constructor(data) { constructor(data) {
this.data = data; this.data = data;
@ -130,50 +132,8 @@ class Achievements {
this.events = events; this.events = events;
} }
getAchievementsEarnedByUser(userId, cb) { getAchievementByTag(tag) {
if(!this.isEnabled()) { return this.achievementConfig.achievements[tag];
return cb(Errors.General('Achievements not enabled', ErrorReasons.Disabled));
}
UserDb.all(
`SELECT achievement_tag, timestamp, match, title, text, points
FROM user_achievement
WHERE user_id = ?
ORDER BY DATETIME(timestamp);`,
[ userId ],
(err, rows) => {
if(err) {
return cb(err);
}
const earned = rows.map(row => {
const achievement = Achievement.factory(this.achievementConfig.achievements[row.achievement_tag]);
if(!achievement) {
return;
}
const earnedInfo = {
achievementTag : row.achievement_tag,
type : achievement.data.type,
retroactive : achievement.data.retroactive,
title : row.title,
text : row.text,
points : row.points,
};
switch(earnedInfo.type) {
case [ Achievement.Types.UserStatSet ] :
case [ Achievement.Types.UserStatInc ] :
earnedInfo.statName = achievement.data.statName;
break;
}
return earnedInfo;
}).filter(a => a); // remove any empty records (ie: no achievement.hjson entry exists anymore).
return cb(null, earned);
}
);
} }
isEnabled() { isEnabled() {
@ -341,7 +301,7 @@ class Achievements {
return; return;
} }
const achievement = Achievement.factory(this.achievementConfig.achievements[achievementTag]); const achievement = Achievement.factory(this.getAchievementByTag(achievementTag));
if(!achievement) { if(!achievement) {
return; return;
} }
@ -573,16 +533,63 @@ class Achievements {
} }
} }
let achievements; let achievementsInstance;
function getAchievementsEarnedByUser(userId, cb) {
if(!achievementsInstance) {
return cb(Errors.UnexpectedState('Achievements not initialized'));
}
UserDb.all(
`SELECT achievement_tag, timestamp, match, title, text, points
FROM user_achievement
WHERE user_id = ?
ORDER BY DATETIME(timestamp);`,
[ userId ],
(err, rows) => {
if(err) {
return cb(err);
}
const earned = rows.map(row => {
const achievement = Achievement.factory(achievementsInstance.getAchievementByTag(row.achievement_tag));
if(!achievement) {
return;
}
const earnedInfo = {
achievementTag : row.achievement_tag,
type : achievement.data.type,
retroactive : achievement.data.retroactive,
title : row.title,
text : row.text,
points : row.points,
timestamp : moment(row.timestamp),
};
switch(earnedInfo.type) {
case [ Achievement.Types.UserStatSet ] :
case [ Achievement.Types.UserStatInc ] :
earnedInfo.statName = achievement.data.statName;
break;
}
return earnedInfo;
}).filter(a => a); // remove any empty records (ie: no achievement.hjson entry exists anymore).
return cb(null, earned);
}
);
}
exports.moduleInitialize = (initInfo, cb) => { exports.moduleInitialize = (initInfo, cb) => {
achievements = new Achievements(initInfo.events); achievementsInstance = new Achievements(initInfo.events);
achievements.init( err => { achievementsInstance.init( err => {
if(err) { if(err) {
return cb(err); return cb(err);
} }
exports.achievements = achievements;
return cb(null); return cb(null);
}); });
}; };