chore(release): 0.3.6
[risinglegends.git] / src / server / map.ts
1 import { City, Location, LocationWithCity, Path } from "../shared/map";
2 import type { Player } from '../shared/player';
3 import type { Travel, TravelWithNames } from '../shared/travel';
4 import { db } from './lib/db';
5 import { random } from 'lodash';
6
7 export async function getAllServices(city_id: number): Promise<Location[]> {
8   return db.select('*')
9             .from<Location>('locations')
10             .where({city_id})
11             .orderBy('type')
12             .orderBy('display_order');
13 }
14
15 export async function getService(location_id: number): Promise<LocationWithCity> {
16   return db.select(['locations.*', 'cities.name as city_name']).
17           from<Location>('locations').join('cities', 'locations.city_id', '=', 'cities.id').where({
18     'locations.id': location_id
19   }).first();
20 }
21
22 export async function getAllPaths(city_id: number): Promise<Path[]> {
23   const res = await db.raw(`
24                 select 
25                   paths.*, c1.name as starting_city_name, c2.name as ending_city_name
26                 from paths
27                 join cities c1 on c1.id = paths.starting_city 
28                 join cities c2 on c2.id = paths.ending_city
29                 where paths.starting_city = ?
30                 `, [city_id]);
31
32   return res.rows.map(row => {
33     return {
34       starting_city: row.starting_city,
35       ending_city: row.ending_city,
36       starting_city_name: row.starting_city_name,
37       ending_city_name: row.ending_city_name
38     }
39   });
40 }
41
42 export async function getCityDetails(city_id: number): Promise<City> {
43   return db.first().select('*').from<City>('cities').where({id: city_id});
44 }
45
46 export async function travel(player: Player, dest_id: number): Promise<TravelWithNames> {
47   const city = await getCityDetails(dest_id);
48   const path = await db.first().select('*').from('paths').where({
49     starting_city: player.city_id,
50     ending_city: dest_id
51   });
52   if(!city) {
53     throw new Error('Invalid destination city');
54   }
55   if(!path) {
56     throw new Error(`Invalid path ${player.city_id} => ${dest_id}`);
57   }
58
59   const deviation = Math.floor(path.distance * (random(3,10)/100));
60   const steps = random(path.distance - deviation, path.distance + deviation);
61
62   const rows = await db('travel').insert({
63     player_id: player.id,
64     source_id: player.city_id,
65     destination_id: dest_id,
66     total_distance: steps
67   });
68   
69   return getTravelPlan(player.id);
70 }
71
72 export async function stepForward(player_id: string): Promise<TravelWithNames> {
73   await db('travel').increment('current_position');
74
75   return getTravelPlan(player_id);
76 }
77
78 export async function clearTravelPlan(player_id: string): Promise<Travel> {
79   return db('travel').where({player_id}).delete();
80 }
81
82 export async function completeTravel(player_id: string): Promise<Travel> {
83   const rows = await db('travel').where({player_id}).delete().returning('*');
84   if(rows.length !== 1) {
85     console.log(rows);
86     throw new Error('Unexpected response when moving');
87   }
88
89   return rows[0] as Travel;
90 }
91
92 export async function getTravelPlan(player_id: string): Promise<TravelWithNames> {
93   return db.select([
94     'travel.*',
95     'source.name as source_city_name',
96     'destination.name as destination_city_name'
97   ]).from<Travel>('travel')
98     .join('cities as source', 'travel.source_id', '=', 'source.id')
99     .join('cities as destination', 'travel.destination_id', '=', 'destination.id')
100     .where({
101       'travel.player_id': player_id
102   }).first();
103 }