· 5 years ago · Dec 19, 2020, 01:30 PM
1let 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 console.log("Bot has started.");
83
84 //Create our Member List
85 $.getJSON("https://api.torn.com/faction/?selections=&key="+key, function(data){ //gets the data from that link
86
87 memberData = data.members //gets to the actual content
88
89 for(i = 0; i < Object.keys(memberData).length; i++){
90
91 //adds a new member to our members array.
92 members.push(new Member(
93 Object.entries(memberData)[i][1].name, //gets the members name from API
94 Object.entries(memberData)[i][0] //gets the members ID from the API
95 ));
96
97 }
98
99 console.log("Loaded current member list. Found " + members.length + " members.");
100 });
101
102 for(s = 0; s < stats.length; s++){
103
104 let currentData = stats[s];
105 //cycles 4 times setting each of the old stats
106 $.getJSON("https://api.torn.com/faction/?selections=contributors&stat=gym"+stats[s].GetStatName()+"&key="+key, function(data){ //gets the data from that URL
107
108 let d = Object.entries(data.contributors)[0][1]; //gets the right type of data from the data
109
110 SetDefaultData(d, currentData) //creates the starting 'old' data and lets it know what type it is.
111
112 });
113 }
114
115 //Start Loop
116 StartLoop(id);
117
118 }
119
120 function SetDefaultData(data, stat){
121
122
123 for(i = 0; i < Object.keys(data).length; i++){ //cycles through all the data
124 if(Object.entries(data)[i][1].in_faction == 1){ //if the person is currently a member
125 stat.AddData( //creates a stat entry and
126 Object.entries(data)[i][0], //adds the persons ID
127 Object.entries(data)[i][1].contributed //adds the persons contribution
128 ); //to the stat entry
129 }
130 }
131
132 console.log("Found "+ stat.GetStatData().length +" users who have contributed towards " + stat.GetStatName()); //prints out so we know how many people we got in that stat to start with.
133
134 }
135
136 function StartLoop(id){
137
138 console.log("Loop started");
139 setInterval(function GetData(){
140 for(s = 0; s < stats.length; s++){
141
142 let currentStat = stats[s];
143
144 $.getJSON("https://api.torn.com/faction/?selections=contributors&stat=gym"+stats[s].GetStatName()+"&key="+key, function(data){ //gets the data from that URL
145
146 let d = Object.entries(data.contributors)[0][1];
147 ConvertObjectToArray(d, currentStat, id);
148 //d is our new data
149 //currentstat.name is the stat that we are working on in future functions
150
151 console.log("Timer Restarted.");
152
153 });
154 }
155 }, 60000);
156 }
157
158
159 function ConvertObjectToArray(newData, currentStat, id){
160
161 newMembers = [];
162 newContributions = [];
163
164 for(i = 0; i < Object.keys(newData).length; i++){
165 if(Object.entries(newData)[i][1].in_faction == 1){
166 //adds them to our 2 new arrays
167 newMembers.push(Object.entries(newData)[i][0]);
168 newContributions.push(Object.entries(newData)[i][1].contributed);
169 }
170 }
171
172 CheckForDifferences(currentStat, id);
173
174 }
175
176 function CheckForDifferences(currentStat, id){
177 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.
178
179 for (i = 0; i < newMembers.length; i++){ //cycle through our current users
180
181 for(x = 0; x < currentStat.GetStatData().length; x++){ //check our 'old users'
182 if(newMembers[i] == currentStat.GetStatData()[x].ID){ //find our 'new members' in our 'current stats' data
183 if(newContributions[i] > currentStat.GetStatData()[x].Contribution){ //check if they have spent energy
184 Alert(newMembers[i], currentStat.GetStatName(), currentStat.GetStatData()[x].Contribution, newContributions[i]); //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.
185
186 }
187 }
188 }
189 }
190
191 if(updateOld){
192 currentStat.ClearData() //sets the current stats Data to nothing.
193
194 //cycles through the new data (members and contributions) and writes them to the current stats data.
195 for(y = 0; y < newMembers.length; y++){
196 currentStat.AddData(
197 newMembers[y],
198 newContributions[y]
199 );
200 }
201 console.log("Reassigned old users to new users");
202 }
203 }
204
205 function Alert(who, stat, oldV, newV, id){
206 //works out how much E they've trained.
207 let diff = newV - oldV;
208
209 //variable for storing the persons name
210 let name;
211
212 //cycles through the member list to get the current users name.
213 for(i = 0; i < members.length; i++){
214 if(who == members[i].GetID()){
215 name = members[i].GetName();
216 break;
217 }
218 }
219
220 //prints out the information, name, difference, which stat, old contribution and new.
221 console.log(name + " has spent " + diff + " Energy at the Gym training " + stat +"." + "\n" + "Old contribution: "+ oldV + "\n" + "New Contribution: "+ newV);
222
223 //sets updateOld to true so that we update the old information after we're done printing.
224 updateOld = true;
225
226 }
227
228 StartBot(5); //change 5 to your ID
229