返回提交历史
Modified
src/controllers/api/saveDialogueController.ts
+48
-26
XFEstudio/SpaceNinjaServer
feat: KIM gifts (#1538)
Reviewed-on: https://onlyg.it/OpenWF/SpaceNinjaServer/pulls/1538 Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com> Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com>
a0b61bec
代码差异
1 个文件
+48
-26
@@ -1,4 +1,4 @@
1
import { addEmailItem, getInventory } from "@/src/services/inventoryService";
1
import { addEmailItem, getInventory, updateCurrency } from "@/src/services/inventoryService";
2
2
import { getAccountIdForRequest } from "@/src/services/loginService";
3
3
import { ICompletedDialogue } from "@/src/types/inventoryTypes/inventoryTypes";
4
4
import { IInventoryChanges } from "@/src/types/purchaseTypes";
@@ -9,7 +9,7 @@ export const saveDialogueController: RequestHandler = async (req, res) => {
9
9
const accountId = await getAccountIdForRequest(req);
10
10
const request = JSON.parse(String(req.body)) as SaveDialogueRequest;
11
11
if ("YearIteration" in request) {
12
const inventory = await getInventory(accountId);
12
const inventory = await getInventory(accountId, "DialogueHistory");
13
13
if (inventory.DialogueHistory) {
14
14
inventory.DialogueHistory.YearIteration = request.YearIteration;
15
15
} else {
@@ -22,10 +22,11 @@ export const saveDialogueController: RequestHandler = async (req, res) => {
22
22
if (!inventory.DialogueHistory) {
23
23
throw new Error("bad inventory state");
24
24
}
25
if (request.OtherDialogueInfos.length != 0) {
25
if (request.OtherDialogueInfos.length != 0 || !(request.Data || request.Gift)) {
26
26
logger.error(`saveDialogue request not fully handled: ${String(req.body)}`);
27
27
}
28
28
const inventoryChanges: IInventoryChanges = {};
29
const tomorrowAt0Utc = (Math.trunc(Date.now() / 86400_000) + 1) * 86400_000;
29
30
inventory.DialogueHistory.Dialogues ??= [];
30
31
let dialogue = inventory.DialogueHistory.Dialogues.find(x => x.DialogueName == request.DialogueName);
31
32
if (!dialogue) {
@@ -48,31 +49,46 @@ export const saveDialogueController: RequestHandler = async (req, res) => {
48
49
}
49
50
dialogue.Rank = request.Rank;
50
51
dialogue.Chemistry = request.Chemistry;
51
dialogue.QueuedDialogues = request.QueuedDialogues;
52
for (const bool of request.Booleans) {
53
dialogue.Booleans.push(bool);
54
if (bool == "LizzieShawzin") {
55
await addEmailItem(
56
inventory,
57
"/Lotus/Types/Items/EmailItems/LizzieShawzinSkinEmailItem",
58
inventoryChanges
59
);
52
if (request.Data) {
53
dialogue.QueuedDialogues = request.QueuedDialogues;
54
for (const bool of request.Booleans) {
55
dialogue.Booleans.push(bool);
56
if (bool == "LizzieShawzin") {
57
await addEmailItem(
58
inventory,
59
"/Lotus/Types/Items/EmailItems/LizzieShawzinSkinEmailItem",
60
inventoryChanges
61
);
62
}
60
63
}
61
}
62
for (const bool of request.ResetBooleans) {
63
const index = dialogue.Booleans.findIndex(x => x == bool);
64
if (index != -1) {
65
dialogue.Booleans.splice(index, 1);
64
for (const bool of request.ResetBooleans) {
65
const index = dialogue.Booleans.findIndex(x => x == bool);
66
if (index != -1) {
67
dialogue.Booleans.splice(index, 1);
68
}
66
69
}
70
dialogue.Completed.push(request.Data);
71
dialogue.AvailableDate = new Date(tomorrowAt0Utc);
72
await inventory.save();
73
res.json({
74
InventoryChanges: inventoryChanges,
75
AvailableDate: { $date: { $numberLong: tomorrowAt0Utc.toString() } }
76
});
77
} else if (request.Gift) {
78
const inventoryChanges = updateCurrency(inventory, request.Gift.Cost, false);
79
const gift = dialogue.Gifts.find(x => x.Item == request.Gift!.Item);
80
if (gift) {
81
gift.GiftedQuantity += 1;
82
} else {
83
dialogue.Gifts.push({ Item: request.Gift.Item, GiftedQuantity: 1 });
84
}
85
dialogue.AvailableGiftDate = new Date(tomorrowAt0Utc);
86
await inventory.save();
87
res.json({
88
InventoryChanges: inventoryChanges,
89
AvailableGiftDate: { $date: { $numberLong: tomorrowAt0Utc.toString() } }
90
});
67
91
}
68
dialogue.Completed.push(request.Data);
69
const tomorrowAt0Utc = (Math.trunc(Date.now() / (86400 * 1000)) + 1) * 86400 * 1000;
70
dialogue.AvailableDate = new Date(tomorrowAt0Utc);
71
await inventory.save();
72
res.json({
73
InventoryChanges: inventoryChanges,
74
AvailableDate: { $date: { $numberLong: tomorrowAt0Utc.toString() } }
75
});
76
92
}
77
93
};
78
94
@@ -88,9 +104,15 @@ interface SaveCompletedDialogueRequest {
88
104
Chemistry: number;
89
105
CompletionType: number;
90
106
QueuedDialogues: string[];
107
Gift?: {
108
Item: string;
109
GainedChemistry: number;
110
Cost: number;
111
GiftedQuantity: number;
112
};
91
113
Booleans: string[];
92
114
ResetBooleans: string[];
93
Data: ICompletedDialogue;
115
Data?: ICompletedDialogue;
94
116
OtherDialogueInfos: IOtherDialogueInfo[]; // unsure
95
117
}
96
118