mirror of
https://github.com/2004Scape/Website.git
synced 2026-11-22 21:50:40 -05:00
feat: Hiscores separated by profile
This commit is contained in:
1 parent
373b678044
commit
1588b5b8bb
12 files changed
+75
-36
No files matched your search
@@ -1,6 +1,7 @@
|
||||
# WEB_PORT=80
|
||||
# ADMIN_IP=localhost
|
||||
|
||||
# DB_BACKEND=mysql
|
||||
# DB_NAME=
|
||||
# DB_HOST=
|
||||
# DB_USER=
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
"@fastify/static": "8.0.4",
|
||||
"@fastify/view": "10.0.2",
|
||||
"bcrypt": "5.1.1",
|
||||
"better-sqlite3": "^11.8.1",
|
||||
"dotenv": "16.4.7",
|
||||
"ejs": "3.1.10",
|
||||
"fastify": "5.2.1",
|
||||
@@ -28,6 +29,7 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/bcrypt": "5.0.2",
|
||||
"@types/better-sqlite3": "^7.6.12",
|
||||
"@types/ejs": "3.1.5",
|
||||
"@types/eslint": "9.6.1",
|
||||
"@types/node": "22.10.10",
|
||||
|
||||
+21
-11
@@ -1,19 +1,29 @@
|
||||
import Database from 'better-sqlite3';
|
||||
import { createPool } from 'mysql2';
|
||||
import { Kysely, MysqlDialect } from 'kysely';
|
||||
import { Dialect, Kysely, MysqlDialect, SqliteDialect } from 'kysely';
|
||||
|
||||
import { DB } from '#/db/types.js';
|
||||
|
||||
import Environment from '#/util/Environment.js';
|
||||
|
||||
const dialect = new MysqlDialect({
|
||||
pool: async () =>
|
||||
createPool({
|
||||
database: Environment.DB_NAME,
|
||||
host: Environment.DB_HOST,
|
||||
port: Environment.DB_PORT,
|
||||
user: Environment.DB_USER,
|
||||
password: Environment.DB_PASS
|
||||
})
|
||||
});
|
||||
let dialect: Dialect;
|
||||
|
||||
if (Environment.DB_BACKEND === 'sqlite') {
|
||||
dialect = new SqliteDialect({
|
||||
database: async () => new Database('db.sqlite')
|
||||
});
|
||||
} else {
|
||||
dialect = new MysqlDialect({
|
||||
pool: async () =>
|
||||
createPool({
|
||||
database: Environment.DB_NAME,
|
||||
host: Environment.DB_HOST,
|
||||
port: Environment.DB_PORT,
|
||||
user: Environment.DB_USER,
|
||||
password: Environment.DB_PASS
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
export const db = new Kysely<DB>({
|
||||
dialect,
|
||||
|
||||
+3
-1
@@ -20,7 +20,7 @@ export type account_session = {
|
||||
id: Generated<number>;
|
||||
account_id: number;
|
||||
world: Generated<number>;
|
||||
game: string;
|
||||
profile: Generated<string>;
|
||||
session_uuid: string;
|
||||
timestamp: string;
|
||||
coord: number;
|
||||
@@ -32,6 +32,7 @@ export type friendlist = {
|
||||
friend_account_id: number;
|
||||
};
|
||||
export type hiscore = {
|
||||
profile: Generated<string>;
|
||||
account_id: number;
|
||||
type: number;
|
||||
level: number;
|
||||
@@ -39,6 +40,7 @@ export type hiscore = {
|
||||
date: Generated<string>;
|
||||
};
|
||||
export type hiscore_large = {
|
||||
profile: Generated<string>;
|
||||
account_id: number;
|
||||
type: number;
|
||||
level: number;
|
||||
|
||||
+17
-16
@@ -16,7 +16,22 @@ enum CreateStep {
|
||||
FINISH
|
||||
}
|
||||
|
||||
const profanity = new RegExpMatcher({
|
||||
const staticBlockedUsernames = [
|
||||
// thank you all:
|
||||
'Andrew',
|
||||
'Paul',
|
||||
'Ian',
|
||||
'Ash', // always just "mod ash" but put some respect on em!
|
||||
// and every other mod that contributed to the success and longevity of the game
|
||||
|
||||
// extras that players want to abuse and will surely circumvent :(
|
||||
'Admin',
|
||||
'Administrator',
|
||||
'Mod',
|
||||
'Moderator'
|
||||
];
|
||||
|
||||
const blockedUsernames = new RegExpMatcher({
|
||||
...englishDataset.build(),
|
||||
...englishRecommendedTransformers,
|
||||
});
|
||||
@@ -92,21 +107,7 @@ export default function (f: any, opts: any, next: any) {
|
||||
}
|
||||
|
||||
const displayName = toDisplayName(username);
|
||||
|
||||
const BLOCKED_NAMES = [
|
||||
// hey! stop that
|
||||
'mod',
|
||||
// thank you all:
|
||||
'andrew',
|
||||
'paul',
|
||||
'ian',
|
||||
'ash', // always mod ash but added anyways!
|
||||
// extras
|
||||
'admin',
|
||||
'administrator',
|
||||
];
|
||||
const blocked = BLOCKED_NAMES.includes(name) || profanity.hasMatch(displayName);
|
||||
|
||||
const blocked = blockedUsernames.hasMatch(displayName) || staticBlockedUsernames.includes(displayName);
|
||||
if (blocked || name.startsWith(' ') || name.endsWith(' ') || name.startsWith('mod_') || name.startsWith('m0d_')) {
|
||||
req.session.createStep = CreateStep.USERNAME;
|
||||
req.session.createError = 'That username is not available.';
|
||||
|
||||
@@ -24,6 +24,14 @@ const categories = [
|
||||
{ id: 21, name: 'Runecrafting' }
|
||||
];
|
||||
|
||||
// todo: move to db table
|
||||
const profiles = [
|
||||
{ id: 'alpha1', name: 'Alpha 1.0' },
|
||||
{ id: 'alpha2', name: 'Alpha 2.0' },
|
||||
{ id: 'alpha3', name: 'Alpha 3.0' },
|
||||
{ id: 'beta', name: 'Beta' },
|
||||
];
|
||||
|
||||
const levelExperience = new Int32Array(99);
|
||||
|
||||
let acc = 0;
|
||||
@@ -50,6 +58,11 @@ function numberWithCommas(x: number) {
|
||||
|
||||
export default function (f: any, opts: any, next: any) {
|
||||
f.get('/', async (req: any, res: any) => {
|
||||
let profile = profiles.find(p => p.id == req.query.profile);
|
||||
if (typeof profile === 'undefined') {
|
||||
profile = profiles[0];
|
||||
}
|
||||
|
||||
let category = categories.find(c => c.id == req.query.category);
|
||||
if (typeof category === 'undefined') {
|
||||
category = categories[0];
|
||||
@@ -58,7 +71,7 @@ export default function (f: any, opts: any, next: any) {
|
||||
let query = db.selectFrom(category.large ? 'hiscore_large' : 'hiscore')
|
||||
.innerJoin('account', 'account.id', category.large ? 'hiscore_large.account_id' : 'hiscore.account_id')
|
||||
.select(['account_id', 'type', 'level', 'value', 'date', 'account.username'])
|
||||
.where('type', '=', category.id);
|
||||
.where('type', '=', category.id).where('profile', '=', profile.id);
|
||||
|
||||
if (category.level) {
|
||||
query = query.orderBy('level', 'desc').orderBy('date', 'asc').select((eb) =>
|
||||
@@ -103,6 +116,7 @@ export default function (f: any, opts: any, next: any) {
|
||||
toDisplayName,
|
||||
getLevelByExp,
|
||||
numberWithCommas,
|
||||
profile,
|
||||
categories,
|
||||
category,
|
||||
results,
|
||||
@@ -111,6 +125,11 @@ export default function (f: any, opts: any, next: any) {
|
||||
});
|
||||
|
||||
f.get('/player/:username', async (req: any, res: any) => {
|
||||
let profile = profiles.find(p => p.id == req.query.profile);
|
||||
if (typeof profile === 'undefined') {
|
||||
profile = profiles[0];
|
||||
}
|
||||
|
||||
const username = req.params.username || req.query.username;
|
||||
|
||||
const columnsToSelect = ['account_id','h.type','h.level','h.value','h.date'] as const;
|
||||
@@ -121,7 +140,7 @@ export default function (f: any, opts: any, next: any) {
|
||||
eb.fn.agg<number>('row_number', [])
|
||||
.over((ob) => ob.partitionBy('type').orderBy('value', 'desc').orderBy('date', 'asc'))
|
||||
.as('rank')
|
||||
);
|
||||
).where('profile', '=', profile.id);
|
||||
|
||||
const hiscoreLargeRankQuery = db.selectFrom('hiscore_large as h')
|
||||
.select([...columnsToSelect])
|
||||
@@ -129,7 +148,7 @@ export default function (f: any, opts: any, next: any) {
|
||||
eb.fn.agg<number>('row_number', [])
|
||||
.over((ob) => ob.partitionBy('type').orderBy('level', 'desc').orderBy('date', 'asc'))
|
||||
.as('rank')
|
||||
);
|
||||
).where('profile', '=', profile.id);
|
||||
|
||||
const combinedQuery = db.selectFrom(hiscoreRankQuery.unionAll(hiscoreLargeRankQuery).as('h'))
|
||||
.innerJoin('account', 'account.id', 'h.account_id')
|
||||
@@ -152,6 +171,7 @@ export default function (f: any, opts: any, next: any) {
|
||||
if (results.length === 0) {
|
||||
return res.view('hiscores/no_results', {
|
||||
toDisplayName,
|
||||
profile,
|
||||
username
|
||||
})
|
||||
}
|
||||
@@ -159,6 +179,7 @@ export default function (f: any, opts: any, next: any) {
|
||||
return res.view('hiscores/player', {
|
||||
toDisplayName,
|
||||
numberWithCommas,
|
||||
profile,
|
||||
username,
|
||||
categories,
|
||||
results
|
||||
|
||||
@@ -6,6 +6,7 @@ export default {
|
||||
WEB_PORT: tryParseInt(process.env.WEB_PORT, 80),
|
||||
ADMIN_IP: tryParseString(process.env.ADMIN_IP, 'localhost'),
|
||||
|
||||
DB_BACKEND: tryParseString(process.env.DB_BACKEND, 'sqlite'),
|
||||
DB_HOST: tryParseString(process.env.DB_HOST, ''),
|
||||
DB_PORT: tryParseInt(process.env.DB_PORT, 3306),
|
||||
DB_USER: tryParseString(process.env.DB_USER, ''),
|
||||
|
||||
@@ -12,6 +12,7 @@ interface World {
|
||||
|
||||
const WorldList: World[] = [];
|
||||
|
||||
// todo: move to db table
|
||||
if (fs.existsSync('data/config/worlds.json')) {
|
||||
try {
|
||||
const worlds: World[] = JSON.parse(fs.readFileSync('data/config/worlds.json', 'utf8'));
|
||||
|
||||
+1
-1
@@ -10,7 +10,7 @@
|
||||
"paths": {
|
||||
"#/*": ["src/*"]
|
||||
},
|
||||
"types": ["node", "vitest/globals"],
|
||||
"types": ["node"],
|
||||
"sourceMap": true,
|
||||
"allowJs": true,
|
||||
"checkJs": false,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<%- include('../_partial/header.ejs', { title: 'Hiscores' }); %>
|
||||
<%- include('../_components/frame-top.ejs'); %>
|
||||
<%- include('../_components/content-start.ejs'); %>
|
||||
<%- include('../_components/title-box.ejs', { title: '2004Scape Hiscores' }); %>
|
||||
<%- include('../_components/title-box.ejs', { title: '2004Scape ' + profile.name + ' Hiscores' }); %>
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
@@ -29,7 +29,7 @@
|
||||
|
||||
<td width=290>
|
||||
<center>
|
||||
<b><%= category.name %> Hiscores</b><br>
|
||||
<b><%= category.name %> <%= profile.name %> Hiscores</b><br>
|
||||
|
||||
<table width=300 height=400 bgcolor=black cellpadding=4>
|
||||
<tr>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<%- include('../_partial/header.ejs', { title: 'Hiscores' }); %>
|
||||
<%- include('../_components/frame-top.ejs'); %>
|
||||
<%- include('../_components/content-start.ejs'); %>
|
||||
<%- include('../_components/title-box.ejs', { title: '2004Scape Hiscores', links: [{ text: 'All Hiscores', href: '/hiscores' }] }); %>
|
||||
<%- include('../_components/title-box.ejs', { title: '2004Scape ' + profile.name + ' Hiscores', links: [{ text: 'All Hiscores', href: '/hiscores' }] }); %>
|
||||
|
||||
<table bgcolor="black" cellpadding="4" border="0">
|
||||
<tbody>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<%- include('../_partial/header.ejs', { title: 'Hiscores' }); %>
|
||||
<%- include('../_components/frame-top.ejs'); %>
|
||||
<%- include('../_components/content-start.ejs'); %>
|
||||
<%- include('../_components/title-box.ejs', { title: '2004Scape Hiscores', links: [{ text: 'All Hiscores', href: '/hiscores' }] }); %>
|
||||
<%- include('../_components/title-box.ejs', { title: '2004Scape ' + profile.name + ' Hiscores' }); %>
|
||||
|
||||
<table bgcolor="black" cellpadding="4" border="0">
|
||||
<tbody>
|
||||
|
||||
Reference in new issue
Block a user