· 4 years ago · Mar 20, 2021, 08:24 PM
1registerPlugin({
2 requiredModules: ['http'],
3 name: 'WoT CW Upcoming Battles',
4 version: '0.0.1',
5 description: 'World of Tanks upcoming Clan Wars list',
6 author: 'icarusSB <dmillen04@gmail.com>',
7 vars: [{
8 name: 'clanTag1',
9 title: 'Clan Tag for first Clan',
10 type: 'string'
11 },{
12 name: 'clanTag2',
13 title: 'Clan Tag for second Clan',
14 type: 'string'
15 },{
16 name: 'region',
17 title: 'Region of clan',
18 type: 'select',
19 options: ['RU','EU','NA','ASIA']
20 },{
21 name: 'apikey',
22 title: 'WG API key (get one at https://developers.wargaming.net)',
23 type: 'string'
24 },{
25 name: 'tsChannel',
26 title: 'Channel to display info',
27 type: 'channel'
28 },{
29 name: 'updatePeriod',
30 title: 'Update Period (mins)',
31 type: 'number',
32 placeholder: '1'
33 }]
34},
35function(sinusbot, config) {
36 var backend = require('backend');
37 var engine = require('engine');
38 var http = require('http');
39 var regionArr = ['ru','eu','com','asia'];
40 var apiURL = "https://api.worldoftanks.";
41 var region = regionArr[config.region];
42 var clanTag1 = config.clanTag1;
43 var clanTag2 = config.clanTag2;
44 var apikey = "?application_id="+config.apikey;
45 var output;
46 var timeArr1 = [];
47 var timeArr2 = [];
48 var tierArr1 = [];
49 var tierArr2 = [];
50 var mapArr1 = [];
51 var mapArr2 = [];
52 var vsArr1 = [];
53 var vsArr2 = [];
54 var wotlifeurl = "https://wot-life.com/eu/clan/";
55 //https://wot-life.com/eu/clan/TF-X-500203218/
56
57 function getClanData1(){
58 var clanID1;
59 http.simpleRequest({
60 method: 'GET',
61 timeout: 60000,
62 url: apiURL+region+'/wgn/clans/list/'+apikey+'&game=wot&language=de&limit=1&search='+clanTag1
63 },
64 function (error, clanResponse) {
65 var clanData1 = JSON.parse(clanResponse.data);
66 clanID1 = clanData1.data[0].clan_id;
67 //engine.log('clanID: ' + clanID1);
68 output += "[b]Clan 1:[/b] "+clanData1.data[0].tag;
69 getBattles1(clanID1);
70 });
71 }
72 function getClanData2(){
73 var clanID2;
74 http.simpleRequest({
75 method: 'GET',
76 timeout: 60000,
77 url: apiURL+region+'/wgn/clans/list/'+apikey+'&game=wot&language=de&limit=1&search='+clanTag2
78 },
79 function (error, clanResponse) {
80 var clanData2 = JSON.parse(clanResponse.data) ;
81 clanID2 = clanData2.data[0].clan_id;
82 //engine.log('ClanID: ' + clanID2);
83 output += "\n\n[b]Clan 2:[/b] "+clanData2.data[0].tag;
84 getBattles2(clanID2);
85 });
86 }
87 function getBattles1(clanID1){
88 //engine.log("getBattles() recieved: "+clanID1);
89 http.simpleRequest({
90 method: 'GET',
91 timeout: 60000,
92 url:apiURL+region+'/wot/globalmap/clanbattles/'+apikey+'&clan_id='+clanID1+'&language=de'
93 },function (error, battleResponse1) {
94 var battleData1 = JSON.parse(battleResponse1.data);
95 //engine.log(battleData1.meta.count);
96 output += " [b]Battle Count:[/b] " + battleData1.meta.count;
97
98 if(battleData1.meta.count == 0){
99
100 mapArr1 = [];
101 vsArr1 = [];
102 //backend.getChannelByID(config.tsChannel).setDescription(output);
103 } else {
104 //engine.log(output);
105 for (i in battleData1.data) {
106 var timestamp = battleData1.data[i].time;
107 var date = new Date(timestamp * 1000);
108 if (date.getMinutes() < 10){ minutes = "0" + date.getMinutes(); } else{ minutes = date.getMinutes();}
109 timeArr1[i] = date.getHours() + ":" + minutes + " GMT";
110 tierArr1[i] = " [b]Tier:[/b] " + battleData1.data[i].vehicle_level;
111 var frontID1 = battleData1.data[i].front_id;
112 var provinceID1 = battleData1.data[i].province_id;
113 getArena1(frontID1, provinceID1, i);
114 var competitorID1 = battleData1.data[i].competitor_id;
115 getCompetitor1(competitorID1, i);
116 getBattles2(clanID2);
117 }
118 for (i in timeArr1){
119 output += "\n"+timeArr1[i]+tierArr1[i]+mapArr1[i]+vsArr1[i];
120 }
121 backend.getChannelByID(config.tsChannel).setDescription(output);
122 }
123 });
124 }
125 function getBattles2(clanID2){
126 //engine.log("getBattles() recieved: "+clanID2);
127 http.simpleRequest({
128 method: 'GET',
129 timeout: 60000,
130 url:apiURL+region+'/wot/globalmap/clanbattles/'+apikey+'&clan_id='+clanID2+'&language=de'
131 },function (error, battleResponse2) {
132 var battleData2 = JSON.parse(battleResponse2.data);
133 //engine.log(battleData2.meta.count);
134 output += " [b]Battle Count:[/b] " + battleData2.meta.count;
135
136 if(battleData2.meta.count == 0) {
137 mapArr2 = [];
138 vsArr2 = [];
139 backend.getChannelByID(config.tsChannel).setDescription(output);
140
141 } else {
142 //engine.log(output);
143 for (i in battleData2.data) {
144 var test = battleData2.data[i].time;
145 var date = new Date(test * 1000);
146 if (date.getMinutes() < 10){ minutes = "0" + date.getMinutes(); } else{ minutes = date.getMinutes();}
147 timeArr2[i] = date.getHours() + ":" + minutes + " GMT";
148 tierArr2[i] = " [b]Tier:[/b] " + battleData2.data[i].vehicle_level;
149 var frontID2 = battleData2.data[i].front_id;
150 var provinceID2 = battleData2.data[i].province_id;
151 getArena2(frontID2, provinceID2, i);
152 var competitorID2 = battleData2.data[i].competitor_id;
153 getCompetitor2(competitorID2, i);
154 }
155 for (i in timeArr2){
156 output += "\n"+timeArr2[i]+tierArr2[i]+mapArr2[i]+vsArr2[i];
157 };
158 //backend.getChannelByID(config.tsChannel).setDescription(output);
159 return;
160 }
161 });
162 //engine.log(output);
163 }
164 function getArena1(frontID1, provinceID1, i){
165 http.simpleRequest({
166 method: 'GET',
167 timeout: 60000,
168 url: apiURL+region+'/wot/globalmap/provinces/'+apikey+'&front_id='+frontID1+'&province_id='+provinceID1+'&language=de&fields=arena_name'
169 },function (error, provinceResponse1) {
170 var provinceData1 = JSON.parse(provinceResponse1.data);
171 var battleMap1 = provinceData1.data[0].arena_name;
172 mapArr1[i] = " [b]Map:[/b] " + battleMap2;
173 return;
174 });
175 }
176 function getArena2(frontID2, provinceID2, i){
177 http.simpleRequest({
178 method: 'GET',
179 timeout: 60000,
180 url: apiURL+region+'/wot/globalmap/provinces/'+apikey+'&front_id='+frontID2+'&province_id='+provinceID2+'&language=de&fields=arena_name'
181 },function(error, provinceResponse2) {
182 var provinceData2 = JSON.parse(provinceResponse2.data);
183 var battleMap2 = provinceData2.data[0].arena_name;
184 mapArr2[i] = " [b]Map:[/b] " + battleMap2;
185 return;
186 })
187 }
188 function getCompetitor1(competitorID1, i){
189 http.simpleRequest({
190 method: 'GET',
191 timeout: 60000,
192 url: apiURL+region+'/wot/globalmap/claninfo/'+apikey+'&clan_id='+competitorID1
193 },
194 function (error, competitorResponse1) {
195 var competitorData1 = JSON.parse(competitorResponse1.data);
196 var competitorTag1 = competitorData1.data[competitorID1].tag;
197 //engine.log(output);
198 vsArr1[i] = " [b]vs:[/b] [url=" + wotlifeurl + competitorTag1 + "-" + competitorID1 + "/]" + competitorTag1 + "[/url]";
199 return;
200 });
201 }
202 function getCompetitor2(competitorID2, i) {
203 http.simpleRequest({
204 method: 'GET',
205 timeout: 60000,
206 url: apiURL+region+'/wot/globalmap/claninfo/'+apikey+'&clan_id='+competitorID2
207 },
208 function (error, competitorResponse2) {
209 var competitorData2 = JSON.parse(competitorResponse2.data);
210 var competitorTag2 = competitorData2.data[competitorID2].tag;
211 //engine.log(output);
212 vsArr2[i] = " [b]vs:[/b] [url=" + wotlifeurl + competitorTag2 + "-" + competitorID2 + "/]" + competitorTag2 + "[/url]";
213 return;
214 })
215 }
216 setInterval(function refreshBattleList(){
217 output = "[b]Upcoming CW Battles:[/b]\n";
218 timeArr1 = [];
219 tierArr1 = [];
220 getClanData1();
221 timeArr2 = [];
222 tierArr2 = [];
223 getClanData2();
224 }, config.updatePeriod * 60000);
225});
226