-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
95 lines (87 loc) · 2.93 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
const axios = require('axios');
const versionChecker = require('sw-exporter-plugin-version-checker');
module.exports = {
defaultConfig: {
enabled: true,
key: '',
},
defaultConfigDetails: {
key: {
label: 'API Key',
type: 'input'
},
},
pluginName: 'RTA',
pluginDescription: 'Synchronize real-time arena matches data with rtapicks.info.',
init (proxy, config) {
const pluginConfig = config.Config.Plugins[this.pluginName];
versionChecker.proceed({
name: this.pluginName,
config: require('./package.json'),
proxy: proxy
});
['getRankerRtpvpReplayList', 'getRtpvpReplayList'].forEach(event => {
proxy.on(event, (request, response) => {
if (! pluginConfig.enabled || ! pluginConfig.key) {
return;
}
proxy.log({
name: this.pluginName,
source: 'plugin',
type: 'debug',
message: `Dispatching ${event} to server...`,
});
axios[this.getRequestMethod(event)](
this.getRequestEndpoint(event, response),
{
event: response
},
this.getRequestHeaders(pluginConfig.key)
).then(response => {
proxy.log({
name: this.pluginName,
source: 'plugin',
type: 'success',
message: response.data.message,
});
}).catch(error => {
proxy.log({
name: this.pluginName,
source: 'plugin',
type: 'error',
message: this.getErrorMessage(error.response.status),
});
proxy.log({
name: this.pluginName,
source: 'plugin',
type: 'debug',
message: JSON.stringify(error.response.data),
});
});
});
});
},
getRequestMethod (event) {
return 'post';
},
getRequestEndpoint (event, response) {
return 'https://www.rtapicks.info/api/replays/'
+ (event == 'getRankerRtpvpReplayList' ? 'best' : 'self');
},
getRequestHeaders (key) {
return {
headers: {
Authorization: `Bearer ${key}`
},
};
},
getErrorMessage(status) {
if (status == 401) {
return 'The API key you\'ve provided is invalid.';
}
if (status == 500) {
return 'An unexpected error has occurred. Please try again later.';
}
return 'Unable to synchronize data with the server.'
},
};