Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package Controller;
- import Controller.AssistanceLogic.BuildingConstruction;
- import Controller.AssistanceLogic.Recruit;
- import Controller.Interaction.GetVillages;
- import Controller.Interaction.Market;
- import Controller.Scanner.IncomingScanner;
- import Controller.Scanner.TargetScanner;
- import Model.Data.Order;
- import Model.Data.Village;
- import Model.Enums.PlaceEnums;
- import Model.VillageController;
- import communication.CommunicationLevel;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import reports.Reporter;
- import webbot.aufrufe.GetAufruf;
- import webbot.aufrufe.LogoutAufruf;
- import webbot.core.Session;
- import webbot.core.SessionFactory;
- /**
- *
- * @author Andreas
- */
- public class Logic extends Thread {
- private Controller control;
- private long lastPage = 0;
- private long nextCycle = 0;
- private int minSleep = 600000; //10min
- private int maxSleep = 1800000; //30min
- boolean firstCycle = true;
- private Reporter reporter = new Reporter(this);
- public Logic(Controller control) {
- this.control = control;
- start();
- }
- public void run() {
- try {
- while (true) {
- nextCycle = System.currentTimeMillis()+maxSleep;//Cycle zuruecksetzen
- if (firstCycle) {//Alles was nur beim ersten Durchlauf durchgefuert werden soll
- firstCycle = false;
- login(); //Login nur einmal nötig, wird danach bei jedem Aufruf von getPage() mit loginCheck auf true geprueft
- GetVillages vills = new GetVillages(this, control.getPreference()); //Holt die Dorfliste
- if (control.getModel().getTargets().getTargetsAsList().size() < 1) {
- TargetScanner scan = new TargetScanner(this, 140, false);
- scan.run();
- synchronized (this) { //Auf den Scanner warten.
- wait();
- }
- }
- }
- System.out.println("Reports");
- //control.getStatisticController().addReports(reporter.getAllReportsByDay(false, new Date(System.currentTimeMillis())));
- control.getStatisticController().getNewReports();
- GetVillages vills = new GetVillages(this, control.getPreference()); //Holt die Dorfliste
- System.out.println("Cycle");
- // IncomingScanner scan = new IncomingScanner(this); //Scan nach Angriffen!
- //Ausbau / Rekrutierung / ... durchfuehren
- for (Village vill : getVillageController().getVillages()) {
- BuildingConstruction cons = new BuildingConstruction(this, vill); //Ausbau
- Recruit rec = new Recruit(this, vill); //Rekrutierung
- }
- control.startNewScavengeRound(this);
- control.shutDown(); // Später wieder entfernen - NICHT VERGESSEN!
- pause();
- }
- // logout();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- control.shutDown();
- }
- public VillageController getVillageController() {
- return control.getModel().getVillages();
- }
- public void login() {
- PreferenceController prefs = control.getPreference();
- String username = prefs.loadPref("login-name");
- String password = prefs.loadPref("password");
- String server = prefs.loadPref("world");
- getControl().setSession(SessionFactory.getLoginSession(username, password, server, this));
- }
- /**
- * Nicht zu empfehlen falls noch Aktionen ausgefuehrt werden sollen, z.B. in Threads!
- */
- public void logout() {
- getControl().getSession().execute(new LogoutAufruf(getControl().getSession()), false);
- getControl().setSession(null);
- }
- public ArrayList<String> getPage(String relUrl) throws Exception {
- ArrayList<String> temp;
- synchronized (getSession()) {
- nextPage();
- GetAufruf ruf = new GetAufruf(getSession(), relUrl);
- temp = (ArrayList<String>) getSession().execute(ruf, true);
- }
- if (temp == null) {
- System.err.println("Ausgeloggt! Logge neu ein!");
- login();
- temp = getPage(relUrl);
- }
- return temp;
- }
- public Session getSession() throws Exception {
- if (getControl().getSession() == null) {
- throw new Exception("Session == null -> Stopping!");
- }
- return getControl().getSession();
- }
- private void nextPage() {
- long diff = System.currentTimeMillis() - lastPage;
- if (diff < 300) {
- try {
- Thread.sleep(diff + 50);
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- lastPage = System.currentTimeMillis();
- }
- private void pause() {
- long sleep = nextCycle - System.currentTimeMillis();
- if (sleep < minSleep) {
- sleep = minSleep;
- } else if (sleep > maxSleep) {
- sleep = maxSleep;
- }
- Date d = new Date(sleep + System.currentTimeMillis());
- control.getCommunicator().write("Pausiere bis " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds(), CommunicationLevel.MESSAGE);
- try {
- sleep(sleep);
- } catch (InterruptedException ex) {
- Logger.getLogger(Logic.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- public void setNextCycle(long timestamp) {
- if (timestamp < nextCycle) {
- nextCycle = timestamp;
- }
- }
- /**
- * @return the control
- */
- public Controller getControl() {
- return control;
- }
- public Reporter getReporter() {
- return reporter;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement