This repository has been archived by the owner on Jul 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
55 lines (48 loc) · 1.58 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
"use strict"
let fs = require('fs');
let express = require('express');
let fetch = require('node-fetch');
let d2gsi = require('./dota2-gsi-fork');
let rp = require('request-promise');
let server = new d2gsi(
{
tokens: 'hello1234',
}
);
let getIngameResources = (typename, name) => {
let filepath = require('path').join(__dirname, 'cache', typename, name + '.png');
return rp.get({ url: `http://cdn.dota2.com/apps/dota2/images/${typename}/${name}_lg.png`, encoding: null })
.then((image) => {
try {
let fd = fs.openSync(filepath, 'wx');
fs.writeSync(fd, image, 0, image.length);
}
finally {
return filepath;
}
});
};
server.app.use(express.static('static'));
server.app.get('/image/hero/:hero_name.png', (req, res) =>
getIngameResources('heroes', req.params.hero_name).then(
(filepath) => res.sendFile(filepath)
)
);
server.app.get('/image/item/:item_name.png', (req, res) =>
getIngameResources('items', req.params.item_name).then(
(filepath) => res.sendFile(filepath)
)
);
let io = require('socket.io').listen(server.server);
server.events.on('newclient', (client) => {
// check if client is a spectator
if ('team2' in client.gamestate.player) {
io.sockets.on('connection', (socket) => {
console.log("connected to a client");
client.on('newdata', (newdata) => {
socket.emit('newdata', newdata);
});
client.on('disconnect', () => socket.close());
});
}
});