From fdf8d1a788a468ec8cfdc6eec95528b83c745ac1 Mon Sep 17 00:00:00 2001 From: Sainan Date: Tue, 4 Mar 2025 12:21:36 +0100 Subject: [PATCH] ignore contributions to a finished component --- .../contributeToDojoComponentController.ts | 89 ++++++++++--------- 1 file changed, 48 insertions(+), 41 deletions(-) diff --git a/src/controllers/api/contributeToDojoComponentController.ts b/src/controllers/api/contributeToDojoComponentController.ts index 827475b0..7496ea0b 100644 --- a/src/controllers/api/contributeToDojoComponentController.ts +++ b/src/controllers/api/contributeToDojoComponentController.ts @@ -13,53 +13,60 @@ export const contributeToDojoComponentController: RequestHandler = async (req, r // Any clan member should have permission to contribute although notably permission is denied if they have not crafted the dojo key and were simply invited in. const request = JSON.parse(String(req.body)) as IContributeToDojoComponentRequest; const component = guild.DojoComponents.id(request.ComponentId)!; - const componentMeta = Object.values(ExportDojoRecipes.rooms).find(x => x.resultType == component.pf)!; + const inventoryChanges: IInventoryChanges = {}; - component.RegularCredits ??= 0; - if (component.RegularCredits + request.RegularCredits > scaleRequiredCount(componentMeta.price)) { - request.RegularCredits = scaleRequiredCount(componentMeta.price) - component.RegularCredits; - } - component.RegularCredits += request.RegularCredits; - const inventoryChanges: IInventoryChanges = updateCurrency(inventory, request.RegularCredits, false); + if (!component.CompletionTime) { + const componentMeta = Object.values(ExportDojoRecipes.rooms).find(x => x.resultType == component.pf)!; - component.MiscItems ??= []; - const miscItemChanges: IMiscItem[] = []; - for (const ingredientContribution of request.IngredientContributions) { - const componentMiscItem = component.MiscItems.find(x => x.ItemType == ingredientContribution.ItemType); - if (componentMiscItem) { - const ingredientMeta = componentMeta.ingredients.find(x => x.ItemType == ingredientContribution.ItemType)!; - if ( - componentMiscItem.ItemCount + ingredientContribution.ItemCount > - scaleRequiredCount(ingredientMeta.ItemCount) - ) { - ingredientContribution.ItemCount = - scaleRequiredCount(ingredientMeta.ItemCount) - componentMiscItem.ItemCount; - } - componentMiscItem.ItemCount += ingredientContribution.ItemCount; - } else { - component.MiscItems.push(ingredientContribution); + component.RegularCredits ??= 0; + if (component.RegularCredits + request.RegularCredits > scaleRequiredCount(componentMeta.price)) { + request.RegularCredits = scaleRequiredCount(componentMeta.price) - component.RegularCredits; } - miscItemChanges.push({ - ItemType: ingredientContribution.ItemType, - ItemCount: ingredientContribution.ItemCount * -1 - }); - } - addMiscItems(inventory, miscItemChanges); - inventoryChanges.MiscItems = miscItemChanges; + component.RegularCredits += request.RegularCredits; + inventoryChanges.RegularCredits = -request.RegularCredits; + updateCurrency(inventory, request.RegularCredits, false); - if (component.RegularCredits >= scaleRequiredCount(componentMeta.price)) { - let fullyFunded = true; - for (const ingredient of componentMeta.ingredients) { - const componentMiscItem = component.MiscItems.find(x => x.ItemType == ingredient.ItemType); - if (!componentMiscItem || componentMiscItem.ItemCount < scaleRequiredCount(ingredient.ItemCount)) { - fullyFunded = false; - break; + component.MiscItems ??= []; + const miscItemChanges: IMiscItem[] = []; + for (const ingredientContribution of request.IngredientContributions) { + const componentMiscItem = component.MiscItems.find(x => x.ItemType == ingredientContribution.ItemType); + if (componentMiscItem) { + const ingredientMeta = componentMeta.ingredients.find( + x => x.ItemType == ingredientContribution.ItemType + )!; + if ( + componentMiscItem.ItemCount + ingredientContribution.ItemCount > + scaleRequiredCount(ingredientMeta.ItemCount) + ) { + ingredientContribution.ItemCount = + scaleRequiredCount(ingredientMeta.ItemCount) - componentMiscItem.ItemCount; + } + componentMiscItem.ItemCount += ingredientContribution.ItemCount; + } else { + component.MiscItems.push(ingredientContribution); } + miscItemChanges.push({ + ItemType: ingredientContribution.ItemType, + ItemCount: ingredientContribution.ItemCount * -1 + }); } - if (fullyFunded) { - component.RegularCredits = undefined; - component.MiscItems = undefined; - component.CompletionTime = new Date(Date.now() + componentMeta.time * 1000); + addMiscItems(inventory, miscItemChanges); + inventoryChanges.MiscItems = miscItemChanges; + + if (component.RegularCredits >= scaleRequiredCount(componentMeta.price)) { + let fullyFunded = true; + for (const ingredient of componentMeta.ingredients) { + const componentMiscItem = component.MiscItems.find(x => x.ItemType == ingredient.ItemType); + if (!componentMiscItem || componentMiscItem.ItemCount < scaleRequiredCount(ingredient.ItemCount)) { + fullyFunded = false; + break; + } + } + if (fullyFunded) { + component.RegularCredits = undefined; + component.MiscItems = undefined; + component.CompletionTime = new Date(Date.now() + componentMeta.time * 1000); + } } }