返回提交历史
Modified
src/models/personalRoomsModel.ts
+2
-1
Modified
src/services/shipCustomizationsService.ts
+68
-3
Modified
src/types/shipTypes.ts
+9
-3
XFEstudio/SpaceNinjaServer
fix: move ship decorations (#300)
21db3b5c
代码差异
3 个文件
+79
-7
@@ -7,7 +7,8 @@ const placedDecosSchema = new Schema<IPlacedDecosDatabase>(
7
7
{
8
8
Type: String,
9
9
Pos: [Number],
10
Rot: [Number]
10
Rot: [Number],
11
Scale: Number
11
12
},
12
13
{ id: false }
13
14
);
@@ -6,6 +6,7 @@ import {
6
6
IShipDecorationsRequest,
7
7
IShipDecorationsResponse
8
8
} from "@/src/types/shipTypes";
9
import { logger } from "@/src/utils/logger";
9
10
import { Types } from "mongoose";
10
11
11
12
export const setShipCustomizations = async (shipCustomization: ISetShipCustomizationsRequest) => {
@@ -37,12 +38,74 @@ export const handleSetShipDecorations = async (
37
38
38
39
const rooms = placedDecoration.IsApartment ? personalRooms.Apartment.Rooms : personalRooms.Ship.Rooms;
39
40
40
const room = rooms.find(room => room.Name === placedDecoration.Room);
41
const roomToPlaceIn = rooms.find(room => room.Name === placedDecoration.Room);
42
43
if (!roomToPlaceIn) {
44
logger.error("room not found");
45
throw new Error("room not found");
46
}
47
48
if (placedDecoration.MoveId) {
49
//moved within the same room
50
if (placedDecoration.OldRoom === placedDecoration.Room) {
51
const existingDecorationIndex = roomToPlaceIn?.PlacedDecos?.findIndex(
52
deco => deco._id.toString() === placedDecoration.MoveId
53
);
54
55
if (existingDecorationIndex === -1) {
56
logger.error("decoration to be moved not found");
57
throw new Error("decoration to be moved not found");
58
}
59
60
roomToPlaceIn.PlacedDecos[existingDecorationIndex].Pos = placedDecoration.Pos;
61
roomToPlaceIn.PlacedDecos[existingDecorationIndex].Rot = placedDecoration.Rot;
62
63
if (placedDecoration.Scale) {
64
roomToPlaceIn.PlacedDecos[existingDecorationIndex].Scale = placedDecoration.Scale;
65
}
66
67
await personalRooms.save();
68
return {
69
OldRoom: placedDecoration.OldRoom,
70
NewRoom: placedDecoration.Room,
71
IsApartment: placedDecoration.IsApartment,
72
MaxCapacityIncrease: 0 // TODO: calculate capacity change upon removal
73
};
74
}
75
76
//moved to a different room
77
const oldRoom = rooms.find(room => room.Name === placedDecoration.OldRoom);
78
79
if (!oldRoom) {
80
logger.error("old room not found");
81
throw new Error("old room not found");
82
}
83
84
oldRoom.PlacedDecos.pull({ _id: placedDecoration.MoveId });
85
86
const newDecoration = {
87
Type: placedDecoration.Type,
88
Pos: placedDecoration.Pos,
89
Rot: placedDecoration.Rot,
90
Scale: placedDecoration.Scale || 1,
91
_id: placedDecoration.MoveId
92
};
93
94
//the new room is still roomToPlaceIn
95
roomToPlaceIn.PlacedDecos.push(newDecoration);
96
await personalRooms.save();
97
return {
98
OldRoom: placedDecoration.OldRoom,
99
NewRoom: placedDecoration.Room,
100
IsApartment: placedDecoration.IsApartment,
101
MaxCapacityIncrease: 0 // TODO: calculate capacity change upon removal
102
};
103
}
41
104
42
105
//TODO: check whether to remove from shipitems
43
106
44
107
if (placedDecoration.RemoveId) {
45
room?.PlacedDecos?.pull({ _id: placedDecoration.RemoveId });
108
roomToPlaceIn.PlacedDecos.pull({ _id: placedDecoration.RemoveId });
46
109
await personalRooms.save();
47
110
return {
48
111
DecoId: placedDecoration.RemoveId,
@@ -54,11 +117,13 @@ export const handleSetShipDecorations = async (
54
117
55
118
// TODO: handle capacity
56
119
120
//place decoration
57
121
const decoId = new Types.ObjectId();
58
room?.PlacedDecos?.push({
122
roomToPlaceIn.PlacedDecos?.push({
59
123
Type: placedDecoration.Type,
60
124
Pos: placedDecoration.Pos,
61
125
Rot: placedDecoration.Rot,
126
Scale: placedDecoration.Scale || 1,
62
127
_id: decoId
63
128
});
64
129
@@ -69,6 +69,7 @@ export interface IPlacedDecosDatabase {
69
69
Type: string;
70
70
Pos: [number, number, number];
71
71
Rot: [number, number, number];
72
Scale: number;
72
73
_id: Types.ObjectId;
73
74
}
74
75
@@ -100,12 +101,17 @@ export interface IShipDecorationsRequest {
100
101
Rot: [number, number, number];
101
102
Room: string;
102
103
IsApartment: boolean;
103
RemoveId: string;
104
RemoveId?: string;
105
MoveId?: string;
106
OldRoom?: string;
107
Scale?: number;
104
108
}
105
109
106
110
export interface IShipDecorationsResponse {
107
DecoId: string;
108
Room: string;
111
DecoId?: string;
112
Room?: string;
109
113
IsApartment: boolean;
110
114
MaxCapacityIncrease?: number;
115
OldRoom?: string;
116
NewRoom?: string;
111
117
}