forked from OpenWF/SpaceNinjaServer
The wiki says this is also supposed to do partial fills, but didn't see that in my testing on live. Reviewed-on: OpenWF/SpaceNinjaServer#1435 Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com> Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com>
98 lines
3.7 KiB
TypeScript
98 lines
3.7 KiB
TypeScript
import {
|
|
getDojoClient,
|
|
getGuildForRequestEx,
|
|
getVaultMiscItemCount,
|
|
hasAccessToDojo,
|
|
hasGuildPermission,
|
|
processDojoBuildMaterialsGathered,
|
|
scaleRequiredCount
|
|
} from "@/src/services/guildService";
|
|
import { getInventory } from "@/src/services/inventoryService";
|
|
import { getAccountIdForRequest } from "@/src/services/loginService";
|
|
import { GuildPermission } from "@/src/types/guildTypes";
|
|
import { RequestHandler } from "express";
|
|
import { Types } from "mongoose";
|
|
import { ExportDojoRecipes } from "warframe-public-export-plus";
|
|
|
|
export const placeDecoInComponentController: RequestHandler = async (req, res) => {
|
|
const accountId = await getAccountIdForRequest(req);
|
|
const inventory = await getInventory(accountId, "GuildId LevelKeys");
|
|
const guild = await getGuildForRequestEx(req, inventory);
|
|
if (!hasAccessToDojo(inventory) || !(await hasGuildPermission(guild, accountId, GuildPermission.Decorator))) {
|
|
res.json({ DojoRequestStatus: -1 });
|
|
return;
|
|
}
|
|
const request = JSON.parse(String(req.body)) as IPlaceDecoInComponentRequest;
|
|
const component = guild.DojoComponents.id(request.ComponentId)!;
|
|
|
|
if (component.DecoCapacity === undefined) {
|
|
component.DecoCapacity = Object.values(ExportDojoRecipes.rooms).find(
|
|
x => x.resultType == component.pf
|
|
)!.decoCapacity;
|
|
}
|
|
|
|
component.Decos ??= [];
|
|
const deco =
|
|
component.Decos[
|
|
component.Decos.push({
|
|
_id: new Types.ObjectId(),
|
|
Type: request.Type,
|
|
Pos: request.Pos,
|
|
Rot: request.Rot,
|
|
Name: request.Name
|
|
}) - 1
|
|
];
|
|
|
|
const meta = Object.values(ExportDojoRecipes.decos).find(x => x.resultType == request.Type);
|
|
if (meta) {
|
|
if (meta.capacityCost) {
|
|
component.DecoCapacity -= meta.capacityCost;
|
|
}
|
|
if (meta.price == 0 && meta.ingredients.length == 0) {
|
|
deco.CompletionTime = new Date();
|
|
} else if (guild.AutoContributeFromVault && guild.VaultRegularCredits && guild.VaultMiscItems) {
|
|
if (guild.VaultRegularCredits >= scaleRequiredCount(guild.Tier, meta.price)) {
|
|
let enoughMiscItems = true;
|
|
for (const ingredient of meta.ingredients) {
|
|
if (
|
|
getVaultMiscItemCount(guild, ingredient.ItemType) <
|
|
scaleRequiredCount(guild.Tier, ingredient.ItemCount)
|
|
) {
|
|
enoughMiscItems = false;
|
|
break;
|
|
}
|
|
}
|
|
if (enoughMiscItems) {
|
|
guild.VaultRegularCredits -= meta.price;
|
|
deco.RegularCredits = meta.price;
|
|
|
|
deco.MiscItems = [];
|
|
for (const ingredient of meta.ingredients) {
|
|
guild.VaultMiscItems.find(x => x.ItemType == ingredient.ItemType)!.ItemCount -=
|
|
scaleRequiredCount(guild.Tier, ingredient.ItemCount);
|
|
deco.MiscItems.push({
|
|
ItemType: ingredient.ItemType,
|
|
ItemCount: scaleRequiredCount(guild.Tier, ingredient.ItemCount)
|
|
});
|
|
}
|
|
|
|
deco.CompletionTime = new Date(Date.now() + meta.time * 1000);
|
|
processDojoBuildMaterialsGathered(guild, meta);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
await guild.save();
|
|
res.json(await getDojoClient(guild, 0, component._id));
|
|
};
|
|
|
|
interface IPlaceDecoInComponentRequest {
|
|
ComponentId: string;
|
|
Revision: number;
|
|
Type: string;
|
|
Pos: number[];
|
|
Rot: number[];
|
|
Name?: string;
|
|
}
|