Advertisement
Guest User

Untitled

a guest
May 3rd, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.08 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package Controller;
  6.  
  7. import Controller.AssistanceLogic.BuildingConstruction;
  8. import Controller.AssistanceLogic.Recruit;
  9. import Controller.Interaction.GetVillages;
  10. import Controller.Interaction.Market;
  11. import Controller.Scanner.IncomingScanner;
  12. import Controller.Scanner.TargetScanner;
  13. import Model.Data.Order;
  14. import Model.Data.Village;
  15. import Model.Enums.PlaceEnums;
  16. import Model.VillageController;
  17. import communication.CommunicationLevel;
  18. import java.io.File;
  19. import java.util.ArrayList;
  20. import java.util.Date;
  21. import java.util.logging.Level;
  22. import java.util.logging.Logger;
  23. import reports.Reporter;
  24. import webbot.aufrufe.GetAufruf;
  25. import webbot.aufrufe.LogoutAufruf;
  26. import webbot.core.Session;
  27. import webbot.core.SessionFactory;
  28.  
  29. /**
  30.  *
  31.  * @author Andreas
  32.  */
  33. public class Logic extends Thread {
  34.  
  35.     private Controller control;
  36.     private long lastPage = 0;
  37.     private long nextCycle = 0;
  38.     private int minSleep = 600000; //10min
  39.     private int maxSleep = 1800000; //30min
  40.     boolean firstCycle = true;
  41.     private Reporter reporter = new Reporter(this);
  42.  
  43.     public Logic(Controller control) {
  44.         this.control = control;
  45.         start();
  46.     }
  47.  
  48.     public void run() {
  49.         try {
  50.             while (true) {
  51.                 nextCycle = System.currentTimeMillis()+maxSleep;//Cycle zuruecksetzen
  52.                 if (firstCycle) {//Alles was nur beim ersten Durchlauf durchgefuert werden soll
  53.                     firstCycle = false;
  54.                     login(); //Login nur einmal nötig, wird danach bei jedem Aufruf von getPage() mit loginCheck auf true geprueft
  55.                     GetVillages vills = new GetVillages(this, control.getPreference()); //Holt die Dorfliste
  56.                     if (control.getModel().getTargets().getTargetsAsList().size() < 1) {
  57.                         TargetScanner scan = new TargetScanner(this, 140, false);
  58.                         scan.run();
  59.                         synchronized (this) { //Auf den Scanner warten.
  60.                             wait();
  61.                         }
  62.                     }
  63.                 }
  64.                 System.out.println("Reports");
  65.                 //control.getStatisticController().addReports(reporter.getAllReportsByDay(false, new Date(System.currentTimeMillis())));
  66.                 control.getStatisticController().getNewReports();
  67.                 GetVillages vills = new GetVillages(this, control.getPreference()); //Holt die Dorfliste
  68.                 System.out.println("Cycle");
  69. //                IncomingScanner scan = new IncomingScanner(this); //Scan nach Angriffen!
  70.                 //Ausbau / Rekrutierung / ... durchfuehren
  71.                 for (Village vill : getVillageController().getVillages()) {
  72.                     BuildingConstruction cons = new BuildingConstruction(this, vill); //Ausbau
  73.                    
  74.                     Recruit rec = new Recruit(this, vill); //Rekrutierung
  75.  
  76.                 }
  77.                 control.startNewScavengeRound(this);
  78.                 control.shutDown(); // Später wieder entfernen - NICHT VERGESSEN!
  79.                 pause();
  80.             }
  81.  
  82. //            logout();
  83.  
  84.  
  85.         } catch (Exception ex) {
  86.             ex.printStackTrace();
  87.         }
  88.         control.shutDown();
  89.     }
  90.  
  91.     public VillageController getVillageController() {
  92.         return control.getModel().getVillages();
  93.     }
  94.  
  95.     public void login() {
  96.         PreferenceController prefs = control.getPreference();
  97.         String username = prefs.loadPref("login-name");
  98.         String password = prefs.loadPref("password");
  99.         String server = prefs.loadPref("world");
  100.         getControl().setSession(SessionFactory.getLoginSession(username, password, server, this));
  101.     }
  102.  
  103.     /**
  104.      * Nicht zu empfehlen falls noch Aktionen ausgefuehrt werden sollen, z.B. in Threads!
  105.      */
  106.     public void logout() {
  107.         getControl().getSession().execute(new LogoutAufruf(getControl().getSession()), false);
  108.         getControl().setSession(null);
  109.     }
  110.  
  111.     public ArrayList<String> getPage(String relUrl) throws Exception {
  112.         ArrayList<String> temp;
  113.         synchronized (getSession()) {
  114.             nextPage();
  115.             GetAufruf ruf = new GetAufruf(getSession(), relUrl);
  116.             temp = (ArrayList<String>) getSession().execute(ruf, true);
  117.         }
  118.         if (temp == null) {
  119.             System.err.println("Ausgeloggt! Logge neu ein!");
  120.             login();
  121.             temp = getPage(relUrl);
  122.         }
  123.         return temp;
  124.     }
  125.  
  126.     public Session getSession() throws Exception {
  127.         if (getControl().getSession() == null) {
  128.             throw new Exception("Session == null -> Stopping!");
  129.         }
  130.         return getControl().getSession();
  131.     }
  132.  
  133.     private void nextPage() {
  134.         long diff = System.currentTimeMillis() - lastPage;
  135.         if (diff < 300) {
  136.             try {
  137.                 Thread.sleep(diff + 50);
  138.             } catch (Exception ex) {
  139.                 ex.printStackTrace();
  140.             }
  141.         }
  142.         lastPage = System.currentTimeMillis();
  143.     }
  144.  
  145.     private void pause() {
  146.         long sleep = nextCycle - System.currentTimeMillis();
  147.         if (sleep < minSleep) {
  148.             sleep = minSleep;
  149.         } else if (sleep > maxSleep) {
  150.             sleep = maxSleep;
  151.         }
  152.         Date d = new Date(sleep + System.currentTimeMillis());
  153.         control.getCommunicator().write("Pausiere bis " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds(), CommunicationLevel.MESSAGE);
  154.         try {
  155.             sleep(sleep);
  156.         } catch (InterruptedException ex) {
  157.             Logger.getLogger(Logic.class.getName()).log(Level.SEVERE, null, ex);
  158.         }
  159.     }
  160.  
  161.     public void setNextCycle(long timestamp) {
  162.         if (timestamp < nextCycle) {
  163.             nextCycle = timestamp;
  164.         }
  165.     }
  166.  
  167.     /**
  168.      * @return the control
  169.      */
  170.     public Controller getControl() {
  171.         return control;
  172.     }
  173.  
  174.     public Reporter getReporter() {
  175.         return reporter;
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement