Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Peer Notification Cleanup #8004

Merged
merged 5 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { t } from 'i18next'
import React, { useEffect } from 'react'

import {
Expand All @@ -11,15 +12,18 @@ import {
} from '@etherealengine/client-core/src/common/services/MediaInstanceConnectionService'
import { ChatAction, ChatService, ChatState } from '@etherealengine/client-core/src/social/services/ChatService'
import { LocationState } from '@etherealengine/client-core/src/social/services/LocationService'
import { UserId } from '@etherealengine/common/src/interfaces/UserId'
import { Engine } from '@etherealengine/engine/src/ecs/classes/Engine'
import { EngineState } from '@etherealengine/engine/src/ecs/classes/EngineState'
import { PresentationSystemGroup } from '@etherealengine/engine/src/ecs/functions/EngineFunctions'
import { useSystem } from '@etherealengine/engine/src/ecs/functions/SystemFunctions'
import { WorldState } from '@etherealengine/engine/src/networking/interfaces/WorldState'
import { NetworkState } from '@etherealengine/engine/src/networking/NetworkState'
import { dispatchAction, getMutableState, none, useHookstate } from '@etherealengine/hyperflux'

import { Groups } from '@mui/icons-material'

import { NotificationService } from '../common/services/NotificationService'
import { FriendService } from '../social/services/FriendService'
import { PartyService, PartyState, PartySystem } from '../social/services/PartyService'
import FriendsMenu from '../user/components/UserMenu/menus/FriendsMenu'
Expand Down Expand Up @@ -236,14 +240,30 @@ export const PartyInstanceProvisioning = () => {
return null
}

export const WorldNetworkPeer = (props: { userID: UserId; name: string }) => {
HexaField marked this conversation as resolved.
Show resolved Hide resolved
useEffect(() => {
if (props.userID === Engine.instance.userId) return
NotificationService.dispatchNotify(`${props.name} ${t('common:toast.joined')}`, { variant: 'default' })
return () => {
NotificationService.dispatchNotify(`${props.name} ${t('common:toast.left')}`, { variant: 'default' })
}
}, [])

return <></>
}

export const InstanceProvisioning = () => {
const networkConfigState = useHookstate(getMutableState(NetworkState).config)
const userNames = useHookstate(getMutableState(WorldState).userNames)
HexaField marked this conversation as resolved.
Show resolved Hide resolved

return (
<>
{networkConfigState.world.value && <WorldInstanceProvisioning />}
{networkConfigState.media.value && <MediaInstanceProvisioning />}
{networkConfigState.friends.value && <PartyInstanceProvisioning />}
{Object.entries(userNames.value).map(([userID, name]) => (
<WorldNetworkPeer userID={userID as UserId} name={name} />
HexaField marked this conversation as resolved.
Show resolved Hide resolved
))}
</>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -419,19 +419,11 @@ export async function onConnectToInstance(network: SocketWebRTCClientNetwork) {

function peerUpdateHandler(peers: Array<PeersUpdateType>) {
for (const peer of peers) {
const hasPeer = network.peers.get(peer.peerID)
NetworkPeerFunctions.createPeer(network, peer.peerID, peer.peerIndex, peer.userID, peer.userIndex, peer.name)
if (network.topic === NetworkTopics.world && !hasPeer && network.peers.get(peer.peerID)) {
NotificationService.dispatchNotify(`${peer.name} ${t('common:toast.joined')}`, { variant: 'default' })
}
}
for (const [peerID, peer] of network.peers)
if (!peers.find((p) => p.peerID === peerID)) {
NetworkPeerFunctions.destroyPeer(network, peerID)
if (network.topic === NetworkTopics.world) {
const name = getState(WorldState).userNames[peer.userId]
NotificationService.dispatchNotify(`${name} ${t('common:toast.left')}`, { variant: 'default' })
}
}
if (network.topic === NetworkTopics.media) removePeerMediaChannels(SelfPeerID)
logger.info('Updated peers %o', { topic: network.topic, peers })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { PeerID } from '@etherealengine/common/src/interfaces/PeerID'
import { UserId } from '@etherealengine/common/src/interfaces/UserId'
import { dispatchAction, getMutableState } from '@etherealengine/hyperflux'
import { Action, ResolvedActionType } from '@etherealengine/hyperflux/functions/ActionFunctions'
import { getState } from '@etherealengine/hyperflux/functions/StateFunctions'
import { getState, none } from '@etherealengine/hyperflux/functions/StateFunctions'

import { Engine } from '../../ecs/classes/Engine'
import { EngineActions } from '../../ecs/classes/EngineState'
import { removeEntity } from '../../ecs/functions/EntityFunctions'
import { Network } from '../classes/Network'
import { Network, NetworkTopics } from '../classes/Network'
import { WorldState } from '../interfaces/WorldState'
import { NetworkState, updateNetwork } from '../NetworkState'
import { WorldNetworkAction } from './WorldNetworkAction'
Expand Down Expand Up @@ -43,8 +43,11 @@ function createPeer(
//TODO: remove this once all network state properties are reactively set
updateNetwork(network)

const worldState = getMutableState(WorldState)
worldState.userNames[userID].set(name)
// TODO: we probably want an explicit config for detecting a non-user peer
if (peerID !== 'server') {
const worldState = getMutableState(WorldState)
worldState.userNames[userID].set(name)
}
}

function destroyPeer(network: Network, peerID: PeerID) {
Expand All @@ -66,7 +69,11 @@ function destroyPeer(network: Network, peerID: PeerID) {
const userPeers = network.users.get(userID)!
const peerIndexInUserPeers = userPeers.indexOf(peerID)
userPeers.splice(peerIndexInUserPeers, 1)
if (!userPeers.length) network.users.delete(userID)
if (!userPeers.length) {
network.users.delete(userID)
const worldState = getMutableState(WorldState)
if (worldState.userNames.value[userID]) worldState.userNames[userID].set(none)
}

//TODO: remove this once all network state properties are reactively set
updateNetwork(network)
Expand Down
12 changes: 6 additions & 6 deletions packages/engine/src/networking/systems/OutgoingActionSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ export const sendActionsAsPeer = (network: Network) => {
if (!network.ready) return
const actions = [...Engine.instance.store.actions.outgoing[network.topic].queue]
if (!actions.length) return
for (const peerID of network.transport.peers) {
network.transport.messageToPeer(network.hostPeerID, {
type: MessageTypes.ActionData.toString(),
/*encode(*/ data: actions
}) //)
}
// for (const peerID of network.transport.peers) {
network.transport.messageToPeer(network.hostPeerID, {
type: MessageTypes.ActionData.toString(),
/*encode(*/ data: actions
}) //)
// }
clearOutgoingActions(network.topic)
}

Expand Down