From dda41875ae75f81254db571cb6a82d982b112891 Mon Sep 17 00:00:00 2001 From: Sainan Date: Mon, 23 Dec 2024 09:15:41 +0100 Subject: [PATCH] fix: acquiring ships (#619) --- src/controllers/api/getShipController.ts | 4 +- src/controllers/api/inventoryController.ts | 2 +- .../api/setShipCustomizationsController.ts | 4 +- src/models/personalRoomsModel.ts | 2 + src/models/shipModel.ts | 1 - src/services/inventoryService.ts | 43 +++++++++++++------ src/services/shipCustomizationsService.ts | 34 +++++++-------- src/services/shipService.ts | 11 +++-- src/types/personalRoomsTypes.ts | 3 ++ src/types/shipTypes.ts | 5 +-- static/fixed_responses/getShip.json | 32 -------------- 11 files changed, 67 insertions(+), 74 deletions(-) delete mode 100644 static/fixed_responses/getShip.json diff --git a/src/controllers/api/getShipController.ts b/src/controllers/api/getShipController.ts index 5d3689d6..4dad54a8 100644 --- a/src/controllers/api/getShipController.ts +++ b/src/controllers/api/getShipController.ts @@ -15,7 +15,7 @@ export const getShipController: RequestHandler = async (req, res) => { const personalRoomsDb = await getPersonalRooms(accountId); const personalRooms = personalRoomsDb.toJSON(); const loadout = await getLoadout(accountId); - const ship = await getShip(personalRoomsDb.activeShipId, "ShipInteriorColors ShipAttachments SkinFlavourItem"); + const ship = await getShip(personalRoomsDb.activeShipId, "ShipAttachments SkinFlavourItem"); const getShipResponse: IGetShipResponse = { ShipOwnerId: accountId, @@ -24,7 +24,7 @@ export const getShipController: RequestHandler = async (req, res) => { ...personalRooms.Ship, ShipId: toOid(personalRoomsDb.activeShipId), ShipInterior: { - Colors: ship.ShipInteriorColors, + Colors: personalRooms.ShipInteriorColors, ShipAttachments: ship.ShipAttachments, SkinFlavourItem: ship.SkinFlavourItem } diff --git a/src/controllers/api/inventoryController.ts b/src/controllers/api/inventoryController.ts index c0272373..af6a420e 100644 --- a/src/controllers/api/inventoryController.ts +++ b/src/controllers/api/inventoryController.ts @@ -20,7 +20,7 @@ const inventoryController: RequestHandler = async (request, response) => { const inventory = await Inventory.findOne({ accountOwnerId: account._id.toString() }) .populate<{ LoadOutPresets: ILoadoutDatabase }>("LoadOutPresets") - .populate<{ Ships: IShipInventory }>("Ships", "-ShipInteriorColors"); + .populate<{ Ships: IShipInventory }>("Ships"); if (!inventory) { response.status(400).json({ error: "inventory was undefined" }); diff --git a/src/controllers/api/setShipCustomizationsController.ts b/src/controllers/api/setShipCustomizationsController.ts index 54372562..01198665 100644 --- a/src/controllers/api/setShipCustomizationsController.ts +++ b/src/controllers/api/setShipCustomizationsController.ts @@ -1,3 +1,4 @@ +import { getAccountIdForRequest } from "@/src/services/loginService"; import { setShipCustomizations } from "@/src/services/shipCustomizationsService"; import { ISetShipCustomizationsRequest } from "@/src/types/shipTypes"; import { logger } from "@/src/utils/logger"; @@ -5,9 +6,10 @@ import { RequestHandler } from "express"; export const setShipCustomizationsController: RequestHandler = async (req, res) => { try { + const accountId = await getAccountIdForRequest(req); const setShipCustomizationsRequest = JSON.parse(req.body as string) as ISetShipCustomizationsRequest; - const setShipCustomizationsResponse = await setShipCustomizations(setShipCustomizationsRequest); + const setShipCustomizationsResponse = await setShipCustomizations(accountId, setShipCustomizationsRequest); res.json(setShipCustomizationsResponse); } catch (error: unknown) { if (error instanceof Error) { diff --git a/src/models/personalRoomsModel.ts b/src/models/personalRoomsModel.ts index 88d3d126..2388ae45 100644 --- a/src/models/personalRoomsModel.ts +++ b/src/models/personalRoomsModel.ts @@ -1,4 +1,5 @@ import { toOid } from "@/src/helpers/inventoryHelpers"; +import { colorSchema } from "@/src/models/inventoryModels/inventoryModel"; import { IOrbiter, IPersonalRoomsDatabase, PersonalRoomsModelType } from "@/src/types/personalRoomsTypes"; import { IApartment, @@ -131,6 +132,7 @@ const tailorShopDefault: ITailorShopDatabase = { export const personalRoomsSchema = new Schema({ personalRoomsOwnerId: Schema.Types.ObjectId, activeShipId: Schema.Types.ObjectId, + ShipInteriorColors: colorSchema, Ship: orbiterSchema, Apartment: apartmentSchema, TailorShop: { type: tailorShopSchema, default: tailorShopDefault } diff --git a/src/models/shipModel.ts b/src/models/shipModel.ts index 4b63bfb8..13e1ef53 100644 --- a/src/models/shipModel.ts +++ b/src/models/shipModel.ts @@ -8,7 +8,6 @@ const shipSchema = new Schema( { ItemType: String, ShipOwnerId: Schema.Types.ObjectId, - ShipInteriorColors: colorSchema, ShipExteriorColors: colorSchema, AirSupportPower: String, ShipAttachments: { HOOD_ORNAMENT: String }, diff --git a/src/services/inventoryService.ts b/src/services/inventoryService.ts index cf43ca3d..2f1ddd10 100644 --- a/src/services/inventoryService.ts +++ b/src/services/inventoryService.ts @@ -41,6 +41,7 @@ import { ExportResources, ExportUpgrades } from "warframe-public-export-plus"; +import { createShip } from "./shipService"; export const createInventory = async ( accountOwnerId: Types.ObjectId, @@ -129,19 +130,35 @@ export const addItem = async ( } if (typeName in ExportResources) { const inventory = await getInventory(accountId); - const miscItemChanges = [ - { - ItemType: typeName, - ItemCount: quantity - } satisfies IMiscItem - ]; - addMiscItems(inventory, miscItemChanges); - await inventory.save(); - return { - InventoryChanges: { - MiscItems: miscItemChanges - } - }; + if (ExportResources[typeName].productCategory == "Ships") { + const oid = await createShip(new Types.ObjectId(accountId), typeName); + inventory.Ships.push(oid); + await inventory.save(); + return { + InventoryChanges: { + Ships: [ + { + ItemId: { $oid: oid }, + ItemType: typeName + } + ] + } + }; + } else { + const miscItemChanges = [ + { + ItemType: typeName, + ItemCount: quantity + } satisfies IMiscItem + ]; + addMiscItems(inventory, miscItemChanges); + await inventory.save(); + return { + InventoryChanges: { + MiscItems: miscItemChanges + } + }; + } } if (typeName in ExportCustoms) { return { diff --git a/src/services/shipCustomizationsService.ts b/src/services/shipCustomizationsService.ts index 35dbf9cb..ddf70f59 100644 --- a/src/services/shipCustomizationsService.ts +++ b/src/services/shipCustomizationsService.ts @@ -2,7 +2,6 @@ import { getPersonalRooms } from "@/src/services/personalRoomsService"; import { getShip } from "@/src/services/shipService"; import { ISetShipCustomizationsRequest, - IShipDatabase, IShipDecorationsRequest, IShipDecorationsResponse, ISetPlacedDecoInfoRequest @@ -10,25 +9,26 @@ import { import { logger } from "@/src/utils/logger"; import { Types } from "mongoose"; -export const setShipCustomizations = async (shipCustomization: ISetShipCustomizationsRequest) => { - const ship = await getShip(new Types.ObjectId(shipCustomization.ShipId)); - - let shipChanges: Partial; +export const setShipCustomizations = async ( + accountId: string, + shipCustomization: ISetShipCustomizationsRequest +): Promise => { if (shipCustomization.IsExterior) { - shipChanges = { - ShipExteriorColors: shipCustomization.Customization.Colors, - SkinFlavourItem: shipCustomization.Customization.SkinFlavourItem, - ShipAttachments: shipCustomization.Customization.ShipAttachments, - AirSupportPower: shipCustomization.AirSupportPower! - }; + const ship = await getShip(new Types.ObjectId(shipCustomization.ShipId)); + if (ship.ShipOwnerId.toString() == accountId) { + ship.set({ + ShipExteriorColors: shipCustomization.Customization.Colors, + SkinFlavourItem: shipCustomization.Customization.SkinFlavourItem, + ShipAttachments: shipCustomization.Customization.ShipAttachments, + AirSupportPower: shipCustomization.AirSupportPower! + }); + await ship.save(); + } } else { - shipChanges = { - ShipInteriorColors: shipCustomization.Customization.Colors - }; + const personalRooms = await getPersonalRooms(accountId); + personalRooms.ShipInteriorColors = shipCustomization.Customization.Colors; + await personalRooms.save(); } - ship.set(shipChanges); - - await ship.save(); }; export const handleSetShipDecorations = async ( diff --git a/src/services/shipService.ts b/src/services/shipService.ts index ae11c9d2..1f19fbd9 100644 --- a/src/services/shipService.ts +++ b/src/services/shipService.ts @@ -3,10 +3,13 @@ import { ILoadoutDatabase } from "@/src/types/saveLoadoutTypes"; import { logger } from "@/src/utils/logger"; import { Types } from "mongoose"; -export const createShip = async (accountOwnerId: Types.ObjectId) => { +export const createShip = async ( + accountOwnerId: Types.ObjectId, + typeName: string = "/Lotus/Types/Items/Ships/DefaultShip" +) => { try { const ship = new Ship({ - ItemType: "/Lotus/Types/Items/Ships/DefaultShip", + ItemType: typeName, ShipOwnerId: accountOwnerId }); const newShip = await ship.save(); @@ -23,8 +26,8 @@ export const getShip = async (shipId: Types.ObjectId, fieldSelection: string = " const ship = await Ship.findOne({ _id: shipId }, fieldSelection); if (!ship) { - logger.error(`error finding a ship for account ${shipId}`); - throw new Error(`error finding a ship for account ${shipId}`); + logger.error(`error finding a ship with id ${shipId}`); + throw new Error(`error finding a ship with id ${shipId}`); } return ship; diff --git a/src/types/personalRoomsTypes.ts b/src/types/personalRoomsTypes.ts index c99d8f5a..9b44339d 100644 --- a/src/types/personalRoomsTypes.ts +++ b/src/types/personalRoomsTypes.ts @@ -1,3 +1,4 @@ +import { IColor } from "@/src/types/inventoryTypes/commonInventoryTypes"; import { IApartment, IRoom, @@ -16,12 +17,14 @@ export interface IOrbiter { } export interface IPersonalRooms { + ShipInteriorColors: IColor; Ship: IOrbiter; Apartment: IApartment; TailorShop: ITailorShop; } export interface IPersonalRoomsDatabase { + ShipInteriorColors: IColor; personalRoomsOwnerId: Types.ObjectId; activeShipId: Types.ObjectId; Ship: IOrbiter; diff --git a/src/types/shipTypes.ts b/src/types/shipTypes.ts index d5f02d3d..1868e5f3 100644 --- a/src/types/shipTypes.ts +++ b/src/types/shipTypes.ts @@ -1,4 +1,4 @@ -import { Schema, Types } from "mongoose"; +import { Types } from "mongoose"; import { IOid } from "@/src/types/commonTypes"; import { IColor } from "@/src/types/inventoryTypes/commonInventoryTypes"; import { ILoadoutClient } from "./saveLoadoutTypes"; @@ -34,8 +34,7 @@ export interface IShip { export interface IShipDatabase { ItemType: string; - ShipOwnerId: Schema.Types.ObjectId; - ShipInteriorColors?: IColor; + ShipOwnerId: Types.ObjectId; ShipExteriorColors?: IColor; AirSupportPower: string; ShipAttachments?: IShipAttachments; diff --git a/static/fixed_responses/getShip.json b/static/fixed_responses/getShip.json deleted file mode 100644 index c9b83d1c..00000000 --- a/static/fixed_responses/getShip.json +++ /dev/null @@ -1,32 +0,0 @@ -{ - "ShipOwnerId": "647bce8a1caba352f90b6a09", - "Ship": { - "Rooms": [ - { "Name": "AlchemyRoom", "MaxCapacity": 1600 }, - { "Name": "BridgeRoom", "MaxCapacity": 1600 }, - { "Name": "LisetRoom", "MaxCapacity": 1000 }, - { "Name": "OperatorChamberRoom", "MaxCapacity": 1600 }, - { "Name": "OutsideRoom", "MaxCapacity": 1600 }, - { "Name": "PersonalQuartersRoom", "MaxCapacity": 1600 } - ], - "ContentUrlSignature": "removed", - "Features": [ - "/Lotus/Types/Items/ShipFeatureItems/EarthNavigationFeatureItem", - "/Lotus/Types/Items/ShipFeatureItems/ArsenalFeatureItem", - "/Lotus/Types/Items/ShipFeatureItems/SocialMenuFeatureItem", - "/Lotus/Types/Items/ShipFeatureItems/ModsFeatureItem", - "/Lotus/Types/Items/ShipFeatureItems/FoundryFeatureItem", - "/Lotus/Types/Items/ShipFeatureItems/MercuryNavigationFeatureItem" - ] - }, - "Apartment": { - "Rooms": [ - { "Name": "ElevatorLanding", "MaxCapacity": 1600 }, - { "Name": "ApartmentRoomA", "MaxCapacity": 1000 }, - { "Name": "ApartmentRoomB", "MaxCapacity": 1600 }, - { "Name": "ApartmentRoomC", "MaxCapacity": 1600 }, - { "Name": "DuviriHallway", "MaxCapacity": 1600 } - ], - "FavouriteLoadouts": [] - } -}