· 5 years ago · Dec 21, 2020, 07:16 PM
1 let members = []; //stores our members
2
3 //class for our members
4 let Member = class{
5 constructor(_Name, _ID){
6 this.Name = _Name;
7 this.ID = _ID;
8 }
9
10 GetName() {
11 return this.Name;
12 }
13
14 GetID(){
15 return this.ID;
16 }
17 }
18
19 let Data = class {
20 constructor(_ID, _Contribution){
21 this.ID = _ID;
22 this.Contribution = _Contribution
23 }
24
25 GetContribution(){
26 return this.Contribution;
27 }
28
29 GetID(){
30 return this.ID
31 }
32 }
33
34 let speed = [];
35 let defense = [];
36 let strength = [];
37 let dexterity = [];
38
39 let stats = [];
40
41 let Stat = class{
42 constructor(_Name, _Data){
43 this.Name = _Name;
44 this.Data = _Data;
45 }
46
47 GetStatName(){
48 return this.Name;
49 }
50
51 GetStatData(){
52 return this.Data;
53 }
54
55 AddData(_ID, _Contribution){
56 this.ID = _ID;
57 this.Contribution = _Contribution;
58
59 this.Data.push(new Data(this.ID, this.Contribution));
60 }
61
62 ClearData(){
63 this.Data = [];
64 }
65
66 }
67
68 stats.push(new Stat("speed", speed));
69 stats.push(new Stat("strength", strength));
70 stats.push(new Stat("dexterity", dexterity));
71 stats.push(new Stat("defense", defense));
72
73
74 let newMembers = [];
75 let newContributions = [];
76
77 let updateOld;
78
79 let key = "CgWsfUhtfSeEqizO"
80
81 function StartBot(id){
82
83 console.log("Bot started. Getting Member List");
84
85 //runs this immediately
86 $.getJSON("https://api.torn.com/faction/?selections=&key="+key, function(data){ //gets the data from that link
87
88 setTimeout(function(){
89 memberData = data.members //gets to the actual content
90
91 for(i = 0; i < Object.keys(memberData).length; i++){
92
93 //adds a new member to our members array.
94 members.push(new Member(
95 Object.entries(memberData)[i][1].name, //gets the members name from API
96 Object.entries(memberData)[i][0] //gets the members ID from the API
97 ));
98
99 }
100
101 console.log("Loaded current member list. Found " + members.length + " members.");
102
103 }, 5000);
104 });
105
106 //runs this after 15 seconds
107 setTimeout(function(){
108 for(s = 0; s < stats.length; s++){
109 GetStatDataFromAPI(1, stats[s], id);
110 }
111 }, 15000);
112
113 //starts the loop after 30 seconds.
114 setTimeout(function(){
115 StartLoop(id);
116 console.log("Loop started");
117 }, 30000)
118
119 }
120
121 function GetStatDataFromAPI(type, stat, id){
122
123 $.getJSON("https://api.torn.com/faction/?selections=contributors&stat=gym"+stat.GetStatName()+"&key="+key, function(d){ //gets the data from that URL
124
125 //does this after 5 seconds
126 setTimeout(function(){
127
128 let data = Object.entries(d.contributors)[0][1]; //gets the right type of data from the data
129
130 newMembers = [];
131 newContributions = [];
132
133 for(i = 0; i < Object.keys(data).length; i++){ //cycles through all the data
134 if(Object.entries(data)[i][1].in_faction == 1){ //if the person is currently a member
135
136 if(type == 1){
137
138 stat.AddData( //creates a stat entry and
139 Object.entries(data)[i][0], //adds the persons ID
140 Object.entries(data)[i][1].contributed //adds the persons contribution
141 ); //to the stat entry
142 //prints out so we know how many people we got in that stat to start with.
143
144 } else {
145
146 //adds them to our 2 new arrays
147 newMembers.push(Object.entries(data)[i][0]);
148 newContributions.push(Object.entries(data)[i][1].contributed);
149 }
150 }
151 }
152
153 if(type == 1){
154 console.log("Found "+ stat.GetStatData().length +" users who have contributed towards " + stat.GetStatName());
155 } else {
156 CheckForDifferences(stat, id);
157 }
158 }, 5000)
159
160 });
161 }
162
163 function StartLoop(id){
164
165 //change the regularity to the how many minutes you want to check
166 let regularity = 2;
167
168 setInterval(function GetData(){
169
170 let day = new Date();
171
172 if(day.getMinutes() % regularity == 0 ){
173 CheckActivity(id);
174 }
175
176 }, 60000);
177 }
178
179
180 function CheckActivity(id){
181
182 console.log("Performing Faction Activity Checks")
183
184 for(s = 0; s < stats.length; s++){
185
186 GetStatDataFromAPI(2, stats[s], id);
187
188 }
189 }
190
191 function CheckForDifferences(currentStat, id){
192
193 updateOld = false; //we dont need to update the old at this point. We want this to only get set if we find differences in peoples contributions.
194
195 for (i = 0; i < newMembers.length; i++){ //cycle through our current users
196 for(x = 0; x < currentStat.GetStatData().length; x++){ //check our 'old users'
197 if(newMembers[i] == currentStat.GetStatData()[x].ID){ //find our 'new members' in our 'current stats' data
198 if(newContributions[i] > currentStat.GetStatData()[x].Contribution){ //check if they have spent energy
199
200 Alert(newMembers[i], currentStat.GetStatName(), currentStat.GetStatData()[x].Contribution, newContributions[i], id); //sends who, oldValue and newValue to the alert method. Best to get this away from here so you can fuck around with discord or do what you like.
201
202 }
203 }
204 }
205 }
206
207 if(updateOld){
208
209 currentStat.ClearData() //sets the current stats Data to nothing.
210
211 //cycles through the new data (members and contributions) and writes them to the current stats data.
212 for(y = 0; y < newMembers.length; y++){
213
214 currentStat.AddData(
215 newMembers[y],
216 newContributions[y]
217 );
218
219 }
220
221 }
222 }
223
224 function Alert(who, stat, oldV, newV, id){
225 //works out how much E they've trained.
226 let diff = newV - oldV;
227
228 //variable for storing the persons name
229 let name;
230
231 //cycles through the member list to get the current users name.
232 for(i = 0; i < members.length; i++){
233 if(who == members[i].GetID()){
234 name = members[i].GetName();
235 break;
236 }
237 }
238
239 //prints out the information, name, difference, which stat, old contribution and new.
240 console.log(name + " has spent " + diff + " Energy at the Gym training " + stat +"." + "\n" + "Old contribution: "+ oldV + "\n" + "New Contribution: "+ newV);
241
242 //sets updateOld to true so that we update the old information after we're done printing.
243 updateOld = true;
244
245 }
246
247 StartBot(5); //change 5 to your ID
248
249
250
251