diff --git a/.gitignore b/.gitignore index efa137e..ab02ea8 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ tsconfig.tsbuildinfo node_modules/ data/config/worlds.json +data/players/ # coverage reports coverage/ diff --git a/src/routes/account/index.ts b/src/routes/account/index.ts new file mode 100644 index 0000000..474c4e3 --- /dev/null +++ b/src/routes/account/index.ts @@ -0,0 +1,116 @@ +import bcrypt from 'bcrypt'; +import fs from 'fs'; + +import { db } from '#/db/query.js'; +import { profiles, resolveSelectedProfile } from '#/util/Profile.js'; + +export default function (f: any, opts: any, next: any) { + f.get('/', async (req: any, res: any) => { + // todo: validation handler instead of in route + if (!req.session.account) { + return res.redirect('/account/login', 302); + } + + const profile = resolveSelectedProfile(req); + + const { success, error} = req.session; + delete req.session.success; + delete req.session.error; + + return res.view('account/index', { + success, + error, + profile, + profiles + }); + }); + + f.get('/login', async (req: any, res: any) => { + const { success, error} = req.session; + delete req.session.success; + delete req.session.error; + + return res.view('account/login', { + success, + error + }); + }); + + f.get('/logout', async (req: any, res: any) => { + req.session.account = null; + return res.redirect('/account/login', 302); + }); + + f.get('/forgot', async (req: any, res: any) => { + const { success, error} = req.session; + delete req.session.success; + delete req.session.error; + + return res.view('account/forgot', { + success, + error + }); + }); + + f.post('/login', async (req: any, res: any) => { + const { username, password } = req.body; + + const account = await db.selectFrom('account').where('username', '=', username).selectAll().executeTakeFirst(); + + if (!account || !(await bcrypt.compare(password.toLowerCase(), account.password))) { + req.session.error = 'Invalid username or password.'; + return res.redirect('/account/login', 302); + } + + req.session.account = { + id: account.id, + username: account.username + }; + + return res.redirect('/account', 302); + }); + + f.post('/password', async (req: any, res: any) => { + if (!req.session.account) { + return res.redirect('/account/login', 302); + } + + const { password, password2 } = req.body; + + if (!password || password.length < 5 || password.length > 20) { + req.session.error = 'Your password must be between 5 and 20 characters long.'; + return res.redirect('/account', 302); + } + + if (password !== password2) { + req.session.error = 'Your passwords do not match.'; + return res.redirect('/account', 302); + } + + const hash = await bcrypt.hash(password.toLowerCase(), 10); + await db.updateTable('account').set({ password: hash }).where('id', '=', req.session.account.id).execute(); + + req.session.success = 'Your password has been updated.'; + return res.redirect('/account', 302); + }); + + f.post('/forgot', async (req: any, res: any) => { + return res.redirect('/account/forgot', 302); + }); + + f.post('/export', async (req: any, res: any) => { + // todo: move to input validation + if (!req.session.account) { + return res.redirect('/account', 302); + } + + const profile = resolveSelectedProfile(req); + if (!fs.existsSync(`data/players/${profile.id}/${req.session.account.username}.sav`)) { + return res.redirect('/account', 302); + } + + return fs.readFileSync(`data/players/${profile.id}/${req.session.account.username}.sav`); + }); + + next(); +} diff --git a/src/routes/hiscores/index.ts b/src/routes/hiscores/index.ts index 38bf317..33c7965 100644 --- a/src/routes/hiscores/index.ts +++ b/src/routes/hiscores/index.ts @@ -1,5 +1,6 @@ import { toDisplayName, toSafeName } from '#/jstring/JString.js'; import { db } from '#/db/query.js'; +import { profiles, resolveSelectedProfile } from '#/util/Profile.js'; const categories = [ { id: 0, name: 'Overall', large: true, level: true }, @@ -24,14 +25,6 @@ const categories = [ { id: 21, name: 'Runecrafting' } ]; -// todo: move to db table -const profiles = [ - { id: 'alpha3', name: 'Alpha 3.0' }, // todo: primary/fallback should be configurable - { id: 'alpha1', name: 'Alpha 1.0' }, - { id: 'alpha2', name: 'Alpha 2.0' }, - { id: 'beta', name: 'Beta' }, -]; - const levelExperience = new Int32Array(99); let acc = 0; @@ -56,20 +49,6 @@ function numberWithCommas(x: number) { return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ','); } -function resolveSelectedProfile(req: any): { id: string } { - let profile = profiles.find((p) => p.id === req.query.profile); - - if (!profile && req.session.selectedProfile) { - profile = profiles.find((p) => p.id === req.session.selectedProfile); - } - if (!profile) { - profile = profiles[0]; - } - req.session.selectedProfile = profile.id; - - return profile; -} - export default function (f: any, opts: any, next: any) { f.get('/', async (req: any, res: any) => { const profile = resolveSelectedProfile(req); diff --git a/src/routes/website.ts b/src/routes/website.ts index 013240e..d4e881f 100644 --- a/src/routes/website.ts +++ b/src/routes/website.ts @@ -92,4 +92,8 @@ export default async function (app: FastifyInstance) { app.get('/worldmap', async (req, reply) => { return reply.view('worldmap/applet'); }); + + app.get('/guide', async (req, reply) => { + return reply.view('guide'); + }); } diff --git a/src/util/Profile.ts b/src/util/Profile.ts new file mode 100644 index 0000000..21ecaaa --- /dev/null +++ b/src/util/Profile.ts @@ -0,0 +1,21 @@ +// todo: move to db table +export const profiles = [ + { id: 'beta', name: 'Beta' }, + { id: 'alpha1', name: 'Alpha 1.0' }, + { id: 'alpha2', name: 'Alpha 2.0' }, + { id: 'alpha3', name: 'Alpha 3.0' }, +]; + +export function resolveSelectedProfile(req: any): { id: string } { + let profile = profiles.find((p) => p.id === req.query.profile); + + if (!profile && req.session.selectedProfile) { + profile = profiles.find((p) => p.id === req.session.selectedProfile); + } + if (!profile) { + profile = profiles[0]; + } + req.session.selectedProfile = profile.id; + + return profile; +} diff --git a/view/account/forgot.ejs b/view/account/forgot.ejs new file mode 100644 index 0000000..f7155f5 --- /dev/null +++ b/view/account/forgot.ejs @@ -0,0 +1,23 @@ +<%- include('../_partial/header.ejs', { title: 'Forgot Password - Security Centre' }); %> +<%- include('../_components/frame-top.ejs'); %> +<%- include('../_components/content-start.ejs'); %> +<%- include('../_components/title-box.ejs', { title: 'Forgot Password - Security Centre' }); %> + +<%- include('../_components/content-body-start.ejs'); %> + +<% if (success) { %> +

<%= success %>

+<% } else if (error) { %> +

<%= error %>

+<% } %> + +
+ +Gotcha! Get a better memory next time.

+ +Only kidding - Reach out on Discord for help until this automated system is complete :) +<%- include('../_components/content-body-end.ejs'); %> +
+ +<%- include('../_components/content-end.ejs', { nobr: true }); %> +<%- include('../_partial/footer.ejs'); %> diff --git a/view/account/index.ejs b/view/account/index.ejs new file mode 100644 index 0000000..2eb06f4 --- /dev/null +++ b/view/account/index.ejs @@ -0,0 +1,47 @@ +<%- include('../_partial/header.ejs', { title: 'Security Centre' }); %> +<%- include('../_components/frame-top.ejs'); %> +<%- include('../_components/content-start.ejs'); %> +<%- include('../_components/title-box.ejs', { title: 'Security Centre', links: [ { text: 'Logout', href: '/account/logout' } ] }); %> + +<%- include('../_components/content-body-start.ejs'); %> + +<% if (success) { %> +

<%= success %>

+<% } else if (error) { %> +

<%= error %>

+<% } else { %> +

Change your password:

+<% } %> + +
+ + + +
+ + + + +

+ + +
+ +<%- include('../_components/content-body-end.ejs'); %> +
+ +<%- include('../_components/content-body-start.ejs'); %> +
+ +Export your player save:

+ +<%- include('../hiscores/partials/profile.ejs', { profile, profiles }) %> +
+ +
+ +<%- include('../_components/content-body-end.ejs'); %> +
+ +<%- include('../_components/content-end.ejs', { nobr: true }); %> +<%- include('../_partial/footer.ejs'); %> diff --git a/view/account/login.ejs b/view/account/login.ejs new file mode 100644 index 0000000..0f56b09 --- /dev/null +++ b/view/account/login.ejs @@ -0,0 +1,51 @@ +<%- include('../_partial/header.ejs', { title: 'Security Centre Login' }); %> +<%- include('../_components/frame-top.ejs'); %> +<%- include('../_components/content-start.ejs'); %> +<%- include('../_components/title-box.ejs', { title: 'Security Centre Login' }); %> + +<%- include('../_components/content-body-start.ejs'); %> +<% if (success) { %> +

<%= success %>

+<% } else if (error) { %> +

<%= error %>

+<% } else { %> +

Enter your username & password.

+<% } %> + +
+ + + +
+ + + + +

+ + +
+<%- include('../_components/content-body-end.ejs'); %> +
+ +<%- include('../_components/content-body-start.ejs'); %> +

Or have you forgotten your password?

+ +
+ + + +
Or
+ + + + +

+ + +
+<%- include('../_components/content-body-end.ejs'); %> +
+ +<%- include('../_components/content-end.ejs', { nobr: true }); %> +<%- include('../_partial/footer.ejs'); %> diff --git a/view/create/password.ejs b/view/create/password.ejs index 5b68af4..d19abf8 100644 --- a/view/create/password.ejs +++ b/view/create/password.ejs @@ -57,7 +57,7 @@ - Recovery email (optional): + Recovery Email (optional): diff --git a/view/guide.ejs b/view/guide.ejs new file mode 100644 index 0000000..a1ef200 --- /dev/null +++ b/view/guide.ejs @@ -0,0 +1,12 @@ +<%- include('./_partial/header.ejs', { title: 'Guide' }); %> +<%- include('./_components/frame-top.ejs'); %> +<%- include('./_components/content-start.ejs'); %> +<%- include('./_components/title-box.ejs', { title: 'Guide' }); %> + +<%- include('./_components/content-body-start.ejs'); %> +

This area is under construction - Coming Soon!

+<%- include('./_components/content-body-end.ejs'); %> +
+ +<%- include('./_components/content-end.ejs', { nobr: true }); %> +<%- include('./_partial/footer.ejs'); %> diff --git a/view/manual.ejs b/view/manual.ejs deleted file mode 100644 index 0dab3c8..0000000 --- a/view/manual.ejs +++ /dev/null @@ -1,203 +0,0 @@ - - - - - - -RuneScape - the massive online adventure game by Jagex Ltd - - - - -
- - - - - - -
- - - -
-
-
- - -
How To Play RuneScape
Main menu
-
- - - - - - - - - - - - - - - - - - - -
- - -
- -
-
-
- -
-
- - -
Getting Started
-
- - - - -
How to take the first step to playing RuneScape!
Click here
-
-
- - -
- -
-
-
- -
-
- - -
About RuneScape
-
- - - - -
What is RuneScape?
 
Click here
-
-
 
- - -
- -
-
-
- -
-
- - -
Skills
-
- - - - -
See what skills you can learn in the game.
Click here
-
-
- - -
- -
-
-
- -
-
- - -
Controls
-
- - - - -
Learn how to move around the world.
Click here
-
-
 
- - -
- -
-
-
- -
-
- - -
World Map
-
- - - - -
A full map of the RuneScape World!
Click here
-
-
- - -
- -
-
-
- -
-
- - -
Quests
-
- - - - -
View all the Quests in RuneScape!
 
Click here
-
-
- - - - - - -
-
-This webpage and its contents is copyright 2004 Jagex Ltd
-To use our service you must agree to our -Terms+Conditions -+ -Privacy policy -
-
- -
-
\ No newline at end of file diff --git a/view/title.ejs b/view/title.ejs index d009407..88993d3 100644 --- a/view/title.ejs +++ b/view/title.ejs @@ -86,7 +86,7 @@ To view a full list of news and updates, click here.
Hiscores Table
- Is your character in the top 500,000?
+ Is your character in the top 250,000?
Click Here @@ -111,6 +111,32 @@ To view a full list of news and updates, click here. <%- include('./_components/stones-border-top.ejs'); %> <%- include('./_components/stones-content-start.ejs'); %> <%- include('./_components/options-start.ejs'); %> +
+ +
+
+ +
+
+
Security Centre
+ Manage your account security.
+ Click Here + + + +
+ +
+
+ +
+
+
Guide
+ Answers to all your questions.
+ Click Here + + +<%- include('./_components/options-continue.ejs'); %>