Package com.poker.ui.robot

Source Code of com.poker.ui.robot.UIRobot

package com.poker.ui.robot;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Random;

import javax.imageio.ImageIO;

import com.poker.data.PokerData;
import com.poker.ui.robot.reaction.UIReactionDoing;
import com.poker.ui.robot.reaction.UIReactionPrimitive;
import com.poker.ui.robot.reaction.UIReactionSeq;
import com.poker.ui.robot.reaction.UIReactionStep;
import com.poker.ui.settings.tables.Table;
import com.poker.ui.windows.PlayWindow;

public class UIRobot {

    Robot robot;
    int   delayBetweenActions = 50;

    public UIRobot() {
        super();
        //
    }

    public void sleepThis(final int ms) {

        try {
            Thread.sleep(ms);
        } catch (final InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void initRobot() throws AWTException {

        this.robot = new Robot();
    }

    public BufferedImage captureScreen(final Rectangle rect) {

        return this.robot.createScreenCapture(rect);
    }
   
    /*  @Deprecated
      public BufferedImage[] captureScreenStacks(Rectangle rect,RoomSettings roomSettings){
        Rectangle tmpRect;
        BufferedImage[] image = new BufferedImage[10];
        for (int i = 0; i<10; i++){
          tmpRect = new Rectangle(rect.x + roomSettings.getRectsPlayersStack(i).x,
                      rect.y + roomSettings.getRectsPlayersStack(i).y,
                      roomSettings.getRectsPlayersStack(i).width,
                      roomSettings.getRectsPlayersStack(i).height);
          image[i] =    robot.createScreenCapture(tmpRect);
        }

        return image;
      }*/
    public BufferedImage[] captureScreenStacks(final PokerData pdata, final PlayWindow playWnd) {

        final int topX = playWnd.getLeftTopX();
        final int topY = playWnd.getLeftTopY();
        final Table table = pdata.getRoomSettings().getTableSettings(
                pdata.getProgramSettings().getCountPlayers());

        Rectangle tmpRect;
        final BufferedImage[] image = new BufferedImage[pdata.getProgramSettings()
                .getCountPlayers()];
        for (int i = 0; i < pdata.getProgramSettings().getCountPlayers(); i++) {
            tmpRect = new Rectangle(topX + table.getRectPlayerStack(i).x, topY
                    + table.getRectPlayerStack(i).y, table.getRectPlayerStack(i).width, table
                    .getRectPlayerStack(i).height);
            image[i] = this.robot.createScreenCapture(tmpRect);
        }

        return image;
    }

    @Deprecated
    public void saveScreenToFile(final BufferedImage image, final String filename) {

        try {
            ImageIO.write(image, "BMP", new File(filename));
        } catch (final IOException e) {
            e.printStackTrace();
        }
    }

    @Deprecated
    public void saveStacksToFile(final BufferedImage[] image, final String filename) {

        for (int i = 0; i < image.length; i++) {
            try {
                ImageIO.write(image[i], "BMP", new File(filename + "_" + i + ".bmp"));
            } catch (final IOException e) {
                e.printStackTrace();
            }
        }
    }

    public Color GetPixel(final int x, final int y) {

        return this.robot.getPixelColor(x, y);
    }

    @Deprecated
    public void pickUpABufferInfo(final int topX, final int topY, final int x, final int y) {

        final int randX = new Random().nextInt() % 30 - 15;
        final int randY = new Random().nextInt() % 30 - 15;

        this.robot.delay(this.delayBetweenActions);

        this.robot.mouseMove(topX + x + randX, topY + y + randY);
        this.robot.mousePress(InputEvent.BUTTON1_MASK);
        this.robot.delay(this.delayBetweenActions);
        this.robot.mouseRelease(InputEvent.BUTTON1_MASK);
        this.robot.delay(this.delayBetweenActions);
        this.robot.keyPress(KeyEvent.VK_CONTROL);
        this.robot.delay(this.delayBetweenActions);
        this.robot.keyPress(KeyEvent.VK_A);
        this.robot.delay(this.delayBetweenActions);
        this.robot.keyRelease(KeyEvent.VK_A);
        this.robot.delay(this.delayBetweenActions);

        this.robot.keyPress(KeyEvent.VK_C);
        this.robot.delay(this.delayBetweenActions);
        this.robot.keyRelease(KeyEvent.VK_C);
        this.robot.delay(this.delayBetweenActions);
        this.robot.keyRelease(KeyEvent.VK_CONTROL);
        this.robot.delay(100);

        final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        String output = new String();
        try {
            output = (String) clipboard.getData(DataFlavor.stringFlavor);
        } catch (final UnsupportedFlavorException e) {
            //
            e.printStackTrace();
        } catch (final IOException e) {
            //
            e.printStackTrace();
        }
        System.out.println(output);
        System.out.println("=========================================");
    }

    public Boolean analyzePixel(final int x, final int y, final Color colorFrom, final Color colorTo) {

        final Color pixel = this.robot.getPixelColor(x, y);
        final int r = pixel.getRed();
        final int g = pixel.getGreen();
        final int b = pixel.getBlue();

        //  System.out.println("A: " + "x=" + x + "  y=" + y + "  r=" + r + "  g=" + g + "  b="+ b );
        //  System.out.println("F: " + "x=" + x + "  y=" + y + "  r=" + colorFrom.getRed() + "  g=" + colorFrom.getGreen() + "  b="+ colorFrom.getBlue() );
        //  System.out.println("T: " + "x=" + x + "  y=" + y + "  r=" + colorTo.getRed() + "  g=" + colorTo.getGreen() + "  b="+ colorTo.getBlue() );

        return r >= colorFrom.getRed() && r <= colorTo.getRed() && g >= colorFrom.getGreen()
                && g <= colorTo.getGreen() && b >= colorFrom.getBlue() && b <= colorTo.getBlue();

    }

    protected void clickPoint(final int x, final int y) {

        //robot.delay(delayBetweenActions);
        sleepThis(this.delayBetweenActions);
        this.robot.mouseMove(x, y);
        this.robot.mousePress(InputEvent.BUTTON1_MASK);
        //robot.delay(delayBetweenActions);
        sleepThis(this.delayBetweenActions);
        this.robot.mouseRelease(InputEvent.BUTTON1_MASK);
    }

    public void clickPointInArea(final int x, final int y, int deltaX, int deltaY) {

        if (deltaX != 0) {
            deltaX = new Random().nextInt() % deltaX;
        }

        if (deltaY != 0) {
            deltaY = new Random().nextInt() % deltaY;
        }

        clickPoint(x + deltaX, y + deltaY);
    }

    public void mouseMove(final int x, final int y, final int deltaX, final int deltaY) {

        this.robot.mouseMove(x, y);
        //InputEvent.BUTTON1_MASK
    }

    public void mousePress(final int key) {

        this.robot.mousePress(key);
    }

    public void mouseRelease(final int key) {

        this.robot.mouseRelease(key);
        //
    }

    public void pressButton(final int buttonType) {

        sleepThis(this.delayBetweenActions);
        //robot.delay(delayBetweenActions);
        this.robot.keyPress(buttonType);
        sleepThis(this.delayBetweenActions);
        //robot.delay(delayBetweenActions);
        this.robot.keyRelease(buttonType);
    }

    public void pressButtonOnly(final int buttonType) {

        sleepThis(this.delayBetweenActions);
        //robot.delay(delayBetweenActions);
        this.robot.keyPress(buttonType);
    }

    public void releaseButtonOnly(final int buttonType) {

        sleepThis(this.delayBetweenActions);
        //robot.delay(delayBetweenActions);
        this.robot.keyRelease(buttonType);
    }

    public void pressButtons(final int buttonType[]) {

        for (int i = 0; i < buttonType.length; i++) {
            sleepThis(this.delayBetweenActions);
            //robot.delay(delayBetweenActions);
            this.robot.keyPress(buttonType[i]);
        }
        for (int i = buttonType.length - 1; i >= 0; i--) {
            sleepThis(this.delayBetweenActions);
            //robot.delay(delayBetweenActions);
            this.robot.keyRelease(buttonType[i]);
        }

    }

    protected void pressButtonsToFillClipboard() {

        sleepThis(this.delayBetweenActions);
        //robot.delay(delayBetweenActions);
        this.robot.keyPress(KeyEvent.VK_CONTROL);
        sleepThis(this.delayBetweenActions);
        //robot.delay(delayBetweenActions);
        this.robot.keyPress(KeyEvent.VK_A);
        sleepThis(this.delayBetweenActions);
        //robot.delay(delayBetweenActions);
        this.robot.keyRelease(KeyEvent.VK_A);
        sleepThis(this.delayBetweenActions);
        //robot.delay(delayBetweenActions);

        this.robot.keyPress(KeyEvent.VK_C);
        sleepThis(this.delayBetweenActions);
        //robot.delay(delayBetweenActions);
        this.robot.keyRelease(KeyEvent.VK_C);
        sleepThis(this.delayBetweenActions);
        //robot.delay(delayBetweenActions);
        this.robot.keyRelease(KeyEvent.VK_CONTROL);
        sleepThis(this.delayBetweenActions * 2);
        //robot.delay(100);
    }

    public String getConsoleBuffer(final int x, final int y, final int deltaX, final int deltaY)
            throws UnsupportedFlavorException, IOException {

        clickPointInArea(x, y, deltaX, deltaY);
        pressButtonsToFillClipboard();

        try {
            Thread.sleep(50);
        } catch (final InterruptedException e) {
            e.printStackTrace();
        }
        return getStringFromClipboard();
    }

    public String getStringFromClipboard() throws UnsupportedFlavorException, IOException {

        final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();

        return (String) clipboard.getData(DataFlavor.stringFlavor);

    }

    public void react(final int topX, final int topY, final UIReactionSeq reactionSeq) {

        final List<UIReactionStep> reactionQueue = reactionSeq.getReactionQueue();
       
        try {
          Thread.sleep(50);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
       
        UIReactionStep reactionStep;
        for (int i = 0; i < reactionQueue.size(); i++) {
          try {
              Thread.sleep(10);
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
            reactionStep = reactionQueue.get(i);
            if (reactionStep.getUiReactionPrimitive() == UIReactionPrimitive.UIRP_WAIT) {
              sleepThis(reactionStep.getUiReactionKey());             
            }
            else
            if (reactionStep.getUiReactionPrimitive() == UIReactionPrimitive.UIRP_KEYBOARD) {
                if (reactionStep.getUiReactionDoing() == UIReactionDoing.KEY_CLICK) {
                    pressButton(reactionStep.getUiReactionKey());
                } else if (reactionStep.getUiReactionDoing() == UIReactionDoing.KEY_PRESS) {
                    pressButtonOnly(reactionStep.getUiReactionKey());
                } else if (reactionStep.getUiReactionDoing() == UIReactionDoing.KEY_RELEASE) {
                    releaseButtonOnly(reactionStep.getUiReactionKey());
                }
            } else if (reactionStep.getUiReactionPrimitive() == UIReactionPrimitive.UIRP_MOUSE) {
                if (reactionStep.getUiReactionDoing() == UIReactionDoing.MOUSE_CLICK) {
                    clickPointInArea(reactionStep.getPtPosition().x + topX, reactionStep
                            .getPtPosition().y
                            + topY, 2, 0);
                } else if (reactionStep.getUiReactionDoing() == UIReactionDoing.MOUSE_PRESS) {
                    mousePress(reactionStep.getUiReactionKey());
                }
                if (reactionStep.getUiReactionDoing() == UIReactionDoing.MOUSE_RELEASE) {
                    mouseRelease(reactionStep.getUiReactionKey());
                }
                if (reactionStep.getUiReactionDoing() == UIReactionDoing.MOUSE_MOVE) {
                    mouseMove(reactionStep.getPtPosition().x + topX, reactionStep.getPtPosition().y
                            + topY, 2, 2);
                }
            }

        }

    }

}
TOP

Related Classes of com.poker.ui.robot.UIRobot

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.