universo-platform-3d
352 строки · 7.5 Кб
1import * as mongoose from 'mongoose'
2import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'
3import { Document } from 'mongoose'
4import { User, UserPublicData } from '../user/user.schema'
5import { ASSET_TYPE } from '../option-sets/asset-type'
6import { ApiProperty } from '@nestjs/swagger'
7import { Vector4AsArray } from '../option-sets/vectors'
8import { CustomData } from '../custom-data/models/custom-data.schema'
9import {
10PurchaseOption,
11PurchaseOptionSchema
12} from '../marketplace/purchase-option.subdocument.schema'
13import {
14LicenseSchema,
15License
16} from '../marketplace/license.subdocument.schema'
17import { Role, RoleSchema } from '../roles/models/role.schema'
18import { Tags, TagsSchema } from '../tag/models/tags.schema'
19import { ObjectId } from 'mongodb'
20
21export type AssetDiscriminators = 'MapAsset' | 'Material' | 'Texture'
22
23// Properties must not be undefined so that getPublicPropertiesForMongooseQuery can work
24export class AssetPublicData {
25@ApiProperty() // @ApiProperty must be included to be exposed by the API and flow to FE codegen
26_id = '' // Must not be undefined
27@ApiProperty()
28createdAt = new Date()
29@ApiProperty()
30updatedAt = new Date()
31@ApiProperty()
32name = ''
33@ApiProperty({
34enum: ASSET_TYPE
35})
36assetType = ASSET_TYPE.IMAGE
37@ApiProperty()
38description = ''
39@ApiProperty()
40mirrorPublicLibrary = false
41
42/**
43* @deprecated Use .role property instead
44* @date 2023-05-05 09:25
45*/
46@ApiProperty({
47type: UserPublicData,
48description: 'Deprecated: use .role property instead'
49})
50owner = new UserPublicData()
51
52@ApiProperty({
53type: [PurchaseOption],
54required: false,
55description:
56'Array of PurchaseOptions, e.g. for sale at xyz price with abc license. Each purchaseOption has an enabled boolean'
57})
58purchaseOptions = []
59
60@ApiProperty({
61type: UserPublicData
62})
63creator = new UserPublicData()
64@ApiProperty()
65public = true
66@ApiProperty()
67thumbnail = ''
68@ApiProperty({ type: Tags })
69tags = {}
70// Transform properties.
71@ApiProperty()
72initPositionX = 0
73@ApiProperty()
74initPositionZ = 0
75@ApiProperty()
76initRotationX = 0
77@ApiProperty()
78initRotationY = 0
79@ApiProperty()
80initRotationZ = 0
81@ApiProperty()
82initScaleX = 0
83@ApiProperty()
84initScaleY = 0
85@ApiProperty()
86initScaleZ = 0
87// Physics properties.
88@ApiProperty()
89collisionEnabled = true
90@ApiProperty()
91staticEnabled = true
92@ApiProperty()
93massKg = 1.0
94@ApiProperty()
95gravityScale = 1.0
96// Material properties.
97@ApiProperty()
98objectColor = [1.0, 1.0, 1.0, 1.0]
99@ApiProperty()
100isSoftDeleted: boolean
101@ApiProperty()
102softDeletedAt: string
103@ApiProperty()
104fileHash: string
105@ApiProperty()
106purchasedParentAssetId: string
107// asset pack properties
108@ApiProperty()
109assetPack: boolean
110@ApiProperty()
111assetsInPack: ObjectId[]
112}
113
114export type AssetDocument = Asset & Document // Note: we also have subclasses/disciminators for materials and textures (as of 2023-02-04 20:18:08). Walkthrough: https://www.loom.com/share/7e09d2777ef94368bcd5fd8c8341b5ef
115
116@Schema({
117timestamps: true,
118toJSON: {
119virtuals: true
120}
121})
122export class Asset {
123@ApiProperty()
124_id: string
125
126@ApiProperty()
127createdAt: Date // this is managed by mongoose timestamps: true, but defining it here so types will align
128@ApiProperty()
129updatedAt: Date // this is managed by mongoose timestamps: true, but defining it here so types will align
130
131@Prop({
132required: true
133})
134@ApiProperty()
135name: string
136
137@Prop({
138required: true,
139default: () => ASSET_TYPE.PANEL,
140enum: ASSET_TYPE,
141type: String
142})
143@ApiProperty({ enum: ASSET_TYPE })
144assetType: string
145
146@Prop({
147required: false,
148default: () => ASSET_TYPE.PANEL,
149enum: ASSET_TYPE,
150type: String
151})
152@ApiProperty({ enum: ASSET_TYPE })
153gameplayType: string
154
155@Prop()
156@ApiProperty()
157description: string
158
159@Prop({
160required: true,
161default: false
162})
163@ApiProperty()
164mirrorPublicLibrary: boolean // also see thirdPartySource tag
165
166@Prop({ type: mongoose.Schema.Types.ObjectId, ref: 'CustomData' })
167@ApiProperty()
168customData: CustomData
169
170/**
171* @deprecated Use .role property instead
172* @date 2023-05-05 09:25
173*/
174@Prop({ type: mongoose.Schema.Types.ObjectId, ref: 'User', required: false })
175@ApiProperty({
176description: 'Deprecated: use .role property instead'
177})
178owner: User
179
180@Prop({ type: [PurchaseOptionSchema], required: false })
181@ApiProperty({
182type: () => PurchaseOption,
183description:
184'Array of PurchaseOptions, e.g. for sale at xyz price with abc license. Each purchaseOption has an enabled boolean'
185})
186purchaseOptions?: PurchaseOption[]
187
188@Prop({ type: LicenseSchema, required: false })
189@ApiProperty({
190type: () => License,
191description: 'License for the entity. By default, a creator has all rights.'
192})
193license?: License
194
195@Prop({ type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true })
196@ApiProperty()
197creator: User
198
199/**
200* @deprecated Use .role property instead
201* @date 2023-05-05 09:25
202*/
203@Prop({
204required: true,
205default: true
206})
207@ApiProperty({
208description: 'Deprecated: use .role property instead'
209})
210public: boolean // should the asset be viewable by public?
211
212@Prop(String)
213@ApiProperty()
214thumbnail: string
215
216@Prop()
217@ApiProperty()
218currentFile: string
219
220@Prop({ required: false })
221@ApiProperty()
222fileHash?: string
223
224// Transform properties.
225@Prop({})
226@ApiProperty()
227initPositionX: number
228
229@Prop({})
230@ApiProperty()
231initPositionY: number
232
233@Prop({})
234@ApiProperty()
235initPositionZ: number
236
237@Prop({
238default: 0
239})
240@ApiProperty()
241initRotationX: number
242
243@Prop({
244default: 0
245})
246@ApiProperty()
247initRotationY: number
248
249@Prop({
250default: 0
251})
252@ApiProperty()
253initRotationZ: number
254
255@Prop({
256default: 0
257})
258@ApiProperty()
259initScaleX: number
260
261@Prop({
262default: 0
263})
264@ApiProperty()
265initScaleY: number
266
267@Prop({
268default: 0
269})
270@ApiProperty()
271initScaleZ: number
272
273// Physics properties.
274@Prop({
275default: true
276})
277@ApiProperty()
278collisionEnabled: boolean
279
280@Prop({
281default: true
282})
283@ApiProperty()
284staticEnabled: boolean
285
286@Prop({
287default: 1.0
288})
289@ApiProperty()
290massKg: number
291
292@Prop({
293default: 1.0
294})
295@ApiProperty()
296gravityScale: number
297
298@Prop({ type: TagsSchema, required: false, default: {} })
299@ApiProperty()
300tags: Tags
301
302@Prop({ type: Boolean, required: false })
303@ApiProperty()
304isSoftDeleted: boolean
305
306@Prop({ type: Date, required: false })
307@ApiProperty()
308softDeletedAt: string
309
310@Prop({ type: Boolean, required: false })
311@ApiProperty()
312isEquipable: boolean
313
314// Material properties.
315@Prop({
316default: [1.0, 1.0, 1.0, 1.0],
317type: mongoose.Types.Array
318})
319@ApiProperty()
320objectColor: Vector4AsArray
321
322/**
323* START Section: ISchemaWithRole implementer
324*/
325@Prop({
326required: true,
327type: RoleSchema
328})
329@ApiProperty()
330role: Role
331/**
332* END Section: ISchemaWithRole implementer
333*/
334// If the asset was purchased, this is the ID of that original asset since assets are copied upon purchase
335@Prop({ type: String, required: false })
336@ApiProperty()
337purchasedParentAssetId: string
338
339@Prop({ type: Boolean, required: false, default: undefined })
340@ApiProperty()
341assetPack: boolean
342
343@Prop({
344type: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Asset' }],
345required: false,
346default: undefined
347})
348@ApiProperty()
349assetsInPack: ObjectId[]
350}
351
352export const AssetSchema = SchemaFactory.createForClass(Asset)
353