-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.js
134 lines (116 loc) · 3.74 KB
/
server.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
128
129
130
131
132
133
134
const express = require("express");
const app = express();
const http = require("http");
const server = require("http").Server(app);
const io = require("socket.io")(server);
const fs = require("fs");
const request = require("request");
const favicon = require("serve-favicon");
app.use(express.static("public"));
app.use(favicon("favicon.ico"));
const config = require("./nsgsi/config.js")("config.json");
const teaminfo = require("./nsgsi/teaminfo.js")(config.team_info);
let gamestate = require("./nsgsi/gamestate.js");
const CsgoConsole = require("./nsgsi/csgoconsole.js");
const hudport = process.env.PORT || 8001;
const csgoport = 3001;
app.get("/", (req, res) => {
res.sendFile(__dirname + "/index.html");
});
app.get("/spec", (req, res) => {
res.sendFile(__dirname + "/spec.html");
});
app.get("/pauseScreen", (req, res) => {
res.sendFile(__dirname + "/pauseScreen.html");
});
let csgoconsole;
if (config.use_telnet) {
csgoconsole = new CsgoConsole();
csgoconsole.on("connection-fail", () => {
console.log("try reconnect in 5 seconds");
setTimeout(() => {
csgoconsole.reconnect();
}, 5000);
});
// send chat messages to hud
csgoconsole.on("chat-msg", (data) => {
io.emit("chat-msg", data);
});
}
io.on("connection", (socket) => {
console.log("a user connected");
// write raw game state (as received by game) to a file in folder 'debug'
socket.on("print-gsi", () => {
console.log(gamestate.raw);
const data = JSON.stringify(gamestate.raw);
if (!fs.existsSync("./debug")) {
fs.mkdirSync("./debug");
}
fs.writeFileSync(`debug/gamestate_${Date.now()}.json`, data);
});
socket.on("send-avatar", (steamid) => {
if (!config.show_player_avatars || !("steam_api_key" in config)) {
return;
}
let url = `http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=${config.steam_api_key}&steamids=${steamid}`;
request.get(url, function (error, steamHttpResponse, steamHttpBody) {
try {
let info = JSON.parse(steamHttpBody);
if (info.response.players.length === 0) {
return;
}
let avatar_url = info.response.players["0"].avatarfull;
io.emit("get-avatar", { id: steamid, url: avatar_url });
} catch (error) {
console.error(
`Error in reading avatar url from steam api. Error: ${error}`
);
}
});
});
if (config.use_telnet) {
socket.on("cs-exec", (data) => {
csgoconsole.exec(`${data}`);
});
}
io.emit("config", config);
io.emit("teaminfo", teaminfo);
//gamestate.update(dummydata, config);
io.emit("csgo-gsi-update", gamestate);
});
server.listen(hudport, () => {
console.log(`Socket.IO server running at http://localhost:${hudport}/`);
});
http
.createServer(function (req, res) {
let date_ob = new Date();
let date = ("0" + date_ob.getDate()).slice(-2);
let month = ("0" + (date_ob.getMonth() + 1)).slice(-2);
let year = date_ob.getFullYear();
let hours = date_ob.getHours();
let minutes = date_ob.getMinutes();
let seconds = date_ob.getSeconds();
// prints date & time in YYYY-MM-DD HH:MM:SS format
//console.log('Receiving data: ' + year + "-" + month + "-" + date + " " + hours + ":" + minutes + ":" + seconds);
if (req.method == "POST") {
res.writeHead(200, {
"Content-Type": "text/html",
});
var body = "";
req.on("data", function (data) {
body += data;
});
req.on("end", function () {
gamestate.update(JSON.parse(body), config);
io.emit("csgo-gsi-update", gamestate);
res.end("");
});
} else {
res.writeHead(200, {
"Content-Type": "text/html",
});
var html = "yes";
res.end(html);
}
})
.listen(csgoport);