diff --git a/src/models/personalRoomsModel.ts b/src/models/personalRoomsModel.ts index 8f4abede..8a7caaf0 100644 --- a/src/models/personalRoomsModel.ts +++ b/src/models/personalRoomsModel.ts @@ -7,7 +7,8 @@ const placedDecosSchema = new Schema( { Type: String, Pos: [Number], - Rot: [Number] + Rot: [Number], + Scale: Number }, { id: false } ); diff --git a/src/services/shipCustomizationsService.ts b/src/services/shipCustomizationsService.ts index 98125e50..d21510e7 100644 --- a/src/services/shipCustomizationsService.ts +++ b/src/services/shipCustomizationsService.ts @@ -6,6 +6,7 @@ import { IShipDecorationsRequest, IShipDecorationsResponse } from "@/src/types/shipTypes"; +import { logger } from "@/src/utils/logger"; import { Types } from "mongoose"; export const setShipCustomizations = async (shipCustomization: ISetShipCustomizationsRequest) => { @@ -37,12 +38,74 @@ export const handleSetShipDecorations = async ( const rooms = placedDecoration.IsApartment ? personalRooms.Apartment.Rooms : personalRooms.Ship.Rooms; - const room = rooms.find(room => room.Name === placedDecoration.Room); + const roomToPlaceIn = rooms.find(room => room.Name === placedDecoration.Room); + + if (!roomToPlaceIn) { + logger.error("room not found"); + throw new Error("room not found"); + } + + if (placedDecoration.MoveId) { + //moved within the same room + if (placedDecoration.OldRoom === placedDecoration.Room) { + const existingDecorationIndex = roomToPlaceIn?.PlacedDecos?.findIndex( + deco => deco._id.toString() === placedDecoration.MoveId + ); + + if (existingDecorationIndex === -1) { + logger.error("decoration to be moved not found"); + throw new Error("decoration to be moved not found"); + } + + roomToPlaceIn.PlacedDecos[existingDecorationIndex].Pos = placedDecoration.Pos; + roomToPlaceIn.PlacedDecos[existingDecorationIndex].Rot = placedDecoration.Rot; + + if (placedDecoration.Scale) { + roomToPlaceIn.PlacedDecos[existingDecorationIndex].Scale = placedDecoration.Scale; + } + + await personalRooms.save(); + return { + OldRoom: placedDecoration.OldRoom, + NewRoom: placedDecoration.Room, + IsApartment: placedDecoration.IsApartment, + MaxCapacityIncrease: 0 // TODO: calculate capacity change upon removal + }; + } + + //moved to a different room + const oldRoom = rooms.find(room => room.Name === placedDecoration.OldRoom); + + if (!oldRoom) { + logger.error("old room not found"); + throw new Error("old room not found"); + } + + oldRoom.PlacedDecos.pull({ _id: placedDecoration.MoveId }); + + const newDecoration = { + Type: placedDecoration.Type, + Pos: placedDecoration.Pos, + Rot: placedDecoration.Rot, + Scale: placedDecoration.Scale || 1, + _id: placedDecoration.MoveId + }; + + //the new room is still roomToPlaceIn + roomToPlaceIn.PlacedDecos.push(newDecoration); + await personalRooms.save(); + return { + OldRoom: placedDecoration.OldRoom, + NewRoom: placedDecoration.Room, + IsApartment: placedDecoration.IsApartment, + MaxCapacityIncrease: 0 // TODO: calculate capacity change upon removal + }; + } //TODO: check whether to remove from shipitems if (placedDecoration.RemoveId) { - room?.PlacedDecos?.pull({ _id: placedDecoration.RemoveId }); + roomToPlaceIn.PlacedDecos.pull({ _id: placedDecoration.RemoveId }); await personalRooms.save(); return { DecoId: placedDecoration.RemoveId, @@ -54,11 +117,13 @@ export const handleSetShipDecorations = async ( // TODO: handle capacity + //place decoration const decoId = new Types.ObjectId(); - room?.PlacedDecos?.push({ + roomToPlaceIn.PlacedDecos?.push({ Type: placedDecoration.Type, Pos: placedDecoration.Pos, Rot: placedDecoration.Rot, + Scale: placedDecoration.Scale || 1, _id: decoId }); diff --git a/src/types/shipTypes.ts b/src/types/shipTypes.ts index 53cf1f87..6b0e019f 100644 --- a/src/types/shipTypes.ts +++ b/src/types/shipTypes.ts @@ -69,6 +69,7 @@ export interface IPlacedDecosDatabase { Type: string; Pos: [number, number, number]; Rot: [number, number, number]; + Scale: number; _id: Types.ObjectId; } @@ -100,12 +101,17 @@ export interface IShipDecorationsRequest { Rot: [number, number, number]; Room: string; IsApartment: boolean; - RemoveId: string; + RemoveId?: string; + MoveId?: string; + OldRoom?: string; + Scale?: number; } export interface IShipDecorationsResponse { - DecoId: string; - Room: string; + DecoId?: string; + Room?: string; IsApartment: boolean; MaxCapacityIncrease?: number; + OldRoom?: string; + NewRoom?: string; }