ignore contributions to a finished component
This commit is contained in:
		
							parent
							
								
									0869bbfb27
								
							
						
					
					
						commit
						fdf8d1a788
					
				@ -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.
 | 
					    // 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 request = JSON.parse(String(req.body)) as IContributeToDojoComponentRequest;
 | 
				
			||||||
    const component = guild.DojoComponents.id(request.ComponentId)!;
 | 
					    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.CompletionTime) {
 | 
				
			||||||
    if (component.RegularCredits + request.RegularCredits > scaleRequiredCount(componentMeta.price)) {
 | 
					        const componentMeta = Object.values(ExportDojoRecipes.rooms).find(x => x.resultType == component.pf)!;
 | 
				
			||||||
        request.RegularCredits = scaleRequiredCount(componentMeta.price) - component.RegularCredits;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    component.RegularCredits += request.RegularCredits;
 | 
					 | 
				
			||||||
    const inventoryChanges: IInventoryChanges = updateCurrency(inventory, request.RegularCredits, false);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    component.MiscItems ??= [];
 | 
					        component.RegularCredits ??= 0;
 | 
				
			||||||
    const miscItemChanges: IMiscItem[] = [];
 | 
					        if (component.RegularCredits + request.RegularCredits > scaleRequiredCount(componentMeta.price)) {
 | 
				
			||||||
    for (const ingredientContribution of request.IngredientContributions) {
 | 
					            request.RegularCredits = scaleRequiredCount(componentMeta.price) - component.RegularCredits;
 | 
				
			||||||
        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({
 | 
					        component.RegularCredits += request.RegularCredits;
 | 
				
			||||||
            ItemType: ingredientContribution.ItemType,
 | 
					        inventoryChanges.RegularCredits = -request.RegularCredits;
 | 
				
			||||||
            ItemCount: ingredientContribution.ItemCount * -1
 | 
					        updateCurrency(inventory, request.RegularCredits, false);
 | 
				
			||||||
        });
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    addMiscItems(inventory, miscItemChanges);
 | 
					 | 
				
			||||||
    inventoryChanges.MiscItems = miscItemChanges;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (component.RegularCredits >= scaleRequiredCount(componentMeta.price)) {
 | 
					        component.MiscItems ??= [];
 | 
				
			||||||
        let fullyFunded = true;
 | 
					        const miscItemChanges: IMiscItem[] = [];
 | 
				
			||||||
        for (const ingredient of componentMeta.ingredients) {
 | 
					        for (const ingredientContribution of request.IngredientContributions) {
 | 
				
			||||||
            const componentMiscItem = component.MiscItems.find(x => x.ItemType == ingredient.ItemType);
 | 
					            const componentMiscItem = component.MiscItems.find(x => x.ItemType == ingredientContribution.ItemType);
 | 
				
			||||||
            if (!componentMiscItem || componentMiscItem.ItemCount < scaleRequiredCount(ingredient.ItemCount)) {
 | 
					            if (componentMiscItem) {
 | 
				
			||||||
                fullyFunded = false;
 | 
					                const ingredientMeta = componentMeta.ingredients.find(
 | 
				
			||||||
                break;
 | 
					                    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) {
 | 
					        addMiscItems(inventory, miscItemChanges);
 | 
				
			||||||
            component.RegularCredits = undefined;
 | 
					        inventoryChanges.MiscItems = miscItemChanges;
 | 
				
			||||||
            component.MiscItems = undefined;
 | 
					
 | 
				
			||||||
            component.CompletionTime = new Date(Date.now() + componentMeta.time * 1000);
 | 
					        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);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user