Android Open Source - rock-paper-scissors Displaying Result State






From Project

Back to project page rock-paper-scissors.

License

The source code is released under:

MIT License

If you think the Android project rock-paper-scissors listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package au.com.codeka.rps.game;
//  w ww  .  j  a v a  2  s  . c o m
import android.os.Handler;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

import au.com.codeka.rps.DebugLog;
import au.com.codeka.rps.WatchConnection;

/**
 * Created by dean on 2/08/14.
 */
public class DisplayingResultState extends State {
    private final StateManager stateManager;
    private final MatchInfo matchInfo;
    private final ResultInfo resultInfo;
    private final Handler handler;

    public DisplayingResultState(StateManager stateManager, MatchInfo matchInfo,
                                 ResultInfo resultInfo) {
        this.stateManager = stateManager;
        this.matchInfo = matchInfo;
        this.resultInfo = resultInfo;
        handler = new Handler();
    }

    @Override
    public void onEnter() {
        DebugLog.write("Game complete - match: %s, round: %d, result: %s (%s vs %s)",
                matchInfo.getMatchId(), matchInfo.getRound(), resultInfo.getResult(),
                resultInfo.getPlayerChoice(), resultInfo.getOtherChoice());

        JSONObject json = new JSONObject();
        try {
            json.put("round", matchInfo.getRound());
            json.put("player_choice", resultInfo.getPlayerChoice());
            json.put("other_choice", resultInfo.getOtherChoice());
            json.put("result", resultInfo.getResult().toString());
        } catch (JSONException e) {
            DebugLog.write("ERROR : %s", e.getMessage());
        }
        try {
            stateManager.getWatchConnection().sendMessage(new WatchConnection.Message(
                    "/rps/FinalResult", json.toString().getBytes("utf-8")));
        } catch (UnsupportedEncodingException e) {
            DebugLog.write("ERROR : %s", e.getMessage());
        }

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                DebugLog.write("Starting next match!");
                matchInfo.nextRound();
                stateManager.enterState(new GameRunningState(stateManager, matchInfo));
            }
        }, 5000);
    }
}




Java Source Code List

au.com.codeka.rps.ApplicationTest.java
au.com.codeka.rps.DebugLog.java
au.com.codeka.rps.FindingOpponentFragment.java
au.com.codeka.rps.GameActivity.java
au.com.codeka.rps.GameFragment.java
au.com.codeka.rps.MessageListenerService.java
au.com.codeka.rps.NotificationUpdateService.java
au.com.codeka.rps.PhoneConnection.java
au.com.codeka.rps.ResultFragment.java
au.com.codeka.rps.SplashActivity.java
au.com.codeka.rps.WatchConnection.java
au.com.codeka.rps.game.AwaitingPlayerChoiceState.java
au.com.codeka.rps.game.AwaitingPlayerChoiceState.java
au.com.codeka.rps.game.AwaitingResultState.java
au.com.codeka.rps.game.AwaitingResultState.java
au.com.codeka.rps.game.DisplayingResultState.java
au.com.codeka.rps.game.DisplayingResultState.java
au.com.codeka.rps.game.FindingOpponentState.java
au.com.codeka.rps.game.FindingOpponentState.java
au.com.codeka.rps.game.GameRunningState.java
au.com.codeka.rps.game.GameRunningState.java
au.com.codeka.rps.game.MatchInfo.java
au.com.codeka.rps.game.ResultInfo.java
au.com.codeka.rps.game.StateManager.java
au.com.codeka.rps.game.StateManager.java
au.com.codeka.rps.game.State.java
au.com.codeka.rps.game.State.java