From: xangelo Date: Wed, 4 Dec 2024 16:33:53 +0000 (-0500) Subject: chore(admin): add endpoints for refreshing content X-Git-Tag: v0.4.4~28 X-Git-Url: https://git.xangelo.ca/?a=commitdiff_plain;h=d04847c11dd04241e94134e1c9212c852c7af11e;p=risinglegends.git chore(admin): add endpoints for refreshing content The content refresh endpoints are now accessible to admins only via the Admin panel that is integrated with the "Settings" page. --- diff --git a/seeds/monsters.ts b/seeds/monsters.ts index 02eecb8..a8dafe8 100644 --- a/seeds/monsters.ts +++ b/seeds/monsters.ts @@ -75,9 +75,14 @@ export async function createMonsters(): Promise { }); } +export async function refreshAllMonsterData() { + await createFactions(); + await createMonsters(); +} + // run this script manually if(!module.parent) { - createFactions().then(createMonsters).then(() => { + refreshAllMonsterData().then(() => { console.log(stats.monsters); console.log('Complete'); process.exit(0); diff --git a/seeds/shop_items.ts b/seeds/shop_items.ts index 6fca12a..024c6c5 100644 --- a/seeds/shop_items.ts +++ b/seeds/shop_items.ts @@ -82,8 +82,13 @@ export async function createShopItems(): Promise { } +export async function refreshAllShopItems() { + await createShopEquipment(); + await createShopItems(); +} + if(!module.parent) { - createShopEquipment().then(createShopItems).then(() => { + refreshAllShopItems().then(() => { console.log('Complete'); process.exit(0); }).catch(e => { diff --git a/src/server/api.ts b/src/server/api.ts index e869b13..4e0ea3a 100644 --- a/src/server/api.ts +++ b/src/server/api.ts @@ -35,11 +35,13 @@ import { renderSkills } from './views/skills'; import { renderMonsterSelector, renderOnlyMonsterSelector } from './views/monster-selector'; import { renderFight, renderFightPreRound, renderRoundDetails } from './views/fight'; import { renderTravel, travelButton } from './views/travel'; +import { renderAdminActions } from './views/admin'; // TEMP! import { completeDungeonFight, getActiveDungeon, getRoomVists, loadRoom } from './dungeon'; import { renderDungeon, renderDungeonRoom } from './views/dungeons/room'; import { flushBuffer, addEvent } from './events'; +import { Permission } from './permissions'; dotenv(); @@ -359,6 +361,10 @@ app.get('/settings', authEndpoint, async (req: Request, res: Response) => { warning += `
If you log out without signing up first, this account is lost forever.
`; } + if(req.player.permissions.includes(Permission.ADMIN)) { + html += `
${renderAdminActions()}
`; + } + html += 'Logout'; res.send(warning + html); }); diff --git a/src/server/permissions.ts b/src/server/permissions.ts new file mode 100644 index 0000000..23ae11d --- /dev/null +++ b/src/server/permissions.ts @@ -0,0 +1,5 @@ + +export enum Permission { + ADMIN = 'admin', + MODERATOR = 'moderator' +} diff --git a/src/server/routes/admin.ts b/src/server/routes/admin.ts new file mode 100644 index 0000000..0e6ca91 --- /dev/null +++ b/src/server/routes/admin.ts @@ -0,0 +1,34 @@ +import { authEndpoint } from '@server/auth'; +import { Permission } from '@server/permissions'; +import { Request, Response, Router } from 'express'; +import { createAllCitiesAndLocations } from '@seeds/cities'; +import { refreshAllShopItems } from '@seeds/shop_items'; +import { refreshAllMonsterData } from '@seeds/monsters'; +import { createDropTables } from '@seeds/drop-tables'; +import { Alert } from '@server/views/alert'; +import { ADMIN_ACTIONS } from '@server/views/admin'; + +export const adminRouter = Router(); + + +adminRouter.post('/admin/content/refresh/:type', authEndpoint, async (req: Request, res: Response) => { + if(!req.player.permissions.includes(Permission.ADMIN)) { + return res.sendStatus(403); + } + + switch(req.params.type) { + case 'cities': + await createAllCitiesAndLocations(); + res.send(Alert('success', 'Cities refreshed') + ADMIN_ACTIONS.get('cities')?.label); + break; + case 'items': + await refreshAllShopItems(); + res.send(Alert('success', 'Shop items refreshed') + ADMIN_ACTIONS.get('items')?.label); + break; + case 'monsters': + await refreshAllMonsterData(); + await createDropTables(); + res.send(Alert('success', 'Monsters and Drop tables refreshed') + ADMIN_ACTIONS.get('monsters')?.label); + break; + } +}); diff --git a/src/server/routes/index.ts b/src/server/routes/index.ts index 4ee8344..546b214 100644 --- a/src/server/routes/index.ts +++ b/src/server/routes/index.ts @@ -6,3 +6,4 @@ export { dungeonRouter } from './locations/dungeon'; export { healerRouter } from './locations/healer'; export { recruiterRouter } from './locations/recruiter'; export { repairRouter } from './locations/repair'; +export { adminRouter } from './admin'; \ No newline at end of file diff --git a/src/server/views/admin.ts b/src/server/views/admin.ts new file mode 100644 index 0000000..d4d0007 --- /dev/null +++ b/src/server/views/admin.ts @@ -0,0 +1,22 @@ + +export const ADMIN_ACTIONS = new Map(); + +ADMIN_ACTIONS.set('cities', { + label: 'Refresh Cities', + action: 'cities' +}); + + +ADMIN_ACTIONS.set('items', { + label: 'Refresh All Items', + action: 'items' +}); + +ADMIN_ACTIONS.set('monsters', { + label: 'Refresh Monsters', + action: 'monsters' +}); + +export function renderAdminActions() { + return Array.from(ADMIN_ACTIONS.values()).map(action => ``).join(''); +} diff --git a/tsconfig.json b/tsconfig.json index 7d2e2b5..0ca48e7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -17,7 +17,8 @@ "@shared/*": ["src/shared/*"], "@server/*": ["src/server/*"], "@client/*": ["src/client/*"], - "@assets/*": ["public/assets/*"] + "@assets/*": ["public/assets/*"], + "@seeds/*": ["seeds/*"] } }, "include": ["src/server/api.ts"]