Skip to content

Commit

Permalink
13/04
Browse files Browse the repository at this point in the history
  • Loading branch information
theADAMJR committed Apr 13, 2020
1 parent ead0aac commit e0a42de
Show file tree
Hide file tree
Showing 12 changed files with 1,383 additions and 261 deletions.
16 changes: 9 additions & 7 deletions api/modules/image/xp-card-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,26 +71,28 @@ export class XPCardGenerator extends ImageGenerator {
const position = { x: 275, y: canvas.height * 0.775 };
const height = 25;

const info = Leveling.xpInfo(member.xpMessages, this.xpPerMessage);
const nextLevelEXP = info.exp + info.xpForNextLevel;
const { exp, nextLevelXP, level, levelCompletion } = Leveling.xpInfo(member.xpMessages,
this.xpPerMessage);

context.fillStyle = card.secondary || this.colors.secondary;
context.fillRect(position.x, position.y, canvas.width - sizeOffset - 1, height);
context.fillStyle = card.primary || this.colors.tertiary;
context.fillRect(position.x, position.y,
(canvas.width - sizeOffset) * (info.exp / nextLevelEXP), height);
(canvas.width - sizeOffset) * (levelCompletion), height);

context.fillStyle = card.primary || this.colors.primary;
context.font = '16px Roboto, sans-serif';
context.fillText(info.exp, canvas.width / 2.5, canvas.height / 1.175);
context.fillText(exp, canvas.width / 2.5, canvas.height / 1.175);

context.fillStyle = '#0F0F0F';
context.fillText(`/`, canvas.width / 2.5 + context.measureText(info.exp).width, canvas.height / 1.175);
context.fillText(`/`, canvas.width / 2.5 +
context.measureText(exp).width, canvas.height / 1.175);

context.fillStyle = card.secondary || this.colors.secondary;
context.fillText(`${nextLevelEXP}XP`, canvas.width / 2.5 + context.measureText(`${info.exp}/`).width, canvas.height / 1.175);
context.fillText(`${nextLevelXP}XP`, canvas.width / 2.5 +
context.measureText(`${exp}/`).width, canvas.height / 1.175);

context.fillStyle = card.primary || this.colors.primary;
context.fillText(`LEVEL ${info.level}`, canvas.width / 2.5, canvas.height / 1.35);
context.fillText(`LEVEL ${level}`, canvas.width / 2.5, canvas.height / 1.35);
}
}
15 changes: 9 additions & 6 deletions api/routes/guilds-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,15 @@ router.get('/:id/roles', async (req, res) => {
router.get('/:id/members', async (req, res) => {
try {
const members = await SavedMember.find({ guildId: req.params.id }).lean();
const guild = await SavedGuild.findById(req.params.id).lean();
const guild = bot.guilds.cache.get(req.params.id)
const savedGuild = await guilds.get(guild);

let rankedMembers = [];
for (const savedMember of members) {
const member = bot.users.cache.get(savedMember.id);
const xp = Leveling.xpInfo(savedMember.xpMessages, guild.xp.xpPerMessage);
const member = bot.users.cache.get(savedMember.userId);
if (!member) continue;

const xp = Leveling.xpInfo(savedMember.xpMessages, savedGuild.xp.xpPerMessage);

rankedMembers.push({
id: member.id,
Expand All @@ -119,7 +122,7 @@ router.get('/:id/members', async (req, res) => {
rankedMembers.sort((a, b) => b.xpMessages - a.xpMessages);

res.json(rankedMembers);
} catch { res.status(400).send('Bad Request'); }
} catch (error) { res.status(400).send(error?.message); }
});

async function getManagableGuilds(key: string) {
Expand Down Expand Up @@ -166,11 +169,11 @@ router.get('/:guildId/members/:memberId/xp-card', async (req, res) => {

async function validateGuildManager(key: string, id: string) {
if (!key)
throw new Error();
throw new TypeError();
const guilds = await getManagableGuilds(key);

if (!guilds.has(id))
throw Error();
throw TypeError();
}

async function getUser(key: string) {
Expand Down
2 changes: 1 addition & 1 deletion data/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ export default class Users extends DBWrapper<User, UserDocument> {
}

protected async create(user: User) {
return new SavedUser({ id: user.id }).save();
return new SavedUser({ _id: user.id }).save();
}
}
695 changes: 475 additions & 220 deletions dist/lavalink/logs/spring.log

Large diffs are not rendered by default.

Binary file added dist/lavalink/logs/spring.log.2020-04-12.0.gz
Binary file not shown.
4 changes: 2 additions & 2 deletions models/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ export class Change {
}

const LogSchema = new Schema({
id: String,
_id: String,
changes: { type: Array, default: [] }
});

export interface AuditLog extends Document {
id: string;
_id: string;
changes: Change[];
}

Expand Down
15 changes: 9 additions & 6 deletions modules/xp/leveling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ export default class Leveling {
Level Up! ⭐\n**New Level**: \`${newLevel}\``);

const levelRole = this.getLevelRole(newLevel, guild);
if (levelRole) {
if (levelRole)
msg.member?.roles.add(levelRole);
}
}
private getLevelRole(level: number, guild: GuildDocument) {
return guild.xp.levelRoles.find(r => r.level === level)?.role;
Expand All @@ -51,16 +50,20 @@ export default class Leveling {
const preciseLevel = (-75 + Math.sqrt(Math.pow(75, 2) - 300 * (-150 - xp))) / 150;
return Math.floor(preciseLevel);
}
static xpInfo(messages: number, xpPerMessage: number) { // TODO: replace with getLevel
static xpInfo(messages: number, xpPerMessage: number) {
const xp = xpPerMessage * messages;

const preciseLevel = (-75 + Math.sqrt(Math.pow(75, 2) - 300 * (-150 - xp))) / 150;
const level = ~~preciseLevel;
const level = Math.floor(preciseLevel);

const xpForNextLevel = this.xpForNextLevel(level, xp);
return { level, exp: xp, xpForNextLevel };
const nextLevelXP = xp + xpForNextLevel;

const levelCompletion = preciseLevel - level;

return { level, exp: xp, xpForNextLevel, levelCompletion, nextLevelXP };
}
private static xpForNextLevel(currentLevel: number, xp: number) { // TODO: remove - will be handled in webapp xp card
private static xpForNextLevel(currentLevel: number, xp: number) {
return ((75 * Math.pow(currentLevel + 1, 2)) + (75 * (currentLevel + 1)) - 150) - xp;
}
}
Loading

0 comments on commit e0a42de

Please sign in to comment.