fix: move ship decorations #300
@ -7,7 +7,8 @@ const placedDecosSchema = new Schema<IPlacedDecosDatabase>(
 | 
				
			|||||||
    {
 | 
					    {
 | 
				
			||||||
        Type: String,
 | 
					        Type: String,
 | 
				
			||||||
        Pos: [Number],
 | 
					        Pos: [Number],
 | 
				
			||||||
        Rot: [Number]
 | 
					        Rot: [Number],
 | 
				
			||||||
 | 
					        Scale: Number
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    { id: false }
 | 
					    { id: false }
 | 
				
			||||||
);
 | 
					);
 | 
				
			||||||
 | 
				
			|||||||
@ -6,6 +6,7 @@ import {
 | 
				
			|||||||
    IShipDecorationsRequest,
 | 
					    IShipDecorationsRequest,
 | 
				
			||||||
    IShipDecorationsResponse
 | 
					    IShipDecorationsResponse
 | 
				
			||||||
} from "@/src/types/shipTypes";
 | 
					} from "@/src/types/shipTypes";
 | 
				
			||||||
 | 
					import { logger } from "@/src/utils/logger";
 | 
				
			||||||
import { Types } from "mongoose";
 | 
					import { Types } from "mongoose";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export const setShipCustomizations = async (shipCustomization: ISetShipCustomizationsRequest) => {
 | 
					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 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
 | 
					    //TODO: check whether to remove from shipitems
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (placedDecoration.RemoveId) {
 | 
					    if (placedDecoration.RemoveId) {
 | 
				
			||||||
        room?.PlacedDecos?.pull({ _id: placedDecoration.RemoveId });
 | 
					        roomToPlaceIn.PlacedDecos.pull({ _id: placedDecoration.RemoveId });
 | 
				
			||||||
        await personalRooms.save();
 | 
					        await personalRooms.save();
 | 
				
			||||||
        return {
 | 
					        return {
 | 
				
			||||||
            DecoId: placedDecoration.RemoveId,
 | 
					            DecoId: placedDecoration.RemoveId,
 | 
				
			||||||
@ -54,11 +117,13 @@ export const handleSetShipDecorations = async (
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    // TODO: handle capacity
 | 
					    // TODO: handle capacity
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    //place decoration
 | 
				
			||||||
    const decoId = new Types.ObjectId();
 | 
					    const decoId = new Types.ObjectId();
 | 
				
			||||||
    room?.PlacedDecos?.push({
 | 
					    roomToPlaceIn.PlacedDecos?.push({
 | 
				
			||||||
        Type: placedDecoration.Type,
 | 
					        Type: placedDecoration.Type,
 | 
				
			||||||
        Pos: placedDecoration.Pos,
 | 
					        Pos: placedDecoration.Pos,
 | 
				
			||||||
        Rot: placedDecoration.Rot,
 | 
					        Rot: placedDecoration.Rot,
 | 
				
			||||||
 | 
					        Scale: placedDecoration.Scale || 1,
 | 
				
			||||||
        _id: decoId
 | 
					        _id: decoId
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -69,6 +69,7 @@ export interface IPlacedDecosDatabase {
 | 
				
			|||||||
    Type: string;
 | 
					    Type: string;
 | 
				
			||||||
    Pos: [number, number, number];
 | 
					    Pos: [number, number, number];
 | 
				
			||||||
    Rot: [number, number, number];
 | 
					    Rot: [number, number, number];
 | 
				
			||||||
 | 
					    Scale: number;
 | 
				
			||||||
    _id: Types.ObjectId;
 | 
					    _id: Types.ObjectId;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -100,12 +101,17 @@ export interface IShipDecorationsRequest {
 | 
				
			|||||||
    Rot: [number, number, number];
 | 
					    Rot: [number, number, number];
 | 
				
			||||||
    Room: string;
 | 
					    Room: string;
 | 
				
			||||||
    IsApartment: boolean;
 | 
					    IsApartment: boolean;
 | 
				
			||||||
    RemoveId: string;
 | 
					    RemoveId?: string;
 | 
				
			||||||
 | 
					    MoveId?: string;
 | 
				
			||||||
 | 
					    OldRoom?: string;
 | 
				
			||||||
 | 
					    Scale?: number;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export interface IShipDecorationsResponse {
 | 
					export interface IShipDecorationsResponse {
 | 
				
			||||||
    DecoId: string;
 | 
					    DecoId?: string;
 | 
				
			||||||
    Room: string;
 | 
					    Room?: string;
 | 
				
			||||||
    IsApartment: boolean;
 | 
					    IsApartment: boolean;
 | 
				
			||||||
    MaxCapacityIncrease?: number;
 | 
					    MaxCapacityIncrease?: number;
 | 
				
			||||||
 | 
					    OldRoom?: string;
 | 
				
			||||||
 | 
					    NewRoom?: string;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user