Skip to content

Commit

Permalink
Type cleanup post 0.8.x upgrade
Browse files Browse the repository at this point in the history
Fixes a number of issues noted in the 0.8.x upgrade pull request
Performs some cleanup on the types used for actors and items to keep
consistent use of generics.
Fixes an issue where the wrong item class was used for Affiliations in
the proxy class.
  • Loading branch information
StasTserk committed Jun 11, 2021
1 parent fe38722 commit 9613d2e
Show file tree
Hide file tree
Showing 20 changed files with 65 additions and 129 deletions.
30 changes: 12 additions & 18 deletions module/actors/BWActor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { Armor } from "../items/armor.js";
import { PossessionRootData } from "../items/possession.js";
import { ReputationDataRoot } from "../items/reputation.js";
import { TraitDataRoot, Trait } from "../items/trait.js";
import { BWCharacterData, CharacterDataRoot } from "./BWCharacter.js";
import { NpcData, NpcDataRoot } from "./Npc.js";
import { BWCharacterData } from "./BWCharacter.js";
import { NpcData } from "./Npc.js";
import { AffiliationDataRoot } from "../items/affiliation.js";

export class BWActor<T extends BWActorData = BWActorDataTypes> extends Actor<T, BWItem> {
export class BWActor<T extends BWActorData = BWActorData> extends Actor<T, BWItem> {
data: T;

readonly batchAdd = {
Expand Down Expand Up @@ -357,21 +357,17 @@ export class BWActor<T extends BWActorData = BWActorDataTypes> extends Actor<T,
}

public updateArthaForSkill(_skillId: string, persona: number, deeds: number): void {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
this.update({
"data.deeds": this.data.data.deeds - (deeds ? 1 : 0),
"data.persona": this.data.data.persona - persona,
});
const updateData = {};
updateData["data.deeds"] = this.data.data.deeds - (deeds ? 1 : 0);
updateData["data.persona"] = this.data.data.persona - persona;
this.update(updateData);
}

public updateArthaForStat(_accessor: string, persona: number, deeds: number): void {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
this.update({
"data.deeds": this.data.data.deeds - (deeds ? 1 : 0),
"data.persona": this.data.data.persona - persona,
});
public updateArthaForStat(accessor: string, persona: number, deeds: number): void {
const updateData = {};
updateData["data.deeds"] = this.data.data.deeds - (deeds ? 1 : 0);
updateData["data.persona"] = this.data.data.persona - persona;
this.update(updateData);
}
}

Expand Down Expand Up @@ -490,5 +486,3 @@ export interface NewItemData extends StringIndexedObject<any> {
name: string;
type: ItemType;
}

export type BWActorDataTypes = CharacterDataRoot | NpcDataRoot;
13 changes: 0 additions & 13 deletions module/actors/BWCharacter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,6 @@ export class BWCharacter extends BWActor<CharacterDataRoot> {

prepareData(): void {
super.prepareData();
if (!this.data.data.settings) {
this.data.data.settings = {
onlySuccessesCount: 'Faith, Resources, Perception',
showSettings: false,
roundUpHealth: false,
roundUpMortalWound: false,
roundUpReflexes: false,
armorTrained: false,
ignoreSuperficialWounds: false,
showBurner: false
};
}

this._calculatePtgs();

Expand Down Expand Up @@ -363,7 +351,6 @@ export class BWCharacter extends BWActor<CharacterDataRoot> {
}

export interface CharacterDataRoot extends BWActorData<BWCharacterData> {
data: BWCharacterData;
type: "character"
}

Expand Down
6 changes: 2 additions & 4 deletions module/actors/Npc.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Common, BWActor, BWActorData } from "./BWActor.js";
import { ShadeString } from "../helpers.js";

export class Npc extends BWActor {
data: NpcDataRoot;
export class Npc extends BWActor<NpcDataRoot> {

prepareData(): void {
super.prepareData();
Expand All @@ -22,8 +21,7 @@ export class Npc extends BWActor {
}

export interface NpcDataRoot extends BWActorData<NpcData> {
data: NpcData;
// type: "npc"
type: "npc"
}

export interface NpcData extends Common {
Expand Down
4 changes: 2 additions & 2 deletions module/actors/sheets/BWSettingSheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ export class BWSettingSheet extends ActorSheet<BWSettingSheetData> {
}
}
}
}).on('dragenter', _ => {
}).on('dragenter', () => {
enterCount ++;
}).on('dragleave', _ => {
}).on('dragleave', () => {
enterCount --;
if (!enterCount) {
activeDropArea?.removeClass("show-drop");
Expand Down
5 changes: 3 additions & 2 deletions module/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import { Trait } from "./items/trait.js";
import { Npc } from "./actors/Npc.js";
import { Lifepath } from "./items/lifepath.js";
import { BWSetting } from "./actors/BWSetting.js";
import { Affiliation } from "./items/affiliation.js";

function factory(entities: Record<string, typeof FoundryDocument>, baseClass: typeof Entity): unknown {
function factory(entities: Record<string, typeof FoundryDocument>, baseClass: typeof FoundryDocument): typeof FoundryDocument {
return new Proxy(baseClass, {
construct: (target, args) => {
const [data, options] = args;
Expand Down Expand Up @@ -80,7 +81,7 @@ itemTypes["relationship"] = Relationship as typeof FoundryDocument;
itemTypes["melee weapon"] = MeleeWeapon as typeof FoundryDocument;
itemTypes["ranged weapon"] = RangedWeapon as typeof FoundryDocument;
itemTypes["reputation"] = Reputation as typeof FoundryDocument;
itemTypes["affiliation"] = Relationship as typeof FoundryDocument;
itemTypes["affiliation"] = Affiliation as typeof FoundryDocument;
itemTypes["spell"] = Spell as typeof FoundryDocument;
itemTypes["lifepath"] = Lifepath as typeof FoundryDocument;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
14 changes: 4 additions & 10 deletions module/items/affiliation.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import { BWItem, BWItemData, ItemType } from "./item.js";
import { BWItem, BWItemData } from "./item.js";

export class Affiliation extends BWItem {
data: AffiliationDataRoot;
get type(): ItemType {
return this.type as ItemType;
}
}
export class Affiliation extends BWItem<AffiliationDataRoot> { }

export interface AffiliationDataRoot extends BWItemData {
data: AffiliationData;
type: ItemType;
export interface AffiliationDataRoot extends BWItemData<AffiliationData> {
type: "affiliation";
}

export interface AffiliationData {
Expand Down
8 changes: 3 additions & 5 deletions module/items/armor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { BWItem, BWItemData, DisplayClass, HasPointCost } from "./item.js";
import { rollDice } from "../rolls/rolls.js";
import { ShadeString } from "../helpers.js";

export class Armor extends BWItem {
export class Armor extends BWItem<ArmorRootData> {
prepareData(): void {
super.prepareData();
this.data.data.cssClass = "equipment-armor";
Expand All @@ -17,8 +17,6 @@ export class Armor extends BWItem {
this.data.data.shieldDisplayClass = this.calculateDisplayClass(dice, this.data.data.damageShield);
}

data: ArmorRootData;

calculateDisplayClass(dice: number, locationDice: string): string {
if (parseInt(locationDice) >= dice) {
return "armor-broken";
Expand Down Expand Up @@ -58,8 +56,8 @@ export class Armor extends BWItem {
}
}

export interface ArmorRootData extends BWItemData {
data: ArmorData;
export interface ArmorRootData extends BWItemData<ArmorData> {
type: "armor"
}

export interface ArmorData extends DisplayClass, HasPointCost {
Expand Down
3 changes: 1 addition & 2 deletions module/items/belief.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { simpleBroadcast, SimpleBroadcastMessageData } from "../chat.js";
import { BWActor } from "../actors/BWActor.js";
import { ArthaEarner, BWItem, BWItemData } from "./item.js";

export class Belief extends BWItem {
data: BWItemData & { data: BeliefData };
export class Belief extends BWItem<BWItemData<BeliefData>> {

async generateChatMessage(actor: BWActor): Promise<ChatMessage | null> {
const data: SimpleBroadcastMessageData = {
Expand Down
4 changes: 1 addition & 3 deletions module/items/instinct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import { simpleBroadcast, SimpleBroadcastMessageData } from "../chat.js";
import { BWActor } from "../actors/BWActor.js";
import { ArthaEarner, BWItem, BWItemData } from "./item.js";

export class Instinct extends BWItem {
data: BWItemData & { data: InstinctData };

export class Instinct extends BWItem<BWItemData<InstinctData>> {
async generateChatMessage(actor: BWActor): Promise<ChatMessage | null> {
const data: SimpleBroadcastMessageData = {
title: this.name,
Expand Down
8 changes: 3 additions & 5 deletions module/items/lifepath.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ItemType, BWItemData, BWItem } from "./item.js";

export class Lifepath extends BWItem {
data: LifepathRootData;
export class Lifepath extends BWItem<LifepathRootData> {
get type(): ItemType {
return super.type as ItemType;
}
Expand All @@ -13,9 +12,8 @@ export class Lifepath extends BWItem {
}
}

export interface LifepathRootData extends BWItemData {
data: LifepathData;
type: ItemType;
export interface LifepathRootData extends BWItemData<LifepathData> {
type: "lifepath";
}

export interface LifepathData {
Expand Down
8 changes: 3 additions & 5 deletions module/items/possession.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { BWItem, BWItemData, DisplayClass, HasPointCost } from "./item.js";

export class Possession extends BWItem {
export class Possession extends BWItem<PossessionRootData> {
prepareData(): void {
super.prepareData();
this.data.data.cssClass = "equipment-possession";
}

data: PossessionRootData;
}

export interface PossessionRootData extends BWItemData {
data: PossessionData;
export interface PossessionRootData extends BWItemData<PossessionData> {
type: "possession";
}

export interface PossessionData extends DisplayClass, HasPointCost {
Expand Down
13 changes: 4 additions & 9 deletions module/items/property.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import { DisplayClass, ItemType, HasPointCost, BWItemData, BWItem } from "./item.js";
import { DisplayClass, HasPointCost, BWItemData, BWItem } from "./item.js";

export class Property extends BWItem {
export class Property extends BWItem<PropertyRootData> {
prepareData(): void {
super.prepareData();
this.data.data.cssClass = "equipment-property";
}

data: PropertyRootData;
get type(): ItemType {
return super.type as ItemType;
}
}

export interface PropertyRootData extends BWItemData {
export interface PropertyRootData extends BWItemData<PropertyData> {
data: PropertyData;
type: ItemType;
type: "property";
}

export interface PropertyData extends DisplayClass, HasPointCost {
Expand Down
8 changes: 3 additions & 5 deletions module/items/rangedWeapon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as helpers from "../helpers.js";
import { QualityString } from "../constants.js";
import { BWActor } from "../actors/BWActor.js";

export class RangedWeapon extends BWItem {
export class RangedWeapon extends BWItem<RangedWeaponRootData> {
prepareData(): void {
super.prepareData();
const actor = this.actor as unknown as BWActor;
Expand Down Expand Up @@ -52,12 +52,10 @@ export class RangedWeapon extends BWItem {
element.appendChild(helpers.DivOfText("" + roll, "roll-die"));
return element.outerHTML;
}

data: RangedWeaponRootData;
}

export interface RangedWeaponRootData extends BWItemData {
data: RangedWeaponData;
export interface RangedWeaponRootData extends BWItemData<RangedWeaponData> {
type: "ranged weapon"
}

export interface RangedWeaponData extends DisplayClass, HasPointCost {
Expand Down
8 changes: 3 additions & 5 deletions module/items/relationship.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BWItem, BWItemData, DisplayClass } from "./item.js";

export class Relationship extends BWItem {
export class Relationship extends BWItem<RelationshipDataRoot> {
prepareData(): void {
super.prepareData();
this.data.data.safeId = this.id;
Expand All @@ -16,12 +16,10 @@ export class Relationship extends BWItem {
this.data.data.cssClass = "relationship-neutral";
}
}

data: RelationshipDataRoot;
}

export interface RelationshipDataRoot extends BWItemData {
data: RelationshipData;
export interface RelationshipDataRoot extends BWItemData<RelationshipData> {
type: "relationship";
}

export interface RelationshipData extends DisplayClass {
Expand Down
8 changes: 3 additions & 5 deletions module/items/reputation.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { BWItem, BWItemData, DisplayClass } from "./item.js";

export class Reputation extends BWItem {
export class Reputation extends BWItem<ReputationDataRoot> {
prepareData(): void {
super.prepareData();
this.data.data.cssClass = this.data.data.infamous ? "reputation-infamous" : "reputation-famous";
}

data: ReputationDataRoot;
}

export interface ReputationDataRoot extends BWItemData {
data: ReputationData;
export interface ReputationDataRoot extends BWItemData<ReputationData> {
type: "reputation"
}

export interface ReputationData extends DisplayClass {
Expand Down
4 changes: 0 additions & 4 deletions module/items/sheets/affiliation-sheet.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { BWItemSheet } from "./bwItemSheet.js";

export class AffiliationSheet extends BWItemSheet {
static get defaultOptions(): BaseEntitySheet.Options {
return mergeObject(super.defaultOptions, {});
}

get template(): string {
return "systems/burningwheel/templates/items/affiliation.hbs";
}
Expand Down
14 changes: 4 additions & 10 deletions module/items/skill.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { skillRootSelect, SkillTypeString } from "../constants.js";
import { Ability, BWActor, TracksTests } from "../actors/BWActor.js";
import { ShadeString, StringIndexedObject, TestString, updateTestsNeeded } from "../helpers.js";
import { DisplayClass, ItemType, BWItemData, BWItem } from "./item.js";
import { DisplayClass, BWItemData, BWItem } from "./item.js";
import { DifficultyDialog } from "../dialogs/DifficultyDialog.js";
import * as helpers from "../helpers.js";

export class Skill extends BWItem {
export class Skill extends BWItem<SkillDataRoot> {
getRootSelect(): StringIndexedObject<string> {
const roots = {};
const actor = this.actor as unknown as BWActor | null;
Expand Down Expand Up @@ -57,12 +57,6 @@ export class Skill extends BWItem {
}
}

data: SkillDataRoot;

get type(): ItemType {
return super.type as ItemType;
}

canAdvance(): boolean {

const enoughRoutine = (this.data.data.routine >= (this.data.data.routineNeeded || 0 ));
Expand Down Expand Up @@ -198,8 +192,8 @@ export class Skill extends BWItem {
}
}

export interface SkillDataRoot extends BWItemData {
data: SkillData;
export interface SkillDataRoot extends BWItemData<SkillData> {
type: "skill";
}

export interface SkillData extends TracksTests, DisplayClass {
Expand Down
Loading

0 comments on commit 9613d2e

Please sign in to comment.