forked from d-rk/q3-server-docker-mattermost-webhook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
102 lines (88 loc) · 3.14 KB
/
client.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
const https = require('https');
const mattermostHost = process.env.MATTERMOST_HOST || '';
const mattermostPath = process.env.MATTERMOST_PATH || '';
console.log('mattermostHost: ' + mattermostHost);
console.log('mattermostPath: ' + mattermostPath);
function sendNotification(message) {
const data = JSON.stringify({
text: message
});
const options = {
hostname: mattermostHost,
port: 443,
path: mattermostPath,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length
}
};
const req = https.request(options, res => {
console.log(`statusCode: ${res.statusCode}`);
});
req.on('error', error => {
console.error(error)
});
req.write(data);
req.end();
}
function sanitizePlayerName(name) {
return name.replace(/\^\d+/g, "")
}
function startClient() {
const host = process.env.REST_API_HOST || '127.0.0.1';
const port = process.env.REST_API_PORT || '9009';
const url = 'http://' + host + ':' + port;
console.log('url: ' + url);
let socket = require('socket.io-client')(url);
socket.on('connect', function () {
console.log("connected");
});
socket.on('say', function (payload) {
console.log("say: " + JSON.stringify(payload));
});
socket.on('playerBegin', function (payload) {
console.log("playerBegin: " + JSON.stringify(payload));
sendNotification("player joined: **" + sanitizePlayerName(payload.data.n) + "**");
});
socket.on('playerInfoChanged', function (payload) {
console.log("playerInfoChanged: " + JSON.stringify(payload));
});
socket.on('playerConnect', function (payload) {
console.log("playerConnect: " + JSON.stringify(payload));
});
socket.on('playerDisconnect', function (payload) {
console.log("playerDisconnect: " + JSON.stringify(payload));
});
socket.on('gameStart', function (payload) {
console.log("gameStart: " + JSON.stringify(payload));
sendNotification("new game started: **" + payload.data.properties.mapname + "**");
});
socket.on('gameEnd', function (payload) {
console.log("gameEnd: " + JSON.stringify(payload));
let bestPlayer = undefined;
Object.keys(payload.data.players).forEach(function(id) {
let player = payload.data.players[id];
if (bestPlayer === undefined || player.score > bestPlayer.score) {
bestPlayer = player;
}
});
if (bestPlayer !== undefined) {
const playerName = sanitizePlayerName(bestPlayer.n);
sendNotification("**" + playerName + "** won the game.");
}
});
socket.on('gameShutdown', function (payload) {
console.log("gameShutdown: " + JSON.stringify(payload));
});
socket.on('item', function (payload) {
console.log("item: " + JSON.stringify(payload));
});
socket.on('kill', function (payload) {
console.log("kill: " + JSON.stringify(payload));
});
socket.on('disconnect', function () {
console.log("disconnected");
});
}
startClient();