Skip to content

Commit

Permalink
Fix delete component network message
Browse files Browse the repository at this point in the history
  • Loading branch information
gonpombo8 committed Nov 27, 2023
1 parent f719c45 commit 48dc501
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 44 deletions.
6 changes: 6 additions & 0 deletions packages/@dcl/ecs/src/serialization/crdt/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { DeleteComponent } from './deleteComponent'
import { DeleteEntity } from './deleteEntity'
import { AppendValueOperation } from './appendValue'
import { PutNetworkComponentOperation } from './network/putComponentNetwork'
import { DeleteComponentNetwork } from './network/deleteComponentNetwork'
import { DeleteEntityNetwork } from './network/deleteEntityNetwork'

export function readMessage(buf: ByteBuffer): CrdtMessage | null {
const header = CrdtMessageProtocol.getHeader(buf)
Expand All @@ -17,10 +19,14 @@ export function readMessage(buf: ByteBuffer): CrdtMessage | null {
return PutNetworkComponentOperation.read(buf)

Check warning on line 19 in packages/@dcl/ecs/src/serialization/crdt/message.ts

View check run for this annotation

Codecov / codecov/patch

packages/@dcl/ecs/src/serialization/crdt/message.ts#L19

Added line #L19 was not covered by tests
} else if (header.type === CrdtMessageType.DELETE_COMPONENT) {
return DeleteComponent.read(buf)
} else if (header.type === CrdtMessageType.DELETE_COMPONENT_NETWORK) {
return DeleteComponentNetwork.read(buf)

Check warning on line 23 in packages/@dcl/ecs/src/serialization/crdt/message.ts

View check run for this annotation

Codecov / codecov/patch

packages/@dcl/ecs/src/serialization/crdt/message.ts#L23

Added line #L23 was not covered by tests
} else if (header.type === CrdtMessageType.APPEND_VALUE) {
return AppendValueOperation.read(buf)
} else if (header.type === CrdtMessageType.DELETE_ENTITY) {
return DeleteEntity.read(buf)
} else if (header.type === CrdtMessageType.DELETE_ENTITY_NETWORK) {
return DeleteEntityNetwork.read(buf)

Check warning on line 29 in packages/@dcl/ecs/src/serialization/crdt/message.ts

View check run for this annotation

Codecov / codecov/patch

packages/@dcl/ecs/src/serialization/crdt/message.ts#L29

Added line #L29 was not covered by tests
}

return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,12 @@ export namespace DeleteComponentNetwork {
throw new Error('DeleteComponentOperation tried to read another message type.')

Check warning on line 40 in packages/@dcl/ecs/src/serialization/crdt/network/deleteComponentNetwork.ts

View check run for this annotation

Codecov / codecov/patch

packages/@dcl/ecs/src/serialization/crdt/network/deleteComponentNetwork.ts#L40

Added line #L40 was not covered by tests
}

const msg = {
return {

Check warning on line 43 in packages/@dcl/ecs/src/serialization/crdt/network/deleteComponentNetwork.ts

View check run for this annotation

Codecov / codecov/patch

packages/@dcl/ecs/src/serialization/crdt/network/deleteComponentNetwork.ts#L43

Added line #L43 was not covered by tests
...header,
entityId: buf.readUint32() as Entity,
componentId: buf.readUint32(),
timestamp: buf.readUint32(),
networkId: buf.readUint32()
}

return msg
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CrdtMessageProtocol } from '../crdtMessageProtocol'
import { Entity } from '../../../engine/entity'
import { ByteBuffer } from '../../ByteBuffer'
import { CrdtMessageType, CRDT_MESSAGE_HEADER_LENGTH, PutComponentMessage } from '../types'
import { CrdtMessageType, CRDT_MESSAGE_HEADER_LENGTH, PutComponentMessage, PutNetworkComponentMessage } from '../types'

Check warning on line 4 in packages/@dcl/ecs/src/serialization/crdt/network/putComponentNetwork.ts

View workflow job for this annotation

GitHub Actions / lint

'PutComponentMessage' is defined but never used. Allowed unused vars must match /^_|ReactEcs/u

/**
* @public
Expand Down Expand Up @@ -37,11 +37,11 @@ export namespace PutNetworkComponentOperation {
buf.setUint32(startMessageOffset + 12, componentId)
buf.setUint32(startMessageOffset + 16, timestamp)
buf.setUint32(startMessageOffset + 20, networkId)
const newLocal = messageLength - MESSAGE_HEADER_LENGTH - CRDT_MESSAGE_HEADER_LENGTH
buf.setUint32(startMessageOffset + 24, newLocal)
const dataLength = messageLength - MESSAGE_HEADER_LENGTH - CRDT_MESSAGE_HEADER_LENGTH
buf.setUint32(startMessageOffset + 24, dataLength)

Check warning on line 41 in packages/@dcl/ecs/src/serialization/crdt/network/putComponentNetwork.ts

View check run for this annotation

Codecov / codecov/patch

packages/@dcl/ecs/src/serialization/crdt/network/putComponentNetwork.ts#L36-L41

Added lines #L36 - L41 were not covered by tests
}

export function read(buf: ByteBuffer): (PutComponentMessage & { networkId: number }) | null {
export function read(buf: ByteBuffer): PutNetworkComponentMessage | null {
const header = CrdtMessageProtocol.readHeader(buf)

Check warning on line 45 in packages/@dcl/ecs/src/serialization/crdt/network/putComponentNetwork.ts

View check run for this annotation

Codecov / codecov/patch

packages/@dcl/ecs/src/serialization/crdt/network/putComponentNetwork.ts#L45

Added line #L45 was not covered by tests

if (!header) {
Expand Down
1 change: 0 additions & 1 deletion packages/@dcl/ecs/src/serialization/crdt/network/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ export function fixTransformParent(
if (!transform) throw new Error('Invalid parent transform')
// Generate new transform raw data with the parent
const newTransform = { ...transform, parent }
console.log('Fix transform', message.entityId, newTransform)
buffer.resetBuffer()
TransformSchema.serialize(newTransform, buffer)
return buffer.toBinary()

Check warning on line 86 in packages/@dcl/ecs/src/serialization/crdt/network/utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/@dcl/ecs/src/serialization/crdt/network/utils.ts#L83-L86

Added lines #L83 - L86 were not covered by tests
Expand Down
1 change: 0 additions & 1 deletion packages/@dcl/ecs/src/systems/crdt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,6 @@ export function crdtSceneSystem(engine: PreEngine, onProcessEntityComponentChang
continue

Check warning on line 307 in packages/@dcl/ecs/src/systems/crdt/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/@dcl/ecs/src/systems/crdt/index.ts#L307

Added line #L307 was not covered by tests
}
}

// Common message
transportBuffer.writeBuffer(message.messageBuffer, false)
}
Expand Down
22 changes: 19 additions & 3 deletions packages/@dcl/playground-assets/etc/playground-assets.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1393,6 +1393,21 @@ export interface INetowrkEntityType {
networkId: number;
}

// Warning: (ae-missing-release-tag) "INetowrkParent" is part of the package's API, but it is missing a release tag (@alpha, @beta, @public, or @internal)
//
// @public (undocumented)
export type INetowrkParent = LastWriteWinElementSetComponentDefinition<INetowrkParentType>;

// Warning: (ae-missing-release-tag) "INetowrkParentType" is part of the package's API, but it is missing a release tag (@alpha, @beta, @public, or @internal)
//
// @public (undocumented)
export interface INetowrkParentType {
// (undocumented)
entityId: Entity;
// (undocumented)
networkId: number;
}

// Warning: (tsdoc-html-tag-missing-string) The HTML element has an invalid attribute: Expecting an HTML string starting with a single-quote or double-quote character
// Warning: (tsdoc-escape-greater-than) The ">" character should be escaped using a backslash to avoid confusion with an HTML tag
// Warning: (tsdoc-malformed-inline-tag) Expecting a TSDoc tag starting with "{@"
Expand Down Expand Up @@ -1868,6 +1883,9 @@ export interface NameType {
// @alpha
export const NetworkEntity: INetowrkEntity;

// @alpha
export const NetworkParent: INetowrkParent;

// @public (undocumented)
export const enum NftFrameType {
// (undocumented)
Expand Down Expand Up @@ -3300,9 +3318,7 @@ export namespace PutNetworkComponentOperation {
const // (undocumented)
MESSAGE_HEADER_LENGTH = 20;
// (undocumented)
export function read(buf: ByteBuffer): (PutComponentMessage & {
networkId: number;
}) | null;
export function read(buf: ByteBuffer): PutNetworkComponentMessage | null;
export function write(entity: Entity, timestamp: number, componentId: number, networkId: number, data: Uint8Array, buf: ByteBuffer): void;
}

Expand Down
1 change: 0 additions & 1 deletion packages/@dcl/sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,4 @@ export async function onStart() {
}
}
}
console.log('[onStart]: Finished', response.data)
}
16 changes: 11 additions & 5 deletions packages/@dcl/sdk/src/network/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import {
EntityUtils,
RESERVED_STATIC_ENTITIES,
CrdtMessageType,
SyncComponents,
NetworkEntity
SyncComponents as _SyncComponents,
NetworkEntity as _NetworkEntity,
engine
} from '@dcl/ecs'

export function syncFilter(message: Omit<TransportMessage, 'messageBuffer'>) {
Expand All @@ -23,11 +24,16 @@ export function syncFilter(message: Omit<TransportMessage, 'messageBuffer'>) {
return false

Check warning on line 24 in packages/@dcl/sdk/src/network/filter.ts

View check run for this annotation

Codecov / codecov/patch

packages/@dcl/sdk/src/network/filter.ts#L24

Added line #L24 was not covered by tests
}

// Network Entity Always
if (message.type === CrdtMessageType.DELETE_ENTITY) {
const NetworkEntity = engine.getComponent(_NetworkEntity.componentId) as typeof _NetworkEntity
const network = NetworkEntity.getOrNull(message.entityId)

Check warning on line 28 in packages/@dcl/sdk/src/network/filter.ts

View check run for this annotation

Codecov / codecov/patch

packages/@dcl/sdk/src/network/filter.ts#L27-L28

Added lines #L27 - L28 were not covered by tests
// Delete Network Entity Always
if (
message.type === CrdtMessageType.DELETE_ENTITY_NETWORK ||
(network && message.type === CrdtMessageType.DELETE_ENTITY)

Check warning on line 32 in packages/@dcl/sdk/src/network/filter.ts

View check run for this annotation

Codecov / codecov/patch

packages/@dcl/sdk/src/network/filter.ts#L32

Added line #L32 was not covered by tests
) {
return true

Check warning on line 34 in packages/@dcl/sdk/src/network/filter.ts

View check run for this annotation

Codecov / codecov/patch

packages/@dcl/sdk/src/network/filter.ts#L34

Added line #L34 was not covered by tests
}

const SyncComponents = engine.getComponent(_SyncComponents.componentId) as typeof _SyncComponents
const sync = SyncComponents.getOrNull(message.entityId)

Check warning on line 37 in packages/@dcl/sdk/src/network/filter.ts

View check run for this annotation

Codecov / codecov/patch

packages/@dcl/sdk/src/network/filter.ts#L36-L37

Added lines #L36 - L37 were not covered by tests
if (!sync) return false

Expand Down
2 changes: 1 addition & 1 deletion packages/@dcl/sdk/src/network/message-bus-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { BinaryMessageBus, CommsMessage } from './binary-message-bus'
import {

Check warning on line 8 in packages/@dcl/sdk/src/network/message-bus-sync.ts

View check run for this annotation

Codecov / codecov/patch

packages/@dcl/sdk/src/network/message-bus-sync.ts#L4-L8

Added lines #L4 - L8 were not covered by tests
definePlayersInScene,
fetchProfile,
oldestUser,
oldestUser as _oldestUser,
setInitialized,
stateInitialized,
stateInitializedChecker,
Expand Down
56 changes: 34 additions & 22 deletions packages/@dcl/sdk/src/network/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { EngineInfo, Entity, IEngine, Schemas } from '@dcl/ecs'
import {

Check warning on line 1 in packages/@dcl/sdk/src/network/utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/@dcl/sdk/src/network/utils.ts#L1

Added line #L1 was not covered by tests
EngineInfo as _EngineInfo,
Entity,
IEngine,
NetworkEntity as _NetworkEntity,
Schemas,
LastWriteWinElementSetComponentDefinition,
PBEngineInfo
} from '@dcl/ecs'
import { componentNumberFromName } from '@dcl/ecs/dist/components/component-number'

Check warning on line 10 in packages/@dcl/sdk/src/network/utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/@dcl/sdk/src/network/utils.ts#L10

Added line #L10 was not covered by tests

import { getUserData } from '~system/UserIdentity'

Check warning on line 12 in packages/@dcl/sdk/src/network/utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/@dcl/sdk/src/network/utils.ts#L12

Added line #L12 was not covered by tests
import { getConnectedPlayers } from '~system/Players'
import { SyncEntity } from './entities'
import { IProfile } from './message-bus-sync'

Expand Down Expand Up @@ -60,7 +67,6 @@ export function createPlayerTimestampData(engine: IEngine, profile: IProfile, sy
* Check if I'm the older user to send the initial state
*/
export function oldestUser(engine: IEngine, profile: IProfile, syncEntity: SyncEntity): boolean {
console.log('[ME]: ', profile)
const PlayersInScene = definePlayersInScene(engine)

Check warning on line 70 in packages/@dcl/sdk/src/network/utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/@dcl/sdk/src/network/utils.ts#L69-L70

Added lines #L69 - L70 were not covered by tests
// When the user leaves the scene but it's still connected.
if (!PlayersInScene.has(playerSceneEntity)) {
Expand All @@ -69,7 +75,6 @@ export function oldestUser(engine: IEngine, profile: IProfile, syncEntity: SyncE
}
const { timestamp } = PlayersInScene.get(playerSceneEntity)
for (const [_, player] of engine.getEntitiesWith(PlayersInScene)) {

Check warning on line 77 in packages/@dcl/sdk/src/network/utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/@dcl/sdk/src/network/utils.ts#L76-L77

Added lines #L76 - L77 were not covered by tests
console.log(player)
if (player.timestamp < timestamp) return false
}
return true

Check warning on line 80 in packages/@dcl/sdk/src/network/utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/@dcl/sdk/src/network/utils.ts#L80

Added line #L80 was not covered by tests
Expand All @@ -79,6 +84,9 @@ export function oldestUser(engine: IEngine, profile: IProfile, syncEntity: SyncE
* Ignore CRDT's initial messages from the renderer.
*/
export function syncTransportIsReady(engine: IEngine) {
const EngineInfo = engine.getComponent(

Check warning on line 87 in packages/@dcl/sdk/src/network/utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/@dcl/sdk/src/network/utils.ts#L86-L87

Added lines #L86 - L87 were not covered by tests
_EngineInfo.componentId
) as LastWriteWinElementSetComponentDefinition<PBEngineInfo>
if (!INITIAL_CRDT_RENDERER_MESSAGES_SENT) {
const engineInfo = EngineInfo.getOrNull(engine.RootEntity)

Check warning on line 91 in packages/@dcl/sdk/src/network/utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/@dcl/sdk/src/network/utils.ts#L91

Added line #L91 was not covered by tests
if (engineInfo && engineInfo.tickNumber > 2) {
Expand All @@ -93,22 +101,26 @@ export function syncTransportIsReady(engine: IEngine) {
* Add the playerSceneData component and syncronize it till we receive the state.
* This fn should be added as a system so it runs on every tick
*/
export function stateInitializedChecker(engine: IEngine, profile: IProfile, syncEntity: SyncEntity) {
const PlayersInScene = definePlayersInScene(engine)
// TODO: Had to comment all the logic because getConnectedPlayers was not working as expected
// A lot of raise conditions. For now we will go with the approach that every client that it's initialized will send his crdt state.
export function stateInitializedChecker(engine: IEngine, _profile: IProfile, _syncEntity: SyncEntity) {

Check warning on line 106 in packages/@dcl/sdk/src/network/utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/@dcl/sdk/src/network/utils.ts#L106

Added line #L106 was not covered by tests
// const PlayersInScene = definePlayersInScene(engine)
const EngineInfo = engine.getComponent(_EngineInfo.componentId) as typeof _EngineInfo

Check warning on line 108 in packages/@dcl/sdk/src/network/utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/@dcl/sdk/src/network/utils.ts#L108

Added line #L108 was not covered by tests
// const NetworkEntity = engine.getComponent(_NetworkEntity.componentId) as INetowrkEntity
async function enterScene() {

Check warning on line 110 in packages/@dcl/sdk/src/network/utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/@dcl/sdk/src/network/utils.ts#L110

Added line #L110 was not covered by tests
if (!playerSceneEntity) {
createPlayerTimestampData(engine, profile, syncEntity)
}
// if (!playerSceneEntity) {
// createPlayerTimestampData(engine, profile, syncEntity)
// }

/**
* Keeps PlayersInScene up-to-date with the current players.
*/
const connectedPlayers = await getConnectedPlayers({})
for (const [entity, player] of engine.getEntitiesWith(PlayersInScene)) {
if (!connectedPlayers.players.find(($) => $.userId === player.userId)) {
PlayersInScene.deleteFrom(entity)
}
}
// const connectedPlayers = await getConnectedPlayers({})
// for (const [entity, player] of engine.getEntitiesWith(PlayersInScene)) {
// if (!connectedPlayers.players.find(($) => $.userId === player.userId)) {
// PlayersInScene.deleteFrom(entity)
// }
// }

// Wait for comms to be ready ?? ~3000ms
if ((EngineInfo.getOrNull(engine.RootEntity)?.tickNumber ?? 0) > 100) {
Expand All @@ -117,14 +129,14 @@ export function stateInitializedChecker(engine: IEngine, profile: IProfile, sync
}

// If we already have data from players, dont send the heartbeat messages
if (connectedPlayers.players.length) return
// if (connectedPlayers.players.length) return

if (!stateInitialized && playerSceneEntity) {
// Send this data to all the players connected (new and old)
// So everyone can decide if it's the oldest one or no.
// It's for the case that multiple users enters ~ at the same time.
PlayersInScene.getMutable(playerSceneEntity)
}
// if (!stateInitialized && playerSceneEntity) {
// // Send this data to all the players connected (new and old)
// // So everyone can decide if it's the oldest one or no.
// // It's for the case that multiple users enters ~ at the same time.
// PlayersInScene.getMutable(playerSceneEntity)
// }
}
void enterScene()

Check warning on line 141 in packages/@dcl/sdk/src/network/utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/@dcl/sdk/src/network/utils.ts#L141

Added line #L141 was not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,15 @@ export function cubeSystem() {
for (const [entity, grabbed] of engine.getEntitiesWith(Grabbed)) {
if (grabbed.userId === myProfile.userId) {
if (inputSystem.isTriggered(InputAction.IA_JUMP, PointerEventType.PET_DOWN)) {
Grabbed.getMutable(entity).userId = ''
// Grabbed.getMutable(entity).userId = ''
// TODO: Fix deleteFrom not working.
// Grabbed.deleteFrom(entity)
Grabbed.deleteFrom(entity)
continue
}
const { x, y, z } = Transform.get(engine.PlayerEntity).position
if (JSON.stringify(Transform.get(entity).position) === JSON.stringify({ x, y, z })) {
continue
}
Transform.getMutable(entity).position = { x, y, z }
const textEntity = getFirstChild(entity)!
TextShape.getMutable(textEntity).text = `${x.toFixed(1)}, ${y.toFixed(1)}, ${z.toFixed(1)}`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export async function main() {
const userId = (await getUserData({})).data?.userId ?? ''
setupUi(userId)
engine.addSystem(moveHummingBirds)
gameStatusServer()
// gameStatusServer()
// createMovingPlatforms()
// createCubes()

Expand Down
1 change: 1 addition & 0 deletions test/sdk/network/parent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ import { addSyncTransport } from '../../../packages/@dcl/sdk/network'
describe('Network Parenting', () => {
const engineA = Engine()
const engineB = Engine()
addSyncTransport()
})

0 comments on commit 48dc501

Please sign in to comment.