Many many more improvements
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
import { BaseService } from '#application/base/baseService'
|
||||
import config from '#application/config'
|
||||
import { gameLogger } from '#application/logger'
|
||||
import { Character } from '#entities/character'
|
||||
import { Zone } from '#entities/zone'
|
||||
import ZoneManager from '#managers/zoneManager'
|
||||
@ -9,40 +9,25 @@ import ZoneRepository from '#repositories/zoneRepository'
|
||||
type Position = { x: number; y: number }
|
||||
export type Node = Position & { parent?: Node; g: number; h: number; f: number }
|
||||
|
||||
export class CharacterService {
|
||||
class CharacterService extends BaseService {
|
||||
private readonly MOVEMENT_DELAY_MS = 250
|
||||
private readonly DIRECTIONS = [
|
||||
{ x: 0, y: -1 }, // Up
|
||||
{ x: 0, y: 1 }, // Down
|
||||
{ x: -1, y: 0 }, // Left
|
||||
{ x: 1, y: 0 }, // Right
|
||||
{ x: -1, y: -1 },
|
||||
{ x: -1, y: 1 },
|
||||
{ x: 1, y: -1 },
|
||||
{ x: 1, y: 1 }
|
||||
{ x: -1, y: -1 }, // Up left
|
||||
{ x: -1, y: 1 }, // Up right
|
||||
{ x: 1, y: -1 }, // Down left
|
||||
{ x: 1, y: 1 } // Down right
|
||||
]
|
||||
|
||||
public async updateCharacterPosition(id: number, positionX: number, positionY: number, rotation: number, zoneId: number) {
|
||||
const character = await CharacterRepository.getById(id)
|
||||
if (!character) return null
|
||||
|
||||
character
|
||||
.setPositionX(positionX)
|
||||
.setPositionY(positionY)
|
||||
.setRotation(rotation)
|
||||
.setZone((await ZoneRepository.getById(zoneId)) as Zone)
|
||||
|
||||
await character.save()
|
||||
|
||||
return character
|
||||
}
|
||||
|
||||
public async calculatePath(character: Character, targetX: number, targetY: number): Promise<Position[] | null> {
|
||||
const zone = ZoneManager.getZoneById(character.zone!.id)
|
||||
const grid = await zone?.getGrid()
|
||||
|
||||
if (!grid?.length) {
|
||||
gameLogger.error('character:move error', 'Grid not found or empty')
|
||||
this.logger.error('character:move error: Grid not found or empty')
|
||||
return null
|
||||
}
|
||||
|
||||
@ -144,3 +129,5 @@ export class CharacterService {
|
||||
return path
|
||||
}
|
||||
}
|
||||
|
||||
export default new CharacterService()
|
||||
|
@ -1,13 +1,13 @@
|
||||
import { Server } from 'socket.io'
|
||||
|
||||
import { gameLogger } from '#application/logger'
|
||||
import { BaseService } from '#application/base/baseService'
|
||||
import { TSocket } from '#application/types'
|
||||
import { Chat } from '#entities/chat'
|
||||
import CharacterRepository from '#repositories/characterRepository'
|
||||
import ChatRepository from '#repositories/chatRepository'
|
||||
import ZoneRepository from '#repositories/zoneRepository'
|
||||
|
||||
class ChatService {
|
||||
class ChatService extends BaseService {
|
||||
async sendZoneMessage(io: Server, socket: TSocket, message: string, characterId: number, zoneId: number): Promise<boolean> {
|
||||
try {
|
||||
const character = await CharacterRepository.getById(characterId)
|
||||
@ -28,7 +28,7 @@ class ChatService {
|
||||
io.to(zoneId.toString()).emit('chat:message', chat)
|
||||
return true
|
||||
} catch (error: any) {
|
||||
gameLogger.error(`Failed to save chat message: ${error instanceof Error ? error.message : String(error)}`)
|
||||
this.logger.error(`Failed to save chat message: ${error instanceof Error ? error.message : String(error)}`)
|
||||
return false
|
||||
}
|
||||
}
|
||||
@ -46,4 +46,4 @@ class ChatService {
|
||||
}
|
||||
}
|
||||
|
||||
export default ChatService
|
||||
export default new ChatService()
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { appLogger } from '#application/logger'
|
||||
import { BaseService } from '#application/base/baseService'
|
||||
import passwordResetTokenRepository from '#repositories/passwordResetTokenRepository'
|
||||
|
||||
class PasswordResetTokenService {
|
||||
class PasswordResetTokenService extends BaseService {
|
||||
/**
|
||||
* Delete token
|
||||
* @param token
|
||||
@ -17,10 +17,10 @@ class PasswordResetTokenService {
|
||||
|
||||
return true
|
||||
} catch (error: any) {
|
||||
appLogger.error(`Error deleting password reset token: ${error instanceof Error ? error.message : String(error)}`)
|
||||
this.logger.error(`Error deleting password reset token: ${error instanceof Error ? error.message : String(error)}`)
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default PasswordResetTokenService
|
||||
export default new PasswordResetTokenService()
|
||||
|
@ -3,8 +3,8 @@ import NodeMailer from 'nodemailer'
|
||||
|
||||
import PasswordResetTokenService from './passwordResetTokenService' // @TODO: Correctly implement this
|
||||
|
||||
import { BaseService } from '#application/base/baseService'
|
||||
import config from '#application/config'
|
||||
import { httpLogger } from '#application/logger'
|
||||
import { PasswordResetToken } from '#entities/passwordResetToken'
|
||||
import { User } from '#entities/user'
|
||||
import PasswordResetTokenRepository from '#repositories/passwordResetTokenRepository'
|
||||
@ -15,7 +15,7 @@ import UserRepository from '#repositories/userRepository'
|
||||
* Handles user login and registration
|
||||
* @class UserService
|
||||
*/
|
||||
class UserService {
|
||||
class UserService extends BaseService {
|
||||
async login(username: string, password: string): Promise<boolean | User> {
|
||||
try {
|
||||
const user = await UserRepository.getByUsername(username)
|
||||
@ -25,13 +25,13 @@ class UserService {
|
||||
|
||||
const passwordMatch = await bcrypt.compare(password, user.password)
|
||||
if (!passwordMatch) {
|
||||
httpLogger.error(`Failed to login user: ${username}`)
|
||||
this.logger.error(`Failed to login user: ${username}`)
|
||||
return false
|
||||
}
|
||||
|
||||
return user
|
||||
} catch (error: any) {
|
||||
httpLogger.error(`Error logging in user: ${error instanceof Error ? error.message : String(error)}`)
|
||||
this.logger.error(`Error logging in user: ${error instanceof Error ? error.message : String(error)}`)
|
||||
return false
|
||||
}
|
||||
}
|
||||
@ -42,7 +42,7 @@ class UserService {
|
||||
const [userByName, userByEmail] = await Promise.all([UserRepository.getByUsername(username), UserRepository.getByEmail(email)])
|
||||
|
||||
if (userByName || userByEmail) {
|
||||
httpLogger.error(`User already exists: ${userByEmail ? email : username}`)
|
||||
this.logger.error(`User already exists: ${userByEmail ? email : username}`)
|
||||
return false
|
||||
}
|
||||
|
||||
@ -52,7 +52,7 @@ class UserService {
|
||||
|
||||
return newUser
|
||||
} catch (error: any) {
|
||||
httpLogger.error(`Error registering user: ${error instanceof Error ? error.message : String(error)}`)
|
||||
this.logger.error(`Error registering user: ${error instanceof Error ? error.message : String(error)}`)
|
||||
return false
|
||||
}
|
||||
}
|
||||
@ -101,7 +101,7 @@ class UserService {
|
||||
|
||||
return true
|
||||
} catch (error: any) {
|
||||
httpLogger.error(`Error sending password reset email: ${error instanceof Error ? error.message : String(error)}`)
|
||||
this.logger.error(`Error sending password reset email: ${error instanceof Error ? error.message : String(error)}`)
|
||||
return false
|
||||
}
|
||||
}
|
||||
@ -126,10 +126,10 @@ class UserService {
|
||||
|
||||
return true
|
||||
} catch (error: any) {
|
||||
httpLogger.error(`Error setting new password: ${error instanceof Error ? error.message : String(error)}`)
|
||||
this.logger.error(`Error setting new password: ${error instanceof Error ? error.message : String(error)}`)
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default UserService
|
||||
export default new UserService()
|
||||
|
@ -1,8 +1,8 @@
|
||||
import { gameLogger } from '#application/logger'
|
||||
import { BaseService } from '#application/base/baseService'
|
||||
import { World } from '#entities/world'
|
||||
import WorldRepository from '#repositories/worldRepository'
|
||||
|
||||
class WorldService {
|
||||
class WorldService extends BaseService {
|
||||
async update(worldData: Partial<World>): Promise<boolean> {
|
||||
try {
|
||||
let world = await WorldRepository.getFirst()
|
||||
@ -29,7 +29,7 @@ class WorldService {
|
||||
|
||||
return true
|
||||
} catch (error: any) {
|
||||
gameLogger.error(`Failed to update world: ${error instanceof Error ? error.message : String(error)}`)
|
||||
this.logger.error(`Failed to update world: ${error instanceof Error ? error.message : String(error)}`)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
@ -1,17 +1,17 @@
|
||||
import { Server } from 'socket.io'
|
||||
|
||||
import { gameLogger } from '#application/logger'
|
||||
import { BaseService } from '#application/base/baseService'
|
||||
import { ExtendedCharacter, TSocket } from '#application/types'
|
||||
import { ZoneEventTileTeleport } from '#entities/zoneEventTileTeleport'
|
||||
import ZoneManager from '#managers/zoneManager'
|
||||
|
||||
export class ZoneEventTileService {
|
||||
class ZoneEventTileService extends BaseService {
|
||||
public async handleTeleport(io: Server, socket: TSocket, character: ExtendedCharacter, teleport: ZoneEventTileTeleport): Promise<void> {
|
||||
if (teleport.toZone.id === character.zone!.id) return
|
||||
|
||||
const loadedZone = ZoneManager.getZoneById(teleport.toZone.id)
|
||||
if (!loadedZone) {
|
||||
gameLogger.error('zone:character:join error', 'Loaded zone not found')
|
||||
this.logger.error('zone:character:join error', 'Loaded zone not found')
|
||||
return
|
||||
}
|
||||
|
||||
@ -45,3 +45,5 @@ export class ZoneEventTileService {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export default new ZoneEventTileService()
|
||||
|
@ -1,4 +1,6 @@
|
||||
class ZoneService {
|
||||
import { BaseService } from '#application/base/baseService'
|
||||
|
||||
class ZoneService extends BaseService {
|
||||
public flattenZoneArray(tiles: string[][]) {
|
||||
const normalArray = []
|
||||
|
||||
@ -10,4 +12,4 @@ class ZoneService {
|
||||
}
|
||||
}
|
||||
|
||||
export default ZoneService
|
||||
export default new ZoneService()
|
||||
|
Reference in New Issue
Block a user