Skip to content

Commit

Permalink
Create new type of items (#1)
Browse files Browse the repository at this point in the history
* ♻️ Move botão da Forja para aba de items

* ♻️ Refactor Weapon and create generic Item type

* ✨ Create shield

* ✨ Create Misc Items

* ✨ Gerador de poções

* 🚨 Fix typecheck error
  • Loading branch information
itsmegrave authored Feb 10, 2024
1 parent fc7aebb commit b5f2103
Show file tree
Hide file tree
Showing 14 changed files with 151 additions and 154 deletions.
85 changes: 68 additions & 17 deletions src/adapters/forge-adapter.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,66 @@
import { ForgedWeapon, InteligenciaArma } from '../types/covil/forged-weapon';
import { Weapon } from '../types/olddragon2/weapon';

import { ForgedItem, WeaponIntelligence } from '../types/covil/forged-item';
import { OldDragon2Weapon, OldDragon2Item, OldDragon2Shield } from '../types/olddragon2';

const itemTypes = {
weapon: 'Arma',
misc: 'Itens Gerais',
shield: 'Escudo',
potion: 'Poções',
};
export class ForgeAdapter {
transformToOldDragon2Weapon(forgedWeapon: ForgedWeapon): Weapon {
transformToOldDragon2Item(forgedItem: ForgedItem): OldDragon2Item {
switch (forgedItem.tipoItem) {
case itemTypes.weapon:
return this.transformToOldDragon2Weapon(forgedItem);
case itemTypes.misc:
case itemTypes.potion:
return this.transformToOldDragon2MiscItem(forgedItem);
case itemTypes.shield:
return this.transformToOldDragon2Shield(forgedItem);
default:
console.debug(forgedItem.tipoItem);
throw new Error('Tipo de Item desconhecido!');
break;
}
}

transformToOldDragon2Shield(forgedShield: ForgedItem): OldDragon2Shield {
return {
name: this._generateName(forgedShield.qeNome, forgedShield.sufixo),
type: 'shield',
img: forgedShield.qeImg,
system: {
...this._generateSystemInformation(forgedShield),
bonus_ca: forgedShield.bnCa || 0,
type: 'shield',
},
};
}
transformToOldDragon2MiscItem(forgedItem: ForgedItem): OldDragon2Item {
console.log(forgedItem);
return {
name: `${forgedWeapon.qeNome} - ${Object.keys(forgedWeapon.sufixo).join(', ')}`,
name: this._generateName(forgedItem.qeNome, forgedItem.sufixo),
type: 'misc',
system: {
...this._generateSystemInformation(forgedItem),
},
};
}

transformToOldDragon2Weapon(forgedWeapon: ForgedItem): OldDragon2Weapon {
return {
name: this._generateName(forgedWeapon.qeNome, forgedWeapon.sufixo),
img: forgedWeapon.qeImg,
type: 'weapon',
system: {
description: this._parseDescription(forgedWeapon.propriedades, forgedWeapon.sufixo, forgedWeapon.inteligencia),
cost: forgedWeapon.custo,
...this._generateSystemInformation(forgedWeapon),
damage_type: forgedWeapon.fkDano,
weight_in_load: Number(forgedWeapon.qePeso),
weight_in_grams: Number(forgedWeapon.qePeso) * 1000,
magic_item: this._isMagicItem(forgedWeapon.bnBa, forgedWeapon.bnCa, forgedWeapon.bnDano),
bonus_ca: forgedWeapon.bnCa,
damage: forgedWeapon.qeDano,
bonus_damage: forgedWeapon.bnDano,
bonus_ba: forgedWeapon.bnBa,
shoot_range: Number(forgedWeapon.qeAlcDips),
throw_range: Number(forgedWeapon.qeAlcArr),
quantity: forgedWeapon.qtde,
bolt: forgedWeapon.bolt,
bolt_small: forgedWeapon.bolt_small,
arrow: forgedWeapon.arrow,
Expand All @@ -32,11 +72,22 @@ export class ForgeAdapter {
};
}

private _isMagicItem(ba: number = 0, ca: number = 0, damage: number = 0) {
return Boolean(ba > 0 || ca > 0 || damage > 0);
private _generateSystemInformation(forgedItem: ForgedItem) {
return {
description: this._parseDescription(forgedItem.propriedades, forgedItem.sufixo, forgedItem.inteligencia),
cost: forgedItem.custo,
weight_in_load: Number(forgedItem.qePeso),
weight_in_grams: Number(forgedItem.qePeso) * 1000,
magic_item: true,
quantity: forgedItem.qtde,
};
}

private _generateName(name: string, suffix: Map<string, string>) {
return `${name} - ${Object.keys(suffix).join(', ')}`;
}

private _parseWeaponType(forgedWeapon: ForgedWeapon): 'melee' | 'ranged' | 'ammunition' | 'throwing' {
private _parseWeaponType(forgedWeapon: ForgedItem): 'melee' | 'ranged' | 'ammunition' | 'throwing' {
if (forgedWeapon.arrow || forgedWeapon.bolt || forgedWeapon.bolt_small) {
return 'ammunition';
}
Expand All @@ -52,17 +103,17 @@ export class ForgeAdapter {
return 'melee';
}

private _parseDescription(properties: string, suffix: Map<string, string>, intelligent: InteligenciaArma) {
private _parseDescription(properties: string, suffix: Map<string, string>, intelligent: WeaponIntelligence) {
const description = [properties, Object.keys(suffix).join(', ')];

return `${description.join(', ')}\n\n${this._parseMap(suffix)}${this._parseIntelligentWeapon(intelligent)}`;
}

private _isIntelligentWeapon(intelligent: InteligenciaArma): boolean {
private _isIntelligentWeapon(intelligent: WeaponIntelligence): boolean {
return !!intelligent;
}

private _parseIntelligentWeapon(intelligent: InteligenciaArma) {
private _parseIntelligentWeapon(intelligent: WeaponIntelligence) {
if (!this._isIntelligentWeapon(intelligent)) return '';

const intelligentWeaponBlock = ['\n'];
Expand Down
8 changes: 4 additions & 4 deletions src/interactors/forge-item.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ForgeAdapter } from '../adapters/forge-adapter';
import { Covil } from '../module/config';
import { CovilService } from '../services/covil-service';
import { ForgedWeapon } from '../types/covil/forged-weapon';
import { Weapon } from '../types/olddragon2/weapon';
import { ForgedItem } from '../types/covil/forged-item';
import { OldDragon2Item } from '../types/olddragon2';

export class ForgeItem {
service: CovilService;
Expand All @@ -20,8 +20,8 @@ export class ForgeItem {
this.createItem(response);
}

async createItem(json: ForgedWeapon) {
const itemData: Weapon = this.adapter.transformToOldDragon2Weapon(json);
async createItem(json: ForgedItem) {
const itemData: OldDragon2Item = this.adapter.transformToOldDragon2Item(json);

const item = await Item.create(itemData);
console.debug(Covil.logPrefix, item);
Expand Down
2 changes: 1 addition & 1 deletion src/lang/en.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"covil-velho-dragao": {
"title": "Covil do Velho Dragão",
"forge": "Forjar Item",
"forge": "Forjar Item Mágico",
"dialogs": {
"forge": {
"content": "Bem vindo a forja do covil onde você pode testar sua sorte e conquistar o melhor dos pergaminhos mágicos ou sair com aquela espada sem bonus nenhum!"
Expand Down
2 changes: 1 addition & 1 deletion src/lang/pt-BR.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"covil-velho-dragao": {
"title": "Covil do Velho Dragão",
"forge": "Forjar Item",
"forge": "Forjar Item Mágico",
"dialogs": {
"forge": {
"content": "Bem vindo a forja do covil onde você pode testar sua sorte e conquistar o melhor dos pergaminhos mágicos ou sair com aquela espada sem bonus nenhum!"
Expand Down
2 changes: 1 addition & 1 deletion src/module/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import { CovilConfig } from '../types/covil-config';
export const Covil: CovilConfig = {
logPrefix: 'covil-velho-dragao | ',
baseUrl: 'https://covil.karlz.com.br/api/',
forgeUrl: 'criaitem/A', //para retornar apenas armas. depois remover o `/A` para retornar qualquer item
forgeUrl: 'criaitem',
};
78 changes: 0 additions & 78 deletions src/module/covil-layer.js

This file was deleted.

5 changes: 0 additions & 5 deletions src/module/covil-velho-dragao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import { registerSettings } from './settings';
import { preloadTemplates } from './preloadTemplates';
import { Covil } from './config';

// @ts-expect-error need to refactor this class
import { CovilLayer } from './covil-layer';
import { registerHooks } from './hooks';

// Initialize module
Expand All @@ -38,9 +36,6 @@ Hooks.once('init', async () => {
// Register custom sheets (if any)

registerHooks();

const layers = { covil: { layerClass: CovilLayer, group: 'interface' } };
CONFIG.Canvas.layers = foundry.utils.mergeObject(Canvas.layers, layers);
});

// Setup module
Expand Down
31 changes: 4 additions & 27 deletions src/module/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,7 @@
import { showForgeDialog } from '../dialog/forge-dialog';
import { localize, isClientGM } from './utils/game';
// @ts-expect-error - Forge type definitions are not available
import { renderForgeButton } from './utils/render-forge-button';

export const registerHooks = () => {
Hooks.on('getSceneControlButtons', onGetSceneControlButtons);
};

const onGetSceneControlButtons = (buttons: SceneControl[]) => {
const covilTools = {
activeTool: 'select',
icon: 'cvd-main-icon',
layer: 'covil',
name: 'covil',
title: localize('covil-velho-dragao.title'),
visible: isClientGM(),
tools: [
{
name: 'forge',
icon: 'ra ra-anvil',
title: localize('covil-velho-dragao.forge'),
button: true,
onClick: () => {
showForgeDialog();
},
},
],
};

buttons.push(covilTools);
// Hooks.on('getSceneControlButtons', onGetSceneControlButtons);
Hooks.on('renderItemDirectory', renderForgeButton);
};
30 changes: 30 additions & 0 deletions src/module/utils/render-forge-button.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { showForgeDialog } from '../../dialog/forge-dialog';
import { localize, isClientGM } from './game';

/**
* @param {Application} app
* @param {jQuery} html
*/

export const renderForgeButton = (app, html) => {
if (isClientGM()) {
const section = document.createElement('header');
section.classList.add('character-generator');
section.classList.add('directory-header');

const dirHeader = html[0].querySelector('.directory-header');
dirHeader.parentNode.insertBefore(section, dirHeader);
section.insertAdjacentHTML(
'afterbegin',
`
<div class="header-actions action-buttons flexrow">
<button class="forge-item-button"><i class="ra ra-anvil"></i>${localize('covil-velho-dragao.forge')}</button>
</div>
`,
);
section.querySelector('.forge-item-button').addEventListener('click', () => {
console.log('forjando');
showForgeDialog();
});
}
};
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export type ForgedWeapon = {
custo: string | undefined;
export type ForgedItem = {
custo?: string;
id_equipamento: number;
fkTipoEquipamento?: string;
qeNome?: string;
qeNome: string;
qePeso?: string;
qeCustoPo?: string;
qeCustoPp?: string;
Expand Down Expand Up @@ -31,10 +31,11 @@ export type ForgedWeapon = {
polearm?: boolean;
two_handed?: boolean;
versatile?: boolean;
inteligencia: InteligenciaArma;
inteligencia: WeaponIntelligence;
tipoItem: string;
};

export type InteligenciaArma = {
export type WeaponIntelligence = {
INT: number;
Comunic: string;
Idiomas: number;
Expand Down
5 changes: 5 additions & 0 deletions src/types/olddragon2/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { OldDragon2Item } from './item';
import { OldDragon2Shield } from './shield';
import { OldDragon2Weapon } from './weapon';

export { OldDragon2Item, OldDragon2Weapon, OldDragon2Shield };
18 changes: 18 additions & 0 deletions src/types/olddragon2/item.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export type OldDragon2Item = {
name: string;
type: string;
_id?: string;
img?: string;
system?: {
odo_id?: string;
description?: string;
quantity?: number;
cost?: string;
weight_in_load?: number;
weight_in_grams?: number;
magic_item?: boolean;
type?: string;
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
effects?: any[];
};
9 changes: 9 additions & 0 deletions src/types/olddragon2/shield.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { OldDragon2Item } from './item';

export interface OldDragon2Shield extends Omit<OldDragon2Item, 'type'> {
type: 'shield';
system?: OldDragon2Item['system'] & {
bonus_ca: number;
type: 'shield';
};
}
Loading

0 comments on commit b5f2103

Please sign in to comment.