Skip to content

Commit e066c71

Browse files
committed
start news
1 parent 6eb4c65 commit e066c71

File tree

6 files changed

+87
-11
lines changed

6 files changed

+87
-11
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ rsc-data-server sends these to certain (or all) clients with the each header
241241
corresponding to the `.handler` property:
242242

243243
### playerLoggedIn
244-
sent to rsc-server instances when players login.
244+
sent to data clients when players login.
245245

246246
```javascript
247247
{
@@ -252,7 +252,7 @@ sent to rsc-server instances when players login.
252252
```
253253

254254
### playerLoggedOut
255-
sent to rsc-server instances when players logout
255+
sent to data clients when players logout.
256256

257257
```javascript
258258
{
@@ -262,7 +262,7 @@ sent to rsc-server instances when players logout
262262
```
263263

264264
### playerWorldChange
265-
sent to rsc-server instances when players change world appearance.
265+
sent to data clients when players change world appearance.
266266

267267
```javascript
268268
{
@@ -273,7 +273,7 @@ sent to rsc-server instances when players change world appearance.
273273
```
274274

275275
### playerMessage
276-
sent to the rsc-server instance corresponding to the the world `toUsername`
276+
sent to the data client corresponding to the the world `toUsername`
277277
resides in the `playerMessage` handler.
278278

279279
```javascript

rsc-data-server.sql renamed to create-tables.sql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,17 @@ CREATE TABLE IF NOT EXISTS `login_attempts` (
129129

130130
CREATE UNIQUE INDEX IF NOT EXISTS `login_attempts_ip_unique` on `login_attempts`
131131
(`ip`);
132+
133+
CREATE TABLE IF NOT EXISTS `news` (
134+
`id` integer not null primary key autoincrement,
135+
`date` datetime default (strftime('%s', 'now')),
136+
`category` integer default '0',
137+
`title` varchar(255),
138+
`body` TEXT
139+
);
140+
141+
CREATE TABLE IF NOT EXISTS `uploads` (
142+
`id` integer not null primary key autoincrement,
143+
`name` varchar(255),
144+
`file` blob
145+
);

src/handlers/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const handlers = [
22
require('./authenticate'),
33
require('./hiscores'),
4+
require('./news'),
45
require('./player'),
56
require('./world')
67
];

src/handlers/news.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
async function getNews({
2+
token,
3+
id = -1,
4+
terms = '',
5+
page = 0,
6+
category = -1,
7+
before = -1,
8+
after = 1
9+
}) {
10+
const queryHandler = this.server.queryHandler;
11+
12+
const articles = queryHandler.getNews({
13+
id,
14+
terms,
15+
page,
16+
category,
17+
before,
18+
after
19+
});
20+
21+
this.socket.sendMessage({
22+
token,
23+
articles
24+
});
25+
}
26+
27+
async function addNews({ token, title, category, body }) {}
28+
29+
async function editNews({ token, id, title, category, body }) {}
30+
31+
async function uploadFile({ token, name, file }) {}
32+
33+
module.exports = { getNews, addNews, editNews, uploadFile };

src/query-handler.js

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const snakeCaseKeys = require('snakecase-keys');
55
const { experienceToLevel } = require('./skills');
66

77
const CREATE_TABLES = fs
8-
.readFileSync(`${__dirname}/../rsc-data-server.sql`)
8+
.readFileSync(`${__dirname}/../create-tables.sql`)
99
.toString();
1010

1111
const SET_PLAYER_ATTRIBUTES = [
@@ -61,7 +61,10 @@ const PLAYER_JSON_FIELDS = [
6161
const TOTAL_EXP = `(\`exp_${skills.join('` + `exp_')}\`) AS \`experience\``;
6262
const RANKS_PER_PAGE = 16;
6363

64-
function getPages(database) {
64+
const SUMMARY_LENGTH = 140;
65+
const NEWS_PER_PAGE = 10;
66+
67+
function getHiscorePages(database) {
6568
return Math.ceil(
6669
database.prepare('SELECT COUNT(1) FROM `hiscore_ranks`').pluck().get() /
6770
RANKS_PER_PAGE
@@ -138,8 +141,21 @@ class QueryHandler {
138141
getPlayerRanks:
139142
`SELECT ${skills.map(
140143
(skill) => `\`exp_${skill}\`, \`rank_${skill}\``
141-
)}, \`total_level\`, ${TOTAL_EXP}, \`rank_total\` `+
142-
'FROM `players` WHERE `username` = ?'
144+
)}, \`total_level\`, ${TOTAL_EXP}, \`rank_total\` ` +
145+
'FROM `players` WHERE `username` = ?',
146+
searchNews:
147+
'SELECT `id`, `date`, `category`, `title`, ' +
148+
`substr(\`body\`, 0, ${SUMMARY_LENGTH}) AS \`summary\` ` +
149+
'FROM `news` WHERE ' +
150+
'CASE WHEN :category > -1 THEN `category` = :category ELSE ' +
151+
'true END AND ' +
152+
'CASE WHEN LENGTH(:terms) > 0 THEN `title` LIKE :terms OR ' +
153+
'`body` LIKE :terms ELSE true END AND ' +
154+
'CASE WHEN :before > -1 AND :after > -1 THEN ' +
155+
'`date` < :before AND `date` >= :after ELSE true END ' +
156+
'ORDER BY `date` DESC ' +
157+
`LIMIT ${NEWS_PER_PAGE} OFFSET (:page * ${NEWS_PER_PAGE})`,
158+
getNews: 'SELECT * FROM `news` WHERE `id` = ?'
143159
};
144160

145161
for (const [name, statement] of Object.entries(this.statements)) {
@@ -162,7 +178,7 @@ class QueryHandler {
162178
'`players`.`id`)'
163179
);
164180

165-
return getPages(this.database);
181+
return getHiscorePages(this.database);
166182
});
167183

168184
this.statements.getSkillHiscoreRanks = {};
@@ -205,7 +221,7 @@ class QueryHandler {
205221
'`players`.`id`);'
206222
);
207223

208-
return getPages(this.database);
224+
return getHiscorePages(this.database);
209225
}
210226
);
211227
}
@@ -435,6 +451,17 @@ class QueryHandler {
435451
return ranks;
436452
}
437453

454+
getNews(query) {
455+
if (query.id !== -1) {
456+
return this.statements.getNews.get(query.id);
457+
}
458+
459+
delete query.id;
460+
query.terms = `%${query.terms.replace(/%|_/g, '')}%`;
461+
462+
return this.statements.searchNews.all(query);
463+
}
464+
438465
sync() {
439466
this.database.pragma('journal_mode = WAL');
440467
this.createTables();

src/server.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,13 @@ class Server {
115115
if (this.config.sockFile) {
116116
try {
117117
fs.unlinkSync(this.config.sockFile);
118+
118119
log.debug(
119120
'removing existing socket file',
120121
this.config.sockFile
121122
);
122123
} catch (e) {
123-
// ignore
124+
// pass
124125
}
125126

126127
this.ipcServer.listen(this.config.sockFile);

0 commit comments

Comments
 (0)