1
0
forked from noxious/server

Map event tile improvements

This commit is contained in:
2025-01-05 06:22:22 +01:00
parent 57b21f1499
commit d7982493e1
23 changed files with 198 additions and 115 deletions

View File

@ -6,7 +6,10 @@ class CharacterHairRepository extends BaseRepository {
async getFirst() {
try {
const repository = this.getEntityManager().getRepository(CharacterHair)
return await repository.findOne({ id: { $exists: true } })
const result = await repository.findOne({ id: { $exists: true } })
if (result) result.setEntityManager(this.getEntityManager())
return result
} catch (error: any) {
this.logger.error(`Failed to get first character hair: ${error instanceof Error ? error.message : String(error)}`)
return null
@ -16,7 +19,10 @@ class CharacterHairRepository extends BaseRepository {
async getAll(): Promise<CharacterHair[]> {
try {
const repository = this.getEntityManager().getRepository(CharacterHair)
return await repository.findAll()
const results = await repository.findAll()
for (const result of results) result.setEntityManager(this.getEntityManager())
return results
} catch (error: any) {
this.logger.error(`Failed to get all character hair: ${error instanceof Error ? error.message : String(error)}`)
return []
@ -26,7 +32,10 @@ class CharacterHairRepository extends BaseRepository {
async getAllSelectable(populate?: any): Promise<CharacterHair[]> {
try {
const repository = this.getEntityManager().getRepository(CharacterHair)
return await repository.find({ isSelectable: true }, { populate })
const results = await repository.find({ isSelectable: true }, { populate })
for (const result of results) result.setEntityManager(this.getEntityManager())
return results
} catch (error: any) {
this.logger.error(`Failed to get selectable character hair: ${error instanceof Error ? error.message : String(error)}`)
return []
@ -36,7 +45,10 @@ class CharacterHairRepository extends BaseRepository {
async getById(id: UUID): Promise<CharacterHair | null> {
try {
const repository = this.getEntityManager().getRepository(CharacterHair)
return await repository.findOne({ id })
const result = await repository.findOne({ id })
if (result) result.setEntityManager(this.getEntityManager())
return result
} catch (error: any) {
this.logger.error(`Failed to get character hair by ID: ${error instanceof Error ? error.message : String(error)}`)
return null

View File

@ -6,7 +6,10 @@ class CharacterRepository extends BaseRepository {
async getByUserId(userId: UUID): Promise<Character[]> {
try {
const repository = this.getEntityManager().getRepository(Character)
return await repository.find({ user: userId })
const results = await repository.find({ user: userId })
for (const result of results) result.setEntityManager(this.getEntityManager())
return results
} catch (error: any) {
this.logger.error(`Failed to get character by user ID: ${error instanceof Error ? error.message : String(error)}`)
return []
@ -16,7 +19,10 @@ class CharacterRepository extends BaseRepository {
async getByUserAndId(userId: UUID, characterId: UUID): Promise<Character | null> {
try {
const repository = this.getEntityManager().getRepository(Character)
return await repository.findOne({ user: userId, id: characterId })
const result = await repository.findOne({ user: userId, id: characterId })
if (result) result.setEntityManager(this.getEntityManager())
return result
} catch (error: any) {
this.logger.error(`Failed to get character by user ID and character ID: ${error instanceof Error ? error.message : String(error)}`)
return null
@ -26,7 +32,10 @@ class CharacterRepository extends BaseRepository {
async getById(id: UUID, populate?: any): Promise<Character | null> {
try {
const repository = this.getEntityManager().getRepository(Character)
return await repository.findOne({ id }, { populate })
const result = await repository.findOne({ id }, { populate })
if (result) result.setEntityManager(this.getEntityManager())
return result
} catch (error: any) {
this.logger.error(`Failed to get character by ID: ${error instanceof Error ? error.message : String(error)}`)
return null
@ -36,7 +45,10 @@ class CharacterRepository extends BaseRepository {
async getByName(name: string, populate?: any): Promise<Character | null> {
try {
const repository = this.getEntityManager().getRepository(Character)
return await repository.findOne({ name }, { populate })
const result = await repository.findOne({ name }, { populate })
if (result) result.setEntityManager(this.getEntityManager())
return result
} catch (error: any) {
this.logger.error(`Failed to get character by name: ${error instanceof Error ? error.message : String(error)}`)
return null

View File

@ -6,7 +6,10 @@ class CharacterTypeRepository extends BaseRepository {
async getFirst() {
try {
const repository = this.getEntityManager().getRepository(CharacterType)
return await repository.findOne({ id: { $exists: true } })
const result = await repository.findOne({ id: { $exists: true } })
if (result) result.setEntityManager(this.getEntityManager())
return result
} catch (error: any) {
this.logger.error(`Failed to get first character type: ${error instanceof Error ? error.message : String(error)}`)
return null
@ -16,7 +19,10 @@ class CharacterTypeRepository extends BaseRepository {
async getAll() {
try {
const repository = this.getEntityManager().getRepository(CharacterType)
return await repository.findAll()
const results = await repository.findAll()
for (const result of results) result.setEntityManager(this.getEntityManager())
return results
} catch (error: any) {
this.logger.error(`Failed to get all character types: ${error instanceof Error ? error.message : String(error)}`)
return []
@ -26,7 +32,10 @@ class CharacterTypeRepository extends BaseRepository {
async getById(id: UUID) {
try {
const repository = this.getEntityManager().getRepository(CharacterType)
return await repository.findOne({ id })
const result = await repository.findOne({ id })
if (result) result.setEntityManager(this.getEntityManager())
return result
} catch (error: any) {
this.logger.error(`Failed to get character type by ID: ${error instanceof Error ? error.message : String(error)}`)
return null

View File

@ -6,9 +6,10 @@ class ChatRepository extends BaseRepository {
async getById(id: UUID): Promise<Chat[]> {
try {
const repository = this.getEntityManager().getRepository(Chat)
return await repository.find({
id
})
const results = await repository.find({ id })
for (const result of results) result.setEntityManager(this.getEntityManager())
return results
} catch (error: any) {
this.logger.error(`Failed to get chat by ID: ${error instanceof Error ? error.message : String(error)}`)
return []
@ -18,7 +19,10 @@ class ChatRepository extends BaseRepository {
async getAll(): Promise<Chat[]> {
try {
const repository = this.getEntityManager().getRepository(Chat)
return await repository.findAll()
const results = await repository.findAll()
for (const result of results) result.setEntityManager(this.getEntityManager())
return results
} catch (error: any) {
this.logger.error(`Failed to get all chats: ${error instanceof Error ? error.message : String(error)}`)
return []
@ -28,7 +32,10 @@ class ChatRepository extends BaseRepository {
async getByCharacterId(characterId: UUID): Promise<Chat[]> {
try {
const repository = this.getEntityManager().getRepository(Chat)
return await repository.find({ character: characterId })
const results = await repository.find({ character: characterId })
for (const result of results) result.setEntityManager(this.getEntityManager())
return results
} catch (error: any) {
this.logger.error(`Failed to get chats by character ID: ${error instanceof Error ? error.message : String(error)}`)
return []
@ -38,7 +45,10 @@ class ChatRepository extends BaseRepository {
async getByMapId(mapId: UUID): Promise<Chat[]> {
try {
const repository = this.getEntityManager().getRepository(Chat)
return await repository.find({ map: mapId })
const results = await repository.find({ map: mapId })
for (const result of results) result.setEntityManager(this.getEntityManager())
return results
} catch (error: any) {
this.logger.error(`Failed to get chats by map ID: ${error instanceof Error ? error.message : String(error)}`)
return []

View File

@ -6,7 +6,10 @@ class ItemRepository extends BaseRepository {
async getById(id: UUID): Promise<Item | null> {
try {
const repository = this.getEntityManager().getRepository(Item)
return await repository.findOne({ id })
const result = await repository.findOne({ id })
if (result) result.setEntityManager(this.getEntityManager())
return result
} catch (error: any) {
this.logger.error(`Failed to get item by ID: ${error instanceof Error ? error.message : String(error)}`)
return null
@ -16,7 +19,10 @@ class ItemRepository extends BaseRepository {
async getByIds(ids: UUID[]): Promise<Item[]> {
try {
const repository = this.getEntityManager().getRepository(Item)
return await repository.find({ id: ids })
const results = await repository.find({ id: ids })
for (const result of results) result.setEntityManager(this.getEntityManager())
return results
} catch (error: any) {
this.logger.error(`Failed to get items by IDs: ${error instanceof Error ? error.message : String(error)}`)
return []
@ -26,7 +32,10 @@ class ItemRepository extends BaseRepository {
async getAll(): Promise<Item[]> {
try {
const repository = this.getEntityManager().getRepository(Item)
return await repository.findAll()
const results = await repository.findAll()
for (const result of results) result.setEntityManager(this.getEntityManager())
return results
} catch (error: any) {
this.logger.error(`Failed to get all items: ${error instanceof Error ? error.message : String(error)}`)
return []

View File

@ -6,9 +6,10 @@ class MapEventTileRepository extends BaseRepository {
async getAll(id: UUID): Promise<MapEventTile[]> {
try {
const repository = this.getEntityManager().getRepository(MapEventTile)
return await repository.find({
map: id
})
const results = await repository.find({ map: id })
for (const result of results) result.setEntityManager(this.getEntityManager())
return results
} catch (error: any) {
this.logger.error(`Failed to get map event tiles: ${error.message}`)
return []
@ -18,11 +19,14 @@ class MapEventTileRepository extends BaseRepository {
async getEventTileByMapIdAndPosition(mapId: UUID, positionX: number, positionY: number) {
try {
const repository = this.getEntityManager().getRepository(MapEventTile)
return await repository.findOne({
const result = await repository.findOne({
map: mapId,
positionX: positionX,
positionY: positionY
})
if (result) result.setEntityManager(this.getEntityManager())
return result
} catch (error: any) {
this.logger.error(`Failed to get map event tile: ${error.message}`)
return null

View File

@ -6,7 +6,10 @@ class MapObjectRepository extends BaseRepository {
async getById(id: UUID): Promise<MapObject | null> {
try {
const repository = this.getEntityManager().getRepository(MapObject)
return await repository.findOne({ id })
const result = await repository.findOne({ id })
if (result) result.setEntityManager(this.getEntityManager())
return result
} catch (error: any) {
return null
}
@ -15,7 +18,10 @@ class MapObjectRepository extends BaseRepository {
async getAll(): Promise<MapObject[]> {
try {
const repository = this.getEntityManager().getRepository(MapObject)
return await repository.findAll()
const results = await repository.findAll()
for (const result of results) result.setEntityManager(this.getEntityManager())
return results
} catch (error: any) {
return []
}

View File

@ -8,7 +8,10 @@ class MapRepository extends BaseRepository {
async getFirst(): Promise<Map | null> {
try {
const repository = this.getEntityManager().getRepository(Map)
return await repository.findOne({ id: { $exists: true } })
const result = await repository.findOne({ id: { $exists: true } })
if (result) result.setEntityManager(this.getEntityManager())
return result
} catch (error: any) {
this.logger.error(`Failed to get first map: ${error instanceof Error ? error.message : String(error)}`)
return null
@ -18,7 +21,10 @@ class MapRepository extends BaseRepository {
async getAll(): Promise<Map[]> {
try {
const repository = this.getEntityManager().getRepository(Map)
return await repository.findAll()
const results = await repository.findAll()
for (const result of results) result.setEntityManager(this.getEntityManager())
return results
} catch (error: any) {
this.logger.error(`Failed to get all map: ${error.message}`)
return []
@ -41,7 +47,10 @@ class MapRepository extends BaseRepository {
async getEventTiles(id: UUID): Promise<MapEventTile[]> {
try {
const repository = this.getEntityManager().getRepository(MapEventTile)
return await repository.find({ map: id })
const results = await repository.find({ map: id })
for (const result of results) result.setEntityManager(this.getEntityManager())
return results
} catch (error: any) {
this.logger.error(`Failed to get map event tiles: ${error.message}`)
return []
@ -51,26 +60,19 @@ class MapRepository extends BaseRepository {
async getFirstEventTile(mapId: UUID, positionX: number, positionY: number): Promise<MapEventTile | null> {
try {
const repository = this.getEntityManager().getRepository(MapEventTile)
return await repository.findOne({
const result = await repository.findOne({
map: mapId,
positionX: positionX,
positionY: positionY
})
if (result) result.setEntityManager(this.getEntityManager())
return result
} catch (error: any) {
this.logger.error(`Failed to get map event tile: ${error.message}`)
return null
}
}
async getMapObjects(id: UUID): Promise<MapObject[]> {
try {
const repository = this.getEntityManager().getRepository(MapObject)
return await repository.find({ map: id })
} catch (error: any) {
this.logger.error(`Failed to get map objects: ${error.message}`)
return []
}
}
}
export default MapRepository

View File

@ -1,4 +1,4 @@
import { BaseRepository } from '#application/base/baseRepository' // Import the global Prisma instance
import { BaseRepository } from '#application/base/baseRepository'
import { UUID } from '#application/types'
import { PasswordResetToken } from '#entities/passwordResetToken'
@ -6,7 +6,10 @@ class PasswordResetTokenRepository extends BaseRepository {
async getById(id: UUID): Promise<any> {
try {
const repository = this.getEntityManager().getRepository(PasswordResetToken)
return await repository.findOne({ id })
const result = await repository.findOne({ id })
if (result) result.setEntityManager(this.getEntityManager())
return result
} catch (error: any) {
// Handle error
this.logger.error(`Failed to get password reset token by ID: ${error instanceof Error ? error.message : String(error)}`)
@ -16,9 +19,12 @@ class PasswordResetTokenRepository extends BaseRepository {
async getByUserId(userId: UUID): Promise<any> {
try {
const repository = this.getEntityManager().getRepository(PasswordResetToken)
return await repository.findOne({
const result = await repository.findOne({
user: userId
})
if (result) result.setEntityManager(this.getEntityManager())
return result
} catch (error: any) {
// Handle error
this.logger.error(`Failed to get password reset token by user ID: ${error instanceof Error ? error.message : String(error)}`)
@ -28,7 +34,10 @@ class PasswordResetTokenRepository extends BaseRepository {
async getByToken(token: string): Promise<any> {
try {
const repository = this.getEntityManager().getRepository(PasswordResetToken)
return await repository.findOne({ token })
const result = await repository.findOne({ token })
if (result) result.setEntityManager(this.getEntityManager())
return result
} catch (error: any) {
// Handle error
this.logger.error(`Failed to get password reset token by token: ${error instanceof Error ? error.message : String(error)}`)

View File

@ -6,7 +6,10 @@ class SpriteRepository extends BaseRepository {
async getById(id: UUID, populate?: any) {
try {
const repository = this.getEntityManager().getRepository(Sprite)
return await repository.findOne({ id }, { populate })
const result = await repository.findOne({ id }, { populate })
if (result) result.setEntityManager(this.getEntityManager())
return result
} catch (error: any) {
return null
}

View File

@ -11,7 +11,10 @@ class TileRepository extends BaseRepository {
async getById(id: UUID) {
try {
const repository = this.getEntityManager().getRepository(Tile)
return await repository.findOne({ id })
const result = await repository.findOne({ id })
if (result) result.setEntityManager(this.getEntityManager())
return result
} catch (error: any) {
return null
}
@ -20,9 +23,12 @@ class TileRepository extends BaseRepository {
async getByIds(ids: UUID[]) {
try {
const repository = this.getEntityManager().getRepository(Tile)
return await repository.find({
const results = await repository.find({
id: ids
})
for (const result of results) result.setEntityManager(this.getEntityManager())
return results
} catch (error: any) {
return []
}
@ -31,7 +37,10 @@ class TileRepository extends BaseRepository {
async getAll() {
try {
const repository = this.getEntityManager().getRepository(Tile)
return await repository.findAll()
const results = await repository.findAll()
for (const result of results) result.setEntityManager(this.getEntityManager())
return results
} catch (error: any) {
return []
}
@ -39,17 +48,20 @@ class TileRepository extends BaseRepository {
async getByMapId(mapId: UUID) {
try {
const repository = this.getEntityManager().getRepository(Map)
const mapRepository = this.getEntityManager().getRepository(Map)
const tileRepository = this.getEntityManager().getRepository(Tile)
const map = await repository.findOne({ id: mapId })
const map = await mapRepository.findOne({ id: mapId })
if (!map) return []
const mapTileArray = unduplicateArray(MapService.flattenMapArray(JSON.parse(JSON.stringify(map.tiles))))
return await tileRepository.find({
const results = await tileRepository.find({
id: mapTileArray
})
for (const result of results) result.setEntityManager(this.getEntityManager())
return results
} catch (error: any) {
return []
}

View File

@ -6,7 +6,10 @@ class UserRepository extends BaseRepository {
async getById(id: UUID) {
try {
const repository = this.getEntityManager().getRepository(User)
return await repository.findOne({ id })
const result = await repository.findOne({ id })
if (result) result.setEntityManager(this.getEntityManager())
return result
} catch (error: any) {
this.logger.error(`Failed to get user by ID: ${error instanceof Error ? error.message : String(error)}`)
return null
@ -16,7 +19,10 @@ class UserRepository extends BaseRepository {
async getByUsername(username: string) {
try {
const repository = this.getEntityManager().getRepository(User)
return await repository.findOne({ username })
const result = await repository.findOne({ username })
if (result) result.setEntityManager(this.getEntityManager())
return result
} catch (error: any) {
this.logger.error(`Failed to get user by username: ${error instanceof Error ? error.message : String(error)}`)
return null
@ -26,7 +32,10 @@ class UserRepository extends BaseRepository {
async getByEmail(email: string) {
try {
const repository = this.getEntityManager().getRepository(User)
return await repository.findOne({ email })
const result = await repository.findOne({ email })
if (result) result.setEntityManager(this.getEntityManager())
return result
} catch (error: any) {
this.logger.error(`Failed to get user by email: ${error instanceof Error ? error.message : String(error)}`)
return null

View File

@ -5,7 +5,10 @@ class WorldRepository extends BaseRepository {
async getFirst() {
try {
const repository = this.getEntityManager().getRepository(World)
return await repository.findOne({ date: { $exists: true } })
const result = await repository.findOne({ date: { $exists: true } })
if (result) result.setEntityManager(this.getEntityManager())
return result
} catch (error: any) {
this.logger.error(`Failed to get first world: ${error instanceof Error ? error.message : String(error)}`)
}