Removed redundant code
This commit is contained in:
parent
e571cf2230
commit
a95c67b5fe
@ -43,10 +43,10 @@ export class Character extends BaseEntity {
|
|||||||
|
|
||||||
// Customization
|
// Customization
|
||||||
@ManyToOne()
|
@ManyToOne()
|
||||||
characterType?: CharacterType
|
characterType?: CharacterType | null | undefined
|
||||||
|
|
||||||
@ManyToOne()
|
@ManyToOne()
|
||||||
characterHair?: CharacterHair
|
characterHair?: CharacterHair | null | undefined
|
||||||
|
|
||||||
// Inventory
|
// Inventory
|
||||||
@OneToMany({ mappedBy: 'character' })
|
@OneToMany({ mappedBy: 'character' })
|
||||||
@ -173,7 +173,7 @@ export class Character extends BaseEntity {
|
|||||||
return this.rotation
|
return this.rotation
|
||||||
}
|
}
|
||||||
|
|
||||||
setCharacterType(characterType: CharacterType | undefined) {
|
setCharacterType(characterType: CharacterType | null | undefined) {
|
||||||
this.characterType = characterType
|
this.characterType = characterType
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
@ -182,7 +182,7 @@ export class Character extends BaseEntity {
|
|||||||
return this.characterType
|
return this.characterType
|
||||||
}
|
}
|
||||||
|
|
||||||
setCharacterHair(characterHair: CharacterHair | undefined) {
|
setCharacterHair(characterHair: CharacterHair | null | undefined) {
|
||||||
this.characterHair = characterHair
|
this.characterHair = characterHair
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
@ -143,5 +143,3 @@ export class Server {
|
|||||||
// Start the server
|
// Start the server
|
||||||
const server = new Server()
|
const server = new Server()
|
||||||
server.start()
|
server.start()
|
||||||
|
|
||||||
appLogger.info('Server started')
|
|
||||||
|
@ -18,44 +18,6 @@ interface Position {
|
|||||||
export class CharacterService {
|
export class CharacterService {
|
||||||
private readonly MOVEMENT_DELAY_MS = 250
|
private readonly MOVEMENT_DELAY_MS = 250
|
||||||
|
|
||||||
async create(name: string, userId: number) {
|
|
||||||
const user = await UserRepository.getById(userId)
|
|
||||||
if (!user) return null
|
|
||||||
|
|
||||||
const character = new Character()
|
|
||||||
character.setName(name).setUser(user)
|
|
||||||
|
|
||||||
return await character.save()
|
|
||||||
}
|
|
||||||
|
|
||||||
async updateHair(characterId: number, characterHairId: number | null) {
|
|
||||||
const character = await CharacterRepository.getById(characterId)
|
|
||||||
if (!character) return null
|
|
||||||
|
|
||||||
if (characterHairId === null) {
|
|
||||||
character.setCharacterHair(undefined)
|
|
||||||
return await character.save()
|
|
||||||
}
|
|
||||||
|
|
||||||
const characterHair = await CharacterHairRepository.getById(characterHairId)
|
|
||||||
character.setCharacterHair(characterHair ?? undefined)
|
|
||||||
|
|
||||||
return await character.save()
|
|
||||||
}
|
|
||||||
|
|
||||||
async deleteByUserIdAndId(userId: number, characterId: number): Promise<Character | null> {
|
|
||||||
try {
|
|
||||||
const character = await CharacterRepository.getByUserAndId(userId, characterId)
|
|
||||||
if (!character) return null
|
|
||||||
|
|
||||||
return await character.delete()
|
|
||||||
} catch (error: any) {
|
|
||||||
// Handle error
|
|
||||||
appLogger.error(`Failed to delete character by user ID and character ID: ${error instanceof Error ? error.message : String(error)}`)
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async updateCharacterPosition(id: number, positionX: number, positionY: number, rotation: number, zoneId: number) {
|
async updateCharacterPosition(id: number, positionX: number, positionY: number, rotation: number, zoneId: number) {
|
||||||
const character = await CharacterRepository.getById(id)
|
const character = await CharacterRepository.getById(id)
|
||||||
if (!character) return null
|
if (!character) return null
|
||||||
@ -71,19 +33,6 @@ export class CharacterService {
|
|||||||
return character
|
return character
|
||||||
}
|
}
|
||||||
|
|
||||||
public updatePosition(character: Character, position: Position, newZoneId?: number): void {
|
|
||||||
if (!this.isValidPosition(position)) {
|
|
||||||
gameLogger.error(`Invalid position coordinates: ${position.x}, ${position.y}`)
|
|
||||||
}
|
|
||||||
|
|
||||||
Object.assign(character, {
|
|
||||||
positionX: position.x,
|
|
||||||
positionY: position.y,
|
|
||||||
rotation: Rotation.calculate(character.positionX, character.positionY, position.x, position.y),
|
|
||||||
zoneId: newZoneId ?? character.zone!.id
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
public async calculatePath(character: Character, targetX: number, targetY: number): Promise<Position[] | null> {
|
public async calculatePath(character: Character, targetX: number, targetY: number): Promise<Position[] | null> {
|
||||||
const zone = ZoneManager.getZoneById(character.zone!.id)
|
const zone = ZoneManager.getZoneById(character.zone!.id)
|
||||||
const grid = await zone?.getGrid()
|
const grid = await zone?.getGrid()
|
||||||
|
@ -4,6 +4,7 @@ import CharacterRepository from '#repositories/characterRepository'
|
|||||||
import { gameLogger } from '#application/logger'
|
import { gameLogger } from '#application/logger'
|
||||||
import ZoneManager from '#managers/zoneManager'
|
import ZoneManager from '#managers/zoneManager'
|
||||||
import { CharacterService } from '#services/characterService'
|
import { CharacterService } from '#services/characterService'
|
||||||
|
import CharacterHairRepository from '#repositories/characterHairRepository'
|
||||||
|
|
||||||
interface CharacterConnectPayload {
|
interface CharacterConnectPayload {
|
||||||
characterId: number
|
characterId: number
|
||||||
@ -32,16 +33,16 @@ export default class CharacterConnectEvent {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update hair
|
|
||||||
const characterService = new CharacterService()
|
|
||||||
await characterService.updateHair(characterId, characterHairId ?? null)
|
|
||||||
|
|
||||||
const character = await CharacterRepository.getByUserAndId(this.socket.userId!, characterId)
|
const character = await CharacterRepository.getByUserAndId(this.socket.userId!, characterId)
|
||||||
|
|
||||||
if (!character) {
|
if (!character) {
|
||||||
this.emitError('Character not found or does not belong to this user')
|
this.emitError('Character not found or does not belong to this user')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const characterHair = await CharacterHairRepository.getById(characterHairId ?? 0)
|
||||||
|
await character.setCharacterHair(characterHair).save()
|
||||||
|
|
||||||
this.socket.characterId = character.id
|
this.socket.characterId = character.id
|
||||||
this.socket.emit('character:connect', character)
|
this.socket.emit('character:connect', character)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
@ -2,10 +2,10 @@ import { Server } from 'socket.io'
|
|||||||
import { TSocket } from '#application/types'
|
import { TSocket } from '#application/types'
|
||||||
import { Character } from '#entities/character'
|
import { Character } from '#entities/character'
|
||||||
import CharacterRepository from '#repositories/characterRepository'
|
import CharacterRepository from '#repositories/characterRepository'
|
||||||
import { CharacterService } from '#services/characterService'
|
|
||||||
import { ZCharacterCreate } from '#application/zodTypes'
|
import { ZCharacterCreate } from '#application/zodTypes'
|
||||||
import { gameLogger } from '#application/logger'
|
import { gameLogger } from '#application/logger'
|
||||||
import { ZodError } from 'zod'
|
import { ZodError } from 'zod'
|
||||||
|
import UserRepository from '#repositories/userRepository'
|
||||||
|
|
||||||
export default class CharacterCreateEvent {
|
export default class CharacterCreateEvent {
|
||||||
constructor(
|
constructor(
|
||||||
@ -22,7 +22,11 @@ export default class CharacterCreateEvent {
|
|||||||
try {
|
try {
|
||||||
data = ZCharacterCreate.parse(data)
|
data = ZCharacterCreate.parse(data)
|
||||||
|
|
||||||
const user_id = this.socket.userId!
|
const user = await UserRepository.getById(this.socket.userId!)
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
return this.socket.emit('notification', { message: 'User not found' })
|
||||||
|
}
|
||||||
|
|
||||||
// Check if character name already exists
|
// Check if character name already exists
|
||||||
const characterExists = await CharacterRepository.getByName(data.name)
|
const characterExists = await CharacterRepository.getByName(data.name)
|
||||||
@ -31,18 +35,21 @@ export default class CharacterCreateEvent {
|
|||||||
return this.socket.emit('notification', { message: 'Character name already exists' })
|
return this.socket.emit('notification', { message: 'Character name already exists' })
|
||||||
}
|
}
|
||||||
|
|
||||||
let characters: Character[] = (await CharacterRepository.getByUserId(user_id)) as Character[]
|
let characters: Character[] = (await CharacterRepository.getByUserId(user.getId()))
|
||||||
|
|
||||||
if (characters.length >= 4) {
|
if (characters.length >= 4) {
|
||||||
return this.socket.emit('notification', { message: 'You can only have 4 characters' })
|
return this.socket.emit('notification', { message: 'You can only have 4 characters' })
|
||||||
}
|
}
|
||||||
|
|
||||||
const characterService = new CharacterService()
|
const newCharacter = new Character()
|
||||||
const character = await characterService.create(data.name, user_id)
|
await newCharacter
|
||||||
|
.setName(data.name)
|
||||||
|
.setUser(user)
|
||||||
|
.save()
|
||||||
|
|
||||||
if (!character) return this.socket.emit('notification', { message: 'Failed to create character. Please try again (later).' })
|
if (!newCharacter) return this.socket.emit('notification', { message: 'Failed to create character. Please try again (later).' })
|
||||||
|
|
||||||
characters = [...characters, character]
|
characters = [...characters, newCharacter]
|
||||||
|
|
||||||
this.socket.emit('character:create:success')
|
this.socket.emit('character:create:success')
|
||||||
this.socket.emit('character:list', characters)
|
this.socket.emit('character:list', characters)
|
||||||
|
@ -26,8 +26,10 @@ export default class CharacterDeleteEvent {
|
|||||||
|
|
||||||
private async handleCharacterDelete(data: TypePayload, callback: (response: TypeResponse) => void): Promise<any> {
|
private async handleCharacterDelete(data: TypePayload, callback: (response: TypeResponse) => void): Promise<any> {
|
||||||
try {
|
try {
|
||||||
const characterService = new CharacterService()
|
const character = await CharacterRepository.getByUserAndId(this.socket.userId!, data.characterId!)
|
||||||
await characterService.deleteByUserIdAndId(this.socket.userId!, data.characterId!)
|
if (character) {
|
||||||
|
await character.delete()
|
||||||
|
}
|
||||||
|
|
||||||
const characters: Character[] = (await CharacterRepository.getByUserId(this.socket.userId!)) as Character[]
|
const characters: Character[] = (await CharacterRepository.getByUserId(this.socket.userId!)) as Character[]
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user