Back to project page umpire_buddy.
The source code is released under:
Apache License
If you think the Android project umpire_buddy listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.bluefairyapps.java.android.umpirebuddy.systems; // www .j av a 2s.co m import android.os.Bundle; import android.util.Log; import com.bluefairyapps.java.android.umpirebuddy.enums.EventsEnum; import com.bluefairyapps.java.android.umpirebuddy.enums.GameStatsEnum; import com.bluefairyapps.java.android.umpirebuddy.event.Event; import com.bluefairyapps.java.android.umpirebuddy.event.EventListener; import com.bluefairyapps.java.android.umpirebuddy.event.game.GameStatUpdateEvent; import com.bluefairyapps.java.android.umpirebuddy.utils.BaseballRules; import com.bluefairyapps.java.android.umpirebuddy.utils.GameStats; /** * Contains the elements and logic for a baseball game. * @author Matt Parish */ public class BaseballGameSystem implements EventListener { private static final String TAG = BaseballGameSystem.class.getSimpleName(); private static BaseballGameSystem sInstance = null; private GameStats mStats = new GameStats(); private boolean mGameInProgress = false; private EventManager mEventManager; public BaseballGameSystem() { Log.d(TAG, "Initializing..."); mEventManager = EventManager.get(); mEventManager.registerForEvent(this, EventsEnum.EVENT_STRIKE); mEventManager.registerForEvent(this, EventsEnum.EVENT_BALL); mEventManager.registerForEvent(this, EventsEnum.EVENT_RUNNER_OUT); mEventManager.registerForEvent(this, EventsEnum.EVENT_NEW_GAME); mEventManager.registerForEvent(this, EventsEnum.EVENT_GAME_OVER); mEventManager.dispatch(new Event(EventsEnum.EVENT_NEW_GAME)); } public static void init() { if(sInstance == null) { sInstance = new BaseballGameSystem(); } } public static BaseballGameSystem get() { return sInstance; } public void persist(Bundle bundle) { bundle.putInt(GameStatsEnum.STRIKES.name(), mStats.getStat(GameStatsEnum.STRIKES)); bundle.putInt(GameStatsEnum.BALLS.name(), mStats.getStat(GameStatsEnum.BALLS)); bundle.putInt(GameStatsEnum.OUTS.name(), mStats.getStat(GameStatsEnum.OUTS)); bundle.putInt(GameStatsEnum.INNING.name(), mStats.getStat(GameStatsEnum.INNING)); } public void restore(Bundle bundle) { mStats.updateStat(GameStatsEnum.STRIKES, bundle.getInt(GameStatsEnum.STRIKES.name())); mStats.updateStat(GameStatsEnum.BALLS, bundle.getInt(GameStatsEnum.BALLS.name())); mStats.updateStat(GameStatsEnum.OUTS, bundle.getInt(GameStatsEnum.OUTS.name())); mStats.updateStat(GameStatsEnum.INNING, bundle.getInt(GameStatsEnum.INNING.name())); sendStatsUpdateEvent(GameStatsEnum.values()); } @Override public void onEvent(Event event) { EventsEnum type = event.getType(); Log.d(TAG, type.name() + " received."); if(mGameInProgress || type.equals(EventsEnum.EVENT_NEW_GAME) || type.equals(EventsEnum.EVENT_GAME_OVER) ) { switch(type) { case EVENT_STRIKE: incrementStrikes(); break; case EVENT_BALL: incrementBalls(); break; case EVENT_RUNNER_OUT: incrementOuts(); break; case EVENT_NEW_GAME: mGameInProgress = true; newGame(); break; case EVENT_GAME_OVER: mGameInProgress = false; break; default: Log.d(TAG, "Unable to handle event."); } } } public GameStats getStats() { return mStats; } private void newGame() { mStats.resetAll(); sendStatsUpdateEvent(GameStatsEnum.values()); } private void incrementStrikes() { Log.d(TAG, "Incrementing strikes..."); int strikes = mStats.getStat(GameStatsEnum.STRIKES); if(++strikes >= BaseballRules.MAX_STRIKES) { incrementOuts(); } else { mStats.updateStat(GameStatsEnum.STRIKES, strikes); mEventManager.dispatch(new Event(EventsEnum.EVENT_AUDIO_STRIKE)); sendStatsUpdateEvent(new GameStatsEnum[]{GameStatsEnum.STRIKES}); } } private void incrementBalls() { Log.d(TAG, "Incrementing balls..."); int balls = mStats.getStat(GameStatsEnum.BALLS); if(++balls >= BaseballRules.MAX_BALLS) { mStats.reset(GameStatsEnum.BALLS); mStats.reset(GameStatsEnum.STRIKES); sendStatsUpdateEvent(new GameStatsEnum[]{GameStatsEnum.BALLS, GameStatsEnum.STRIKES}); mEventManager.dispatch(new Event(EventsEnum.EVENT_BATTER_WALKS)); } else { mStats.updateStat(GameStatsEnum.BALLS, balls); mEventManager.dispatch(new Event(EventsEnum.EVENT_AUDIO_BALL)); sendStatsUpdateEvent(new GameStatsEnum[]{GameStatsEnum.BALLS}); } } private void incrementOuts() { Log.d(TAG, "Incrementing outs..."); mStats.reset(GameStatsEnum.STRIKES); mStats.reset(GameStatsEnum.BALLS); int outs = mStats.getStat(GameStatsEnum.OUTS); if(++outs >= BaseballRules.MAX_OUTS) { incrementInning(); mEventManager.dispatch(new Event(EventsEnum.EVENT_BATTER_OUT)); mEventManager.dispatch(new Event(EventsEnum.EVENT_NEW_INNING)); } else { mStats.updateStat(GameStatsEnum.OUTS, outs); mEventManager.dispatch(new Event(EventsEnum.EVENT_BATTER_OUT)); sendStatsUpdateEvent(new GameStatsEnum[]{ GameStatsEnum.STRIKES, GameStatsEnum.BALLS, GameStatsEnum.OUTS }); } } private void incrementInning() { Log.d(TAG, "Incrementing inning..."); mStats.reset(GameStatsEnum.OUTS); int inning = mStats.getStat(GameStatsEnum.INNING); if(++inning > BaseballRules.NUM_INNINGS) { Log.d(TAG, "Game Over."); mEventManager.dispatch(new Event(EventsEnum.EVENT_GAME_OVER)); } else { mStats.updateStat(GameStatsEnum.INNING, inning); sendStatsUpdateEvent(new GameStatsEnum[]{ GameStatsEnum.STRIKES, GameStatsEnum.BALLS, GameStatsEnum.OUTS, GameStatsEnum.INNING }); } } private void sendStatsUpdateEvent(GameStatsEnum[] updatedStats) { int[] newValues = new int[updatedStats.length]; for(int i=0; i<updatedStats.length; i++) { newValues[i] = mStats.getStat(updatedStats[i]); } mEventManager.dispatch(new GameStatUpdateEvent(updatedStats, newValues)); } }