Skip to content

Commit 60fa2b4

Browse files
committed
add hiscore message handlers
1 parent f2881cf commit 60fa2b4

File tree

2 files changed

+60
-13
lines changed

2 files changed

+60
-13
lines changed

src/handlers/hiscores.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1+
const skills = new Set(require('../skills.json'));
2+
skills.add('overall');
3+
14
async function getHiscoreRanks({
25
token,
36
skill = 'overall',
47
rank = -1,
58
page = 0
69
}) {
10+
if (!skills.has(skill)) {
11+
this.socket.sendMessage({ token, ranks: [] });
12+
return;
13+
}
14+
715
const queryHandler = this.server.queryHandler;
816
const ranks = queryHandler.getHiscoreRanks(skill, rank, page);
917
this.socket.sendMessage({ token, ranks });

src/query-handler.js

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,8 @@ class QueryHandler {
132132
'`rank_total` BETWEEN ? AND ? ORDER BY `rank_total` ASC'
133133
};
134134

135-
for (const statementName of Object.keys(this.statements)) {
136-
this.statements[statementName] = this.database.prepare(
137-
this.statements[statementName]
138-
);
135+
for (const [name, statement] of Object.entries(this.statements)) {
136+
this.statements[name] = this.database.prepare(statement);
139137
}
140138

141139
this.updateTotalHiscoreRanks = this.database.transaction(() => {
@@ -155,9 +153,26 @@ class QueryHandler {
155153
);
156154
});
157155

156+
this.statements.getSkillHiscoreRanks = {};
157+
this.statements.getSkillHiscoreRanksBetween = {};
158158
this.updateSkillHiscoreRanks = {};
159159

160160
for (const skill of skills) {
161+
this.statements.getSkillHiscoreRanks[skill] = this.database.prepare(
162+
`SELECT \`username\`, \`rank_${skill}\` as \`rank\`, ` +
163+
`\`exp_${skill}\` AS \`experience\` FROM \`players\` ` +
164+
`ORDER BY \`rank_${skill}\` ASC LIMIT ${RANKS_PER_PAGE} ` +
165+
'OFFSET ?'
166+
);
167+
168+
this.statements.getSkillHiscoreRanksBetween[
169+
skill
170+
] = this.database.prepare(
171+
`SELECT \`username\`, \`rank_${skill}\` as \`rank\`, ` +
172+
`\`exp_${skill}\` AS \`experience\` FROM \`players\` WHERE ` +
173+
`\`rank_${skill}\` BETWEEN ? AND ? ORDER BY \`rank_total\` ASC`
174+
);
175+
161176
this.updateSkillHiscoreRanks[skill] = this.database.transaction(
162177
() => {
163178
this.statements.dropRankTable.run();
@@ -332,18 +347,42 @@ class QueryHandler {
332347
getHiscoreRanks(skill = 'overall', rank = -1, page = 0) {
333348
let ranks;
334349

335-
if (skill === 'overall' && rank === -1) {
336-
ranks = this.statements.getTotalHiscoreRanks.all(
337-
page * RANKS_PER_PAGE
338-
);
339-
} else if (skill === 'overall' && rank > -1) {
340-
ranks = this.statements.getTotalHiscoreRanksBetween.all(
341-
rank - RANKS_PER_PAGE / 2,
342-
rank + RANKS_PER_PAGE / 2
343-
);
350+
if (rank > -1) {
351+
rank = rank < 1 ? 1 : rank;
352+
353+
const min = Math.max(1, rank - RANKS_PER_PAGE / 2);
354+
let max = rank + RANKS_PER_PAGE / 2;
355+
356+
if (rank < RANKS_PER_PAGE / 2) {
357+
max += RANKS_PER_PAGE / 2 - min;
358+
}
359+
360+
if (skill === 'overall') {
361+
ranks = this.statements.getTotalHiscoreRanksBetween.all(
362+
min,
363+
max
364+
);
365+
} else {
366+
ranks = this.statements.getSkillHiscoreRanksBetween[skill].all(
367+
min,
368+
max
369+
);
370+
}
371+
} else {
372+
const offset = page * RANKS_PER_PAGE;
373+
374+
if (skill === 'overall') {
375+
ranks = this.statements.getTotalHiscoreRanks.all(offset);
376+
} else {
377+
this.statements.getSkillHiscoreRanks[skill].all(offset);
378+
}
344379
}
345380

346381
return ranks.map((entry) => {
382+
if (!entry.level) {
383+
entry.level = experienceToLevel(entry.experience);
384+
}
385+
347386
entry.experience = Math.floor(entry.experience / 4);
348387
return entry;
349388
});

0 commit comments

Comments
 (0)