1 import { db } from './lib/db';
2 import { Fight, Monster, MonsterWithFaction } 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): 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 }).returning<Fight[]>('*');
86 export async function clearFight(authToken: string) {
87 return db('fight').where({