Skip to content

Commit d55cf5b

Browse files
committed
hiscore rank updating
1 parent fa4dfbb commit d55cf5b

File tree

3 files changed

+56
-7
lines changed

3 files changed

+56
-7
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
node_modules/
2-
rsc-data-server.sqlite
2+
rsc-data-server.sqlite*
33
.eslintcache
44
.node_repl_history

rsc-data-server.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ CREATE TABLE IF NOT EXISTS `players` (
6969
`exp_thieving` integer default '0',
7070
`total_level` integer default '18',
7171
`rank_attack` integer default '0',
72-
`rank_defence` integer default '0',
72+
`rank_defense` integer default '0',
7373
`rank_strength` integer default '0',
7474
`rank_hits` integer default '0',
7575
`rank_ranged` integer default '0',

src/query-handler.js

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,60 @@ class QueryHandler {
111111
'`tcp_port`, `websocket_port`, `country`, `members`) ' +
112112
'VALUES(:id, :ip, :tcp_port, :websocket_port, :country, ' +
113113
':members)',
114-
getWorlds: 'SELECT * FROM `worlds`'
114+
getWorlds: 'SELECT * FROM `worlds`',
115+
dropRankTable: 'DROP TABLE IF EXISTS `hiscore_ranks`',
116+
createRankTable:
117+
'CREATE TEMPORARY TABLE `hiscore_ranks` ' +
118+
'(`player_id` integer, `experience` integer)'
115119
};
116120

117121
for (const statementName of Object.keys(this.statements)) {
118122
this.statements[statementName] = this.database.prepare(
119123
this.statements[statementName]
120124
);
121125
}
126+
127+
this.updateTotalHiscoreRanks = this.database.transaction(() => {
128+
this.statements.dropRankTable.run();
129+
this.statements.createRankTable.run();
130+
131+
this.database.exec(
132+
'INSERT INTO `hiscore_ranks` SELECT `id` AS `player_id`, ' +
133+
`(\`exp_${skills.join('` + `exp_')}\`) AS \`experience\` ` +
134+
'FROM `players` ORDER BY `experience` DESC'
135+
);
136+
137+
this.database.exec(
138+
'UPDATE `players` SET `rank_total` = (SELECT ROWID FROM ' +
139+
'`hiscore_ranks` WHERE `hiscore_ranks`.`player_id` = ' +
140+
'`players`.`id`);'
141+
);
142+
});
143+
144+
this.updateSkillHiscoreRanks = {};
145+
146+
for (const skill of skills) {
147+
this.updateSkillHiscoreRanks[skill] = this.database.transaction(
148+
() => {
149+
this.statements.dropRankTable.run();
150+
this.statements.createRankTable.run();
151+
152+
this.database.exec(
153+
'INSERT INTO `hiscore_ranks` SELECT `id` AS ' +
154+
`\`player_id\`, \`exp_${skill}\` AS ` +
155+
'`experience` FROM `players` ORDER BY ' +
156+
'`experience` DESC'
157+
);
158+
159+
this.database.exec(
160+
`UPDATE \`players\` SET \`rank_${skill}\` = ` +
161+
'(SELECT ROWID FROM `hiscore_ranks` WHERE ' +
162+
'`hiscore_ranks`.`player_id` = ' +
163+
'`players`.`id`);'
164+
);
165+
}
166+
);
167+
}
122168
}
123169

124170
createTables() {
@@ -139,7 +185,7 @@ class QueryHandler {
139185
}
140186

141187
getPlayerCount() {
142-
return this.statements.getPlayerCount.get().count;
188+
return this.statements.getPlayerCount.pluck().get();
143189
}
144190

145191
insertPlayer({ username, password, ip }) {
@@ -259,13 +305,16 @@ class QueryHandler {
259305
});
260306
}
261307

262-
updateHiscoreTotal() {}
263-
264308
updateHiscoreRanks() {
265-
this.updateHiscoreTotal();
309+
this.updateTotalHiscoreRanks();
310+
311+
for (const skill of skills) {
312+
this.updateSkillHiscoreRanks[skill]();
313+
}
266314
}
267315

268316
sync() {
317+
this.database.pragma('journal_mode = WAL');
269318
this.createTables();
270319
this.init();
271320

0 commit comments

Comments
 (0)