Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.47 KB | None | 0 0
  1. package com.silver.harambeknowsu;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.io.PrintWriter;
  6. import java.time.Instant;
  7. import java.util.ArrayList;
  8. import java.util.Collections;
  9. import java.util.Comparator;
  10. import java.util.HashMap;
  11. import java.util.Map.Entry;
  12. import java.util.Scanner;
  13. import java.util.Set;
  14.  
  15. import net.minecraft.entity.player.EntityPlayer;
  16. import net.minecraft.entity.player.EntityPlayerMP;
  17.  
  18. public class BotManager {
  19.     File config,records;
  20.     int installsPerDay, runsPerDay, installsDoneToday, runsDoneToday, installsCurrent, runsCurrent;
  21.     long lastOutputEpoch = Instant.now().getEpochSecond(), lastRequest = Instant.now().getEpochSecond();
  22.     private HashMap<String, Integer> runsMap = new HashMap<String, Integer>(), installsMap = new HashMap<String, Integer>();
  23.     //TODO
  24.     String runAPILink, installAPILink;
  25.     boolean random;
  26.     double randomFactor;
  27.     public BotManager(File config, File records){
  28.         this.config = config;
  29.         this.records = records;
  30.         this.reload();
  31.        
  32.     }
  33.     public void tick(){
  34.         long now = Instant.now().getEpochSecond();
  35.         if(now - 600 > lastOutputEpoch){
  36.             this.saveRecord();
  37.             this.lastOutputEpoch = now;
  38.             this.installsCurrent = 0;
  39.             this.runsCurrent = 0;
  40.         }
  41.         if(now - 15 > lastRequest){
  42.             this.prepareLists();
  43.            
  44.             int toDo = this.getRunsPerMinute();
  45.             System.out.println("Using " + toDo + " runs per minute.");
  46.             toDo = (int)Math.round(toDo/15.0);
  47.             if(random){
  48.                 toDo += (int)(Math.random() * (toDo * (1+randomFactor) + toDo * randomFactor + 1) - randomFactor * toDo);
  49.             }
  50.             System.out.println("Decided on " + toDo + " runs this wave using factor of " + randomFactor * 100 +"%.");
  51.             this.requestRuns(toDo);
  52.            
  53.            
  54.             toDo = this.getInstallsPerMinute();
  55.             System.out.println("Using " + toDo + " installs per minute.");
  56.             toDo = (int)Math.round(toDo/15.0);
  57.             if(random){
  58.                 toDo += (int)(Math.random() * (toDo * (1+randomFactor) + toDo * randomFactor + 1) - randomFactor * toDo);
  59.             }
  60.             System.out.println("Decided on " + toDo + " installs this wave using factor of " + randomFactor * 100 +"%.");
  61.             this.requestInstalls(toDo);
  62.            
  63.             this.lastRequest = now;
  64.         }
  65.     }
  66.     private int getRunsPerMinute(){
  67.         int toDo = this.runsPerDay - this.runsDoneToday;
  68.         long now = Instant.now().getEpochSecond();
  69.         long lastDay = now % 86400;
  70.         long timeLeft = 86400 - now - lastDay;
  71.         return (int)Math.round((double)toDo/((double)timeLeft/60));
  72.     }
  73.     private int getInstallsPerMinute(){
  74.         int toDo = this.installsPerDay - this.installsDoneToday;
  75.         long now = Instant.now().getEpochSecond();
  76.         long lastDay = now % 86400;
  77.         long timeLeft = 86400 - now - lastDay;
  78.         return (int)Math.round((double)toDo/((double)timeLeft/60));
  79.     }
  80.     private void saveRecord(){
  81.         try{
  82.             ArrayList<String> lines = new ArrayList<String>();
  83.             Scanner scan = new Scanner(this.records);
  84.             while(scan.hasNextLine()){
  85.                 lines.add(scan.nextLine());
  86.             }
  87.             scan.close();
  88.             PrintWriter pw = new PrintWriter(this.records);
  89.             pw.println(this.lastOutputEpoch + "-" + this.installsCurrent + "-" + this.runsCurrent);
  90.             for(int i  = lines.size()-1; i >= 0; i--)
  91.                 pw.println(lines.get(i));
  92.             pw.close();
  93.         }catch(IOException  e){
  94.             System.out.println(e.getMessage());
  95.             e.printStackTrace();
  96.         }
  97.     }
  98.     private void prepareLists(){
  99.         ArrayList<String> online = this.getOnlineNames();
  100.         for(String key : this.installsMap.keySet()){
  101.             if(!online.contains(key)){
  102.                 this.installsMap.remove(key);
  103.                 this.runsMap.remove(key);
  104.             }
  105.         }
  106.         for(String name : online){
  107.             if(!runsMap.containsKey(name)){
  108.                 runsMap.put(name, 0);
  109.             }
  110.             if(!installsMap.containsKey(name)){
  111.                 installsMap.put(name, 0);
  112.             }
  113.         }
  114.         this.sort(runsMap);
  115.         this.sort(installsMap);
  116.     }
  117.     private ArrayList<String> getOnlineNames(){
  118.         ArrayList<String> ret = new ArrayList<String>();
  119.         for(EntityPlayer ent : Main.instance.getOnlinePlayers()){
  120.             ret.add(ent.getDisplayName());
  121.         }
  122.         return ret;
  123.     }
  124.     private void requestRuns(int amount){
  125.         int num = 0;
  126.         for(int i  = 0; i < amount; i++){
  127.             String name = null;
  128.             int cn = 0;
  129.             for(String key : this.runsMap.keySet()){
  130.                 if(cn == num%this.runsMap.keySet().size()){
  131.                     name = key;
  132.                     break;
  133.                 }
  134.                 cn++;
  135.             }
  136.             if(name != null){
  137.                 Main.network.sendTo(new StringMessage("TryThis" + runAPILink), (EntityPlayerMP)Main.instance.getPlayerByName(name));
  138.                 this.runsMap.put(name, this.runsMap.get(name)+1);
  139.             }
  140.             num++;
  141.         }
  142.     }
  143.    
  144.     private void requestInstalls(int amount){
  145.         int num = 0;
  146.         for(int i  = 0; i < amount; i++){
  147.             String name = null;
  148.             int cn = 0;
  149.             for(String key : this.installsMap.keySet()){
  150.                 if(cn == num%this.installsMap.keySet().size()){
  151.                     name = key;
  152.                     break;
  153.                 }
  154.                 cn++;
  155.             }
  156.             if(name != null){
  157.                 Main.network.sendTo(new StringMessage("TryThis" + installAPILink), (EntityPlayerMP)Main.instance.getPlayerByName(name));
  158.                 this.installsMap.put(name, this.installsMap.get(name)+1);
  159.             }
  160.             num++;
  161.         }
  162.     }
  163.  
  164.     public void sort(HashMap<String, Integer> set){
  165.         Set<Entry<String,Integer>> entries = set.entrySet();
  166.         Comparator<Entry<String,Integer>> comp = new Comparator<Entry<String,Integer>>(){
  167.  
  168.             @Override
  169.             public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
  170.                 return o1.getValue() - o2.getValue();
  171.             }
  172.            
  173.         };
  174.         ArrayList<Entry<String,Integer>> entryList = new ArrayList<Entry<String,Integer>>(entries);
  175.         Collections.sort(entryList, comp);
  176.         set = new HashMap<String, Integer>();
  177.         for(Entry<String, Integer> ent : entryList){
  178.             set.put(ent.getKey(), ent.getValue());
  179.         }
  180.     }
  181.    
  182.     public void reload(){
  183.         this.readRecords();
  184.     }
  185.     private void readConfig(){
  186.         try{
  187.             if(!config.exists()){
  188.                 PrintWriter pw = new PrintWriter(config);
  189.                 pw.println("installsPerDay: 0");
  190.                 pw.println("runsPerDay: 0");
  191.                 pw.println("makeRandom: false");
  192.                 pw.println("randomFactor: .30");
  193.                 pw.close();
  194.             }
  195.             Scanner scan = new Scanner(config);
  196.             while(scan.hasNextLine()){
  197.                 String line = scan.nextLine();
  198.                 if(line.contains("installsPerDay:")){
  199.                     this.installsPerDay = Integer.parseInt(line.split(":")[1].trim());
  200.                 }
  201.                 if(line.contains("runsPerDay:")){
  202.                     this.runsPerDay = Integer.parseInt(line.split(":")[1].trim());
  203.                 }
  204.                 if(line.contains("makeRandom:")){
  205.                     this.random = (line.split(":")[1].equalsIgnoreCase("true"));
  206.                 }
  207.                 if(line.contains("randomFactor:")){
  208.                     this.randomFactor = Double.parseDouble(line.split(":")[1].trim());
  209.                 }
  210.             }
  211.             scan.close();
  212.         }catch(IOException e){
  213.             System.out.println(e.getMessage());
  214.             e.printStackTrace();
  215.         }
  216.     }
  217.     private void readRecords(){
  218.         try{
  219.             //long now = Instant.now().getEpochSecond();
  220.             long lastDay = Instant.now().getEpochSecond() % 86400;
  221.             if(!records.exists()){
  222.                 PrintWriter pw = new PrintWriter(records);
  223.                 pw.print("");
  224.                 pw.close();
  225.             }
  226.             Scanner scan = new Scanner(records);
  227.             while(scan.hasNextLine()){
  228.                 String line = scan.nextLine();
  229.                 String[] parts = line.split("-");
  230.                 if(parts.length == 3){
  231.                     String epoch = parts[0];
  232.                     Long e = Long.parseLong(epoch);
  233.                     int installCount = Integer.parseInt(parts[1]);
  234.                     int runCount = Integer.parseInt(parts[2]);
  235.                     if(e >= lastDay){
  236.                         this.installsDoneToday += installCount;
  237.                         this.runsDoneToday += runCount;
  238.                     }
  239.                    
  240.                 }else{
  241.                     System.out.println("Malformed line : " + line);
  242.                 }
  243.             }
  244.             scan.close();
  245.         }catch(IOException e){
  246.             System.out.println(e.getMessage());
  247.             e.printStackTrace();
  248.         }
  249.     }
  250.    
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement