public EuchrePlayerController(Activity context, List<Card> cardHandGiven) { playerContext = (ShowCardsActivity) context; play = (Button) context.findViewById(R.id.passOption); bet = (Button) context.findViewById(R.id.betOption); goAlone = (Button) context.findViewById(R.id.goAloneOption); chooseSuit = (ImageView) context.findViewById(R.id.betTrumpSuit); play.setOnClickListener(playClickListener); bet.setOnClickListener(betClickListener); goAlone.setOnClickListener(goAloneClickListener); chooseSuit.setOnClickListener(chooseSuitClickListener); setButtonsEnabled(false); mySM = SoundManager.getInstance(context); cardHand = cardHandGiven; playerName = ""; playerHandLayout = (LinearLayout) playerContext.findViewById(R.id.playerCardContainer); // set up play assist mode SharedPreferences sharedPreferences = playerContext.getSharedPreferences(PREFERENCES, 0); isPlayAssistMode = sharedPreferences.getBoolean(Constants.PREF_PLAY_ASSIST_MODE, false); gameRules = new EuchreGameRules(); connection = ConnectionClient.getInstance(context); }
/* (non-Javadoc) * @see com.worthwhilegames.cardgames.shared.PlayerController#handleBroadcastReceive(android.content.Context, android.content.Intent) */ @Override public void handleBroadcastReceive(Context context, Intent intent) { String action = intent.getAction(); if (ConnectionConstants.MESSAGE_RX_INTENT.equals(action)) { String object = intent.getStringExtra(ConnectionConstants.KEY_MESSAGE_RX); int messageType = intent.getIntExtra(ConnectionConstants.KEY_MESSAGE_TYPE, -1); if (Util.isDebugBuild()) { Log.d(TAG, "message: " + object); } switch (messageType) { case MSG_SETUP: // Parse the Message if it was to start the game over cardHand.removeAll(cardHand); playerContext.removeAllCards(); try { JSONArray arr = new JSONArray(object); for (int i = 0; i < arr.length(); i++) { JSONObject obj = arr.getJSONObject(i); int suit = obj.getInt(KEY_SUIT); int value = obj.getInt(KEY_VALUE); int id = obj.getInt(KEY_CARD_ID); playerContext.addCard(new Card(suit, value, id)); } } catch (JSONException ex) { ex.printStackTrace(); } setButtonsEnabled(false); isTurn = false; cardSuggestedId = -1; break; case FIRST_ROUND_BETTING: /* purposely have both here */ case SECOND_ROUND_BETTING: mySM.sayBet(playerName); isTurn = true; this.setCardLead(object); currentState = messageType; if (currentState == SECOND_ROUND_BETTING) { trumpSuit = -1; } else { trumpSuit = cardLead.getSuit(); } // start select bet activity for round 1 // start select bet activity to let the player bet isBettingNow = true; bettingView(); isTurn = true; setButtonsEnabled(true); break; case PLAY_LEAD_CARD: playingView(); mySM.sayTurn(playerName); currentState = PLAY_LEAD_CARD; setButtonsEnabled(true); isTurn = true; break; case PICK_IT_UP: currentState = PICK_IT_UP; mySM.sayPickItUp(playerName); try { JSONObject obj = new JSONObject(object); int suit = obj.getInt(KEY_SUIT); int value = obj.getInt(KEY_VALUE); int id = obj.getInt(KEY_CARD_ID); playerContext.addCard(new Card(suit, value, id)); } catch (JSONException ex) { ex.printStackTrace(); } isTurn = true; dealerDiscardView(); break; case MSG_PLAY_CARD: playingView(); mySM.sayTurn(playerName); this.setCardLead(object); currentState = MSG_PLAY_CARD; setButtonsEnabled(true); isTurn = true; break; case Constants.MSG_SUGGESTED_CARD: if (isTurn && object != null && isPlayAssistMode) { try { JSONObject obj = new JSONObject(object); int id = obj.getInt(Constants.KEY_CARD_ID); cardSuggestedId = id; // Let the UI know which card was suggested int selectedId = -1; if (cardSelected != null) { selectedId = cardSelected.getIdNum(); } playerContext.setSelected(selectedId, cardSuggestedId); } catch (JSONException ex) { ex.printStackTrace(); } } case MSG_REFRESH: // Parse the refresh Message try { JSONArray arr = new JSONArray(object); JSONObject refreshInfo = arr.getJSONObject(0); isTurn = refreshInfo.getBoolean(Constants.KEY_TURN); currentState = refreshInfo.getInt(KEY_CURRENT_STATE); playerName = refreshInfo.getString(Constants.KEY_PLAYER_NAME); trumpSuit = refreshInfo.getInt(TRUMP); // add more refresh info here playerContext.removeAllCards(); JSONObject obj = arr.getJSONObject(1); int suit = obj.getInt(KEY_SUIT); int value = obj.getInt(KEY_VALUE); int id = obj.getInt(KEY_CARD_ID); cardLead = new Card(suit, value, id); // the 2nd through however many are the cards of the player for (int i = 2; i < arr.length(); i++) { obj = arr.getJSONObject(i); suit = obj.getInt(KEY_SUIT); value = obj.getInt(KEY_VALUE); id = obj.getInt(KEY_CARD_ID); playerContext.addCard(new Card(suit, value, id)); } } catch (JSONException ex) { ex.printStackTrace(); } if ((currentState == FIRST_ROUND_BETTING || currentState == SECOND_ROUND_BETTING)) { if (isTurn && isBettingNow) { // don't want to open 2 betting windows break; } else if (isTurn && !isBettingNow) { bettingView(); setButtonsEnabled(true); } else { playingView(); isTurn = false; cardSuggestedId = -1; setButtonsEnabled(isTurn); isBettingNow = true; } break; } else if (isTurn && currentState == PICK_IT_UP) { dealerDiscardView(); } else { playingView(); } setButtonsEnabled(isTurn); cardSelected = null; break; case ROUND_OVER: currentState = ROUND_OVER; break; case MSG_WINNER: playerContext.unregisterReceiver(); Intent winner = new Intent(playerContext, GameResultsActivity.class); winner.putExtra(GameResultsActivity.IS_WINNER, true); playerContext.startActivityForResult(winner, QUIT_GAME); break; case MSG_LOSER: playerContext.unregisterReceiver(); Intent loser = new Intent(playerContext, GameResultsActivity.class); loser.putExtra(GameResultsActivity.IS_WINNER, false); playerContext.startActivityForResult(loser, QUIT_GAME); break; } } }