SpaceNinjaServer/src/controllers/api/updateSongChallengeController.ts
Sainan bbc40d5534
Some checks are pending
Build / build (18) (push) Waiting to run
Build / build (20) (push) Waiting to run
Build / build (22) (push) Waiting to run
Build Docker image / docker (push) Waiting to run
feat: updateSongChallenge (#1024)
Closes #707

untested but should be correct based on all the information I could find

Reviewed-on: #1024
2025-02-28 18:18:33 -08:00

51 lines
1.9 KiB
TypeScript

import { getJSONfromString } from "@/src/helpers/stringHelpers";
import { addShipDecorations, getInventory } from "@/src/services/inventoryService";
import { getAccountIdForRequest } from "@/src/services/loginService";
import { IInventoryChanges } from "@/src/types/purchaseTypes";
import { RequestHandler } from "express";
export const updateSongChallengeController: RequestHandler = async (req, res) => {
const accountId = await getAccountIdForRequest(req);
const inventory = await getInventory(accountId);
const request = getJSONfromString<IUpdateSongChallengeRequest>(String(req.body));
inventory.SongChallenges ??= [];
let songChallenge = inventory.SongChallenges.find(x => x.Song == request.Song);
if (!songChallenge) {
songChallenge =
inventory.SongChallenges[inventory.SongChallenges.push({ Song: request.Song, Difficulties: [] }) - 1];
}
songChallenge.Difficulties.push(request.Difficulty);
const response: IUpdateSongChallengeResponse = {
Song: request.Song,
Difficulty: request.Difficulty
};
// Handle all songs being completed on all difficulties
if (inventory.SongChallenges.length == 12 && !inventory.SongChallenges.find(x => x.Difficulties.length != 2)) {
response.Reward = "/Lotus/StoreItems/Types/Items/ShipDecos/LisetPropShawzinDuviri";
const shipDecorationChanges = [
{ ItemType: "/Lotus/Types/Items/ShipDecos/LisetPropShawzinDuviri", ItemCount: 1 }
];
response.InventoryChanges = {
ShipDecorations: shipDecorationChanges
};
addShipDecorations(inventory, shipDecorationChanges);
}
await inventory.save();
res.json(response);
};
interface IUpdateSongChallengeRequest {
Song: string;
Difficulty: number;
}
interface IUpdateSongChallengeResponse {
Song: string;
Difficulty: number;
Reward?: string;
InventoryChanges?: IInventoryChanges;
}