Skip to content

Commit

Permalink
fix(worker): workers will now correctly craft items
Browse files Browse the repository at this point in the history
  • Loading branch information
seiyria committed Mar 30, 2023
1 parent 60f2e19 commit f677838
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/app/helpers/refined.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export function canCraftRecipe(resources: Record<string, number>, recipe: IGameR
);
}

export function getRecipeIngredientCosts(recipe: IGameRecipe, amount = 1): Record<string, number> {
export function getRecipeResourceCosts(recipe: IGameRecipe, amount = 1): Record<string, number> {
const recipeIngredients = Object.keys(recipe.ingredients).filter(ingredient => !(recipe.preserve || []).includes(ingredient));
const recipeCosts = recipeIngredients.map(ingredient => -recipe.ingredients[ingredient] * amount);

Expand All @@ -97,7 +97,7 @@ export function startRefineJob(
refundItems: IGameItem[] = []
) {

const recipeCosts = getRecipeIngredientCosts(job, quantity);
const recipeCosts = getRecipeResourceCosts(job, quantity);
const doesRecipeCostAnything = Object.keys(recipeCosts).length > 0;

if(doesRecipeCostAnything) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ <h2>{{ workerName(worker.nameId) }}</h2>
<p>
Currently doing {{ worker.tradeskill }} and making {{ worker.recipe.result }}...
<span *ngIf="worker.currentTick < 0">cooling down...</span>
<span *ngIf="worker.currentTick >= 0">{{ worker.currentTick / worker.recipe.craftTime * 100 | number:'1.0-0' }}%</span>
<span *ngIf="worker.currentTick === 0">100%</span>
<span *ngIf="worker.currentTick > 0">{{ worker.currentTick / worker.recipe.craftTime * 100 | number:'1.0-0' }}%</span>
</p>
</ion-label>

Expand Down
1 change: 0 additions & 1 deletion src/stores/mercantile/mercantile.functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ export function sellItem(ctx: StateContext<IGameMercantile>, { item }: SellItem)

const maxSize = maxShopCounterSize(state.shop.saleCounterLevel);
if(state.shop.forSale.length >= maxSize) {
gainCoins(ctx, { amount: itemValue(item, shopRegisterMultiplier(state.shop.saleBonusLevel)) });
return;
}

Expand Down
38 changes: 32 additions & 6 deletions src/stores/workers/workers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { Injectable } from '@angular/core';
import { Action, Selector, State, StateContext, Store } from '@ngxs/store';
import { patch } from '@ngxs/store/operators';
import { attachAction } from '@seiyria/ngxs-attach-action';
import { random, sample } from 'lodash';
import { canCraftRecipe, getRecipeIngredientCosts, getResourceRewardsForLocation } from '../../app/helpers';
import { IGameRecipe, IGameWorkers, Rarity } from '../../interfaces';
import { GainResources, WorkerCreateItem } from '../charselect/charselect.actions';
import { merge, random, sample } from 'lodash';
import { canCraftRecipe, getRecipeResourceCosts, getResourceRewardsForLocation } from '../../app/helpers';
import { IGameItem, IGameRecipe, IGameWorkers, Rarity } from '../../interfaces';
import { GainResources, RemoveItemFromInventory, WorkerCreateItem } from '../charselect/charselect.actions';
import { TickTimer } from '../game/game.actions';
import { SellItem, SpendCoins } from '../mercantile/mercantile.actions';
import { attachments } from './workers.attachments';
Expand Down Expand Up @@ -106,6 +106,15 @@ export class WorkersState {
}

const allResources = currentCharacter.resources;
const itemList = currentCharacter.inventory;

const allItems: Record<string, number> = {};
itemList.forEach((item: IGameItem) => {
allItems[item.name] = allItems[item.name] ?? 0;
allItems[item.name]++;
});

const allResourcesAndItems = merge({}, allResources, allItems);

// handle upkeep
const workerUpkeepCost = upkeepCost(totalWorkers);
Expand Down Expand Up @@ -177,13 +186,30 @@ export class WorkersState {

// if we don't have the resources, we do not craft
if(alloc.currentTick === 0) {
if(!canCraftRecipe(allResources, alloc.recipe, 1)) {
if(!canCraftRecipe(allResourcesAndItems, alloc.recipe, 1)) {
return alloc;
}

// if we do have the resources, we take them
const resourcesRequired = getRecipeIngredientCosts(alloc.recipe, 1);
const resourcesRequired = getRecipeResourceCosts(alloc.recipe, 1);
ctx.dispatch(new GainResources(resourcesRequired));

const takeItemQuantities: Record<string, number> = {};

Object.keys(alloc.recipe.ingredients)
.filter(key => allItems[key])
.forEach(itemKey => {
takeItemQuantities[itemKey] = alloc.recipe.ingredients[itemKey];
});

const allItemRefsTaken = Object.keys(takeItemQuantities).map(itemKey => {
const validItems = itemList.filter((item: IGameItem) => item.name === itemKey);
return validItems.slice(0, takeItemQuantities[itemKey]);
}).flat();

const allActions = allItemRefsTaken.map(itemRef => [new RemoveItemFromInventory(itemRef)]).flat();

ctx.dispatch(allActions);
}

alloc.currentTick += ticks * workerTimerMultiplier(1);
Expand Down

0 comments on commit f677838

Please sign in to comment.