-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdbSystem.ts
97 lines (87 loc) · 3.02 KB
/
dbSystem.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { createState, Downgraded, useState } from '@speigg/hookstate'
import { Vector3 } from 'three'
import matches from 'ts-matches'
import { UserId } from '@xrengine/common/src/interfaces/UserId'
import { isClient } from '@xrengine/engine/src/common/functions/isClient'
import { World } from '@xrengine/engine/src/ecs/classes/World'
import { addComponent, hasComponent } from '@xrengine/engine/src/ecs/functions/ComponentFunctions'
import { useWorld } from '@xrengine/engine/src/ecs/functions/SystemHooks'
import { isEntityLocalClient } from '@xrengine/engine/src/networking/functions/isEntityLocalClient'
import { NetworkWorldAction } from '@xrengine/engine/src/networking/functions/NetworkWorldAction'
import { AfkCheckComponent } from './components/AfkCheckComponent'
import { ProximityComponent } from './components/ProximityComponent'
import { WebCamInputComponent } from './components/WebCamInputComponent'
export const DBState = createState({
players: [] as Array<{
userId: UserId
isConnected: boolean
}>
})
export type DBStateType = typeof DBState
// Attach logging
DBState.attach(() => ({
id: Symbol('Logger'),
init: () => ({
onSet(arg) {
console.log('DB STATE \n' + JSON.stringify(DBState.attach(Downgraded).value, null, 2))
}
})
}))
export function accessDBState() {
return DBState.attach(Downgraded).value
}
export function useDBState() {
return useState(DBState) as any as typeof DBState
}
globalThis.DBState = DBState
console.log('initializing db system script')
export default async function dbSystem(world: World) {
console.log('init db system')
world.receptors.push((action) => {
matches(action).when(NetworkWorldAction.spawnAvatar.matches, (spawnAction) => {
const world = useWorld()
const entity = world.getNetworkObject(spawnAction.$from, spawnAction.networkId)
if (isClient) {
if (entity && !hasComponent(entity, AfkCheckComponent)) {
addComponent(entity, AfkCheckComponent, {
isAfk: false,
prevPosition: new Vector3(0, 0, 0),
cStep: 0,
cStep2: 0,
timer: 0
})
}
if (isEntityLocalClient(entity)) {
if (!hasComponent(world.localClientEntity, ProximityComponent, world)) {
// && isBot(window)) {
addComponent(
world.localClientEntity,
ProximityComponent,
{
usersInRange: [],
usersInIntimateRange: [],
usersInHarassmentRange: [],
usersLookingTowards: []
},
world
)
}
if (!hasComponent(world.localClientEntity, WebCamInputComponent, world)) {
addComponent(
world.localClientEntity,
WebCamInputComponent,
{
emotions: []
},
world
)
}
console.log('added web cam input component to local client')
}
}
})
})
return () => {
return world
}
}