From 86d54fb5e3146f3262d6c95ba4846008a083ccfc Mon Sep 17 00:00:00 2001 From: xangelo Date: Mon, 19 Jun 2023 14:52:33 -0400 Subject: [PATCH] fix: users city location not persisting --- src/events/travel/server.ts | 12 +++++++++--- src/server/api.ts | 10 +++++++--- src/server/map.ts | 6 +++--- src/server/player.ts | 6 ++++++ src/shared/map.ts | 8 ++++---- src/shared/player.ts | 2 +- 6 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/events/travel/server.ts b/src/events/travel/server.ts index 6b3c5ff..32b8a0b 100644 --- a/src/events/travel/server.ts +++ b/src/events/travel/server.ts @@ -1,25 +1,31 @@ -import {updatePlayer} from "../../server/player"; +import {movePlayer, updatePlayer} from "../../server/player"; import {getCityDetails, getAllServices, getAllPaths} from "../../server/map"; import {SocketEvent} from "../../server/socket-event.server"; export const travel: SocketEvent = { eventName: 'city:travel', handler: async (api, data: { args: string }) => { - const destinationCity = data.args; + const destinationCity = parseInt(data.args); console.log(`${api.player.username} attempting travel to ${destinationCity}`); + if(!destinationCity || isNaN(destinationCity)) { + console.log('Bad input!'); + return; + } + try { const city = await getCityDetails(destinationCity); if(!city) { + console.log('Bad city input'); // do nothing.. not a real place return; } api.player.city_id = city.id; - updatePlayer(api.player); + await movePlayer(city.id, api.player.id); const [locations, paths] = await Promise.all([ getAllServices(city.id), diff --git a/src/server/api.ts b/src/server/api.ts index 20808e3..e353bde 100644 --- a/src/server/api.ts +++ b/src/server/api.ts @@ -510,10 +510,14 @@ function authEndpoint(req: Request, res: Response, next: any) { } app.get('/city/:id', async (req: Request, res: Response) => { + const id = parseInt(req.params.id); + if(!id || isNaN(id)) { + return res.sendStatus(400); + } const [city, locations, paths] = await Promise.all([ - getCityDetails(req.params.id), - getAllServices(req.params.id), - getAllPaths(req.params.id) + getCityDetails(id), + getAllServices(id), + getAllPaths(id) ]); res.json({city, locations, paths}); diff --git a/src/server/map.ts b/src/server/map.ts index 51af071..05fc90f 100644 --- a/src/server/map.ts +++ b/src/server/map.ts @@ -1,7 +1,7 @@ import { City, Location, Path } from "../shared/map"; import { db } from './lib/db'; -export async function getAllServices(city_id: string): Promise { +export async function getAllServices(city_id: number): Promise { return db.select('*') .from('locations') .where({city_id}) @@ -9,7 +9,7 @@ export async function getAllServices(city_id: string): Promise { .orderBy('display_order'); } -export async function getAllPaths(city_id: string): Promise { +export async function getAllPaths(city_id: number): Promise { const res = await db.raw(` select paths.*, c1.name as starting_city_name, c2.name as ending_city_name @@ -29,6 +29,6 @@ export async function getAllPaths(city_id: string): Promise { }); } -export async function getCityDetails(city_id: string): Promise { +export async function getCityDetails(city_id: number): Promise { return db.first().select('*').from('cities').where({id: city_id}); } diff --git a/src/server/player.ts b/src/server/player.ts index 8a3aa7d..3c95080 100644 --- a/src/server/player.ts +++ b/src/server/player.ts @@ -81,6 +81,12 @@ export async function updatePlayer(player: Player) { ]); } +export async function movePlayer(cityId: number, playerId: string) { + return db('players').where({ + id: playerId + }).update({city_id: cityId}) +} + export async function changeProfession(player_id: string, newProfession: Profession): Promise<{level: number, exp: number}> { let level = 1; let exp = 0; diff --git a/src/shared/map.ts b/src/shared/map.ts index 627684d..1bf84dc 100644 --- a/src/shared/map.ts +++ b/src/shared/map.ts @@ -1,5 +1,5 @@ export type City = { - id: string; + id: number; name: string; } @@ -8,15 +8,15 @@ export type LocationType = 'SERVICES' | 'STORES' | 'EXPLORE'; export type Location = { id: number; name: string; - city_id: string; + city_id: number; type: LocationType, display_order: number; event_name: string; } export type Path = { - starting_city: string; - ending_city: string; + starting_city: number; + ending_city: number; starting_city_name: string; ending_city_name: string; } diff --git a/src/shared/player.ts b/src/shared/player.ts index 1b87a69..e571ae1 100644 --- a/src/shared/player.ts +++ b/src/shared/player.ts @@ -17,7 +17,7 @@ export type Player = { level: number; gold: number; hp: number; - city_id: string; + city_id: number; } export type PlayerWithSkills = Player & { -- 2.25.1