Skip to content

Commit

Permalink
move exporter to frontend. closes #25
Browse files Browse the repository at this point in the history
  • Loading branch information
seiyria committed Aug 15, 2024
1 parent 959111b commit b3a0b69
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 175 deletions.
103 changes: 0 additions & 103 deletions app/handlers/moddata.ts

This file was deleted.

2 changes: 1 addition & 1 deletion app/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export function setupIPC(sendToUI: SendToUI) {
if (!res) return;

try {
const fullMod = shouldExport ? handlers.formatMod(modData) : modData;
const fullMod = modData;

fs.writeJSONSync(res, fullMod, { spaces: 2 });
sendToUI('notify', { type: 'info', text: `Saved ${res}!` });
Expand Down
3 changes: 3 additions & 0 deletions src/app/helpers/export/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './item';
export * from './npc';
export * from './spawner';
98 changes: 30 additions & 68 deletions app/handlers/format-item.ts → src/app/helpers/export/item.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,5 @@
import { isNumber, isString, isUndefined } from 'lodash';
import { IItemDefinition, SkillType } from '../../src/interfaces';

const ValidItemTypes = [
'Mace',
'Axe',
'Dagger',
'Wand',
'Sword',
'Twohanded',
'Polearm',
'Ranged',
'Martial',
'Staff',
'Restoration',
'Conjuration',
'Throwing',
'Thievery',
'Shortsword',
];
import { IItemDefinition, SkillType } from '../../../interfaces';

const WeaponClasses = [
'Axe',
Expand Down Expand Up @@ -46,59 +28,11 @@ const WeaponClasses = [
];

const AmmoClasses = ['Arrow'];

const SharpWeaponClasses = [
'Axe',
'Blunderbuss',
'Broadsword',
'Crossbow',
'Dagger',
'Greataxe',
'Greatsword',
'Halberd',
'Longbow',
'Longsword',
'Shortbow',
'Shortsword',
'Spear',
];

const ShieldClasses = ['Shield', 'Saucer'];

const ArmorClasses = ['Tunic', 'Breastplate', 'Fur', 'Fullplate', 'Scaleplate'];

const RobeClasses = ['Cloak', 'Robe'];

const HeadClasses = ['Hat', 'Helm', 'Skull', 'Saucer'];

const NeckClasses = ['Amulet'];

const WaistClasses = ['Sash'];

const WristsClasses = ['Bracers'];

const RingClasses = ['Ring'];

const FeetClasses = ['Boots'];

const HandsClasses = ['Gloves', 'Claws'];

const EarClasses = ['Earring'];

const SackableWeaponClasses = ['Axe', 'Dagger', 'Hammer', 'Saucer'];

const SackableArmorClasses = ['Tunic', 'Fur'];

const EquippableItemClasses = HeadClasses.concat(NeckClasses)
.concat(WaistClasses)
.concat(WristsClasses)
.concat(RingClasses)
.concat(FeetClasses)
.concat(HandsClasses)
.concat(ArmorClasses)
.concat(RobeClasses)
.concat(EarClasses);

const isWeapon = (item: IItemDefinition) =>
WeaponClasses.includes(item.itemClass);
const isArmor = (item: IItemDefinition) =>
Expand Down Expand Up @@ -240,11 +174,39 @@ const conditionallyAddInformation = (item: IItemDefinition) => {
item.secondaryType = item.secondaryType.toLowerCase() as SkillType;
};

export function fillInItemProperties(itemData: IItemDefinition) {
function fillInItemProperties(itemData: IItemDefinition) {
itemData.type ??= 'martial';
if (!itemData.stats) itemData.stats = {};

conditionallyAddInformation(itemData);

return itemData;
}

export function formatItems(items: IItemDefinition[]): IItemDefinition[] {
return items.map((item: any) => {
if (!item.sellValue) delete item.sellValue;
if (!item.maxUpgrades) delete item.maxUpgrades;
if (!item.secondaryType) delete item.secondaryType;
if (!item.quality) delete item.quality;
if (item.succorInfo && !item.succorInfo.map) delete item.succorInfo;
if (item.cosmetic && !item.cosmetic.name) delete item.cosmetic;
if (item.containedItems && !item.containedItems.length)
delete item.containedItems;
if (!item.trait.name) delete item.trait;
if (item.randomTrait.name.length === 0) delete item.randomTrait;
if (item.useEffect && !item.useEffect.name) delete item.useEffect;
if (item.strikeEffect && !item.strikeEffect.name) delete item.strikeEffect;
if (item.breakEffect && !item.breakEffect.name) delete item.breakEffect;
if (item.equipEffect && !item.equipEffect.name) delete item.equipEffect;

if (item.requirements) {
if (!item.requirements.baseClass) delete item.requirements.baseClass;
if (!item.requirements.level) delete item.requirements.level;
if (!item.requirements.baseClass && !item.requirements.level)
delete item.requirements;
}

return fillInItemProperties(item as IItemDefinition);
});
}
31 changes: 29 additions & 2 deletions app/handlers/format-npc.ts → src/app/helpers/export/npc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { isNumber, isString } from 'lodash';
import {
AllegianceType,
INPCDefinition,
ItemSlot,
Skill,
SkillType,
} from '../../src/interfaces';
} from '../../../interfaces';

const allSkills = Object.values(Skill);

Expand Down Expand Up @@ -162,9 +163,35 @@ const conditionallyAddInformation = (npc: INPCDefinition) => {
});
};

export function fillInNPCProperties(npc: INPCDefinition) {
function fillInNPCProperties(npc: INPCDefinition): INPCDefinition {
conditionallyAddInformation(npc);
assignReputations(npc);

return npc;
}

export function formatNPCs(npcs: INPCDefinition[]): INPCDefinition[] {
return npcs.map((npc: any) => {
delete npc.hp;
delete npc.mp;
delete npc.giveXp;
delete npc.gold;

npc.repMod = npc.repMod.filter((rep: any) => rep.delta !== 0);

if (npc.drops.length === 0) delete npc.drops;
if (npc.copyDrops.length === 0) delete npc.copyDrops;
if (npc.dropPool.items.length === 0) delete npc.dropPool;

Object.values(ItemSlot).forEach((slot) => {
if (npc.items.equipment[slot]?.length > 0) return;
delete npc.items.equipment[slot];
});

npc.triggers.leash.messages = npc.triggers.leash.messages.filter(Boolean);
npc.triggers.spawn.messages = npc.triggers.spawn.messages.filter(Boolean);
npc.triggers.leash.messages = npc.triggers.leash.messages.filter(Boolean);

return fillInNPCProperties(npc as INPCDefinition);
});
}
8 changes: 8 additions & 0 deletions src/app/helpers/export/spawner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ISpawnerData } from '../../../interfaces';

export function formatSpawners(spawners: ISpawnerData[]): ISpawnerData[] {
return spawners.map((spawner) => {
delete spawner._paths;
return spawner;
});
}
36 changes: 36 additions & 0 deletions src/app/helpers/exporter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { IModKit } from '../../interfaces';
import { formatItems, formatNPCs, formatSpawners } from './export';

function stripIds(modData: IModKit) {
Object.keys(modData).forEach((modKey) => {
const key = modKey as keyof IModKit;

if (key === 'meta') return;

modData[key].forEach((ident: any) => delete ident._id);
});
}

export function formatMod(modData: IModKit): IModKit {
const backup = structuredClone(modData);

stripIds(modData);

const exported: IModKit = {
meta: {
...structuredClone(modData.meta),
_backup: backup,
},

npcs: formatNPCs(modData.npcs),
items: formatItems(modData.items),
drops: modData.drops,
dialogs: modData.dialogs,
maps: modData.maps,
quests: modData.quests,
recipes: modData.recipes,
spawners: formatSpawners(modData.spawners),
};

return exported;
}
3 changes: 2 additions & 1 deletion src/app/home/home.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { SwalComponent } from '@sweetalert2/ngx-sweetalert2';
import { LocalStorageService } from 'ngx-webstorage';
import { numErrorsForMod } from '../helpers';
import { formatMod } from '../helpers/exporter';
import { DebugService } from '../services/debug.service';
import { ElectronService } from '../services/electron.service';
import { ModService } from '../services/mod.service';
Expand Down Expand Up @@ -78,7 +79,7 @@ export class HomeComponent {
const saveMod = () => {
this.electronService.send('SAVE_MOD', {
shouldExport: true,
modData: this.modService.mod(),
modData: formatMod(this.modService.mod()),
});
};

Expand Down

0 comments on commit b3a0b69

Please sign in to comment.