fix: move ship decorations (#300)
This commit is contained in:
		
							parent
							
								
									f216d5222f
								
							
						
					
					
						commit
						21db3b5c5d
					
				@ -7,7 +7,8 @@ const placedDecosSchema = new Schema<IPlacedDecosDatabase>(
 | 
			
		||||
    {
 | 
			
		||||
        Type: String,
 | 
			
		||||
        Pos: [Number],
 | 
			
		||||
        Rot: [Number]
 | 
			
		||||
        Rot: [Number],
 | 
			
		||||
        Scale: Number
 | 
			
		||||
    },
 | 
			
		||||
    { id: false }
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
@ -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
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -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;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user