2024-05-16 01:34:38 +02:00
|
|
|
import { RequestHandler } from "express";
|
2025-03-14 07:09:28 -07:00
|
|
|
import {
|
|
|
|
getGuildForRequestEx,
|
|
|
|
getGuildVault,
|
|
|
|
hasAccessToDojo,
|
|
|
|
hasGuildPermission,
|
2025-03-30 09:58:51 -07:00
|
|
|
processFundedGuildTechProject,
|
|
|
|
processGuildTechProjectContributionsUpdate,
|
2025-03-16 04:32:11 -07:00
|
|
|
removePigmentsFromGuildMembers,
|
2025-03-30 09:58:51 -07:00
|
|
|
scaleRequiredCount,
|
|
|
|
setGuildTechLogState
|
2025-03-14 07:09:28 -07:00
|
|
|
} from "@/src/services/guildService";
|
2025-03-30 09:58:51 -07:00
|
|
|
import { ExportDojoRecipes } from "warframe-public-export-plus";
|
2025-01-03 09:06:50 +01:00
|
|
|
import { getAccountIdForRequest } from "@/src/services/loginService";
|
2025-03-11 07:56:18 -07:00
|
|
|
import {
|
|
|
|
addItem,
|
|
|
|
addMiscItems,
|
|
|
|
addRecipes,
|
|
|
|
combineInventoryChanges,
|
|
|
|
getInventory,
|
|
|
|
updateCurrency
|
|
|
|
} from "@/src/services/inventoryService";
|
2025-01-03 09:06:50 +01:00
|
|
|
import { IMiscItem } from "@/src/types/inventoryTypes/inventoryTypes";
|
|
|
|
import { IInventoryChanges } from "@/src/types/purchaseTypes";
|
2025-02-28 12:35:14 -08:00
|
|
|
import { config } from "@/src/services/configService";
|
2025-03-30 09:58:51 -07:00
|
|
|
import { GuildPermission, ITechProjectClient } from "@/src/types/guildTypes";
|
|
|
|
import { GuildMember } from "@/src/models/guildModel";
|
2025-03-12 07:59:20 -07:00
|
|
|
import { toMongoDate } from "@/src/helpers/inventoryHelpers";
|
2025-03-20 05:36:09 -07:00
|
|
|
import { logger } from "@/src/utils/logger";
|
2024-05-16 01:34:38 +02:00
|
|
|
|
2025-01-03 09:06:50 +01:00
|
|
|
export const guildTechController: RequestHandler = async (req, res) => {
|
|
|
|
const accountId = await getAccountIdForRequest(req);
|
|
|
|
const inventory = await getInventory(accountId);
|
|
|
|
const guild = await getGuildForRequestEx(req, inventory);
|
|
|
|
const data = JSON.parse(String(req.body)) as TGuildTechRequest;
|
2025-03-16 04:32:11 -07:00
|
|
|
if (data.Action == "Sync") {
|
2025-03-12 07:59:20 -07:00
|
|
|
let needSave = false;
|
|
|
|
const techProjects: ITechProjectClient[] = [];
|
|
|
|
if (guild.TechProjects) {
|
|
|
|
for (const project of guild.TechProjects) {
|
|
|
|
const techProject: ITechProjectClient = {
|
|
|
|
ItemType: project.ItemType,
|
|
|
|
ReqCredits: project.ReqCredits,
|
|
|
|
ReqItems: project.ReqItems,
|
|
|
|
State: project.State
|
|
|
|
};
|
|
|
|
if (project.CompletionDate) {
|
|
|
|
techProject.CompletionDate = toMongoDate(project.CompletionDate);
|
|
|
|
if (Date.now() >= project.CompletionDate.getTime()) {
|
2025-03-30 09:58:51 -07:00
|
|
|
needSave ||= setGuildTechLogState(guild, project.ItemType, 4, project.CompletionDate);
|
2025-03-12 07:59:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
techProjects.push(techProject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (needSave) {
|
|
|
|
await guild.save();
|
|
|
|
}
|
|
|
|
res.json({ TechProjects: techProjects });
|
2025-03-16 04:32:11 -07:00
|
|
|
} else if (data.Action == "Start") {
|
|
|
|
if (!hasAccessToDojo(inventory) || !(await hasGuildPermission(guild, accountId, GuildPermission.Tech))) {
|
2025-03-14 07:09:28 -07:00
|
|
|
res.status(400).send("-1").end();
|
|
|
|
return;
|
|
|
|
}
|
2025-03-16 04:32:11 -07:00
|
|
|
const recipe = ExportDojoRecipes.research[data.RecipeType];
|
2025-01-03 09:06:50 +01:00
|
|
|
guild.TechProjects ??= [];
|
|
|
|
if (!guild.TechProjects.find(x => x.ItemType == data.RecipeType)) {
|
2025-02-28 12:35:14 -08:00
|
|
|
const techProject =
|
|
|
|
guild.TechProjects[
|
|
|
|
guild.TechProjects.push({
|
2025-03-16 04:32:11 -07:00
|
|
|
ItemType: data.RecipeType,
|
2025-03-30 09:58:51 -07:00
|
|
|
ReqCredits: config.noDojoResearchCosts ? 0 : scaleRequiredCount(guild.Tier, recipe.price),
|
2025-02-28 12:35:14 -08:00
|
|
|
ReqItems: recipe.ingredients.map(x => ({
|
|
|
|
ItemType: x.ItemType,
|
2025-03-30 09:58:51 -07:00
|
|
|
ItemCount: config.noDojoResearchCosts ? 0 : scaleRequiredCount(guild.Tier, x.ItemCount)
|
2025-02-28 12:35:14 -08:00
|
|
|
})),
|
|
|
|
State: 0
|
|
|
|
}) - 1
|
|
|
|
];
|
2025-03-30 09:58:51 -07:00
|
|
|
setGuildTechLogState(guild, techProject.ItemType, 5);
|
2025-02-28 12:35:14 -08:00
|
|
|
if (config.noDojoResearchCosts) {
|
2025-03-30 09:58:51 -07:00
|
|
|
processFundedGuildTechProject(guild, techProject, recipe);
|
2025-03-16 04:32:11 -07:00
|
|
|
} else {
|
|
|
|
if (data.RecipeType.substring(0, 39) == "/Lotus/Types/Items/Research/DojoColors/") {
|
|
|
|
guild.ActiveDojoColorResearch = data.RecipeType;
|
|
|
|
}
|
2025-02-28 12:35:14 -08:00
|
|
|
}
|
2025-01-03 09:06:50 +01:00
|
|
|
}
|
|
|
|
await guild.save();
|
|
|
|
res.end();
|
2025-03-16 04:32:11 -07:00
|
|
|
} else if (data.Action == "Contribute") {
|
2025-03-14 07:09:28 -07:00
|
|
|
if (!hasAccessToDojo(inventory)) {
|
|
|
|
res.status(400).send("-1").end();
|
|
|
|
return;
|
|
|
|
}
|
2025-03-27 03:33:08 -07:00
|
|
|
|
|
|
|
const guildMember = (await GuildMember.findOne(
|
|
|
|
{ accountId, guildId: guild._id },
|
|
|
|
"RegularCreditsContributed MiscItemsContributed"
|
|
|
|
))!;
|
|
|
|
|
2025-03-16 04:32:11 -07:00
|
|
|
const contributions = data;
|
2025-01-03 09:06:50 +01:00
|
|
|
const techProject = guild.TechProjects!.find(x => x.ItemType == contributions.RecipeType)!;
|
2025-03-06 21:24:25 -08:00
|
|
|
|
|
|
|
if (contributions.VaultCredits) {
|
|
|
|
if (contributions.VaultCredits > techProject.ReqCredits) {
|
|
|
|
contributions.VaultCredits = techProject.ReqCredits;
|
|
|
|
}
|
|
|
|
techProject.ReqCredits -= contributions.VaultCredits;
|
|
|
|
guild.VaultRegularCredits! -= contributions.VaultCredits;
|
|
|
|
}
|
|
|
|
|
2025-01-03 09:06:50 +01:00
|
|
|
if (contributions.RegularCredits > techProject.ReqCredits) {
|
|
|
|
contributions.RegularCredits = techProject.ReqCredits;
|
|
|
|
}
|
|
|
|
techProject.ReqCredits -= contributions.RegularCredits;
|
2025-03-06 21:24:25 -08:00
|
|
|
|
2025-03-27 03:33:08 -07:00
|
|
|
guildMember.RegularCreditsContributed ??= 0;
|
|
|
|
guildMember.RegularCreditsContributed += contributions.RegularCredits;
|
|
|
|
|
2025-03-06 21:24:25 -08:00
|
|
|
if (contributions.VaultMiscItems.length) {
|
|
|
|
for (const miscItem of contributions.VaultMiscItems) {
|
|
|
|
const reqItem = techProject.ReqItems.find(x => x.ItemType == miscItem.ItemType);
|
|
|
|
if (reqItem) {
|
|
|
|
if (miscItem.ItemCount > reqItem.ItemCount) {
|
|
|
|
miscItem.ItemCount = reqItem.ItemCount;
|
|
|
|
}
|
|
|
|
reqItem.ItemCount -= miscItem.ItemCount;
|
|
|
|
|
|
|
|
const vaultMiscItem = guild.VaultMiscItems!.find(x => x.ItemType == miscItem.ItemType)!;
|
|
|
|
vaultMiscItem.ItemCount -= miscItem.ItemCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-03 09:06:50 +01:00
|
|
|
const miscItemChanges = [];
|
|
|
|
for (const miscItem of contributions.MiscItems) {
|
|
|
|
const reqItem = techProject.ReqItems.find(x => x.ItemType == miscItem.ItemType);
|
|
|
|
if (reqItem) {
|
|
|
|
if (miscItem.ItemCount > reqItem.ItemCount) {
|
|
|
|
miscItem.ItemCount = reqItem.ItemCount;
|
|
|
|
}
|
|
|
|
reqItem.ItemCount -= miscItem.ItemCount;
|
|
|
|
miscItemChanges.push({
|
|
|
|
ItemType: miscItem.ItemType,
|
|
|
|
ItemCount: miscItem.ItemCount * -1
|
|
|
|
});
|
2025-03-27 03:33:08 -07:00
|
|
|
|
|
|
|
guildMember.MiscItemsContributed ??= [];
|
|
|
|
guildMember.MiscItemsContributed.push(miscItem);
|
2025-01-03 09:06:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
addMiscItems(inventory, miscItemChanges);
|
2025-02-26 15:41:07 -08:00
|
|
|
const inventoryChanges: IInventoryChanges = updateCurrency(inventory, contributions.RegularCredits, false);
|
|
|
|
inventoryChanges.MiscItems = miscItemChanges;
|
2025-01-03 09:06:50 +01:00
|
|
|
|
2025-03-30 09:58:51 -07:00
|
|
|
// Check if research is fully funded now.
|
|
|
|
await processGuildTechProjectContributionsUpdate(guild, techProject);
|
2025-01-03 09:06:50 +01:00
|
|
|
|
|
|
|
await guild.save();
|
|
|
|
await inventory.save();
|
2025-03-27 03:33:08 -07:00
|
|
|
await guildMember.save();
|
2025-01-03 09:06:50 +01:00
|
|
|
res.json({
|
2025-03-06 21:24:25 -08:00
|
|
|
InventoryChanges: inventoryChanges,
|
|
|
|
Vault: getGuildVault(guild)
|
2025-01-03 09:06:50 +01:00
|
|
|
});
|
2025-03-16 04:32:11 -07:00
|
|
|
} else if (data.Action.split(",")[0] == "Buy") {
|
2025-03-14 07:09:28 -07:00
|
|
|
if (!hasAccessToDojo(inventory) || !(await hasGuildPermission(guild, accountId, GuildPermission.Fabricator))) {
|
|
|
|
res.status(400).send("-1").end();
|
|
|
|
return;
|
|
|
|
}
|
2025-03-16 04:32:11 -07:00
|
|
|
const purchase = data as IGuildTechBuyRequest;
|
2025-01-04 00:25:23 +01:00
|
|
|
const quantity = parseInt(data.Action.split(",")[1]);
|
|
|
|
const recipeChanges = [
|
|
|
|
{
|
|
|
|
ItemType: purchase.RecipeType,
|
|
|
|
ItemCount: quantity
|
|
|
|
}
|
|
|
|
];
|
|
|
|
addRecipes(inventory, recipeChanges);
|
|
|
|
const currencyChanges = updateCurrency(
|
|
|
|
inventory,
|
|
|
|
ExportDojoRecipes.research[purchase.RecipeType].replicatePrice,
|
|
|
|
false
|
|
|
|
);
|
|
|
|
await inventory.save();
|
|
|
|
// Not a mistake: This response uses `inventoryChanges` instead of `InventoryChanges`.
|
|
|
|
res.json({
|
|
|
|
inventoryChanges: {
|
|
|
|
...currencyChanges,
|
|
|
|
Recipes: recipeChanges
|
|
|
|
}
|
|
|
|
});
|
2025-03-16 04:32:11 -07:00
|
|
|
} else if (data.Action == "Fabricate") {
|
2025-03-14 07:09:28 -07:00
|
|
|
if (!hasAccessToDojo(inventory) || !(await hasGuildPermission(guild, accountId, GuildPermission.Fabricator))) {
|
|
|
|
res.status(400).send("-1").end();
|
|
|
|
return;
|
|
|
|
}
|
2025-03-16 04:32:11 -07:00
|
|
|
const recipe = ExportDojoRecipes.fabrications[data.RecipeType];
|
2025-03-11 07:56:18 -07:00
|
|
|
const inventoryChanges: IInventoryChanges = updateCurrency(inventory, recipe.price, false);
|
|
|
|
inventoryChanges.MiscItems = recipe.ingredients.map(x => ({
|
|
|
|
ItemType: x.ItemType,
|
|
|
|
ItemCount: x.ItemCount * -1
|
|
|
|
}));
|
|
|
|
addMiscItems(inventory, inventoryChanges.MiscItems);
|
2025-03-23 08:26:46 -07:00
|
|
|
combineInventoryChanges(inventoryChanges, await addItem(inventory, recipe.resultType));
|
2025-03-11 07:56:18 -07:00
|
|
|
await inventory.save();
|
|
|
|
// Not a mistake: This response uses `inventoryChanges` instead of `InventoryChanges`.
|
|
|
|
res.json({ inventoryChanges: inventoryChanges });
|
2025-03-16 04:32:11 -07:00
|
|
|
} else if (data.Action == "Pause") {
|
|
|
|
if (!hasAccessToDojo(inventory) || !(await hasGuildPermission(guild, accountId, GuildPermission.Tech))) {
|
|
|
|
res.status(400).send("-1").end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const project = guild.TechProjects!.find(x => x.ItemType == data.RecipeType)!;
|
|
|
|
project.State = -2;
|
|
|
|
guild.ActiveDojoColorResearch = "";
|
|
|
|
await guild.save();
|
|
|
|
await removePigmentsFromGuildMembers(guild._id);
|
|
|
|
res.end();
|
|
|
|
} else if (data.Action == "Unpause") {
|
|
|
|
if (!hasAccessToDojo(inventory) || !(await hasGuildPermission(guild, accountId, GuildPermission.Tech))) {
|
|
|
|
res.status(400).send("-1").end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const project = guild.TechProjects!.find(x => x.ItemType == data.RecipeType)!;
|
|
|
|
project.State = 0;
|
|
|
|
guild.ActiveDojoColorResearch = data.RecipeType;
|
|
|
|
await guild.save();
|
|
|
|
res.end();
|
2025-01-03 09:06:50 +01:00
|
|
|
} else {
|
2025-03-20 05:36:09 -07:00
|
|
|
logger.debug(`data provided to ${req.path}: ${String(req.body)}`);
|
2025-01-03 09:06:50 +01:00
|
|
|
throw new Error(`unknown guildTech action: ${data.Action}`);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2025-03-11 07:56:18 -07:00
|
|
|
type TGuildTechRequest =
|
2025-03-16 04:32:11 -07:00
|
|
|
| { Action: "Sync" | "SomethingElseThatWeMightNotKnowAbout" }
|
|
|
|
| IGuildTechBasicRequest
|
|
|
|
| IGuildTechContributeRequest;
|
2025-01-03 09:06:50 +01:00
|
|
|
|
2025-03-16 04:32:11 -07:00
|
|
|
interface IGuildTechBasicRequest {
|
|
|
|
Action: "Start" | "Fabricate" | "Pause" | "Unpause";
|
2025-01-03 09:06:50 +01:00
|
|
|
Mode: "Guild";
|
|
|
|
RecipeType: string;
|
|
|
|
}
|
|
|
|
|
2025-03-16 04:32:11 -07:00
|
|
|
interface IGuildTechBuyRequest {
|
|
|
|
Action: string;
|
|
|
|
Mode: "Guild";
|
|
|
|
RecipeType: string;
|
|
|
|
}
|
2025-01-04 00:25:23 +01:00
|
|
|
|
2025-03-16 04:32:11 -07:00
|
|
|
interface IGuildTechContributeRequest {
|
|
|
|
Action: "Contribute";
|
2025-01-03 09:06:50 +01:00
|
|
|
ResearchId: "";
|
|
|
|
RecipeType: string;
|
|
|
|
RegularCredits: number;
|
|
|
|
MiscItems: IMiscItem[];
|
|
|
|
VaultCredits: number;
|
|
|
|
VaultMiscItems: IMiscItem[];
|
|
|
|
}
|