SpaceNinjaServer/src/controllers/api/getShipController.ts

61 lines
2.2 KiB
TypeScript
Raw Normal View History

2023-05-19 15:22:48 -03:00
import { RequestHandler } from "express";
import config from "@/config.json";
import allShipFeatures from "@/static/fixed_responses/allShipFeatures.json";
import { parseString } from "@/src/helpers/general";
import { getShip } from "@/src/services/shipService";
import { PersonalRooms } from "@/src/models/personalRoomsModel";
import { Loadout } from "@/src/models/inventoryModels/loadoutModel";
import { logger } from "@/src/utils/logger";
import { toOid } from "@/src/helpers/inventoryHelpers";
import { IGetShipResponse } from "@/src/types/shipTypes";
2023-05-19 15:22:48 -03:00
2023-06-05 04:16:49 +08:00
// eslint-disable-next-line @typescript-eslint/no-misused-promises
export const getShipController: RequestHandler = async (req, res) => {
const accountId = parseString(req.query.accountId);
const personalRooms = await getPersonalRooms(accountId);
const loadout = await getLoadout(accountId);
const ship = await getShip(personalRooms.activeShipId, "ShipInteriorColors ShipAttachments SkinFlavourItem");
const getShipResponse: IGetShipResponse = {
ShipOwnerId: accountId,
LoadOutInventory: { LoadOutPresets: loadout.toJSON() },
Ship: {
...personalRooms.toJSON().Ship,
ShipId: toOid(personalRooms.activeShipId),
ShipInterior: {
Colors: ship.ShipInteriorColors,
ShipAttachments: ship.ShipAttachments,
SkinFlavourItem: ship.SkinFlavourItem
}
},
Apartment: personalRooms.Apartment
};
if (config.unlockallShipFeatures) {
getShipResponse.Ship.Features = allShipFeatures;
2023-06-05 04:16:49 +08:00
}
res.json(getShipResponse);
};
export const getLoadout = async (accountId: string) => {
const loadout = await Loadout.findOne({ loadoutOwnerId: accountId });
if (!loadout) {
logger.error(`loadout not found for account ${accountId}`);
throw new Error("loadout not found");
}
return loadout;
2023-05-19 15:22:48 -03:00
};
export const getPersonalRooms = async (accountId: string) => {
const personalRooms = await PersonalRooms.findOne({ personalRoomsOwnerId: accountId });
if (!personalRooms) {
logger.error(`personal rooms not found for account ${accountId}`);
throw new Error("personal rooms not found");
}
return personalRooms;
};