-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
127 lines (108 loc) · 3.88 KB
/
index.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
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
const http = require("http");
const EventEmitter = require("events");
class CSGOGSI extends EventEmitter {
constructor({ port = 3000, authToken = [] }) {
super();
let tokens = authToken;
if (!Array.isArray(tokens)) {
tokens = [];
}
this.authToken = tokens;
this.app = http.createServer((req, res) => {
if (req.method !== "POST") {
res.writeHead(404, { "Content-Type": "text/plain" });
return res.end("404 Not Found");
}
let body = "";
req.on("data", data => {
body += data;
});
req.on("end", () => {
this.processJson(body);
return res.writeHead(200).end();
});
});
this.bombTime = 40;
this.isBombPlanted = false;
this.bombTimer = null;
this.server = this.app.listen({ port }, () => {
let addr = this.server.address();
console.log(`[@] CSGO GSI server listening on ${addr.address}:${addr.port}`);
});
}
processJson(json) {
try {
let data = JSON.parse(json);
if (!this.isAuthenticated(data)) return;
this.emit("all", data);
this.process(data);
} catch (error) { }
}
isAuthenticated(data) {
return this.authToken.length < 1 || (data["auth"]["token"] && this.authToken.length > 0 && this.authToken.includes(data["auth"]["token"]))
}
process(data) {
if (data["map"]) {
this.emit("gameMap", data["map"]["name"]);
this.emit("gamePhase", data["map"]["phase"]); //warmup etc
this.emit("gameRounds", data["map"]["round"]);
this.emit("gameCTscore", data["map"]["team_ct"]);
this.emit("gameTscore", data["map"]["team_t"]);
}
if (data["round_wins"]) {
this.emit("roundWins", data["round_wins"]);
}
if (data["player"]) {
this.emit("player", data["player"]);
}
if (data["round"]) {
this.emit("roundPhase", data["round"]["phase"]);
switch (data["round"]["phase"]) {
case "live":
break;
case "freezetime":
break;
case "over":
if (this.isBombPlanted) {
this.isBombPlanted = false;
this.stopC4Countdown();
}
this.emit("roundWinTeam", data["round"]["win_team"]);
break;
}
if (data["round"]["bomb"]) {
this.emit("bombState", data["round"]["bomb"]);
switch (data["round"]["bomb"]) {
case "planted":
if (!this.isBombPlanted) {
this.isBombPlanted = true;
let timeleft = this.bombTime - (new Date().getTime() / 1000 - data["provider"]["timestamp"]);
this.emit("bombTimeStart", timeleft);
this.startC4Countdown(timeleft);
}
break;
case "defused":
case "exploded":
this.isBombPlanted = false;
this.stopC4Countdown();
break;
}
}
}
}
stopC4Countdown() {
if (this.bombTimer) clearInterval(this.bombTimer);
}
startC4Countdown(time) {
this.bombTimer = setInterval(() => {
time = time - 1;
if (time <= 0) {
this.stopC4Countdown()
this.isBombPlanted = false;
return this.emit("bombExploded");
}
this.emit("bombTimeLeft", time);
}, 1000);
}
}
module.exports = CSGOGSI;