package rv.comm.rcssserver;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import rv.world.WorldModel;

public class FindGoalsThread extends Thread {

    public interface ResultCallback {

        void goalFound(int goalFrame);

        void finished();
    }

    private final File           file;
    private final ResultCallback callback;

    private WorldModel           world;
    private MessageParser        parser;
    private ILogfileReader       logfile;
    private boolean              lastFrameGoalLeft  = false;
    private boolean              lastFrameGoalRight = false;
    private boolean              aborted            = false;

    public FindGoalsThread(File file, ResultCallback callback) {
        super();
        this.file = file;
        this.callback = callback;
    }

    public void abort() {
        this.aborted = true;
    }

    @Override
    public void run() {
        world = new WorldModel();
        parser = new MessageParser(world);
        logfile = null;

        try {
            logfile = new LogfileReaderBuffered(new Logfile(file), 200);
        } catch (Exception e) {
            e.printStackTrace();
        }

        while (!logfile.isAtEndOfLog() && !aborted) {
            processFrame();
            try {
                logfile.stepForward();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        processFrame();
    }

    private void processFrame() {
        String msg = logfile.getCurrentFrameMessage();
        if (msg != null) {
            try {
                parser.parse(msg);
            } catch (ParseException e) {
                e.printStackTrace();
            }

            switch (world.getGameState().getPlayMode()) {
            case "Goal_Left":
                if (!lastFrameGoalLeft) {
                    callback.goalFound(logfile.getCurrentFrame());
                    lastFrameGoalLeft = true;
                    lastFrameGoalRight = false;
                }
                break;
            case "Goal_Right":
                if (!lastFrameGoalRight) {
                    callback.goalFound(logfile.getCurrentFrame());
                    lastFrameGoalLeft = false;
                    lastFrameGoalRight = true;
                }
                break;
            default:
                lastFrameGoalLeft = false;
                lastFrameGoalRight = false;
                break;
            }
        }
    }
}
@@ -501,39 +501,39 @@
 le;%0A    private 
-boolean
+int    
               la
@@ -538,25 +538,21 @@
 last
-FrameGoal
+Score
 Left  = 
 fals
@@ -547,21 +547,18 @@
 Left  = 
-false
+-1
 ;%0A    pr
@@ -555,39 +555,39 @@
 -1;%0A    private 
-boolean
+int    
               la
@@ -588,33 +588,29 @@
     last
-FrameGoal
+Score
 Right = 
 false;%0A 
@@ -597,29 +597,26 @@
 coreRight = 
-false
+-1
 ;%0A    privat
@@ -653,20 +653,16 @@
         
-    
 = false;
@@ -1724,16 +1724,24 @@
     
-switch (
+int scoreLeft = 
 worl
@@ -1764,161 +1764,89 @@
 .get
-PlayMode()) %7B%0A            case %22Goal_Left%22:%0A                if (!lastFrameGoalLeft) %7B%0A                    callback.goalFound(logfile.getCurrentFrame()
+ScoreLeft();%0A            int scoreRight = world.getGameState().getScoreRight(
 );%0A
+%0A
     
@@ -1857,89 +1857,55 @@
     
-        lastFrameGoal
+if (lastScore
 Left 
+!
 = 
-true;%0A                    lastFrameGoal
+-1 && lastScore
 Right 
+!
 = 
-false;
+-1
 %0A   
@@ -1921,111 +1921,79 @@
     
-%7D%0A                break;%0A            case %22Goal_Right%22:%0A                if (!lastFrameGoal
+    && (scoreLeft != lastScoreLeft %7C%7C scoreRight != lastScore
 Right)
+)
  %7B%0A
-    
     
@@ -2067,275 +2067,90 @@
     
-        lastFrameGoalLeft = false;%0A                    lastFrameGoalRight = true;%0A                %7D%0A                break;%0A            default:%0A                lastFrameGoalLeft = false;%0A                lastFrameGoalRight = false;%0A                break;%0A            %7D
+%7D%0A%0A            lastScoreLeft = scoreLeft;%0A            lastScoreRight = scoreRight;
 %0A   

fix some goals not being found