-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
146 lines (136 loc) · 5.02 KB
/
Copy pathindex.js
File metadata and controls
146 lines (136 loc) · 5.02 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const WebSocket = require('ws');
const database = require("./database")
const wss = new WebSocket.Server({ port: 8080 });
//const myWsClient = new WebSocket("ws://localhost:8060")
const v4 = require("uuid").v4
const onExit = require("exit-hook")
console.log("Starting WebSocket Server..")
let exitFun = () => {
database.get("select * from lobbies").forEach(values => {
database.run("delete from lobbies where steamID = ?", [values.steamID])
database.run("delete from userData where steamID = ?", [values.steamID])
})
}
onExit(() => {
exitFun()
})
process.on("uncaughtException", () => {
exitFun()
process.exit(0)
})
const sid2wc = {}
let wc2sid = (wc) => {
let ret
Object.keys(sid2wc).forEach(it => {
if (sid2wc[it] == wc) {ret = it; return}
})
return ret
}
const apiVersion = "v1.0"
class LobbyManager {
createLobbyIfNotExists(sID, team) {
let s = this.getLobbyBySID(sID)
if (!s) {
let generated = v4()
database.run("insert into lobbies (lobbyID, steamID, team) values (?, ?, ?)", [generated, sID, team])
database.run("insert into userData (steamID) values (?)", [sID])
}
}
getLobbyBySID(sID) {
return database.one("select * from lobbies where steamID = ?", [sID])
}
}
let manager = new LobbyManager()
let deleteInfoAbout = (wsClient) => {
let sid = wc2sid(wsClient)
if (!sid) return
database.run("delete from lobbies where steamID = ?", [sid])
database.run("delete from userData where steamID = ?", [sid])
delete sid2wc[sid]
}
wss.on("listening", () => {
console.log("Server started!")
})
let attemptResetWc = (wsClient, sid) => {
let mySID = wc2sid(wsClient)
if (!mySID) return true
return mySID == sid
}
wss.on("connection", (wsClient) => {
wsClient.on("close", () => {
deleteInfoAbout(wsClient)
})
wsClient.on("message", (message) => {
/* exchange protocol
splliter command additional data
*/
message = message.replace(" ", "")
let splitter = message[0]
if (![":", ","].includes(splitter)) { //set to default
splitter = ":"
message = ":" + message
}
message = message.slice(1)
let data = message.split(splitter)
let command = data[0]
if (!command) return
switch (command) {
case "deleteInfo": {
deleteInfoAbout(wsClient)
break
}
case "version": {
wsClient.send(apiVersion)
break
}
//case "addSID": {
// let j = {"command": "addSID"}
// let tmpDiscordID = data[1]
// if (!tmpDiscordID) return
// j["data"] = data.slice(2)
// myWsClient.send(JSON.stringify(j))
//}
case "iterateEntities": {
let mySID = data[1]
let myTeam = data[2]
if (!mySID || !myTeam) {wsClient.send(""); return}
let attempt = attemptResetWc(wsClient, mySID)
if (!attempt) {wsClient.send(""); return}
manager.createLobbyIfNotExists(mySID, myTeam)
database.run("update lobbies set team = ? where steamID = ?", [myTeam, mySID])
sid2wc[mySID] = wsClient
let myLobby = manager.getLobbyBySID(mySID)
let all = ""
let realData = data.slice(3)
realData.forEach((sID, idx) => {
if (idx % 3 != 0) return
let tmpLobby = manager.getLobbyBySID(sID)
if (!tmpLobby) return
if (tmpLobby.team == myLobby.team) {
database.run("update lobbies set lobbyID = ? where steamID = ?", [tmpLobby.lobbyID, mySID])
myLobby = tmpLobby
}
})
database.run("update userData set data = ? where steamID = ?", [realData.join(":"), mySID])
database.get("select * from lobbies where lobbyID = ?", [myLobby.lobbyID]).forEach(player => {
if (player.steamID != mySID) {
let tmpGet = database.one("select data from userData where steamID = ?", [player.steamID])
if (!tmpGet) return
let splitArr = tmpGet.data.split(":")
//console.log(splitArr)
splitArr.forEach((sID, idx) => {
if (idx % 2 != 0) return
all += `${sID}:`
let tmpGet2 = splitArr[idx + 1]
if (tmpGet2 == undefined) return
let nextVec = tmpGet2.match(/[+-]?\d+(\.\d+)?/g)
all += `${nextVec[0]}:${nextVec[1]}:${nextVec[2]}:`
})
}
})
wsClient.send(all)
}
break
}
})
})