-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),
}
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});
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})
.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
});
}
-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});
}
]);
}
+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;
export type City = {
- id: string;
+ id: number;
name: string;
}
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;
}
level: number;
gold: number;
hp: number;
- city_id: string;
+ city_id: number;
}
export type PlayerWithSkills = Player & {