--- /dev/null
+import { Knex } from "knex";
+
+
+export async function up(knex: Knex): Promise<void> {
+ return knex.schema.createTable('factions', function(table) {
+ table.increments('id').primary();
+ table.string('name').unique();
+ table.text('description').nullable();
+ }).alterTable('monsters', function(table) {
+ table.integer('faction_id').nullable();
+ });
+}
+
+
+export async function down(knex: Knex): Promise<void> {
+ return knex.schema.dropTable('factions').alterTable('monsters', function(table) {
+ table.dropColumn('faction_id');
+ });
+}
+
const base = Airtable.base('appDfPLPajPNog5Iw');
+export async function createFactions(): Promise<void> {
+ return new Promise(async (resolve, reject) => {
+ base('Factions').select().eachPage(async (records, next) => {
+ await db('factions').insert(records.map(r => {
+ console.log(`Creating faction ${r.fields.Name}`);
+ return {
+ id: r.fields.ID,
+ name: r.fields.Name,
+ description: r.fields.Description
+ }
+ })).onConflict('id').merge();
+
+ next();
+ }).finally(() => {
+ resolve();
+ })
+
+ });
+}
+
export async function createMonsters(): Promise<void> {
return new Promise(async (resolve, reject) => {
- await db('monsters').delete();
await db('fight').delete();
base('Monsters').select().eachPage(async (records, next) => {
}
stats.monsters[r.fields.location_id[0]]++;
+ const factionId = r.fields.faction_id ? r.fields.faction_id[0] : null;
+
return {
id: r.fields.id,
name: r.fields.Name,
chestAp: r.fields.chestAp,
legsAp: r.fields.legsAp,
armsAp: r.fields.armsAm,
- location_id: r.fields.location_id[0]
+ location_id: r.fields.location_id[0],
+ faction_id: factionId
}
})).onConflict('id').merge();
// run this script manually
if(!module.parent) {
- createMonsters().then(() => {
+ createFactions().then(createMonsters).then(() => {
console.log(stats.monsters);
console.log('Complete');
process.exit(0);
import {broadcastMessage, Message} from '../shared/message';
import {expToLevel, maxHp, Player} from '../shared/player';
import { professionList } from '../shared/profession';
-import {clearFight, createFight, getMonsterList, loadMonster, loadMonsterFromFight, saveFightState} from './monster';
+import {clearFight, createFight, getMonsterList, loadMonster, loadMonsterFromFight, loadMonsterWithFaction, saveFightState} from './monster';
import {FightRound} from '../shared/fight';
import {addInventoryItem, deleteInventoryItem, getEquippedItems, getInventory, updateAp} from './inventory';
import {Monster, MonsterForFight, MonsterForList} from '../shared/monsters';
});
socket.on('fight', async (data: {action: 'attack' | 'cast' | 'flee', target: 'head' | 'body' | 'arms' | 'legs'}) => {
- const monster = await loadMonsterFromFight(player.id);
+ const monster = await loadMonsterWithFaction(player.id);
const playerSkills = await getPlayerSkillsAsObject(player.id);
const roundData: FightRound = {
monster,
const playerFinalHeal = Math.floor(boost.hp + hpHealAfterMasteries);
roundData.roundDetails.push(`You targeted the monsters ${data.target.toUpperCase()} with ${attackType} damage!`);
+ console.log(monster);
if(data.target === 'arms') {
if(monster.armsAp > 0) {
monster.armsAp -= playerFinalDamage;
import { db } from './lib/db';
-import { Fight, Monster } from '../shared/monsters';
+import { Fight, Monster, MonsterWithFaction } from '../shared/monsters';
export async function getMonsterList(location_id: string): Promise<Monster[]> {
const res: Monster[] = await db.select('*')
});
}
+export async function loadMonsterWithFaction(authToken: string): Promise<MonsterWithFaction> {
+ const res = await db.raw(`
+ select
+ f.*, fa.id as faction_id, fa.name as faction_name
+ from fight f
+ join monsters m on f.ref_id = m.id
+ left outer join factions fa on m.faction_id = fa.id
+ where f.player_id = ?
+ limit 1
+ `, [authToken]);
+
+ return res.rows[0];
+}
+
export async function saveFightState(authToken: string, monster: Fight) {
return db('fight').where({
player_id: authToken,
--- /dev/null
+export type Faction = {
+ id: number;
+ name: string;
+ description: string;
+}
-import {Fight} from "./monsters"
+import {MonsterWithFaction} from "./monsters"
import {Player} from "./player"
export type FightReward = {
}
export type FightRound = {
- monster: Fight,
+ monster: MonsterWithFaction,
player: Player,
winner: 'player' | 'monster' | 'in-progress',
rewards: FightReward,
legsAp: number;
maxHp: number;
location_id: string;
+ faction_id: number;
}
export type MonsterForList = {
level: number;
}
-export type Fight = Omit<Monster, 'id'> & {
+export type Fight = Omit<Monster, 'id' | 'faction_id'> & {
id: string,
player_id: string,
ref_id: number
};
+export type MonsterWithFaction = Fight & {
+ faction_id: string;
+ faction_name: string;
+}
+
export type MonsterForFight = {
id: number | string;
hp: number;