/
index.js
143 lines (113 loc) · 5.08 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const WebSocket = require('ws');
const request = require('request')
const LCUConnector = require('lcu-connector');
const connector = new LCUConnector();
const events = require('events');
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
module.exports = new events.EventEmitter();
function emitError(err) {
module.exports.emit('error', err);
}
connector.on('connect', (lcu) => {
console.log('connect');
const ws = new WebSocket(`wss://${lcu.username}:${lcu.password}@${lcu.address}:${lcu.port}/`, 'wamp');
const url = `${lcu.protocol}://${lcu.username}:${lcu.password}@${lcu.address}:${lcu.port}`
let timerInterval;
let initState = false;
const data = { timer: undefined };
function gameflowClearInterval(msg) {
this.phase = msg.data;
if (this.phase !== 'ChampSelect') {
clearInterval(timerInterval);
console.log(`cleared interval because gameflow phase is "${this.phase}" instead of "ChampSelect"`);
}
}
function getTimer(adjustedTimeLeftInPhase, internalNowInEpochMs) {
const diff = Date.now() - internalNowInEpochMs;
const timeLeft = adjustedTimeLeftInPhase - diff;
return Math.floor(timeLeft / 1000);
}
function startTimer(adjustedTimeLeftInPhase, internalNowInEpochMs) {
timerInterval = setInterval(() => {
this.timer = getTimer(adjustedTimeLeftInPhase, internalNowInEpochMs);
if (this.timer !== data.timer && this.timer >= 0) {
data.timer = this.timer;
module.exports.emit('timer', data);
module.exports.emit('message', { type: 'timer', timer: data });
}
},100);
}
function resetTimer(adjustedTimeLeftInPhase, internalNowInEpochMs) {
clearInterval(timerInterval);
startTimer(adjustedTimeLeftInPhase, internalNowInEpochMs);
}
async function initChampSelect(msg) {
if (msg.eventType === 'Create') {
initState = true;
}
if (initState === true && (msg.data.myTeam.length !== 0) || (msg.data.theirTeam.length !== 0)) {
module.exports.emit('message', { type: 'create', timestamp: Date.now(), data: await structureData(msg.data) });
initState = false
}
function structureData(data) {
return new Promise(resolve => {
(async () => {
resolve({
numBans: data.bans.numBans,
isSpectating: data.isSpectating,
teamBlue: await getTeamSummonerNames(data.myTeam),
teamRed: await getTeamSummonerNames(data.theirTeam)
})
function getTeamSummonerNames(team) {
console.log("team")
return new Promise(resolve => {
let summoners = [];
if(team.length > 0) {
team.forEach(e => {
//console.log(e)
if(e.playerType === "PLAYER") {
request(url + "/lol-summoner/v1/summoners/" + e.summonerId, (err, res, body) => {
summoners.push(JSON.parse(body))
if (summoners.length == team.length) {
resolve(summoners)
console.log(summoners)
}
})
}
});
}
else {
resolve([])
}
})
}
})()
})
}
}
ws.on('open', () => {
ws.send('[5, "OnJsonApiEvent"]');
});
ws.on('error', (err) => {
emitError(err);
});
ws.on('message', (msg) => {
this.message = JSON.parse(msg)[2];
if (this.message.uri === '/lol-champ-select/v1/session') {
initChampSelect(this.message)
this.adjustedTimeLeftInPhase = this.message.data.timer.adjustedTimeLeftInPhase;
this.internalNowInEpochMs = this.message.data.timer.internalNowInEpochMs;
if (this.message.eventType === 'Delete') {
module.exports.emit('message', { type: 'delete', timestamp: Date.now() })
}
else if (this.message.eventType === 'Update') {
module.exports.emit('message', { type: 'update', timestamp: Date.now(), data: this.message.data })
}
resetTimer(this.adjustedTimeLeftInPhase, this.internalNowInEpochMs);
}
if (this.message.uri === '/lol-gameflow/v1/gameflow-phase') {
gameflowClearInterval(this.message);
}
});
});
connector.start();