Package rbafw.fsmhandler

Source Code of rbafw.fsmhandler.RBAFWHandler

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package rbafw.fsmhandler;

import java.util.logging.Level;
import java.util.logging.Logger;
import robobeans.ErrorMessage;
import robobeans.NonRefereeHearMessage;
import robobeans.PlayerParam;
import robobeans.PlayerType;
import robobeans.RefereeHearMessage;
import robobeans.Score;
import robobeans.SeeMessage;
import robobeans.SenseBodyMessage;
import robobeans.TeamSide;
import robobeans.ServerParam;
import robobeans.callback.Handler;
import robobeans.command.Command;
import rbafw.fsmhandler.FSMState.Decision;

/**
*
* @author ahmed-s-ghonim
*/
public class RBAFWHandler implements Handler {

    public void handleError(ErrorMessage error) {
        System.err.println("ERROR " + error.getMessage());
    }

    public void handlePlayerParam(PlayerParam param) {
        updateWorldHandler.handlePlayerParam(param);
    }

    public void handlePlayerType(PlayerType param) {
        updateWorldHandler.handlePlayerType(param);
    }

    public void handleScore(Score score) {
        updateWorldHandler.handleScore(score);
    }

    public void handleServerParam(ServerParam param) {
        updateWorldHandler.handleServerParam(param);
    }

    boolean isNewCycle() {
        return newCycle;
    }

    void cycleConsumed() {
        newCycle = false;
    }

    public boolean isFreeCommandCycle() {
        return this.freeCommandCycle;
    }

    public void setFreeCommandCycle(boolean freeCommandCycle) {
        this.freeCommandCycle = freeCommandCycle;
    }
    private UpdateWordHandler updateWorldHandler;
    private TeamSide mySide;
    private int myNumber;
    private PlayModeHandler playModeHandler;
    protected boolean newCycle = true;
    protected int time;
    protected FSMState myState;
    protected boolean synchdone = false;
    protected int synchronizingwithserver = 0;
    protected int synchtimeoflastsee = 0;
    protected boolean SynchOptimize = true;
    protected Logger logger = Logger.getLogger(this.getClass().getName());
    private boolean freeCommandCycle = true;

    /**
     * This functions informs the handler to synchronize with the server so that states are informed of newCycle taking into consideration the difference in the send rate of (see) and (sense_body) messages
     */
    public void setSynchOptimize(boolean SynchOptimize) {
        this.SynchOptimize = SynchOptimize;
    }

    public RBAFWHandler(UpdateWordHandler updateWorldHandler, PlayModeHandler playModeHandler) {
        this.updateWorldHandler = updateWorldHandler;
        this.playModeHandler = playModeHandler;
        this.logger.setLevel(Level.OFF);
    }

    /**
     * Some states return no commands and one other state. In this case it would be
     * wastefull to send the null command to the server and wait for a new cycle to give control
     * to the new state.
     * The better approach would to not send any commands to the server and to give control right away to the new state
     * which could also send null command and a new state
     */
    private Command handleDecision() {
        Command com = null;
        FSMState newstate = null;
        while (true) {
            Decision dec = myState.onNewCycle();
            logger.fine(time + " " + myState.getClass().getSimpleName() + " " + dec);
            com = dec.getCommand();
            newstate = dec.getState();
            if (com == null) {
                if (myState.getClass() == newstate.getClass()) // VERY IMORTANT
                {
                    cycleConsumed();
                    return null;
                } else {
                    myState = newstate;
                }
            } else {
                myState = newstate;
                cycleConsumed();
                return com;
            }
        }
    }

    public Command handleNewSenseBodyMessage(SenseBodyMessage sensebodymessage) {
        if (SynchOptimize) {
            if (synchdone) {
                synchronizingwithserver++;
                logger.finest("synchwithserver = " + synchronizingwithserver);
            } else {
            }
        }
        logger.finer(sensebodymessage.toString());
        if (sensebodymessage.getTime() > time) {
            newCycle = true;
        }
        time = sensebodymessage.getTime();
        updateWorldHandler.updateSenseBody(sensebodymessage);
        if (SynchOptimize) {
            if (synchdone) {
                if (synchronizingwithserver == 5) {
                    synchronizingwithserver = 0;
                    logger.finest("synchwithserver = " + synchronizingwithserver);
                    return handleDecision();
                }
            }
        }
        return null;
    }

    public Command handleNewRefereeHearMessage(RefereeHearMessage hearmessage) {
        logger.finer(hearmessage.toString());
        updateWorldHandler.updateRefereeHear(hearmessage);
        myState = onRefereeHearMessage(hearmessage);
        return null;
    }

    public Command handleNewNonRefereeHearMessage(NonRefereeHearMessage hearmessage) {
        logger.finer(hearmessage.toString());
        updateWorldHandler.updateNonRefereeHear(hearmessage);
        myState = myState.onNonRefereeHearMessage(hearmessage);
        return null;
    }

    public Command handleNewSeeMessage(SeeMessage worldmessage) {
        if (SynchOptimize) {
            if (synchdone) {
                synchronizingwithserver++;
                logger.finest("synchwithserver = " + synchronizingwithserver);
            } else {
                if (synchtimeoflastsee == worldmessage.getTime() - 2) {
                    logger.finest("synchdone");
                    synchdone = true;
                    synchronizingwithserver = 2;
                } else {
                    logger.finer("last see  = " + worldmessage.getTime());
                    synchtimeoflastsee = worldmessage.getTime();
                }
            }
        }
        logger.finer(worldmessage.toString());

        if (worldmessage.getTime() > time) {
            newCycle = true;
        }
        time = worldmessage.getTime();
        updateWorldHandler.updateSee(worldmessage);
        if (isNewCycle() || isFreeCommandCycle()) {
            return handleDecision();
        }
        return null;
    }

    public void handleMyNumber(int i) {
        logger.info("my Number " + i);
        myNumber = i;
        myState = updateWorldHandler.onMyNumberAndSide(myNumber, mySide);

    }

    public void handleMySide(TeamSide side) {
        logger.info("my Side " + side);
        mySide = side;
    }

    public Logger getLogger() {
        return logger;
    }

    public FSMState onRefereeHearMessage(RefereeHearMessage messsage) {

        RefereeHearMessage refereemessage = (RefereeHearMessage) messsage;
        messsage = (RefereeHearMessage) messsage;
        if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.PLAY_ON) {
            setFreeCommandCycle(false);
            return playModeHandler.onPLAY_ON();
        }
        if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.DROP_BALL) {
            setFreeCommandCycle(true);
            return playModeHandler.onDROP_BALL();
        }
        if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.HALF_TIME) {
            setFreeCommandCycle(true);
            return playModeHandler.onHALF_TIME();
        }
        if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.TIME_UP) {
            setFreeCommandCycle(true);
            return playModeHandler.onTIME_UP();
        }
        if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.TIME_EXTENDED) {
            setFreeCommandCycle(true);
            return playModeHandler.onTIME_EXTENDED();
        }
        if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.TIME_OVER) {
            setFreeCommandCycle(true);
            return playModeHandler.onTIME_OVER();
        }
        if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.BEFORE_KICK_OFF) {
            setFreeCommandCycle(false);
            return playModeHandler.onBEFORE_KICK_OFF();
        }
        if (mySide == TeamSide.LEFT) {
            if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.KICK_OFF_L) {
                setFreeCommandCycle(false);
                return playModeHandler.onKICK_OFF_US();
            }
            if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.KICK_OFF_R) {
                setFreeCommandCycle(false);
                return playModeHandler.onKICK_OFF_THEM();
            }
            if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.GOAL_L) {
                setFreeCommandCycle(true);
                return playModeHandler.onGOAL_THEM();
            }
            if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.GOAL_R) {
                setFreeCommandCycle(true);
                return playModeHandler.onGOAL_US();
            }
            if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.KICK_IN_R) {
                setFreeCommandCycle(false);
                return playModeHandler.onKICK_IN_THEM();
            }
            if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.KICK_IN_L) {
                setFreeCommandCycle(false);
                return playModeHandler.onKICK_IN_US();
            }
            if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.GOAL_KICK_R) {
                setFreeCommandCycle(false);
                return playModeHandler.onGOAL_KICK_THEM();
            }
            if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.GOAL_KICK_L) {
                setFreeCommandCycle(false);
                return playModeHandler.onGOAL_KICK_US();
            }
            if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.FREE_KICK_L) {
                setFreeCommandCycle(false);
                return playModeHandler.onFREE_KICK_US();
            }
            if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.FREE_KICK_R) {
                setFreeCommandCycle(false);
                return playModeHandler.onFREE_KICK_THEM();
            }
            if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.CORNER_KICK_R) {
                setFreeCommandCycle(false);
                return playModeHandler.onCORNER_KICK_THEM();
            }
            if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.CORNER_KICK_L) {
                setFreeCommandCycle(false);
                return playModeHandler.onCORNER_KICK_US();
            }
            if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.FREE_KICK_L_FAULT) {
                setFreeCommandCycle(false);
                return playModeHandler.onFREE_KICK_US_FAULT();
            }
            if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.FREE_KICK_R_FAULT) {
                setFreeCommandCycle(false);
                return playModeHandler.onFREE_KICK_THEM_FAULT();
            }
            if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.OFFSIDE_LEFT) {
                setFreeCommandCycle(false);
                return playModeHandler.onOFFSIDE_US();
            }
            if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.OFFSIDE_RIGHT) {
                setFreeCommandCycle(false);
                return playModeHandler.onOFFSIDE_THEM();
            }
            if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.GOALIE_CATCH_BALL_LEFT) {
                setFreeCommandCycle(false);
                return playModeHandler.onGOALIE_CATCH_BALL_US();
            }
            if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.GOALIE_CATCH_BALL_RIGHT) {
                setFreeCommandCycle(false);
                return playModeHandler.onGOALIE_CATCH_BALL_THEM();
            }
            if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.FOUL_LEFT) {
                setFreeCommandCycle(false);
                return playModeHandler.onFOUL_US();
            }
            if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.FOUL_RIGHT) {
                setFreeCommandCycle(false);
                return playModeHandler.onFOUL_THEM();
            }
        }
        if (mySide == TeamSide.RIGHT) {
            if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.KICK_OFF_L) {
                setFreeCommandCycle(false);
                return playModeHandler.onKICK_OFF_THEM();
            }
            if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.KICK_OFF_R) {
                setFreeCommandCycle(false);
                return playModeHandler.onKICK_OFF_US();
            }
            if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.GOAL_L) {
                setFreeCommandCycle(true);
                return playModeHandler.onGOAL_US();
            }
            if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.GOAL_R) {
                setFreeCommandCycle(true);
                return playModeHandler.onGOAL_THEM();
            }
            if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.KICK_IN_R) {
                setFreeCommandCycle(false);
                return playModeHandler.onKICK_IN_US();
            }
            if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.KICK_IN_L) {
                setFreeCommandCycle(false);
                return playModeHandler.onKICK_IN_THEM();
            }
            if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.GOAL_KICK_R) {
                setFreeCommandCycle(false);
                return playModeHandler.onGOAL_KICK_US();
            }
            if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.GOAL_KICK_L) {
                setFreeCommandCycle(false);
                return playModeHandler.onGOAL_KICK_THEM();
            }
            if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.FREE_KICK_L) {
                setFreeCommandCycle(false);
                return playModeHandler.onFREE_KICK_THEM();
            }
            if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.FREE_KICK_R) {
                setFreeCommandCycle(false);
                return playModeHandler.onFREE_KICK_US();
            }
            if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.CORNER_KICK_R) {
                setFreeCommandCycle(false);
                return playModeHandler.onCORNER_KICK_US();
            }
            if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.CORNER_KICK_L) {
                setFreeCommandCycle(false);
                return playModeHandler.onCORNER_KICK_THEM();
            }
            if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.FREE_KICK_L_FAULT) {
                setFreeCommandCycle(false);
                return playModeHandler.onFREE_KICK_THEM_FAULT();
            }
            if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.FREE_KICK_R_FAULT) {
                setFreeCommandCycle(false);
                return playModeHandler.onFREE_KICK_US_FAULT();
            }
            if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.OFFSIDE_LEFT) {
                setFreeCommandCycle(false);
                return playModeHandler.onOFFSIDE_THEM();
            }
            if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.OFFSIDE_RIGHT) {
                setFreeCommandCycle(false);
                return playModeHandler.onOFFSIDE_US();
            }
            if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.GOALIE_CATCH_BALL_LEFT) {
                setFreeCommandCycle(false);
                return playModeHandler.onGOALIE_CATCH_BALL_THEM();
            }
            if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.GOALIE_CATCH_BALL_RIGHT) {
                setFreeCommandCycle(false);
                return playModeHandler.onGOALIE_CATCH_BALL_US();
            }
            if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.FOUL_LEFT) {
                setFreeCommandCycle(false);
                return playModeHandler.onFOUL_THEM();
            }
            if (refereemessage.getRefereeMessage() == RefereeHearMessage.RefereMessages.FOUL_RIGHT) {
                setFreeCommandCycle(false);
                return playModeHandler.onFOUL_US();
            }
        }
        return myState;
    }
}
TOP

Related Classes of rbafw.fsmhandler.RBAFWHandler

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.