package com.poker.analyst.parse;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.poker.analyst.element.Card;
import com.poker.analyst.element.Player;
import com.poker.analyst.element.PlayingCards;
import com.poker.analyst.element.Rounds;
import com.poker.ui.robot.reaction.UIReaction;
public class IPokerBufferParser extends BufferParser{
protected String START_NEW_HAND = "Dealer: Starting a new hand";
protected String YOUR_CARDS = "Dealer: Your cards ";
protected String PLAYER = "Player";
protected String DEALER = "Dealer: ";
protected String DEALING = "Dealing";
protected String DEALING_PREFLOP = "Dealer: Dealing cards";
protected String DEALING_FLOP = "Dealer: Dealing Flop";
protected String DEALING_TURN = "Dealer: Dealing Turn";
protected String DEALING_RIVER = "Dealer: Dealing River";
protected String ALLIN = "goes"; // Dealer: dernarr1 goes
// All-in $2.55
protected String FOLD = "folds";
protected String CALL = "calls";
protected String RAISE = "raises";
protected String BET = "bets";
protected String CHECK = "checks";
protected Integer POS_SMALL_BLIND = 1;
protected Integer POS_BIG_BLIND = 2;
protected Integer POS_MY_CARD = 6;
protected Integer PLAYER_POSITION = 1; //Dealer: dernarr1 folds
protected Integer ACTION_POSITION = 2; //Dealer: FUYAN bets $0.20
protected Integer MONEY_POSITION_CALL = 3; //Dealer: FUYAN calls $0.20
protected Integer MONEY_POSITION_BET = 3; //Dealer: FUYAN bets $0.20
protected Integer MONEY_POSITION_RAISE = 4; //Dealer: yueyuewsop raises to $0.60
protected Integer MONEY_POSITION_ALLIN = 4; //Dealer: dernarr1 goes All-in $2.55
public IPokerBufferParser(String heroName) {
super(heroName);
}
public ParserResult getParserResult(final String buffer, Rounds round) {
ParserResult parserResult = new ParserResult();
Rounds currentRound = round;
String currentGameID = "";
String heroCards = "";
String tableCards = "";
String startString = "";
String endString = "";
if (round == Rounds.PREFLOP){
startString = DEALING_PREFLOP;
endString = DEALING_FLOP;
}else
if (round == Rounds.FLOP){
startString = DEALING_FLOP;
endString = DEALING_TURN;
}else
if (round == Rounds.TURN){
startString = DEALING_TURN;
endString = DEALING_RIVER;
}else
if (round == Rounds.RIVER){
startString = DEALING_RIVER;
endString = "";
}
// ����� ������ ����
// ����� ������ ������
Matcher matcher;
int startingGame = -1;
//int startingSubRound = -1;
int startingRound = -1;
int endingRound = -1;
final String[] strBuff = buffer.split("\n");
for(int i = 0; i<strBuff.length; i++)
strBuff[i] = strBuff[i].trim();
// round's end by default
endingRound = - 1;
for (int i = strBuff.length - 1; i >= 0; i--) {
// finding the starting the game
if (strBuff[i].startsWith(START_NEW_HAND)) {
startingGame = i;
matcher = Pattern.compile(".+#(\\d+)").matcher(strBuff[i]);
if (matcher.find())
currentGameID = matcher.group(1);
break;
}
if (strBuff[i].startsWith(YOUR_CARDS)) {
heroCards = strBuff[i];
}
if (round == null){
if (startingRound == -1) {
if (strBuff[i].startsWith(DEALING_PREFLOP)) {
currentRound = Rounds.PREFLOP;
startingRound = i;
tableCards = "";
} else if (strBuff[i].startsWith(DEALING_FLOP)) {
currentRound = Rounds.FLOP;
startingRound = i;
tableCards = strBuff[i].replace(DEALING_FLOP, "");
} else if (strBuff[i].startsWith(DEALING_TURN)) {
currentRound = Rounds.TURN;
startingRound = i;
tableCards = strBuff[i].replace(DEALING_TURN, "");;
} else if (strBuff[i].startsWith(DEALING_RIVER)) {
currentRound = Rounds.RIVER;
startingRound = i;
tableCards = strBuff[i].replace(DEALING_RIVER, "");;
}
}
}
else{
if (startingRound == -1) {
if (strBuff[i].startsWith(startString)) {
startingRound = i;
if (round != Rounds.PREFLOP)
tableCards = strBuff[i];
}
}
if (endingRound == -1) {
if (strBuff[i].startsWith(endString)) {
endingRound = i;
}
}
}
}
if (currentRound == Rounds.PREFLOP)
parserResult.setFirstAuction(true);
else
parserResult.setFirstAuction(false);
heroCards = heroCards.replace("Dealer: Your cards ", "");
if (endingRound == -1 && startingRound != -1) {
endingRound = strBuff.length - 1;
}
if (startingGame == -1 || endingRound == -1 || startingRound == -1) {
return null;
}
parserResult.setCurrentGameID(currentGameID);
parserResult.setCurrentRound(currentRound);
// ���������� ������� �����
final PlayingCards plCards = new PlayingCards();
Card[] retCards;
final String arrHeroCards[] = heroCards.split(" ");
if (arrHeroCards.length != 1) {
retCards = new Card[arrHeroCards.length];
for (int i = 0; i < arrHeroCards.length; i++) {
retCards[i] = parseCard(arrHeroCards[i]);
}
plCards.setPlayerCards(retCards);
}
final String arrTableCards[] = tableCards.split(" ");
if (arrTableCards.length != 1) {
retCards = new Card[arrTableCards.length];
for (int i = 0; i < arrTableCards.length; i++) {
retCards[i] = parseCard(arrTableCards[i]);
}
plCards.setTableCards(retCards);
}
parserResult.setPlayingCards(plCards);
final Float deadMoney = 0f;
String[] strBuffArr;
final List<Player> blindPlayers = new ArrayList<Player>();
{
Player player;
for (int i = startingGame + 1; i < strBuff.length; i++) {
strBuffArr = strBuff[i].split(" ");
if (strBuffArr.length == 6) {
if (strBuffArr[4].equals("Blind")) {
if (strBuffArr[3].equals("Small"))
parserResult.setSmallBlindValue(Float.parseFloat(strBuffArr[5].substring(1)));
if (strBuffArr[3].equals("Big"))
parserResult.setBigBlindValue(Float.parseFloat(strBuffArr[5].substring(1)));
player = new Player();
player.setBet(Float.parseFloat(strBuffArr[5].substring(1)));
player.setName(strBuffArr[1]);
player.setReaction(UIReaction.UIR_BB_SET);
blindPlayers.add(player);
}
} else {
}
}
}
parserResult.setPlayerBlinds(blindPlayers);
//actions
final List<Player> playersAction = new ArrayList<Player>();
String[] tmp;
Player player;
for (int i = startingRound; i <= endingRound; i++) {
tmp = strBuff[i].split(" ");
if (tmp.length < 3) {
continue;
}
if (tmp[2].equals(ALLIN) || tmp[2].equals(CALL) || tmp[2].equals(CHECK)
|| tmp[2].equals(RAISE) || tmp[2].equals(FOLD) || tmp[2].equals(BET)) {
if (tmp[1].equals(heroName))
parserResult.setFirstAuction(false);
player = new Player();
player.setName(tmp[1]);
playersAction.add(player);
if (tmp[2].equals(ALLIN)) {
player.setReaction(UIReaction.UIR_ACTION_RAISE);
player.setBet(Float.parseFloat(tmp[MONEY_POSITION_ALLIN].substring(1)));
}
if (tmp[2].equals(CHECK)) {
player.setReaction(UIReaction.UIR_ACTION_CHECK);
player.setBet(0);
}
if (tmp[2].equals(BET)) {
player.setReaction(UIReaction.UIR_ACTION_RAISE);
player.setBet(Float.parseFloat(tmp[MONEY_POSITION_BET].substring(1)));
}
if (tmp[2].equals(CALL)) {
player.setReaction(UIReaction.UIR_ACTION_CALL);
player.setBet(Float.parseFloat(tmp[MONEY_POSITION_CALL].substring(1)));
}
if (tmp[2].equals(RAISE)) {
player.setReaction(UIReaction.UIR_ACTION_RAISE);
player.setBet(Float.parseFloat(tmp[MONEY_POSITION_ALLIN].substring(1)));
}
if (tmp[2].equals(FOLD)) {
player.setReaction(UIReaction.UIR_ACTION_FOLD);
player.setBet(0);
}
}
}
parserResult.setPlayersAction(playersAction);
return parserResult;
}
public List<String> getPlayersName(final String buffer) {
List<String> names = new ArrayList<String>();
int startingGame = -1;
int startingSubRound = -1;
int startingRound = -1;
Rounds currentRound = null;
String currentGameID = "";
String heroCards = "";
String tableCards = "";
final String[] strBuff = buffer.split("\n");
startingRound = strBuff.length - 1;
for (int i = strBuff.length - 1; i >= 0; i--) {
if (strBuff[i].startsWith(START_NEW_HAND)) {
startingGame = i;
break;
}
if (strBuff[i].startsWith(DEALING_FLOP)) {
startingRound = i;
}
}
if (startingGame == -1) {
return null;
}
//actions
String[] tmp;
Player player;
for (int i = startingGame; i <= startingRound; i++) {
tmp = strBuff[i].replace("\\r","").split(" ");
if (tmp.length < 3)
continue;
if (tmp[2].startsWith(ALLIN) || tmp[2].startsWith(CALL) || tmp[2].startsWith(CHECK)
|| tmp[2].startsWith(RAISE) || tmp[2].startsWith(BET))
names.add("1" + tmp[1]);
if (tmp[2].startsWith(FOLD))
names.add("0" + tmp[1]);
}
return names;
}
}