package com.poker.control;
import java.awt.Point;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;
import com.poker.analyst.AnalystResult;
import com.poker.analyst.element.Board;
import com.poker.analyst.element.Card;
import com.poker.analyst.element.Player;
import com.poker.analyst.element.Rounds;
import com.poker.analyst.elements.state.PlayerState;
import com.poker.analyst.parse.BufferParser;
import com.poker.analyst.parse.ParserException;
import com.poker.analyst.parse.ParserResult;
import com.poker.control.delegate.AnalystDelegate;
import com.poker.control.delegate.OCRDelegate;
import com.poker.control.delegate.RobotDelegate;
import com.poker.control.delegate.WinwrkDelegate;
import com.poker.data.PokerData;
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 PokerControl {
public WinwrkDelegate winDel;
public RobotDelegate robotDel;
OCRDelegate OCRDel;
AnalystDelegate analystDel;
public PokerControl() {
this.winDel = new WinwrkDelegate();
this.robotDel = new RobotDelegate();
this.OCRDel = new OCRDelegate();
this.analystDel = new AnalystDelegate();
}
// WIN
public Boolean updateWindowsFromHandles(final PokerData pdata, final Integer[] handles) {
return this.winDel.updateWindowsFromHandles(pdata, handles);
}
public Boolean updateWindowsFromWaitingList(final PokerData pdata) {
return this.winDel.updateWindowsFromWaitingList(pdata);
}
public boolean updateWaitingListFromHandles(final PokerData pdata, final Integer[] handles) {
return this.winDel.updateWaitingListFromHandles(pdata, handles);
}
public Integer[] getOpenWindowsHandles(final Integer processID) {
return this.winDel.getOpenWindowsHandles(processID);
}
public Integer getProcID(final String processName) {
return this.winDel.getProcID(processName);
}
public Boolean killDeadTablesFromPosition(final PokerData pdata, final Integer[] handles) {
return this.winDel.killDeadTablesFromPosition(pdata, handles);
}
public boolean killAllTablesFromWaitingList(final PokerData pdata, final PokerControl pControl) {
return this.winDel.killAllTablesFromWaitingList(pdata, pControl);
}
public boolean killTableFromWaitingList(final PokerData pdata, final PokerControl pControl,
final PlayWindow playWnd) {
return this.winDel.killTableFromWaitingList(pdata, pControl, playWnd);
}
public Boolean RegMainWindow(final PokerData pdata, final Integer[] handle) {
return this.winDel.RegMainWindow(pdata, handle);
}
/*
public Boolean RegMainWindow(PokerData pdata,Integer handle){
return winDel.RegMainWindow(pdata,handle);
}*/
public WindowPosition FindPositionByHandle(final PokerData pdata, final Integer handle) {
return this.winDel.FindPositionByHandle(pdata, handle);
}
public WindowPosition RegWindowToPosition(final PokerData pdata, final Integer handle) {
return this.winDel.RegWindowToPosition(pdata, handle);
}
public void RegWindowsToPositions(final PokerData pdata, final Integer[] handle) {
this.winDel.RegWindowsToPositions(pdata, handle);
}
public WindowPosition GetFirstFreeWinPosition(final PokerData pdata) {
return this.winDel.GetFirstFreeWinPosition(pdata);
}
public boolean movePlayWindow(final PokerData pdata, final PlayWindow playWnd) {
return this.winDel.movePlayWindow(pdata, playWnd);
}
public Boolean activateWindow(final Window playWnd) {
return this.winDel.activateWindow(playWnd);
}
public Point getWindowPosByHandle(final int handle) {
return this.winDel.getWindowPosByHandle(handle);
}
//ROBOT
public PlayWindow getPlayWindowReadyForAction(final PokerData pdata, boolean bB) {
return this.robotDel.getPlayWindowReadyForAction(pdata, this, bB);
}
public PlayWindow getCloseEmptyWindows(final PokerData pdata) {
System.err.println("Closing empty windows");
return this.robotDel.getCloseEmptyWindows(pdata, this);
}
public EventType getMainWindowEvent(final PokerData pdata, final PokerControl pcontrol) {
return this.robotDel.getMainWindowEvent(pdata, this);
}
public int findTheButtonPosition(final PokerData pdata, final PlayWindow playWnd) {
return this.robotDel.findTheButtonPosition(pdata, playWnd);
}
public BufferedImage[] getStacksImages(final PokerData pdata, final PlayWindow playWnd) {
return this.robotDel.getStacksImages(pdata, playWnd);
}
public String getConsoleBuffer(final PokerData pdata, final PlayWindow playWnd)
throws UnsupportedFlavorException, IOException {
return this.robotDel.getConsoleBuffer(pdata, playWnd);
}
public Boolean turnOnConsoleBuffer(final PokerData pdata, final PlayWindow playWnd) {
return this.robotDel.turnOnConsoleBuffer(pdata, playWnd);
}
public Boolean[] getPlayersWithCards(final PokerData pdata, final PlayWindow playWnd) {
return this.robotDel.getPlayersWithCards(pdata, playWnd);
}
public Boolean[] getSitoutPlayers(final PokerData pdata, final PlayWindow playWnd) {
return this.robotDel.getSitoutPlayers(pdata, playWnd);
}
public Boolean[] getFoldPlayers(final PokerData pdata, final PlayWindow playWnd) {
return this.robotDel.getFoldPlayers(pdata, playWnd);
}
public Boolean[] getEmptyPlace(final PokerData pdata, final PlayWindow playWnd) {
return this.robotDel.getEmptyPlace(pdata, playWnd);
}
public void react(final PokerData pdata, final Window playWnd, final AnalystResult analystResult) {
this.robotDel.react(pdata, playWnd, this.analystDel.correctReact(pdata, playWnd,
analystResult));
}
public Boolean getRidOfSystemWindows(final PokerData pdata, final PlayWindow playWnd) {
return this.robotDel.getRidOfSystemWindows(pdata, playWnd);
}
public Integer getPlayerPosition(final PokerData pdata, final PlayWindow playWnd) {
return this.robotDel.getPlayerPosition(pdata, playWnd);
}
public String getStringFromClipboard() throws UnsupportedFlavorException, IOException {
return this.robotDel.getStringFromClipboard();
}
//OCR
public String[] getStacksStrings(final PokerData pdata, final BufferedImage[] images) {
return this.OCRDel.getStacksStrings(pdata, images);
}
public BufferedImage[] getPreparedStacksImages(final PokerData pdata,
final BufferedImage[] images, final int scale) {
return this.OCRDel.getPreparedStacksImages(pdata, images, scale);
}
//ANALYST
public Board createBoardShablon(final PokerData pdata) {
return this.analystDel.createBoardShablon(pdata);
}
public Float[] getStacksFloats(final PokerData pdata, final String[] strings) {
return this.analystDel.getStacksFloats(pdata, strings);
}
public void analyzeOneTableFromWaitingList(final PokerData pdata, final PokerControl pControl) {
this.analystDel.analyzeOneTableFromWaitingList(pdata, pControl);
}
@Deprecated
public String[] getStacksFloatsTest(final PokerData pdata, final String[] strings) {
return this.analystDel.getStacksFloatsTest(pdata, strings);
}
public AnalystResult getReaction(final PokerData pdata, final PlayWindow playWnd) {
return this.analystDel.getReaction(pdata, playWnd, this);
}
public boolean isNewTableNeeds(final PokerData pdata) {
return this.analystDel.isNewTableNeeds(pdata);
}
public Board setParserResultsToBoard(final PokerData pdata, final PokerControl pControl,Board board, ParserResult parserResult) {
return this.analystDel.setParserResultsToBoard(pdata, pControl, board, parserResult);
}
public Board setNamesToBoard(PokerData pdata, Board board, List<String> names,ParserResult parserResult, boolean isFirstAuction) {
return this.analystDel.setNamesToBoard(pdata, board, names, parserResult, isFirstAuction);
}
public float getAllRoundTotal(PlayWindow playWnd){
return this.analystDel.getAllRoundTotal(playWnd);
}
//CONTROL
public PlayWindow fillNotFullBoardForBB(final PokerData pdata, PlayWindow playWnd) throws Exception{
Board board = createBoardShablon(pdata);
board.setHero(0);
board.setDealer(findTheButtonPosition(pdata, playWnd));
BufferedImage[] hStacks = getStacksImages(pdata, playWnd);
hStacks = getPreparedStacksImages(pdata, hStacks, 2);
final String[] strStacks = getStacksStrings(pdata, hStacks);
final Float[] flStacks = getStacksFloats(pdata, strStacks);
for (int i = 0; i < board.getPlayers().size(); i++) {
board.getPlayers().get(i).setStack(flStacks[i]);
}
playWnd.setCurrentRound(Rounds.PREFLOP);
playWnd.setPreflopBoard(board);
return playWnd;
}
public PlayWindow fillPlayWnd(final PokerData pdata, PlayWindow playWnd) throws Exception{
final BufferParser bufferParser = pdata.getRoomSettings().getBufferParser(pdata.getProgramSettings().getHeroName());
String strBuffer;
ParserResult parserResult = null;
ParserResult parserResultPrev = null;
turnOnConsoleBuffer(pdata, playWnd);
Board board = createBoardShablon(pdata);
Board boardPrev = null;
Board boardPrevNew = null;
//getting buffer
strBuffer = getConsoleBuffer(pdata, playWnd);
parserResult = bufferParser.getParserResult(strBuffer,null);
if (parserResult == null) throw new ParserException();
if (parserResult.getCurrentRound() != Rounds.PREFLOP){
boardPrevNew = createBoardShablon(pdata);
switch (parserResult.getCurrentRound()) {
case FLOP:
parserResultPrev = bufferParser.getParserResult(strBuffer,Rounds.PREFLOP);
break;
case TURN:
parserResultPrev = bufferParser.getParserResult(strBuffer,Rounds.FLOP);
break;
case RIVER:
parserResultPrev = bufferParser.getParserResult(strBuffer,Rounds.TURN);
break;
}
if (parserResultPrev == null) throw new ParserException();
}
playWnd.setCurrentRound(parserResult.getCurrentRound());
BufferedImage[] hStacks = getStacksImages(pdata, playWnd);
hStacks = getPreparedStacksImages(pdata, hStacks, 2);
final String[] strStacks = getStacksStrings(pdata, hStacks);
final Float[] flStacks = getStacksFloats(pdata, strStacks);
final Boolean[] activePlayers = getPlayersWithCards(pdata, playWnd);
final Boolean[] sitoutPlayers = getSitoutPlayers(pdata, playWnd);
final Boolean[] emptyPlaces = getEmptyPlace(pdata, playWnd);
final Boolean[] foldPlayers = getFoldPlayers(pdata, playWnd);
Player player;
for (int i = 0; i < board.getPlayers().size(); i++) {
player = board.getPlayers().get(i);
if (activePlayers[i]) {
player.setPlayerState(PlayerState.PRS_IN_ACTION);
} else if (sitoutPlayers[i]) {
player.setPlayerState(PlayerState.PRS_SITOUT);
} else if (emptyPlaces[i]) {
player.setPlayerState(PlayerState.PRS_NONE);
} else if (foldPlayers[i]) {
player.setPlayerState(PlayerState.PRS_FOLD);
} else {
player.setPlayerState(PlayerState.PRS_SITOUT);
}
if (board.getHero() == i) {
player.setPlayerState(PlayerState.PRS_IN_ACTION);
}
player.setStack(flStacks[i]);
}
board.setDealer(findTheButtonPosition(pdata, playWnd));
System.out.println("first auction: " + parserResult.isFirstAuction());
if (parserResult.getCurrentRound() == Rounds.PREFLOP){
board.setFirstAuction(parserResult.isFirstAuction());
if (parserResult.isFirstAuction()){
board = setNamesToBoard(pdata, board,bufferParser.getPlayersName(strBuffer),parserResult,true);
}else{
for(int i = 0; i< board.getPlayers().size(); i++){
board.getPlayers().get(i).setName(playWnd.getPreflopBoard().getPlayers().get(i).getName());
}
board = setNamesToBoard(pdata, board,bufferParser.getPlayersName(strBuffer),parserResult,false);
}
}else{
boardPrevNew.setDealer(board.getDealer());
if (parserResult.getCurrentRound() == Rounds.FLOP){
for(int i = 0; i< board.getPlayers().size(); i++){
boardPrevNew.getPlayers().get(i).setName(playWnd.getPreflopBoard().getPlayers().get(i).getName());
boardPrevNew.getPlayers().get(i).setPlayerState(board.getPlayers().get(i).getPlayerState());
}
boardPrevNew = setNamesToBoard(pdata, boardPrevNew,bufferParser.getPlayersName(strBuffer),parserResultPrev,false);
for(int i = 0; i< board.getPlayers().size(); i++){
board.getPlayers().get(i).setName(boardPrevNew.getPlayers().get(i).getName());
}
}
else{
for(int i = 0; i< board.getPlayers().size(); i++){
boardPrevNew.getPlayers().get(i).setName(playWnd.getPreflopBoard().getPlayers().get(i).getName());
}
for(int i = 0; i< playWnd.getPreflopBoard().getPlayers().size(); i++){
board.getPlayers().get(i).setName(playWnd.getPreflopBoard().getPlayers().get(i).getName());
}
}
}
/*
System.out.println("first auction: " + parserResult.isFirstAuction());
if (parserResult.getCurrentRound() == Rounds.PREFLOP){
board.setFirstAuction(parserResult.isFirstAuction());
if (!parserResult.isFirstAuction()){
board.setHeroActionOnTurn(playWnd.getPreflopBoard().getHeroActionOnTurn());
for(int i = 0; i< board.getPlayers().size(); i++){
board.getPlayers().get(i).setName(playWnd.getPreflopBoard().getPlayers().get(i).getName());
}
}
else
board = setNamesToBoard(board,bufferParser.getPlayersName(strBuffer),parserResult);
}
else{
for(int i = 0; i< board.getPlayers().size(); i++){
board.getPlayers().get(i).setName(playWnd.getPreflopBoard().getPlayers().get(i).getName());
boardPrevNew.getPlayers().get(i).setName(playWnd.getPreflopBoard().getPlayers().get(i).getName());
board.getPlayers().get(i).setReaction(UIReaction.UIR_ACTION_FOLD);
}
}*/
switch(parserResult.getCurrentRound()){
case PREFLOP :
playWnd.setPreflopBoard(null);
playWnd.setFlopBoard(null);
playWnd.setTurnBoard(null);
playWnd.setRiverBoard(null);
break;
case FLOP :
boardPrev = playWnd.getPreflopBoard();
break;
case TURN :
boardPrev = playWnd.getFlopBoard();
break;
case RIVER :
boardPrev = playWnd.getTurnBoard();
break;
}
System.out.println("===========================================================");
if (parserResult.getCurrentRound() != Rounds.PREFLOP){
boardPrevNew.setCurrentRound(boardPrev.getCurrentRound());
boardPrevNew.setHeroActionOnTurn(boardPrev.getHeroActionOnTurn());
boardPrevNew = setParserResultsToBoard(pdata, this, boardPrevNew, parserResultPrev);
}
board = setParserResultsToBoard(pdata, this, board, parserResult);
switch(parserResult.getCurrentRound()){
case PREFLOP :
break;
case FLOP :
playWnd.setPreflopBoard(boardPrevNew);
break;
case TURN :
playWnd.setFlopBoard(boardPrevNew);
break;
case RIVER :
playWnd.setTurnBoard(boardPrevNew);
break;
}
switch(parserResult.getCurrentRound()){
case PREFLOP :
playWnd.setPreflopBoard(board);
break;
case FLOP :
playWnd.setFlopBoard(board);
break;
case TURN :
playWnd.setTurnBoard(board);
break;
case RIVER :
playWnd.setRiverBoard(board);
break;
}
/*
// TOTALS:
System.out.println("TOTALS: ");
System.out.print("PF: " + playWnd.getPreflopBoard() + ", ");
if (playWnd.getPreflopBoard()!= null){
System.out.print(playWnd.getPreflopBoard().getCurrentRound() + ", ");
System.out.println(playWnd.getPreflopBoard().getTotal() + ", ");
}
System.out.print("F : " + playWnd.getFlopBoard() + ", ");
if (playWnd.getFlopBoard()!= null){
System.out.print(playWnd.getFlopBoard().getCurrentRound() + ", ");
System.out.println(playWnd.getFlopBoard().getTotal() + ", ");
}
System.out.print("T : " + playWnd.getFlopBoard() + ", ");
if (playWnd.getTurnBoard()!= null){
System.out.print(playWnd.getTurnBoard().getCurrentRound() + ", ");
System.out.println(playWnd.getTurnBoard().getTotal() + ", ");
}
*/
/*
if (boardPrevNew != null){
System.out.println("------BoardPrev-----------------------------");
System.out.println("Round " + boardPrevNew.getCurrentRound());
for (int i = 0; i < boardPrevNew.getPlayers().size(); i++) {
player = boardPrevNew.getPlayers().get(i);
System.out.print(i + ": ");
if (boardPrevNew.getDealer() == i) {
System.out.print(" D ");
} else {
System.out.print(" ");
}
System.out.format("%20s ", player.getName());
if (player.getStack() != -1) {
System.out.format("%5.2f", player.getStack());
} else {
System.out.format(" ");
}
switch (player.getPlayerState()) {
case PRS_IN_ACTION:
System.out.print(" ++ ");
break;
case PRS_FOLD:
System.out.print(" F ");
break;
case PRS_SITOUT:
System.out.print(" _ ");
break;
case PRS_NONE:
System.out.print(" ");
break;
default:
System.out.print(" O ");
break;
}
if (player.getBet() != 0) {
System.out.format("%5.2f", player.getBet());
} else {
System.out.format(" ");
}
System.out.print(" " + player.getReaction());
System.out.println();
}
System.out.println("================================");
}
*/
//Painting The Board
/*
if (board.getPlayingCards() != null) {
System.out.print("HCards: ");
if (board.getPlayingCards().getPlayerCards() != null) {
for (final Card card : board.getPlayingCards().getPlayerCards()) {
System.out.print(card + ", ");
}
}
System.out.println();
if (board.getPlayingCards().getTableCards() != null) {
System.out.print("TCards: ");
}
for (final Card card : board.getPlayingCards().getTableCards()) {
System.out.print(card + ", ");
}
System.out.println();
if (board.getBlindPlayers() != null)
for (Player pl: board.getBlindPlayers()){
System.out.println(pl.getName() + ": " + pl.getBet());
}
System.out.println("Dead blinds: " + board.getDeathBlind());
System.out.println("-----------------------------------");
}
System.out.println("RType : " + board.getCurrentRound());
*/
/*
System.out.println("Total : " + getAllRoundTotal(playWnd));
System.out.println("------Board-----------------------------");
for (int i = 0; i < board.getPlayers().size(); i++) {
player = board.getPlayers().get(i);
System.out.print(i + ": ");
if (board.getDealer() == i) {
System.out.print(" D ");
} else {
System.out.print(" ");
}
System.out.format("%20s ", player.getName());
if (player.getStack() != -1) {
System.out.format("%5.2f", player.getStack());
} else {
System.out.format(" ");
}
switch (player.getPlayerState()) {
case PRS_IN_ACTION:
System.out.print(" ++ ");
break;
case PRS_FOLD:
System.out.print(" F ");
break;
case PRS_SITOUT:
System.out.print(" _ ");
break;
case PRS_NONE:
System.out.print(" ");
break;
default:
System.out.print(" O ");
break;
}
if (player.getBet() != 0) {
System.out.format("%5.2f", player.getBet());
} else {
System.out.format(" ");
}
System.out.print(" " + player.getReaction());
System.out.println();
}
System.out.println("================================");
*/
return playWnd;
}
}