Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.silver.harambeknowsu;
- import java.io.File;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.time.Instant;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.HashMap;
- import java.util.Map.Entry;
- import java.util.Scanner;
- import java.util.Set;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.entity.player.EntityPlayerMP;
- public class BotManager {
- File config,records;
- int installsPerDay, runsPerDay, installsDoneToday, runsDoneToday, installsCurrent, runsCurrent;
- long lastOutputEpoch = Instant.now().getEpochSecond(), lastRequest = Instant.now().getEpochSecond();
- private HashMap<String, Integer> runsMap = new HashMap<String, Integer>(), installsMap = new HashMap<String, Integer>();
- //TODO
- String runAPILink, installAPILink;
- boolean random;
- double randomFactor;
- public BotManager(File config, File records){
- this.config = config;
- this.records = records;
- this.reload();
- }
- public void tick(){
- long now = Instant.now().getEpochSecond();
- if(now - 600 > lastOutputEpoch){
- this.saveRecord();
- this.lastOutputEpoch = now;
- this.installsCurrent = 0;
- this.runsCurrent = 0;
- }
- if(now - 15 > lastRequest){
- this.prepareLists();
- int toDo = this.getRunsPerMinute();
- System.out.println("Using " + toDo + " runs per minute.");
- toDo = (int)Math.round(toDo/15.0);
- if(random){
- toDo += (int)(Math.random() * (toDo * (1+randomFactor) + toDo * randomFactor + 1) - randomFactor * toDo);
- }
- System.out.println("Decided on " + toDo + " runs this wave using factor of " + randomFactor * 100 +"%.");
- this.requestRuns(toDo);
- toDo = this.getInstallsPerMinute();
- System.out.println("Using " + toDo + " installs per minute.");
- toDo = (int)Math.round(toDo/15.0);
- if(random){
- toDo += (int)(Math.random() * (toDo * (1+randomFactor) + toDo * randomFactor + 1) - randomFactor * toDo);
- }
- System.out.println("Decided on " + toDo + " installs this wave using factor of " + randomFactor * 100 +"%.");
- this.requestInstalls(toDo);
- this.lastRequest = now;
- }
- }
- private int getRunsPerMinute(){
- int toDo = this.runsPerDay - this.runsDoneToday;
- long now = Instant.now().getEpochSecond();
- long lastDay = now % 86400;
- long timeLeft = 86400 - now - lastDay;
- return (int)Math.round((double)toDo/((double)timeLeft/60));
- }
- private int getInstallsPerMinute(){
- int toDo = this.installsPerDay - this.installsDoneToday;
- long now = Instant.now().getEpochSecond();
- long lastDay = now % 86400;
- long timeLeft = 86400 - now - lastDay;
- return (int)Math.round((double)toDo/((double)timeLeft/60));
- }
- private void saveRecord(){
- try{
- ArrayList<String> lines = new ArrayList<String>();
- Scanner scan = new Scanner(this.records);
- while(scan.hasNextLine()){
- lines.add(scan.nextLine());
- }
- scan.close();
- PrintWriter pw = new PrintWriter(this.records);
- pw.println(this.lastOutputEpoch + "-" + this.installsCurrent + "-" + this.runsCurrent);
- for(int i = lines.size()-1; i >= 0; i--)
- pw.println(lines.get(i));
- pw.close();
- }catch(IOException e){
- System.out.println(e.getMessage());
- e.printStackTrace();
- }
- }
- private void prepareLists(){
- ArrayList<String> online = this.getOnlineNames();
- for(String key : this.installsMap.keySet()){
- if(!online.contains(key)){
- this.installsMap.remove(key);
- this.runsMap.remove(key);
- }
- }
- for(String name : online){
- if(!runsMap.containsKey(name)){
- runsMap.put(name, 0);
- }
- if(!installsMap.containsKey(name)){
- installsMap.put(name, 0);
- }
- }
- this.sort(runsMap);
- this.sort(installsMap);
- }
- private ArrayList<String> getOnlineNames(){
- ArrayList<String> ret = new ArrayList<String>();
- for(EntityPlayer ent : Main.instance.getOnlinePlayers()){
- ret.add(ent.getDisplayName());
- }
- return ret;
- }
- private void requestRuns(int amount){
- int num = 0;
- for(int i = 0; i < amount; i++){
- String name = null;
- int cn = 0;
- for(String key : this.runsMap.keySet()){
- if(cn == num%this.runsMap.keySet().size()){
- name = key;
- break;
- }
- cn++;
- }
- if(name != null){
- Main.network.sendTo(new StringMessage("TryThis" + runAPILink), (EntityPlayerMP)Main.instance.getPlayerByName(name));
- this.runsMap.put(name, this.runsMap.get(name)+1);
- }
- num++;
- }
- }
- private void requestInstalls(int amount){
- int num = 0;
- for(int i = 0; i < amount; i++){
- String name = null;
- int cn = 0;
- for(String key : this.installsMap.keySet()){
- if(cn == num%this.installsMap.keySet().size()){
- name = key;
- break;
- }
- cn++;
- }
- if(name != null){
- Main.network.sendTo(new StringMessage("TryThis" + installAPILink), (EntityPlayerMP)Main.instance.getPlayerByName(name));
- this.installsMap.put(name, this.installsMap.get(name)+1);
- }
- num++;
- }
- }
- public void sort(HashMap<String, Integer> set){
- Set<Entry<String,Integer>> entries = set.entrySet();
- Comparator<Entry<String,Integer>> comp = new Comparator<Entry<String,Integer>>(){
- @Override
- public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
- return o1.getValue() - o2.getValue();
- }
- };
- ArrayList<Entry<String,Integer>> entryList = new ArrayList<Entry<String,Integer>>(entries);
- Collections.sort(entryList, comp);
- set = new HashMap<String, Integer>();
- for(Entry<String, Integer> ent : entryList){
- set.put(ent.getKey(), ent.getValue());
- }
- }
- public void reload(){
- this.readRecords();
- }
- private void readConfig(){
- try{
- if(!config.exists()){
- PrintWriter pw = new PrintWriter(config);
- pw.println("installsPerDay: 0");
- pw.println("runsPerDay: 0");
- pw.println("makeRandom: false");
- pw.println("randomFactor: .30");
- pw.close();
- }
- Scanner scan = new Scanner(config);
- while(scan.hasNextLine()){
- String line = scan.nextLine();
- if(line.contains("installsPerDay:")){
- this.installsPerDay = Integer.parseInt(line.split(":")[1].trim());
- }
- if(line.contains("runsPerDay:")){
- this.runsPerDay = Integer.parseInt(line.split(":")[1].trim());
- }
- if(line.contains("makeRandom:")){
- this.random = (line.split(":")[1].equalsIgnoreCase("true"));
- }
- if(line.contains("randomFactor:")){
- this.randomFactor = Double.parseDouble(line.split(":")[1].trim());
- }
- }
- scan.close();
- }catch(IOException e){
- System.out.println(e.getMessage());
- e.printStackTrace();
- }
- }
- private void readRecords(){
- try{
- //long now = Instant.now().getEpochSecond();
- long lastDay = Instant.now().getEpochSecond() % 86400;
- if(!records.exists()){
- PrintWriter pw = new PrintWriter(records);
- pw.print("");
- pw.close();
- }
- Scanner scan = new Scanner(records);
- while(scan.hasNextLine()){
- String line = scan.nextLine();
- String[] parts = line.split("-");
- if(parts.length == 3){
- String epoch = parts[0];
- Long e = Long.parseLong(epoch);
- int installCount = Integer.parseInt(parts[1]);
- int runCount = Integer.parseInt(parts[2]);
- if(e >= lastDay){
- this.installsDoneToday += installCount;
- this.runsDoneToday += runCount;
- }
- }else{
- System.out.println("Malformed line : " + line);
- }
- }
- scan.close();
- }catch(IOException e){
- System.out.println(e.getMessage());
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement