forked from OpenWF/SpaceNinjaServer
feat: KIM gifts (#1538)
Reviewed-on: OpenWF/SpaceNinjaServer#1538 Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com> Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com>
This commit is contained in:
parent
d3620c00e2
commit
a0b61bec12
@ -1,4 +1,4 @@
|
|||||||
import { addEmailItem, getInventory } from "@/src/services/inventoryService";
|
import { addEmailItem, getInventory, updateCurrency } from "@/src/services/inventoryService";
|
||||||
import { getAccountIdForRequest } from "@/src/services/loginService";
|
import { getAccountIdForRequest } from "@/src/services/loginService";
|
||||||
import { ICompletedDialogue } from "@/src/types/inventoryTypes/inventoryTypes";
|
import { ICompletedDialogue } from "@/src/types/inventoryTypes/inventoryTypes";
|
||||||
import { IInventoryChanges } from "@/src/types/purchaseTypes";
|
import { IInventoryChanges } from "@/src/types/purchaseTypes";
|
||||||
@ -9,7 +9,7 @@ export const saveDialogueController: RequestHandler = async (req, res) => {
|
|||||||
const accountId = await getAccountIdForRequest(req);
|
const accountId = await getAccountIdForRequest(req);
|
||||||
const request = JSON.parse(String(req.body)) as SaveDialogueRequest;
|
const request = JSON.parse(String(req.body)) as SaveDialogueRequest;
|
||||||
if ("YearIteration" in request) {
|
if ("YearIteration" in request) {
|
||||||
const inventory = await getInventory(accountId);
|
const inventory = await getInventory(accountId, "DialogueHistory");
|
||||||
if (inventory.DialogueHistory) {
|
if (inventory.DialogueHistory) {
|
||||||
inventory.DialogueHistory.YearIteration = request.YearIteration;
|
inventory.DialogueHistory.YearIteration = request.YearIteration;
|
||||||
} else {
|
} else {
|
||||||
@ -22,10 +22,11 @@ export const saveDialogueController: RequestHandler = async (req, res) => {
|
|||||||
if (!inventory.DialogueHistory) {
|
if (!inventory.DialogueHistory) {
|
||||||
throw new Error("bad inventory state");
|
throw new Error("bad inventory state");
|
||||||
}
|
}
|
||||||
if (request.OtherDialogueInfos.length != 0) {
|
if (request.OtherDialogueInfos.length != 0 || !(request.Data || request.Gift)) {
|
||||||
logger.error(`saveDialogue request not fully handled: ${String(req.body)}`);
|
logger.error(`saveDialogue request not fully handled: ${String(req.body)}`);
|
||||||
}
|
}
|
||||||
const inventoryChanges: IInventoryChanges = {};
|
const inventoryChanges: IInventoryChanges = {};
|
||||||
|
const tomorrowAt0Utc = (Math.trunc(Date.now() / 86400_000) + 1) * 86400_000;
|
||||||
inventory.DialogueHistory.Dialogues ??= [];
|
inventory.DialogueHistory.Dialogues ??= [];
|
||||||
let dialogue = inventory.DialogueHistory.Dialogues.find(x => x.DialogueName == request.DialogueName);
|
let dialogue = inventory.DialogueHistory.Dialogues.find(x => x.DialogueName == request.DialogueName);
|
||||||
if (!dialogue) {
|
if (!dialogue) {
|
||||||
@ -48,6 +49,7 @@ export const saveDialogueController: RequestHandler = async (req, res) => {
|
|||||||
}
|
}
|
||||||
dialogue.Rank = request.Rank;
|
dialogue.Rank = request.Rank;
|
||||||
dialogue.Chemistry = request.Chemistry;
|
dialogue.Chemistry = request.Chemistry;
|
||||||
|
if (request.Data) {
|
||||||
dialogue.QueuedDialogues = request.QueuedDialogues;
|
dialogue.QueuedDialogues = request.QueuedDialogues;
|
||||||
for (const bool of request.Booleans) {
|
for (const bool of request.Booleans) {
|
||||||
dialogue.Booleans.push(bool);
|
dialogue.Booleans.push(bool);
|
||||||
@ -66,13 +68,27 @@ export const saveDialogueController: RequestHandler = async (req, res) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
dialogue.Completed.push(request.Data);
|
dialogue.Completed.push(request.Data);
|
||||||
const tomorrowAt0Utc = (Math.trunc(Date.now() / (86400 * 1000)) + 1) * 86400 * 1000;
|
|
||||||
dialogue.AvailableDate = new Date(tomorrowAt0Utc);
|
dialogue.AvailableDate = new Date(tomorrowAt0Utc);
|
||||||
await inventory.save();
|
await inventory.save();
|
||||||
res.json({
|
res.json({
|
||||||
InventoryChanges: inventoryChanges,
|
InventoryChanges: inventoryChanges,
|
||||||
AvailableDate: { $date: { $numberLong: tomorrowAt0Utc.toString() } }
|
AvailableDate: { $date: { $numberLong: tomorrowAt0Utc.toString() } }
|
||||||
});
|
});
|
||||||
|
} else if (request.Gift) {
|
||||||
|
const inventoryChanges = updateCurrency(inventory, request.Gift.Cost, false);
|
||||||
|
const gift = dialogue.Gifts.find(x => x.Item == request.Gift!.Item);
|
||||||
|
if (gift) {
|
||||||
|
gift.GiftedQuantity += 1;
|
||||||
|
} else {
|
||||||
|
dialogue.Gifts.push({ Item: request.Gift.Item, GiftedQuantity: 1 });
|
||||||
|
}
|
||||||
|
dialogue.AvailableGiftDate = new Date(tomorrowAt0Utc);
|
||||||
|
await inventory.save();
|
||||||
|
res.json({
|
||||||
|
InventoryChanges: inventoryChanges,
|
||||||
|
AvailableGiftDate: { $date: { $numberLong: tomorrowAt0Utc.toString() } }
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -88,9 +104,15 @@ interface SaveCompletedDialogueRequest {
|
|||||||
Chemistry: number;
|
Chemistry: number;
|
||||||
CompletionType: number;
|
CompletionType: number;
|
||||||
QueuedDialogues: string[];
|
QueuedDialogues: string[];
|
||||||
|
Gift?: {
|
||||||
|
Item: string;
|
||||||
|
GainedChemistry: number;
|
||||||
|
Cost: number;
|
||||||
|
GiftedQuantity: number;
|
||||||
|
};
|
||||||
Booleans: string[];
|
Booleans: string[];
|
||||||
ResetBooleans: string[];
|
ResetBooleans: string[];
|
||||||
Data: ICompletedDialogue;
|
Data?: ICompletedDialogue;
|
||||||
OtherDialogueInfos: IOtherDialogueInfo[]; // unsure
|
OtherDialogueInfos: IOtherDialogueInfo[]; // unsure
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user