fix: users city location not persisting
authorxangelo <git@xangelo.ca>
Mon, 19 Jun 2023 18:52:33 +0000 (14:52 -0400)
committerxangelo <git@xangelo.ca>
Mon, 19 Jun 2023 18:52:33 +0000 (14:52 -0400)
src/events/travel/server.ts
src/server/api.ts
src/server/map.ts
src/server/player.ts
src/shared/map.ts
src/shared/player.ts

index 6b3c5ff7b5b8be28e7cd569c95839776bec26643..32b8a0bba04e30e9b0f1fe3e128e6426c8cf23f5 100644 (file)
@@ -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),
index 20808e3b567a780bce0f8a27f1dd03053fd102f8..e353bde610269760d78bf92ea4a606b4ffdd7b16 100644 (file)
@@ -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});
index 51af071949a912f39313f3cdf428762ff7796239..05fc90f77eef3a4a842fff03f238fc55c37744f8 100644 (file)
@@ -1,7 +1,7 @@
 import { City, Location, Path } from "../shared/map";
 import { db } from './lib/db';
 
-export async function getAllServices(city_id: string): Promise<Location[]> {
+export async function getAllServices(city_id: number): Promise<Location[]> {
   return db.select('*')
             .from<Location>('locations')
             .where({city_id})
@@ -9,7 +9,7 @@ export async function getAllServices(city_id: string): Promise<Location[]> {
             .orderBy('display_order');
 }
 
-export async function getAllPaths(city_id: string): Promise<Path[]> {
+export async function getAllPaths(city_id: number): Promise<Path[]> {
   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<Path[]> {
   });
 }
 
-export async function getCityDetails(city_id: string): Promise<City> {
+export async function getCityDetails(city_id: number): Promise<City> {
   return db.first().select('*').from<City>('cities').where({id: city_id});
 }
index 8a3aa7d7b47edf38ca4db6c275c10b21b8f9d144..3c95080cc510b901440eb54c560763c9d08e09f9 100644 (file)
@@ -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;
index 627684dc4872e209b3d8adb5745431bdf8099bf4..1bf84dc0bb9fbcde3a36591af66cc5e6cec2e22a 100644 (file)
@@ -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;
 }
index 1b87a695acb85de353f88860d4a0ad3b62f29fc6..e571ae168844b7ee0e1dd4287d56845f3a98dddf 100644 (file)
@@ -17,7 +17,7 @@ export type Player = {
   level: number;
   gold: number;
   hp: number;
-  city_id: string;
+  city_id: number;
 }
 
 export type PlayerWithSkills = Player & {