chore(release): 0.2.0
[risinglegends.git] / src / server / monster.ts
1 import { db } from './lib/db';
2 import { Fight, Monster, MonsterWithFaction, MonsterForList, FightTrigger } from '../shared/monsters';
3 import { TimePeriod, TimeManager } from '../shared/time';
4
5 const time = new TimeManager();
6
7 /**
8  * return a list of monsters that
9  * - are at the current location
10  * - in the current time period
11  * - in any time period
12  */
13 export async function getMonsterList(location_id: number, timePeriod: TimePeriod[] = []): Promise<Monster[]> {
14   if(timePeriod.length === 0) {
15     timePeriod.push('any');
16     timePeriod.push(time.getTimePeriod());
17   }
18
19   const res: Monster[] = await db.select('*')
20                       .where({ location_id })
21                       .whereIn('time_period', timePeriod)
22                       .from<Monster>('monsters')
23                       .orderBy('level');
24
25   return res;
26 }
27
28 export async function loadMonster(id: number): Promise<Monster> {
29   return db.select('*').from<Monster>('monsters').where({
30     id
31   }).first();
32 }
33
34 export async function loadMonsterFromFight(authToken: string): Promise<Fight> {
35   return await db.first().select('*').from<Fight>('fight').where({
36     player_id: authToken,
37   });
38 }
39
40 export async function loadMonsterWithFaction(authToken: string): Promise<MonsterWithFaction> {
41   const res = await db.raw(`
42                       select 
43                         f.*, fa.id as faction_id, fa.name as faction_name
44                       from fight f
45                       join monsters m on f.ref_id = m.id
46                       left outer join factions fa on m.faction_id = fa.id
47                       where f.player_id = ?
48                         limit 1
49                       `, [authToken]);
50
51   return res.rows[0];
52 }
53
54 export async function saveFightState(authToken: string, monster: Fight) {
55   return db('fight').where({
56     player_id: authToken,
57     id: monster.id
58   }).update<Fight>({
59     hp: monster.hp
60   });
61 }
62
63 export async function createFight(playerId: string, monster: Monster, fightTrigger: FightTrigger): Promise<Fight> {
64   const res = await db('fight').insert({
65     player_id: playerId,
66     name: monster.name,
67     strength: monster.strength,
68     constitution: monster.constitution,
69     dexterity: monster.dexterity,
70     intelligence: monster.intelligence,
71     exp: monster.exp,
72     level: monster.level,
73     gold: monster.gold,
74     hp: monster.hp,
75     helmAp: monster.helmAp,
76     chestAp: monster.chestAp,
77     legsAp: monster.legsAp,
78     armsAp: monster.armsAp,
79     maxHp: monster.maxHp,
80     ref_id: monster.id,
81     fight_trigger: fightTrigger
82   }).returning<Fight[]>('*');
83
84   return res.pop();
85 }
86
87 /**
88  * Given a list of cities, it will return a monster that 
89  * exists in any of the exploration zones with every monster 
90  * having an equal probability of appearing
91  */
92 export async function getRandomMonster(city_id: number[]): Promise<MonsterForList> {
93   const res = await db.raw('select id,name,level from monsters where location_id in (select id from locations where city_id in (?)) order by random() limit 1', [city_id.join(',')])
94
95   return res.rows[0] as MonsterForList;
96 }
97
98 export async function clearFight(authToken: string) {
99   return db('fight').where({
100     player_id: authToken
101   }).delete();
102 }