package com.poker.tmp;
import java.util.ArrayList;
import java.util.List;
import com.poker.analyst.element.Action;
import com.poker.analyst.element.Card;
import com.poker.analyst.element.CardFace;
import com.poker.analyst.element.PlayingCards;
import com.poker.analyst.element.Suit;
public class ParseClipboard {
final static String START_NEW_HAND = "Dealer: Starting a new hand";
final static String YOUR_CARDS = "Your cards ";
final static String PLAYER = "Player";
final static String DEALER = "Dealer: ";
final static String DEALING = "Dealing";
final static String DEALING_PREFLOP = "Dealer: Dealing cards";
final static String DEALING_FLOP = "Dealer: Dealing Flop";
final static String DEALING_TURN = "Dealer: Dealing Turn";
final static String DEALING_RIVER = "Dealer: Dealing River";
final static String ALLIN = "goes"; // Dealer: dernarr1 goes
// All-in $2.55
final static String FOLD = "folds";
final static String CALL = "calls";
final static String RAISE = "raises";
final static String BET = "bets";
final static String CHECK = "checks";
final static Integer POS_SMALL_BLIND = 1;
final static Integer POS_BIG_BLIND = 2;
final static Integer POS_MY_CARD = 6;
final static Integer PLAYER_POSITION = 2; //Dealer: dernarr1 folds
final static Integer ACTION_POSITION = 3; //Dealer: FUYAN bets $0.20
final static Integer MONEY_POSITION_CALL = 3; //Dealer: FUYAN calls $0.20
final static Integer MONEY_POSITION_BET = 3; //Dealer: FUYAN bets $0.20
final static Integer MONEY_POSITION_RAISE = 5; //Dealer: yueyuewsop raises to $0.60
final static Integer MONEY_POSITION_ALLIN = 5; //Dealer: dernarr1 goes All-in $2.55
public static List<PlayerAction> parseString(final String clipBoard) {
// final TextTransfer textTransfer = new TextTransfer();
// final String clipBoard = textTransfer.getClipboardContents();
final String[] arrClipBoard = clipBoard.split("\n");
final PlayingCards playingCards = new PlayingCards();
final List<PlayerAction> situation = new ArrayList<PlayerAction>();
for (int i = arrClipBoard.length - 1; i > 0; i--) {
if (arrClipBoard[i].startsWith(START_NEW_HAND)) {
for (int j = arrClipBoard.length - 1; j > i; j--) {
if (arrClipBoard[j].startsWith(PLAYER)) {
// System.out.println("skip: " + arrClipBoard[j]);
continue;
}
if (arrClipBoard[j].equals(DEALER)) {
// System.out.println("skip: " + arrClipBoard[j]);
continue;
}
if (arrClipBoard[j].startsWith(DEALER)) {
final String[] tempStr = arrClipBoard[j].split(" ");
if (tempStr[2].equals(FOLD)) {
situation.add(new PlayerAction(Action.Fold, 0D,
tempStr[PLAYER_POSITION]));
// System.out.println(tempStr[1] + " FOLD ");
continue;
}
if (tempStr[2].equals(CHECK)) {
situation.add(new PlayerAction(Action.Check, 0D,
tempStr[PLAYER_POSITION]));
// System.out.println(tempStr[1] + " CHECK ");
continue;
}
if (tempStr[2].equals(ALLIN)) {
situation.add(new PlayerAction(Action.Raise, Double
.parseDouble(tempStr[MONEY_POSITION_ALLIN].substring(1)),
tempStr[PLAYER_POSITION]));
// System.out.println(tempStr[1] + " ALLIN ");
continue;
}
if (tempStr[2].equals(CALL)) {
situation.add(new PlayerAction(Action.Call, Double
.parseDouble(tempStr[MONEY_POSITION_CALL].substring(1)),
tempStr[PLAYER_POSITION]));
// System.out.println(tempStr[1] + " CALL " + tempStr[3]);
continue;
}
if (tempStr[2].equals(RAISE)) {
situation.add(new PlayerAction(Action.Raise, Double
.parseDouble(tempStr[MONEY_POSITION_RAISE].substring(1)),
tempStr[PLAYER_POSITION]));
// System.out.println(tempStr[1] + " RAISE " + tempStr[4]);
continue;
}
if (tempStr[2].equals(BET)) {
situation.add(new PlayerAction(Action.Bet, Double
.parseDouble(tempStr[MONEY_POSITION_BET].substring(1)),
tempStr[PLAYER_POSITION]));
// System.out.println(tempStr[1] + " BET " + tempStr[3]);
continue;
}
continue;
}
// end current turn
if (arrClipBoard[j].startsWith(DEALING)) {
if (arrClipBoard[j].startsWith(DEALING_PREFLOP)) {
System.out.println("Turn is preflop");
continue;
}
System.out.println("->Turn is " + arrClipBoard[j].substring(8));
break;
}
// your cards
if (arrClipBoard[j].startsWith(YOUR_CARDS)) {
// cards
final String[] arrCards = arrClipBoard[j].split(" ");
System.out.println("->Your card is " + parseCard(arrCards[2]) + " and "
+ parseCard(arrCards[3]));
playingCards.getPlayerCards()[0] = parseCard(arrCards[2]);
playingCards.getPlayerCards()[1] = parseCard(arrCards[3]);
continue;
}
// cards
final String[] arrCards = arrClipBoard[j].split(" ");
if (arrCards.length == 3 || arrCards.length == 4 || arrCards.length == 5) {
System.out.print("->Table card is ");
for (int k = 0; k < arrCards.length; k++) {
playingCards.getTableCards()[k] = parseCard(arrCards[k]);
System.out.print(parseCard(arrCards[k]) + " ; ");
}
System.out.println();
continue;
}
System.out.print("unrecognizible string: " + arrClipBoard[j]);
}
break;
}
}
return situation;
}
public static Card parseCard(final String comb) {
if (comb.length() != 2) {
return null;
}
final CardFace face = CardFace.getCardFaceFromShortName(comb.charAt(0));
final Suit suit = Suit.getSuitFromShortName(comb.charAt(1));
if (suit != null && face != null) {
return new Card(suit, face);
}
return null;
}
}