/
main.js
139 lines (112 loc) · 3.3 KB
/
main.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
// this a subset of the features that Apex Legends events provides - however,
// when writing an app that consumes events - it is best if you request
// only those features that you want to handle.
//
// NOTE: in the future we'll have a wildcard option to allow retrieving all
// features
var g_interestedInFeatures = [
'death',
'kill',
'match_state',
'me',
'revive',
'team',
'roster',
'kill_feed',
'rank',
'match_summary',
'location',
'match_info',
'victory',
'damage',
'inventory',
'localization'
];
var onErrorListener,onInfoUpdates2Listener, onNewEventsListener;
function registerEvents() {
onErrorListener = function(info) {
console.log("Error: " + JSON.stringify(info));
}
onInfoUpdates2Listener = function(info) {
console.log("Info UPDATE: " + JSON.stringify(info));
}
onNewEventsListener = function(info) {
console.log("EVENT FIRED: " + JSON.stringify(info));
}
// general events errors
overwolf.games.events.onError.addListener(onErrorListener);
// "static" data changed (total kills, username, steam-id)
// This will also be triggered the first time we register
// for events and will contain all the current information
overwolf.games.events.onInfoUpdates2.addListener(onInfoUpdates2Listener);
// an event triggerd
overwolf.games.events.onNewEvents.addListener(onNewEventsListener);
}
function unregisterEvents() {
overwolf.games.events.onError.removeListener(onErrorListener);
overwolf.games.events.onInfoUpdates2.removeListener(onInfoUpdates2Listener);
overwolf.games.events.onNewEvents.removeListener(onNewEventsListener);
}
function gameLaunched(gameInfoResult) {
if (!gameInfoResult) {
return false;
}
if (!gameInfoResult.gameInfo) {
return false;
}
if (!gameInfoResult.runningChanged && !gameInfoResult.gameChanged) {
return false;
}
if (!gameInfoResult.gameInfo.isRunning) {
return false;
}
// NOTE: we divide by 10 to get the game class id without it's sequence number
if (Math.floor(gameInfoResult.gameInfo.id/10) != 21566) {
return false;
}
console.log("Apex Legends Launched");
return true;
}
function gameRunning(gameInfo) {
if (!gameInfo) {
return false;
}
if (!gameInfo.isRunning) {
return false;
}
// NOTE: we divide by 10 to get the game class id without it's sequence number
if (Math.floor(gameInfo.id/10) != 21566) {
return false;
}
console.log("Apex Legends running");
return true;
}
function setFeatures() {
overwolf.games.events.setRequiredFeatures(g_interestedInFeatures, function(info) {
if (info.status == "error")
{
//console.log("Could not set required features: " + info.reason);
//console.log("Trying in 2 seconds");
window.setTimeout(setFeatures, 2000);
return;
}
console.log("Set required features:");
console.log(JSON.stringify(info));
});
}
// Start here
overwolf.games.onGameInfoUpdated.addListener(function (res) {
if (gameLaunched(res)) {
registerEvents();
unregisterEvents();
setTimeout(setFeatures, 1000);
}
console.log("onGameInfoUpdated: " + JSON.stringify(res));
});
overwolf.games.getRunningGameInfo(function (res) {
if (gameRunning(res)) {
registerEvents();
setTimeout(setFeatures, 1000);
}
console.log("getRunningGameInfo: " + JSON.stringify(res));
});