Skip to content

Commit

Permalink
fix(loadout): loadout should properly migrate items when they change
Browse files Browse the repository at this point in the history
  • Loading branch information
seiyria committed Mar 19, 2023
1 parent 47b6ec1 commit 3e43e23
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 53 deletions.
16 changes: 16 additions & 0 deletions src/app/services/item-creator.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,20 @@ export class ItemCreatorService {

return baseItem;
}

public migrateItem(oldItem: IGameItem): IGameItem | undefined {
if(!oldItem) {
return undefined;
}

const baseItem = this.createItem(oldItem.internalId || '', oldItem.quantity);
if(!baseItem) {
return undefined;
}

baseItem.durability = oldItem.durability;
baseItem.stats = oldItem.stats;

return baseItem;
}
}
34 changes: 6 additions & 28 deletions src/stores/charselect/charselect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,44 +98,22 @@ export class CharSelectState {
delete resources[resource];
});

const inventory = (char.inventory || []).map((oldItem) => {

// can't migrate an item with no id
if(!oldItem.internalId) {
return undefined;
}

const newItem = this.itemCreatorService.createItem(oldItem.internalId, oldItem.quantity);
if(!newItem) {
return undefined;
}

newItem.durability = oldItem.durability;
newItem.stats = oldItem.stats;

return newItem;
}, []).filter(Boolean);
const inventory = (char.inventory || [])
.map((oldItem) => this.itemCreatorService.migrateItem(oldItem))
.filter(Boolean);

const equipment = Object.keys(char.equipment).reduce((acc, key) => {
const oldItem = char.equipment[key as ItemType];
if(!oldItem) {
return { ...acc };
}

// can't migrate an item with no id
if(!oldItem.internalId) {
const migratedItem = this.itemCreatorService.migrateItem(oldItem);
if(!migratedItem) {
return { ...acc };
}

const newItem = this.itemCreatorService.createItem(oldItem.internalId, oldItem.quantity);
if(!newItem) {
return { ...acc };
}

newItem.durability = oldItem.durability;
newItem.stats = oldItem.stats;

return { ...acc, [key]: newItem };
return { ...acc, [key]: migratedItem };
}, {});

return { ...char, inventory, equipment };
Expand Down
38 changes: 13 additions & 25 deletions src/stores/combat/combat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
getPlayerCharacterReadyForCombat, handleCombatEnd, hasAnyoneWonCombat, isDead, isHealEffect
} from '../../app/helpers';
import { ContentService } from '../../app/services/content.service';
import { ItemCreatorService } from '../../app/services/item-creator.service';
import { VisualsService } from '../../app/services/visuals.service';
import {
AchievementStat,
Expand Down Expand Up @@ -42,7 +43,12 @@ import { EnterDungeon } from './dungeon.actions';
@Injectable()
export class CombatState {

constructor(private store: Store, private contentService: ContentService, private visuals: VisualsService) {
constructor(
private store: Store,
private contentService: ContentService,
private itemCreator: ItemCreatorService,
private visuals: VisualsService
) {
attachments.forEach(({ action, handler }) => {
attachAction(CombatState, action, handler);
});
Expand Down Expand Up @@ -115,31 +121,13 @@ export class CombatState {
async updateAllItems(ctx: StateContext<IGameCombat>) {
const state = ctx.getState();

const activeItems = (state.activeItems || []).map(item => {
if(!item) {
return undefined;
}

const baseItem = this.contentService.getItemByName(item.internalId || '');
if(!baseItem) {
return undefined;
}

return merge({}, baseItem, item);
}).filter(Boolean);

const activeFoods = state.activeFoods.map(item => {
if(!item) {
return undefined;
}

const baseItem = this.contentService.getItemByName(item.internalId || '');
if(!baseItem) {
return undefined;
}
const activeItems = (state.activeItems || [])
.map(item => item ? this.itemCreator.migrateItem(item) : undefined)
.filter(Boolean);

return merge({}, baseItem, item);
}).filter(Boolean);
const activeFoods = state.activeFoods
.map(item => item ? this.itemCreator.migrateItem(item) : undefined)
.filter(Boolean);

ctx.setState(patch<IGameCombat>({ activeItems, activeFoods }));
}
Expand Down

0 comments on commit 3e43e23

Please sign in to comment.