package com.poker.control.delegate;
import java.awt.AWTException;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
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.elements.state.PlayerState;
import com.poker.control.PokerControl;
import com.poker.data.PokerData;
import com.poker.ui.robot.UIRobot;
import com.poker.ui.robot.reaction.UIReaction;
import com.poker.ui.robot.reaction.UIReactionSeq;
import com.poker.ui.settings.rooms.ParimatchSettings;
import com.poker.ui.settings.rooms.events.Event;
import com.poker.ui.settings.rooms.events.EventType;
import com.poker.ui.settings.rooms.events.PointCondition;
import com.poker.ui.settings.tables.Table;
import com.poker.ui.windows.PlayWindow;
import com.poker.ui.windows.Window;
import com.poker.ui.windows.auxiluary.WindowPosition;
public class RobotDelegate {
UIRobot robot;
public RobotDelegate() {
this.robot = new UIRobot();
try {
this.robot.initRobot();
} catch (final AWTException e) {
e.printStackTrace();
}
}
public PlayWindow getCloseEmptyWindows(final PokerData pdata, final PokerControl pcontrol) {
final List<WindowPosition> winPositions = pdata.getWindowsPos();
Boolean bIsEmpty = false;
int topX;
int topY;
WindowPosition wpos;
PlayWindow playWnd = null;
Boolean[] activePlayers;
Boolean[] sitoutPlayers;
Boolean[] emptyPlaces;
Boolean[] foldPlayers;
int nCount;
for (int i = 0; i < winPositions.size(); i++) {
nCount = 0;
wpos = winPositions.get(i);
if (wpos.getPlayWindow() != null) {
playWnd = wpos.getPlayWindow();
pcontrol.activateWindow(playWnd);
try {
Thread.sleep(200);
} catch (final InterruptedException e) {
e.printStackTrace();
}
topX = playWnd.getLeftTopX();
topY = playWnd.getLeftTopY();
// activePlayers = getPlayersWithCards(pdata, playWnd);
// sitoutPlayers = getSitoutPlayers(pdata, playWnd);
emptyPlaces = getEmptyPlace(pdata, playWnd);
// foldPlayers = getFoldPlayers(pdata, playWnd);
for (int k = 0; k < emptyPlaces.length; k++) {
if (emptyPlaces[k]) {
nCount++;
}
// System.out.println(k + " : " + emptyPlaces[k]);
}
// System.err.println("free seats = " + nCount);
if (nCount >=7){
pcontrol.react(pdata, playWnd, new AnalystResult(UIReaction.UIR_CLOSE_WINDOW, null));
}
}
}
return null;
}
public PlayWindow getPlayWindowReadyForAction(final PokerData pdata, final PokerControl pcontrol, boolean bB) {
final List<WindowPosition> winPositions = pdata.getWindowsPos();
List<Event> events;
List<PointCondition> pointConditions;
Boolean bIsEventTrue = false;
Boolean result;
int topX;
int topY;
WindowPosition wpos;
for (int i = 0; i < winPositions.size(); i++) {
wpos = winPositions.get(i);
if (wpos.getPlayWindow() != null) {
pcontrol.activateWindow(wpos.getPlayWindow());
try {
Thread.sleep(200);
} catch (final InterruptedException e) {
e.printStackTrace();
}
topX = wpos.getPlayWindow().getLeftTopX();
topY = wpos.getPlayWindow().getLeftTopY();
events = pdata.getRoomSettings().getEvent();
for (final Event ev : events) {
if (bB){
if (ev.getEventType() != EventType.ET_NEED_WAIT_BB ||
ev.getEventType() != EventType.ET_SET_BB )
continue;
}
bIsEventTrue = true;
pointConditions = ev.getEventCondition().getPointConditions();
// System.out.println("===============================================");
for (final PointCondition pcond : pointConditions) {
result = this.robot.analyzePixel(topX + pcond.getPt().x, topY
+ pcond.getPt().y, pcond.getFrom(), pcond.getTo());
if (!pcond.getIsInside()) {
result = !result;
}
bIsEventTrue = bIsEventTrue & result;
}
if (bIsEventTrue) {
wpos.getPlayWindow().setCurrentEvent(ev.getEventType());
return wpos.getPlayWindow();
}
}
}
}
return null;
}
public EventType getMainWindowEvent(final PokerData pdata, final PokerControl pcontrol) {
List<Event> events;
List<PointCondition> pointConditions;
Boolean bIsEventTrue = false;
Boolean result;
int topX;
int topY;
final WindowPosition wpos;
try {
Thread.sleep(100);
} catch (final InterruptedException e) {
e.printStackTrace();
}
topX = pdata.getMainWnd().getLeftTopX();
topY = pdata.getMainWnd().getLeftTopY();
events = pdata.getRoomSettings().getMainEvent();
for (final Event ev : events) {
bIsEventTrue = true;
pointConditions = ev.getEventCondition().getPointConditions();
for (final PointCondition pcond : pointConditions) {
result = this.robot.analyzePixel(topX + pcond.getPt().x, topY + pcond.getPt().y,
pcond.getFrom(), pcond.getTo());
if (!pcond.getIsInside()) {
result = !result;
}
bIsEventTrue = bIsEventTrue & result;
}
if (bIsEventTrue) {
return ev.getEventType();
}
}
return null;
}
public int findTheButtonPosition(final PokerData pdata, final PlayWindow playWnd) {
final int topX = playWnd.getLeftTopX();
final int topY = playWnd.getLeftTopY();
Boolean result;
final Table table = pdata.getRoomSettings().getTableSettings(
pdata.getProgramSettings().getCountPlayers());
for (int i = 0; i < pdata.getProgramSettings().getCountPlayers(); i++) {
for (int x = -2; x < 4; x++) {
for (int y = -2; y < 4; y++) {
result = this.robot.analyzePixel(table.getPtPlayerButton(i).x + topX + x, table
.getPtPlayerButton(i).y
+ topY + y, pdata.getRoomSettings().getButtonColorFrom(), pdata
.getRoomSettings().getButtonColorTo());
if (result) {
return i;
}
}
}
}
return -1;
}
public BufferedImage[] getStacksImages(final PokerData pdata, final PlayWindow playWnd) {
final BufferedImage[] images = this.robot.captureScreenStacks(pdata, playWnd);
return images;
}
public String getConsoleBuffer(final PokerData pdata, final PlayWindow playWnd)
throws UnsupportedFlavorException, IOException {
final int topX = playWnd.getLeftTopX();
final int topY = playWnd.getLeftTopY();
int xBuffer;
int yBuffer;
xBuffer = pdata.getRoomSettings().getBufferInfoX();
yBuffer = pdata.getRoomSettings().getBufferInfoY();
return this.robot.getConsoleBuffer(topX + xBuffer, topY + yBuffer, 10, 10);
}
public String getStringFromClipboard() throws UnsupportedFlavorException, IOException {
return this.robot.getStringFromClipboard();
}
public Boolean turnOnConsoleBuffer(final PokerData pdata, final PlayWindow playWnd) {
final int topX = playWnd.getLeftTopX();
final int topY = playWnd.getLeftTopY();
int xBuffer;
int yBuffer;
xBuffer = pdata.getRoomSettings().getBufferInfoTurnOnX();
yBuffer = pdata.getRoomSettings().getBufferInfoTurnOnY();
this.robot.clickPointInArea(topX + xBuffer, topY + yBuffer, 2, 2);
return true;
}
/*
public int GetTopX(PokerData pdata,WindowPosition wpos){
return wpos.getX() * pdata.getRoomSettings().getPlayWndWidth() + pdata.getProgramSettings().getxDelay();
}
public int GetTopY(PokerData pdata,WindowPosition wpos){
return wpos.getY() * pdata.getRoomSettings().getPlayWndHeight() + pdata.getProgramSettings().getyDelay();
}*/
public Boolean[] getEmptyPlace(final PokerData pdata, final PlayWindow playWnd) {
final Boolean[] emptyPlaces = new Boolean[pdata.getProgramSettings().getCountPlayers()];
final int topX = playWnd.getLeftTopX();
final int topY = playWnd.getLeftTopY();
Boolean result;
final Table table = pdata.getRoomSettings().getTableSettings(
pdata.getProgramSettings().getCountPlayers());
result = false;
for (int i = 0; i < emptyPlaces.length; i++) {
for (int q = -1; q <= 1; q++) {
for (int w = -1; w <= 1; w++) {
result = this.robot.analyzePixel(table.getptEmptyPlace(i).x + topX + q, table
.getptEmptyPlace(i).y
+ topY + w, pdata.getRoomSettings().getColorIsEmptyPlaceFrom(), pdata
.getRoomSettings().getColorIsEmptyPlaceTo());
if (result) {
break;
}
}
if (result) {
break;
}
}
emptyPlaces[i] = new Boolean(result);
}
return emptyPlaces;
}
public Boolean[] getFoldPlayers(final PokerData pdata, final PlayWindow playWnd) {
final Boolean[] foldPlayers = new Boolean[pdata.getProgramSettings().getCountPlayers()];
final int topX = playWnd.getLeftTopX();
final int topY = playWnd.getLeftTopY();
Boolean result;
final Table table = pdata.getRoomSettings().getTableSettings(
pdata.getProgramSettings().getCountPlayers());
result = false;
for (int i = 0; i < foldPlayers.length; i++) {
for (int q = -1; q <= 4; q++) {
for (int w = -3; w <= 3; w++) {
result = this.robot.analyzePixel(table.getPtPlayerFold(i).x + topX + q, table
.getPtPlayerFold(i).y
+ topY + w, pdata.getRoomSettings().getColorFoldFrom(), pdata
.getRoomSettings().getColorFoldTo());
if (result) {
break;
}
}
if (result) {
break;
}
}
foldPlayers[i] = new Boolean(result);
}
return foldPlayers;
}
public Boolean[] getSitoutPlayers(final PokerData pdata, final PlayWindow playWnd) {
final Boolean[] sitoutPlayers = new Boolean[pdata.getProgramSettings().getCountPlayers()];
final int topX = playWnd.getLeftTopX();
final int topY = playWnd.getLeftTopY();
Boolean result;
final Table table = pdata.getRoomSettings().getTableSettings(
pdata.getProgramSettings().getCountPlayers());
result = false;
for (int i = 0; i < sitoutPlayers.length; i++) {
for (int q = -1; q <= 1; q++) {
for (int w = -3; w <= 3; w++) {
result = this.robot.analyzePixel(table.getPtPlayerSitout(i).x + topX + q, table
.getPtPlayerSitout(i).y
+ topY + w, pdata.getRoomSettings().getColorSitoutFrom(), pdata
.getRoomSettings().getColorSitoutTo());
if (result) {
break;
}
}
if (result) {
break;
}
}
sitoutPlayers[i] = new Boolean(result);
}
return sitoutPlayers;
}
public Boolean[] getPlayersWithCards(final PokerData pdata, final PlayWindow playWnd) {
final Boolean[] playersWithCards = new Boolean[pdata.getProgramSettings().getCountPlayers()];
final int topX = playWnd.getLeftTopX();
final int topY = playWnd.getLeftTopY();
Boolean result;
final Table table = pdata.getRoomSettings().getTableSettings(
pdata.getProgramSettings().getCountPlayers());
for (int i = 0; i < playersWithCards.length; i++) {
result = this.robot.analyzePixel(table.getPtPlayerCard(i).x + topX, table
.getPtPlayerCard(i).y
+ topY, pdata.getRoomSettings().getColorPlayerCards(), pdata.getRoomSettings()
.getColorPlayerCards());
playersWithCards[i] = new Boolean(result);
}
return playersWithCards;
}
public void react(final PokerData pdata, final Window playWnd, final AnalystResult analystResult) {
/* if (analystResult == null)
return false;
*/
final int topX = playWnd.getLeftTopX();
final int topY = playWnd.getLeftTopY();
if (analystResult.getSurrogate() != null) {
copyStringToClipboard(analystResult.getSurrogate());
}
final UIReactionSeq reactionSeq = pdata.getRoomSettings().getUiReactionSeq(
analystResult.getReaction());
if (reactionSeq == null) {
System.err.println("Event chain for " + analystResult.getReaction()
+ " is not initialized");
// return false;
}
this.robot.react(topX, topY, reactionSeq);
this.robot.mouseMove(topX + 215, topY + 175, 40, 20);
}
public Boolean copyStringToClipboard(final String str) {
final StringSelection stringSelection = new StringSelection(str);
final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, new ClipboardOwner() {
public void lostOwnership(final Clipboard clipboard, final Transferable contents) {
}
});
return true;
}
public Boolean getRidOfSystemWindows(final PokerData pdata, final PlayWindow playWnd) {
/*Event systemEvent = null;
for (Event ev:pdata.getRoomSettings().getEvent()){
if (ev.getEventType() == EventType.ET_SYSTEM_MESSAGE);
systemEvent = ev;
break;
}
if (systemEvent == null) return true;
int topX;
int topY;
Boolean result;
WindowPosition wpos = playWnd.getCurrentPosition();
topX = GetTopX(pdata,wpos);
topY = GetTopY(pdata,wpos);
robot.
robot.pressButton(KeyEvent.VK_ENTER);
*/
return true;
}
public Integer getPlayerPosition(final PokerData pdata, final PlayWindow playWnd) {
if (pdata.getRoomSettings() instanceof ParimatchSettings) {
return new Integer(0);
} else {
return new Integer(0);
}
}
}