forked from BetterMap/BetterMap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocketConnection.js
57 lines (45 loc) · 1.62 KB
/
socketConnection.js
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
import WebsiteCommunicator from "./../soopyApis/websiteCommunicator"
import socketData from "../soopyApis/socketData"
class BetterMapServer extends WebsiteCommunicator {
constructor() {
super(socketData.serverNameToId.bettermap)
this.datacallback = undefined
this.peopleUsingBMapCallback = new Map()
register("step", () => {
for (let key of this.peopleUsingBMapCallback.keys()) {
if (Date.now() - this.peopleUsingBMapCallback.get(key)[0] < 5000) return
this.peopleUsingBMapCallback.get(key)[1](this.peopleUsingBMapCallback.get(key)[2])
this.peopleUsingBMapCallback.delete(key)
}
}).setDelay(5)
}
onData(data) {
if (data.type === "queryUsingBMap") {
if (!this.peopleUsingBMapCallback.get(data.id)) return
this.peopleUsingBMapCallback.get(data.id)[1](data.data)
this.peopleUsingBMapCallback.delete(data.id)
return
}
if (this.datacallback) this.datacallback(data)
}
sendDungeonData(data) {
this.sendData(data)
}
isUsingBMap(people, callback) {
let id = Math.floor(Math.random() * 1000000)
this.sendData({
queryUsingBMap: people,
id
})
this.peopleUsingBMapCallback.set(id, [Date.now(), callback, new Array(people.length).fill(false)])
}
onConnect() {
}
}
if (!global.betterMapServer) {
global.betterMapServer = new BetterMapServer()
register("gameUnload", () => {
global.betterMapServer = undefined
})
}
export default global.betterMapServer