1 import { db } from './lib/db';
2 import { Fight, Monster, MonsterWithFaction, MonsterForList, FightTrigger } from '../shared/monsters';
3 import { TimePeriod, TimeManager } from '../shared/time';
5 const time = new TimeManager();
8 * return a list of monsters that
9 * - are at the current location
10 * - in the current time period
11 * - in any time period
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());
19 const res: Monster[] = await db.select('*')
20 .where({ location_id })
21 .whereIn('time_period', timePeriod)
22 .from<Monster>('monsters')
28 export async function loadMonster(id: number): Promise<Monster> {
29 return db.select('*').from<Monster>('monsters').where({
34 export async function loadMonsterFromFight(authToken: string): Promise<Fight> {
35 return await db.first().select('*').from<Fight>('fight').where({
40 export async function loadMonsterWithFaction(authToken: string): Promise<MonsterWithFaction> {
41 const res = await db.raw(`
43 f.*, fa.id as faction_id, fa.name as faction_name
45 join monsters m on f.ref_id = m.id
46 left outer join factions fa on m.faction_id = fa.id
54 export async function saveFightState(authToken: string, monster: Fight) {
55 return db('fight').where({
63 export async function createFight(playerId: string, monster: Monster, fightTrigger: FightTrigger): Promise<Fight> {
64 const res = await db('fight').insert({
67 strength: monster.strength,
68 constitution: monster.constitution,
69 dexterity: monster.dexterity,
70 intelligence: monster.intelligence,
75 helmAp: monster.helmAp,
76 chestAp: monster.chestAp,
77 legsAp: monster.legsAp,
78 armsAp: monster.armsAp,
81 fight_trigger: fightTrigger
82 }).returning<Fight[]>('*');
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
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(',')])
95 return res.rows[0] as MonsterForList;
98 export async function clearFight(authToken: string) {
99 return db('fight').where({