Skip to content

Commit

Permalink
Entity force sync
Browse files Browse the repository at this point in the history
  • Loading branch information
NotArandomGUY committed Mar 20, 2024
1 parent f3e0b2f commit add173e
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 10 deletions.
6 changes: 5 additions & 1 deletion src/kcpServer/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ export default class Client extends BaseClass {

// Send packet
async sendPacket(packetName: string, packetHead: PacketHead, obj: object) {
await this.server.socket.sendPacket(this.conv, packetName, packetHead, noCleanupPackets.includes(packetName) ? obj : protoCleanup(obj))
try {
await this.server.socket.sendPacket(this.conv, packetName, packetHead, noCleanupPackets.includes(packetName) ? obj : protoCleanup(obj))
} catch (err) {
logger.error('generic.param1', err)
}
}
}
54 changes: 54 additions & 0 deletions src/kcpServer/packets/EntityForceSync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import Packet, { PacketContext, PacketInterface } from '#/packet'
import { ClientStateEnum } from '@/types/enum'
import { MotionInfo } from '@/types/proto'
import { RetcodeEnum } from '@/types/proto/enum'

export interface EntityForceSyncReq {
roomId: number
entityId: number
motionInfo: MotionInfo
sceneTime: number
}

export interface EntityForceSyncRsp {
retcode: RetcodeEnum
entityId?: number
failMotion?: MotionInfo
sceneTime?: number
}

class EntityForceSyncPacket extends Packet implements PacketInterface {
constructor() {
super('EntityForceSync', {
reqState: ClientStateEnum.IN_GAME,
reqStatePass: true
})
}

async request(context: PacketContext, data: EntityForceSyncReq): Promise<void> {
const { currentScene } = context.player
const { entityManager } = currentScene
const { entityId, motionInfo, sceneTime } = data
const entity = entityManager.getEntity(entityId, true)

if (!entity) {
await this.response(context, { retcode: RetcodeEnum.RET_ENTITY_NOT_EXIST })
return
}

entity.motion.update(motionInfo, sceneTime)

await this.response(context, {
retcode: RetcodeEnum.RET_SUCC,
entityId,
sceneTime
})
}

async response(context: PacketContext, data: EntityForceSyncRsp): Promise<void> {
await super.response(context, data)
}
}

let packet: EntityForceSyncPacket
export default (() => packet = packet || new EntityForceSyncPacket())()
1 change: 0 additions & 1 deletion src/kcpServer/packets/SceneEntityMove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ class SceneEntityMovePacket extends Packet implements PacketInterface {
await this.response(context, {
retcode: RetcodeEnum.RET_SUCC,
entityId,
failMotion: motionInfo,
sceneTime,
reliableSeq
})
Expand Down
21 changes: 13 additions & 8 deletions src/kcpServer/utils/protoCleanup.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import Logger from '@/logger'

const logger = new Logger('PROTOU', 0xc2f542)

function keepValue(val: any): boolean {
if (val == null) return false

Expand All @@ -17,22 +21,23 @@ function keepValue(val: any): boolean {
}
}

export default function protoCleanup(obj: any) {
obj = { ...obj } // clone
export default function protoCleanup(obj: any, layer: number = 0) {
if (layer > 32) {
logger.warn('deep nesting detected:', obj)
throw new Error('Abort')
}

// Clone object
obj = { ...obj }

for (const k in obj) {
const v = obj[k]

if (!keepValue(v)) {
delete obj[k]
continue
}

if (
v != null &&
typeof v === 'object' &&
!Array.isArray(v)
) obj[k] = protoCleanup(v)
if (v != null && typeof v === 'object' && !Array.isArray(v)) obj[k] = protoCleanup(v, layer + 1)
}

return obj
Expand Down

0 comments on commit add173e

Please sign in to comment.