Package com.poker.control.delegate

Source Code of com.poker.control.delegate.AnalystDelegate

package com.poker.control.delegate;

import java.util.ArrayList;
import java.util.List;

import com.poker.analyst.AnalystResult;
import com.poker.analyst.element.Board;
import com.poker.analyst.element.Player;
import com.poker.analyst.element.Rounds;
import com.poker.analyst.elements.state.PlayerState;
import com.poker.analyst.parse.ParserResult;
import com.poker.analyst.strategy.DataForStrategy;
import com.poker.analyst.strategy.Strategy;
import com.poker.analyst.strategy.StrategySSS;
import com.poker.analyst.strategy.temp.SSSStrategy1;
import com.poker.control.PokerControl;
import com.poker.data.PokerData;
import com.poker.test.FillPlayWindow;
import com.poker.test.PlayWindowFactory;
import com.poker.test.ViewBoard;
import com.poker.tmp.PlayerAction;
import com.poker.ui.ocr.RecognizedSpecFloat;
import com.poker.ui.robot.reaction.UIReaction;
import com.poker.ui.settings.rooms.events.EventType;
import com.poker.ui.windows.PlayWindow;
import com.poker.ui.windows.Window;
import com.poker.ui.windows.auxiluary.WindowPosition;

public class AnalystDelegate {

  public Board createBoardShablon(final PokerData pdata) {

    Board board = new Board();

    List<Player> players = new ArrayList<Player>();

    int countPlayers = pdata.getProgramSettings().getCountPlayers();

    for (int i = 0; i < countPlayers; i++) {
      //players.add(new Player(null, 0, 0, false));
      players.add(new Player());
    }
    for (Player pl:players){
      pl.setPlayerState(PlayerState.PRS_OTHER);
      pl.setBet(0);
      pl.setName("");
      pl.setReaction(null);
      pl.setStack(0f);
    }
   
    board.setDealer(0);
    board.setHero(0);

    board.setPlayers(players);
    //board.setPlayingCards(cards);

    return board;
  }

  public Float[] getStacksFloats(final PokerData pdata, final String[] strings) {

    final Float[] floatStacks = new Float[strings.length];

    for (int i = 0; i < strings.length; i++) {
      floatStacks[i] = recognizeFloatFromString(strings[i]);
    }

    return floatStacks;
  }

  @Deprecated
  public String[] getStacksFloatsTest(final PokerData pdata, final String[] strings) {

    final String[] floatStacks = new String[strings.length];

    for (int i = 0; i < strings.length; i++) {
      floatStacks[i] = recognizeFloatFromStringTest(strings[i]);
    }

    return floatStacks;
  }

  @Deprecated
  protected String recognizeFloatFromStringTest(final String str) {

    String strtmp = str.replace("l", "1").trim().replace("o", "0").replace("O", "0");

    if (strtmp.length() <= 1) {
      return "";
    }

    if (strtmp.charAt(0) != '.') {
      strtmp = strtmp.substring(1).trim();
    }

    return strtmp;
  }

  protected Float recognizeFloatFromString(final String str) {

    //  System.out.print(str + "  ");
    String strtmp = str.replace("l", "1").trim().replace("o", "0").replace("O", "0");
    strtmp = strtmp.replace("A", ".4").replace("�", ".4").replace("�", "0").replace("�", "0");
    Float result = RecognizedSpecFloat.FL_ERROR;

    //  System.out.print(strtmp + "  ");
    if (strtmp.length() <= 1) {
      return RecognizedSpecFloat.FL_EMPTY;
    }

    //  System.out.print(strtmp + "  ");
    if (strtmp.charAt(0) != '.') {
      strtmp = strtmp.substring(1).trim();
    }

    //  System.out.print(strtmp + "  ");
    if (strtmp.indexOf(".") != -1) {
      strtmp = strtmp.replace(",", "");
    } else {
      if (strtmp.indexOf(",") == strtmp.lastIndexOf(",")) {
        strtmp = strtmp.replace(",", ".");
      } else {
        strtmp = strtmp.substring(0, strtmp.lastIndexOf(",")).replace(",", "")
        + strtmp.substring(strtmp.lastIndexOf(','), strtmp.length()).replace(",",
        ".");
      }
    }
    //  System.out.print(strtmp + "  ");
    strtmp = strtmp.replace("-", "");

    try {
      result = Float.parseFloat(strtmp);
      //    System.out.println(strtmp + "   " + result);
    } catch (final NumberFormatException e) {
      result = RecognizedSpecFloat.FL_ERROR;
    }

    return result;
  }

  public AnalystResult getReaction(PokerData pdata, PlayWindow playWnd, PokerControl pControl) {

   
    if (playWnd == null) {
      return null;
    }
    DataForStrategy dfs = new DataForStrategy();
    Strategy strategy = pdata.getProgramSettings().getStrategySSS();
   
    if (playWnd.getCurrentEvent() == EventType.ET_NEED_WAIT_BB) {
      return new AnalystResult(UIReaction.UIR_BB_WAIT_FOR, null);     
    }else
    if(playWnd.getCurrentEvent() == EventType.ET_SYSTEM_MESSAGE){
      return new AnalystResult(UIReaction.UIR_ACCEPT_SYSTEM_WINDOW, null);
    }
    else
    if(playWnd.getCurrentEvent() == EventType.ET_SYSTEM_MESSAGE_2){
      return new AnalystResult(UIReaction.UIR_ACCEPT_SYSTEM_WINDOW_2, null);
    }
    if (playWnd.getCurrentEvent() == EventType.ET_SET_BB) {
      dfs.setCurrentRound(playWnd.getCurrentRound());
      dfs.setCurrentBoard(playWnd.getPreflopBoard());
     
      if (strategy.maySitToTable(dfs))           
        return new AnalystResult(UIReaction.UIR_BB_SET, null);
      else 
        return new AnalystResult(UIReaction.UIR_CLOSE_WINDOW, "");
    }else {
     
     
      switch(playWnd.getCurrentRound()){
        case FLOP :
          dfs.setPrevBoard(playWnd.getPreflopBoard());
            break;
          case TURN :
            dfs.setPrevBoard(playWnd.getFlopBoard());
            break;
          case RIVER :
            dfs.setPrevBoard(playWnd.getTurnBoard());
            break;
          default:
            dfs.setPrevBoard(null);
      }
   

      //ViewBoard.viewBoard(playWnd, playWnd.getCurrentRound(), pControl);
      dfs.setCurrentBoard(playWnd.getCurrentBoard());
      dfs.setCurrentRound(playWnd.getCurrentRound());
      /* not realized yet
          dfs.setPlayerMoney(playWnd.getPlayerMoney());
       */
      //  0 - hero place
      dfs.setPlayerMoney(dfs.getCurrentBoard().getPlayers().get(0).getStack());
      dfs.setTotalBank(pControl.getAllRoundTotal(playWnd));

      AnalystResult aResult = strategy.getReaction(dfs);
      //System.out.println(aResult);         

      return aResult;
    }
  }

  public AnalystResult correctReact(final PokerData pdata, final Window playWnd,
      final AnalystResult analystResult) {

    final UIReaction reaction = analystResult.getReaction();
    if (reaction == UIReaction.UIR_ACTION_CHECK) {
      if (playWnd.getCurrentEvent() == EventType.ET_ACTION_FOLD_ANY_RAISE) {
        return analystResult;
      } else if (playWnd.getCurrentEvent() == EventType.ET_ACTION_FOLD_CALL) {
        System.err.println("WRONG REACTION(CHECK WHERE ET_ACTION_FOLD_CALL)");
      }
    }
    if (reaction == UIReaction.UIR_ACTION_FOLD) {
      return analystResult;
    }
    if (reaction == UIReaction.UIR_ACTION_RAISE || reaction == UIReaction.UIR_ACTION_ALLIN) {
      if (playWnd.getCurrentEvent() == EventType.ET_ACTION_FOLD_ANY_RAISE) {

      } else if (playWnd.getCurrentEvent() == EventType.ET_ACTION_FOLD_CALL) {
        return new AnalystResult(UIReaction.UIR_ACTION_CALL, analystResult.getSurrogate());
      }
    }
    if (reaction == UIReaction.UIR_ACTION_CALL) {
      return analystResult;
    }

    return analystResult;
  }

  public boolean isNewTableNeeds(final PokerData pdata) {

    for (final WindowPosition wpos : pdata.getWindowsPos()) {
      if (wpos.getPlayWindow() == null) {
        return true;
      }
    }

    return false;
  }

  public void analyzeOneTableFromWaitingList(final PokerData pdata, final PokerControl pControl) {

    PlayWindow playWnd;
    String buyIn;

    while (pdata.getWaitingWindowList().size() != 0) {
      playWnd = pdata.getWaitingWindowList().get(0);
      if (playWnd.getCurrentEvent() == EventType.ET_READY_FOR_GAME) {
        break;
      }
      pControl.activateWindow(playWnd);
      try {
        Thread.sleep(100);
      } catch (final InterruptedException e1) {
        e1.printStackTrace();
      }

      pControl.react(pdata, playWnd, new AnalystResult(UIReaction.UIR_TAKE_BUYIN_SUMM, null));

      try {
        buyIn = pControl.getStringFromClipboard();
      } catch (final Exception e) {
        buyIn = "";
      }
      //    System.out.println(buyIn + " ");
      try{
        //if (pdata.getProgramSettings().getBuyInAmount().equals(Float.parseFloat(buyIn)) )
        if (pdata.getProgramSettings().getBuyInAmount()<= Float.parseFloat(buyIn) &&
            pdata.getProgramSettings().getBuyInAmount() * 1.10 >= Float.parseFloat(buyIn)) {
          pControl.react(pdata, playWnd, new AnalystResult(UIReaction.UIR_ACCEPT_BUYIN, null));
          playWnd.setCurrentEvent(EventType.ET_READY_FOR_GAME);
          System.out.println("yes");
        } else {
          pControl.killTableFromWaitingList(pdata, pControl, playWnd);
          pdata.getWaitingWindowList().remove(playWnd);

          System.out.println("no");
        }
      }catch(Exception e){
       
      }

      break;
    }       
  }

  public Board setNamesToBoard(PokerData pdata, Board board, List<String> names, ParserResult parserResult, boolean isFirstAuction) {         
    Player player;
    boolean inAction = false;
    String playerName = "";
   
    String heroName = pdata.getProgramSettings().getHeroName();
    boolean starts = false;
   
    int dealer;
    int position;
   
    if (names == null)
      return board;

    if (isFirstAuction){
      String SBName = parserResult.getPlayerBlinds().get(0).getName().trim();
      String BBName = parserResult.getPlayerBlinds().get(1).getName().trim();
     
      if (!BBName.equals(heroName)){
        dealer = board.getDealer();
        // storing required blinds names;
        position= board.getNextInGamePlayer(dealer);   
        board.getPlayers().get(position).setName(SBName);
       
        position= board.getNextInGamePlayer(position);
        board.getPlayers().get(position).setName(BBName);
      }else{
       
        position= board.getHero();
        board.getPlayers().get(position).setName(BBName);       
        // attempt to find the sb;
        position = board.getDealer();
        int tmp;
        tmp = board.getNextActivePlayer(position);
        if (tmp != board.getHero()){
          board.getPlayers().get(tmp).setName(SBName);
        }else{
          tmp = board.getNextInGamePlayer(position);
          if (board.getPlayers().get(tmp).getName().trim().equals(""))
            board.getPlayers().get(tmp).setName(SBName);
        }
       
       
       
       
        position= board.getHero();
      }
           
      for (int i = 0; i < names.size(); i++){
        inAction = names.get(i).startsWith("1") ? true : false;
        if (!inAction) continue;
        playerName = names.get(i).substring(1);     
        if (board.findPlayerOnBoardByName(playerName) != null)
          break;
       
        position = board.getNextActivePlayer(position);
        player = board.getPlayers().get(position);
        player.setName(playerName);     
      }
    }
    else{                 
      position = board.getHero(); // hero position;         
      starts = false;
      //heroName
     
      for (int i = 0; i < names.size(); i++){
        playerName = names.get(i).substring(1);             
        if (!starts){
          if (playerName.equals(heroName)){
            starts = true;
            player = board.getPlayers().get(position);
            player.setName(playerName)
            position = board.getNextActivePlayer(position);
            continue;
          }else{
            continue;
          }         
        }
        inAction = names.get(i).startsWith("1") ? true : false;
        if (!inAction) continue;
       
        if (board.findPlayerOnBoardByName(playerName) != null){
          continue;
        }
        else{
          player = board.getPlayers().get(position);
          if (!player.getName().trim().equals("")) break;
          player.setName(playerName)
          position = board.getNextActivePlayer(position);
        }     
      }
    }
   
    return board;       
  }

  public Board setParserResultsToBoard(PokerData pdata, PokerControl pControl, Board board, ParserResult parserResult) {         
    Player player;
    Player playerAction;
    Float currentBet = 0f;
    int dealer   = board.getDealer();
    int position = 0;
   
    int numOfRaises = 0;


    if (parserResult.getCurrentRound() == Rounds.PREFLOP){
      currentBet = parserResult.getBigBlindValue();
     
      /*
      try{
        playerAction = parserResult.getPlayerBlinds().remove(0);
        position = board.getNextInGamePlayer(dealer);
        player = board.getPlayers().get(position);
        player.setBet(playerAction.getBet());
        player.setReaction(playerAction.getReaction());

        playerAction = parserResult.getPlayerBlinds().remove(0);
        position = board.getNextInGamePlayer(position);
        player = board.getPlayers().get(position);
        player.setBet(playerAction.getBet());
        player.setReaction(playerAction.getReaction());
      }catch(Exception e){
        e.printStackTrace();
      }
      */
      int i=0;
      while(i < parserResult.getPlayerBlinds().size()){
        playerAction = parserResult.getPlayerBlinds().get(i);
        player = board.findPlayerOnBoardByName(playerAction.getName());
        if (player == null)
          i++;
        else{
          player.setBet(playerAction.getBet());
          player.setReaction(playerAction.getReaction());
          parserResult.getPlayerBlinds().remove(i);
        }
      }
     
      board.setDeathBlind(parserResult.getDeadBlinds());
      board.setBlindPlayers(parserResult.getPlayerBlinds());
    }
    board.setBigBlind(parserResult.getBigBlindValue());
    board.setSmallBlind(parserResult.getSmallBlindValue());     
    board.setCurrentGameID(parserResult.getCurrentGameID());
    board.setCurrentRound(parserResult.getCurrentRound());
    board.setPlayingCards(parserResult.getPlayingCards());


    for (int i = 0; i< parserResult.getPlayersAction().size(); i++){
      playerAction = parserResult.getPlayersAction().get(i);
      player = board.findPlayerOnBoardByName(playerAction.getName());
      if (player != null){
        switch (playerAction.getReaction()) {
        case UIR_ACTION_CALL:
          player.setBet(currentBet);         
          player.setReaction(UIReaction.UIR_ACTION_RAISE);
          break;
        case UIR_ACTION_CHECK:
          player.setBet(currentBet);
          player.setReaction(UIReaction.UIR_ACTION_RAISE);
          break
        case UIR_ACTION_FOLD:
          player.setReaction(UIReaction.UIR_ACTION_FOLD);
          //player.setPlayerState(PlayerState.PRS_IN_ACTION);
          break;
        case UIR_ACTION_RAISE:
          currentBet = playerAction.getBet();
          player.setBet(currentBet);
          player.setReaction(UIReaction.UIR_ACTION_RAISE);
          numOfRaises++;
          break;
        }
      }else
      {
        //System.out.println("Not found player " + playerAction.getName());
      }     
    }
    board.setNumOfRaises(numOfRaises);

    return board;
  }



  public float getAllRoundTotal(PlayWindow playWnd){
    float total = 0f;


    if (playWnd.getPreflopBoard() != null)
      total += playWnd.getPreflopBoard().getTotal();
    if (playWnd.getFlopBoard() != null)
      total += playWnd.getFlopBoard().getTotal();
    if (playWnd.getTurnBoard() != null)
      total += playWnd.getTurnBoard().getTotal();
    if (playWnd.getRiverBoard() != null)
      total += playWnd.getRiverBoard().getTotal();
    /* 
    if (playWnd.getPreflopBoard() != null)
    System.out.println("pf total: " + playWnd.getPreflopBoard().getTotal());
    if (playWnd.getFlopBoard() != null)
    System.out.println("f  total: " + playWnd.getFlopBoard().getTotal());
    if (playWnd.getTurnBoard() != null)
    System.out.println("t  total: " + playWnd.getTurnBoard().getTotal());
    if (playWnd.getRiverBoard() != null)
    System.out.println("r  total: " + playWnd.getRiverBoard().getTotal());
     */ 



    return total;
  }

}


TOP

Related Classes of com.poker.control.delegate.AnalystDelegate

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.