SpaceNinjaServer/src/services/shipService.ts

52 lines
1.8 KiB
TypeScript
Raw Normal View History

2023-06-05 04:16:49 +08:00
import { Ship } from "@/src/models/shipModel";
2024-02-16 22:40:00 +01:00
import { ILoadoutDatabase } from "@/src/types/saveLoadoutTypes";
import { logger } from "@/src/utils/logger";
2023-06-05 04:16:49 +08:00
import { Types } from "mongoose";
2024-02-16 22:40:00 +01:00
export const createShip = async (accountOwnerId: Types.ObjectId) => {
2023-06-05 04:16:49 +08:00
try {
const ship = new Ship({
2024-02-16 22:40:00 +01:00
ItemType: "/Lotus/Types/Items/Ships/DefaultShip",
ShipOwnerId: accountOwnerId,
2024-02-16 22:40:00 +01:00
ShipInteriorColors: {
t0: 3828063,
t1: 2502747
},
ShipAttachments: { HOOD_ORNAMENT: "" },
SkinFlavourItem: "/Lotus/Upgrades/Skins/Liset/LisetSkinFlavourItemDefault"
});
2024-02-16 22:40:00 +01:00
const newShip = await ship.save();
console.log(newShip);
return newShip._id;
2023-06-05 04:16:49 +08:00
} catch (error) {
if (error instanceof Error) {
2023-06-05 00:17:01 +02:00
throw new Error(`error creating ship" ${error.message}`);
2023-06-05 04:16:49 +08:00
}
2023-06-05 00:17:01 +02:00
throw new Error("error creating ship that is not of instance Error");
2023-06-05 04:16:49 +08:00
}
};
2024-02-16 22:40:00 +01:00
export const getShip = async (shipOwnerId: string, fieldSelection: string = "") => {
const ship = await Ship.findOne({ ShipOwnerId: shipOwnerId }, fieldSelection);
if (!ship) {
logger.error(`error finding a ship for account ${shipOwnerId}`);
throw new Error(`error finding a ship for account ${shipOwnerId}`);
}
return ship;
};
export const getShipLean = async (shipOwnerId: string) => {
const ship = await Ship.findOne({ ShipOwnerId: shipOwnerId }).lean().populate<{
LoadOutInventory: { LoadOutPresets: ILoadoutDatabase };
}>("LoadOutInventory.LoadOutPresets");
if (!ship) {
logger.error(`error finding a ship for account ${shipOwnerId}`);
throw new Error(`error finding a ship for account ${shipOwnerId}`);
}
return ship;
};