2024-02-18 13:58:43 +01:00
|
|
|
import { getPersonalRooms } from "@/src/services/personalRoomsService";
|
|
|
|
import { getShip } from "@/src/services/shipService";
|
|
|
|
import {
|
|
|
|
ISetShipCustomizationsRequest,
|
|
|
|
IShipDecorationsRequest,
|
2024-10-18 16:54:49 +02:00
|
|
|
IShipDecorationsResponse,
|
|
|
|
ISetPlacedDecoInfoRequest
|
2024-02-18 13:58:43 +01:00
|
|
|
} from "@/src/types/shipTypes";
|
2024-06-15 17:39:13 +02:00
|
|
|
import { logger } from "@/src/utils/logger";
|
2024-02-18 13:58:43 +01:00
|
|
|
import { Types } from "mongoose";
|
2025-01-12 13:38:05 +01:00
|
|
|
import { addShipDecorations, getInventory } from "./inventoryService";
|
|
|
|
import { config } from "./configService";
|
2024-02-18 13:58:43 +01:00
|
|
|
|
2024-12-23 09:15:41 +01:00
|
|
|
export const setShipCustomizations = async (
|
|
|
|
accountId: string,
|
|
|
|
shipCustomization: ISetShipCustomizationsRequest
|
|
|
|
): Promise<void> => {
|
2024-02-18 13:58:43 +01:00
|
|
|
if (shipCustomization.IsExterior) {
|
2024-12-23 09:15:41 +01:00
|
|
|
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();
|
|
|
|
}
|
2024-02-18 13:58:43 +01:00
|
|
|
} else {
|
2024-12-23 09:15:41 +01:00
|
|
|
const personalRooms = await getPersonalRooms(accountId);
|
2025-02-08 17:41:33 -08:00
|
|
|
if (shipCustomization.IsShop) {
|
|
|
|
personalRooms.TailorShop.Colors = shipCustomization.Customization.Colors;
|
|
|
|
personalRooms.TailorShop.LevelDecosVisible = shipCustomization.Customization.LevelDecosVisible;
|
|
|
|
personalRooms.TailorShop.CustomJson = shipCustomization.Customization.CustomJson;
|
|
|
|
} else {
|
|
|
|
personalRooms.ShipInteriorColors = shipCustomization.Customization.Colors;
|
|
|
|
}
|
2024-12-23 09:15:41 +01:00
|
|
|
await personalRooms.save();
|
2024-02-18 13:58:43 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const handleSetShipDecorations = async (
|
|
|
|
accountId: string,
|
|
|
|
placedDecoration: IShipDecorationsRequest
|
|
|
|
): Promise<IShipDecorationsResponse> => {
|
|
|
|
const personalRooms = await getPersonalRooms(accountId);
|
|
|
|
|
2024-12-22 20:32:19 +01:00
|
|
|
const rooms =
|
|
|
|
placedDecoration.BootLocation == "SHOP"
|
|
|
|
? personalRooms.TailorShop.Rooms
|
|
|
|
: placedDecoration.IsApartment
|
|
|
|
? personalRooms.Apartment.Rooms
|
|
|
|
: personalRooms.Ship.Rooms;
|
2024-02-18 13:58:43 +01:00
|
|
|
|
2024-06-15 17:39:13 +02:00
|
|
|
const roomToPlaceIn = rooms.find(room => room.Name === placedDecoration.Room);
|
|
|
|
|
|
|
|
if (!roomToPlaceIn) {
|
|
|
|
throw new Error("room not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (placedDecoration.MoveId) {
|
|
|
|
//moved within the same room
|
|
|
|
if (placedDecoration.OldRoom === placedDecoration.Room) {
|
2025-03-31 04:15:32 -07:00
|
|
|
const existingDecoration = roomToPlaceIn.PlacedDecos.id(placedDecoration.MoveId);
|
2024-06-15 17:39:13 +02:00
|
|
|
|
2025-03-31 04:15:32 -07:00
|
|
|
if (!existingDecoration) {
|
2024-06-15 17:39:13 +02:00
|
|
|
throw new Error("decoration to be moved not found");
|
|
|
|
}
|
|
|
|
|
2025-03-31 04:15:32 -07:00
|
|
|
existingDecoration.Pos = placedDecoration.Pos;
|
|
|
|
existingDecoration.Rot = placedDecoration.Rot;
|
2024-06-15 17:39:13 +02:00
|
|
|
|
|
|
|
if (placedDecoration.Scale) {
|
2025-03-31 04:15:32 -07:00
|
|
|
existingDecoration.Scale = placedDecoration.Scale;
|
2024-06-15 17:39:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2024-12-22 20:34:04 +01:00
|
|
|
Scale: placedDecoration.Scale,
|
2024-06-15 17:39:13 +02:00
|
|
|
_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
|
|
|
|
};
|
|
|
|
}
|
2024-02-18 13:58:43 +01:00
|
|
|
|
|
|
|
if (placedDecoration.RemoveId) {
|
2024-06-15 17:39:13 +02:00
|
|
|
roomToPlaceIn.PlacedDecos.pull({ _id: placedDecoration.RemoveId });
|
2024-02-18 13:58:43 +01:00
|
|
|
await personalRooms.save();
|
2025-01-12 13:38:05 +01:00
|
|
|
|
|
|
|
if (!config.unlockAllShipDecorations) {
|
|
|
|
const inventory = await getInventory(accountId);
|
|
|
|
addShipDecorations(inventory, [{ ItemType: placedDecoration.Type, ItemCount: 1 }]);
|
|
|
|
await inventory.save();
|
|
|
|
}
|
|
|
|
|
2024-02-18 13:58:43 +01:00
|
|
|
return {
|
|
|
|
DecoId: placedDecoration.RemoveId,
|
|
|
|
Room: placedDecoration.Room,
|
|
|
|
IsApartment: placedDecoration.IsApartment,
|
|
|
|
MaxCapacityIncrease: 0
|
|
|
|
};
|
2025-01-12 13:38:05 +01:00
|
|
|
} else {
|
|
|
|
if (!config.unlockAllShipDecorations) {
|
|
|
|
const inventory = await getInventory(accountId);
|
|
|
|
addShipDecorations(inventory, [{ ItemType: placedDecoration.Type, ItemCount: -1 }]);
|
|
|
|
await inventory.save();
|
|
|
|
}
|
2024-02-18 13:58:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: handle capacity
|
|
|
|
|
2024-06-15 17:39:13 +02:00
|
|
|
//place decoration
|
2024-02-18 13:58:43 +01:00
|
|
|
const decoId = new Types.ObjectId();
|
2025-01-20 12:21:39 +01:00
|
|
|
roomToPlaceIn.PlacedDecos.push({
|
2024-02-18 13:58:43 +01:00
|
|
|
Type: placedDecoration.Type,
|
|
|
|
Pos: placedDecoration.Pos,
|
|
|
|
Rot: placedDecoration.Rot,
|
2024-12-22 20:34:04 +01:00
|
|
|
Scale: placedDecoration.Scale,
|
2024-02-18 13:58:43 +01:00
|
|
|
_id: decoId
|
|
|
|
});
|
|
|
|
|
|
|
|
await personalRooms.save();
|
|
|
|
|
|
|
|
return { DecoId: decoId.toString(), Room: placedDecoration.Room, IsApartment: placedDecoration.IsApartment };
|
|
|
|
};
|
2024-10-18 16:54:49 +02:00
|
|
|
|
|
|
|
export const handleSetPlacedDecoInfo = async (accountId: string, req: ISetPlacedDecoInfoRequest): Promise<void> => {
|
|
|
|
const personalRooms = await getPersonalRooms(accountId);
|
|
|
|
|
|
|
|
const room = personalRooms.Ship.Rooms.find(room => room.Name === req.Room);
|
|
|
|
if (!room) {
|
|
|
|
throw new Error("room not found");
|
|
|
|
}
|
|
|
|
|
2025-01-20 12:21:39 +01:00
|
|
|
const placedDeco = room.PlacedDecos.id(req.DecoId);
|
2024-10-18 16:54:49 +02:00
|
|
|
if (!placedDeco) {
|
|
|
|
throw new Error("deco not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
placedDeco.PictureFrameInfo = req.PictureFrameInfo;
|
|
|
|
|
|
|
|
await personalRooms.save();
|
|
|
|
};
|