Monday, February 27, 2017

Flask MVC Progress Reports

2/5/16
  • I am getting further into the tutorial but still struggling with tutorial having messed up code or something.
2/26/16
  • Still, can't figure out what's wrong with the code for the bootstrap part of the tutorial, I am moving on.
2/27/16
  • I am reading over the tutorial again to see if I missed anything but I did not see anything so I am just moving forward because I have to turn something in by tonight.
  • With the help of Nate, I was able to finish Exercise 1 and I am now working on exercise 2. 
  • With what we talked about in class today I should be able to finish it but I have to finish to formalized part of for exercise 1. 

Flask MVC Exercise 1

1) Purpose:
The Purpose of this project was to create MVC that would be able to take 2 numbers and then add them together. This would teach us more about python, creating an MVC and how to apply what we learned to different tasks.

2) Discussion of Solution: Code Walk Through

Controller.py

For the Controller file, we chose the index method and its content because after importing compute and InputForm, the program can get the form data, test if it is valid, send the data to the compute file and then return the view_output.html. Or if the data is not valid, then it will stay on the view_input.html and wait until the data is valid to change.



Model.py

For the model file, the InputForm class(Form) is written because this allows both data values to be taken and then validated to see if the values are the same type the program requires. 

View.py

For the view file,  get_values is written to be able to get the 2 input values and the show_sum was written to be able to calculate the sum of the 2 inputs.
Compute.py
Compute.py was written so that when it received the 2 input values, it would find the sum.



3) Testing Plan:
When I was testing this program, I made sure to test positive numbers, negative numbers, letters, and symbols in different combinations to make sure that it worked as intended.


4) Testing Documentation:
5) Commented Listings:
file:///C:/Users/adria/OneDrive/Documents/Doxygen/html/classes.html
Attached in Blackboard submission.


6) Quality:

For this project, I attempted to blog as I went, but I did not keep up with up with it to the extent that I would have like to. I used Doxygen for my commented listings again but I am still not sure if I am doing it right. For my testing documentation, I made a Youtube video to show the program worked as intended.

Wednesday, February 22, 2017

Sushi Go Round Code

import ImageGrab
import os
import time
import win32api, win32con
import ImageOps
from numpy import *

# All mesure ments are based on a ASUS X550VX-MS71 15.6" Laptop.# The game was opened in google chrome, 100% zoom, without scrolling down or anything like that.# global
x_pad = 190y_pad = 139
# dictionary of how many of each ingredient it on hand at the start of the gamefoodOnHand = {'shrimp ': 5,
              'rice': 10,
              'nori': 10,
              'roe': 10,
              'salmon': 5,
              'unagi': 5}

# Dictionary of sushi types and the greyscale value of each sushi typesushiTypes = {1893: 'onigiri',
              2537: 'caliroll',
              1900: 'gunkan', }


class Cord:
    # coordinates of food to make sushi with    f_shrimp = (47, 427)
    f_rice = (106, 435)
    f_nori = (38, 483)
    f_roe = (98, 488)
    f_salmon = (23, 544)
    f_unagi = (99, 548)
    # -----------------------------------------
    # coordinates of the area where the plates are for clearing the tables    plate1 = (76, 308)
    plate2 = (175, 309)
    plate3 = (382, 310)
    plate4 = (492, 310)
    plate5 = (492, 308)
    plate6 = (592, 312)

    # coordinates of the ingredients and menus needed to buy more food    phone = (574, 453)

    menu_topping = (537, 376)
    menu_rice = (525, 395)
    buy_rice = (542, 385)

    t_shrimp = (491, 322)
    t_unagi = (572, 325)
    t_nori = (488, 375)
    t_roe = (571, 381)
    t_salmon = (490, 436)

    delivery_norm = (487, 399)

    rice_exit = (579, 441)
    other_exit = (593, 440)


class Blank:
    # These are the values of the seats when no one is sitting down and there is no food bubble.   
    seat_1 = 8041    seat_2 = 5908    seat_3 = 10984    seat_4 = 10304    seat_5 = 6519    seat_6 = 8963

def screenGrab():
    # this grabs and image of the screen using the x and y pad to get the image rgb values and pixel locations    b1 = (x_pad + 1, y_pad + 1, x_pad + 638, y_pad + 483)
    im = ImageGrab.grab()

    ##im.save(os.getcwd() + '\\Snap__' + str(int(time.time())) +'.png', 'PNG')    return im


def grab():
    # this grabs an image of the suhsi box and then then converts it to a greyscale value to    
    # be used in the sushi types dictionary    box = (x_pad + 1, y_pad + 1, x_pad + 640, y_pad + 480)
    im = ImageOps.grayscale(ImageGrab.grab(box))
    a = array(im.getcolors())
    a = a.sum()
    print a
    return a


def leftClick():
    # method for left Clicking    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)
    time.sleep(.1)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0)
    print "Click."  # completely optional. But nice for debugging purposes.

def leftDown():
    # method for left Clicking when the button is down    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)
    time.sleep(.1)
    print 'left Down'

def leftUp():
    # method for left Clicking when the button is up    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0)
    time.sleep(.1)
    print 'left release'

def mousePos(cord):
    # method to get the position of the mouse    win32api.SetCursorPos((x_pad + cord[0], y_pad + cord[1]))


def get_cords():
    # method to the get the coordinates of wherever the mouse is when the program is run    x, y = win32api.GetCursorPos()
    x = x - x_pad
    y = y - y_pad
    print x, y


def clear_tables():
    # method to click where all of the empty plates are supposed to be when the customer is done with their food    # plate coordinates for easy reference    """    plate1 = (76, 308)    plate2 = (175, 309)    plate3 = (382, 310)    plate4 = (492, 310)    plate5 = (492, 308)    plate6 = (592, 312)    """    mousePos((76, 310))
    leftClick()

    mousePos((175, 310))
    leftClick()

    mousePos((287, 310))
    leftClick()

    mousePos((382, 310))
    leftClick()

    mousePos((492, 310))
    leftClick()

    mousePos((592, 310))
    leftClick()
    time.sleep(1)


def foldMat():
    # method to fold the mat after the ingredients are selected to make the sushi    mousePos((Cord.f_rice[0] + 40, Cord.f_rice[1]))
    leftClick()
    time.sleep(.1)


def makeFood(food):
    # methods to clink on the right ingredients,    # change the value in the foodOnHand dictionary to reflect how many have been used
    # method to make California rolls    if food == 'caliroll':
        print "Making a caliroll"        foodOnHand['rice'] -= 1        foodOnHand['nori'] -= 1        foodOnHand['roe'] -= 1        mousePos(Cord.f_rice)
        leftClick()
        time.sleep(.05)
        mousePos(Cord.f_nori)
        leftClick()
        time.sleep(.05)
        mousePos(Cord.f_roe)
        leftClick()
        time.sleep(.1)
        foldMat()
        time.sleep(1.5)

    # method to make onigiri    elif food == 'onigiri':
        print 'Making onigiri'        foodOnHand['rice'] -= 2        foodOnHand['nori'] -= 1        mousePos(Cord.f_rice)
        leftClick()
        time.sleep(.05)
        mousePos(Cord.f_rice)
        leftClick()
        time.sleep(.05)
        mousePos(Cord.f_nori)
        leftClick()
        time.sleep(.1)
        foldMat()
        time.sleep(.05)

        time.sleep(1.5)

    # method to make gunkan    elif food == 'gunkan':
        print 'Making a gunkan'        foodOnHand['rice'] -= 1        foodOnHand['nori'] -= 1        foodOnHand['roe'] -= 2        mousePos(Cord.f_rice)
        leftClick()
        time.sleep(.05)
        mousePos(Cord.f_nori)
        leftClick()
        time.sleep(.05)
        mousePos(Cord.f_roe)
        leftClick()
        time.sleep(.05)
        mousePos(Cord.f_roe)
        leftClick()
        time.sleep(.1)
        foldMat()
        time.sleep(1.5)


def buyFood(food):
    # variables names for buying ingredients, they were created above and are here for to be used as a reference
    mousePos(Cord.phone)
    mousePos(Cord.menu_topping)
    mousePos(Cord.t_nori)
    mousePos(Cord.t_roe)
    mousePos(Cord.t_salmon)
    mousePos(Cord.t_unagi)
    mousePos(Cord.rice_exit)
    mousePos(Cord.other_exit)

    mousePos(Cord.menu_rice)
    mousePos(Cord.buy_rice)

    mousePos(Cord.delivery_norm)

    # method to buy more rice    if food == 'rice':
        mousePos(Cord.phone)
        time.sleep(1)
        leftClick()
        print(0)
        mousePos(Cord.menu_rice)
        time.sleep(1)
        leftClick()
        print 'here1'        s = screenGrab()
        print s.getpixel(Cord.buy_rice)
        time.sleep(1)
        if s.getpixel(Cord.buy_rice) != (225, 181, 105):
            print 'rice is available'            mousePos(Cord.buy_rice)
            time.sleep(1)
            leftClick()
            print 'here3'            mousePos(Cord.delivery_norm)
            foodOnHand['rice'] += 10            time.sleep(1)
            leftClick()
            print'4'            time.sleep(1)
        else:
            print 'rice is NOT available'            mousePos(Cord.rice_exit)
            leftClick()
            time.sleep(1)
            buyFood(food)

    # method to buy more nori    if food == 'nori':
        mousePos(Cord.phone)
        time.sleep(1)
        leftClick()
        mousePos(Cord.menu_topping)
        time.sleep(1)
        leftClick()
        s = screenGrab()
        print 'test'        time.sleep(1)
        mousePos(Cord.t_nori)
        if s.getpixel(Cord.t_nori) == (225, 181, 105):
            print 'nori is available'            mousePos(Cord.t_nori)
            time.sleep(1)
            leftClick()
            mousePos(Cord.delivery_norm)
            foodOnHand['nori'] += 10            time.sleep(1)
            leftClick()
            time.sleep(1)
        else:
            print 'nori is NOT available'            mousePos(Cord.other_exit)
            leftClick()
            time.sleep(1)
            buyFood(food)

    # method to buy more roe    if food == 'roe':
        mousePos(Cord.phone)
        time.sleep(1)
        leftClick()
        mousePos(Cord.menu_topping)
        time.sleep(1)
        leftClick()
        s = screenGrab()
        # 202, 55, 25        time.sleep(1)
        if s.getpixel(Cord.t_roe) != (202, 55, 25):
            print 'roe is available'            mousePos(Cord.t_roe)
            time.sleep(1)
            leftClick()
            mousePos(Cord.delivery_norm)
            foodOnHand['roe'] += 10            time.sleep(1)
            leftClick()
            time.sleep(1)
        else:
            print 'roe is NOT available'            mousePos(Cord.other_exit)
            leftClick()
            time.sleep(1)
            buyFood(food)


def get_seat_one():
    # this method gets the greysale value of the sushi bubble in seat 1
    box = (214, 304, 214 + 63, 304 + 16)
    im = ImageOps.grayscale(ImageGrab.grab(box))
    a = array(im.getcolors())
    a = a.sum()
    print a
    im.save(os.getcwd() + '\\seat_one__' + str(int(time.time())) + '.png', 'PNG')
    return a


def get_seat_two():
    # this method gets the greysale value of the sushi bubble in seat 2
    box = (315, 304, 315 + 63, 304 + 16)
    im = ImageOps.grayscale(ImageGrab.grab(box))
    a = array(im.getcolors())
    a = a.sum()
    print a
    im.save(os.getcwd() + '\\seat_two__' + str(int(time.time())) + '.png', 'PNG')
    return a


def get_seat_three():
    # this method gets the greysale value of the sushi bubble in seat 3
    box = (416, 304, 416 + 63, 304 + 16)
    im = ImageOps.grayscale(ImageGrab.grab(box))
    a = array(im.getcolors())
    a = a.sum()
    print a
    im.save(os.getcwd() + '\\seat_three__' + str(int(time.time())) + '.png', 'PNG')
    return a


def get_seat_four():
    # this method gets the greysale value of the sushi bubble in seat 4
    box = (517, 304, 517 + 63, 304 + 16)
    im = ImageOps.grayscale(ImageGrab.grab(box))
    a = array(im.getcolors())
    a = a.sum()
    print a
    im.save(os.getcwd() + '\\seat_four__' + str(int(time.time())) + '.png', 'PNG')
    return a


def get_seat_five():
    # this method gets the greysale value of the sushi bubble in seat 5
    box = (618, 304, 618 + 63, 304 + 16)
    im = ImageOps.grayscale(ImageGrab.grab(box))
    a = array(im.getcolors())
    a = a.sum()
    print a
    im.save(os.getcwd() + '\\seat_five__' + str(int(time.time())) + '.png', 'PNG')
    return a


def get_seat_six():
    # this method gets the greysale value of the sushi bubble in seat 6
    box = (719, 304, 719 + 62, 304 + 15)
    im = ImageOps.grayscale(ImageGrab.grab(box))
    a = array(im.getcolors())
    a = a.sum()
    print a
    im.save(os.getcwd() + '\\seat_six__' + str(int(time.time())) + '.png', 'PNG')
    return a


def get_all_seats():
    # this method gets the greysale value of the sushi bubble in every seat
    get_seat_one()
    get_seat_two()
    get_seat_three()
    get_seat_four()
    get_seat_five()
    get_seat_six()


def startGame():
    # this method clicks through the menus to actually start the game
    # Play menu    mousePos((301, 311))
    leftClick()
    time.sleep(.1)

    # Continue pass ios/android download page    mousePos((356, 488))
    leftClick()
    time.sleep(.1)

    # Continue num 2    mousePos((305, 496))

    # Skip the tutorial    mousePos((587, 566))
    leftClick()
    time.sleep(.1)
    # Start the actual game    mousePos((379, 466))
    leftClick()
    time.sleep(.1)


def checkFood():
    # this method goes through the foodOnHand dictionary and check if any of the ingredients are low    # and if they are it calls buyFood of what ever is missing    for i, j in foodOnHand.items():
        if i == 'nori' or i == 'rice' or i == 'roe':
            if j <= 4:
                print '%s is low and needs to be replenished' % i
                buyFood(i)


def check_bubs():
    # this method checks the amount of each ingredient then takes a screen shot of each chairs thought bubble    #  if there is a person sitting there, it makes their order    # or returns an that the sushi is not found if there is something wrong or if no one is sitting at that spot,    # it returns that the table is unoccupied
    checkFood()
    s1 = get_seat_one()
    if s1 != Blank.seat_1:
        if sushiTypes.has_key(s1):
            print 'table 1 is occupied and needs %s' % sushiTypes[s1]
            makeFood(sushiTypes[s1])
        else:
            print 'sushi not found!\n sushiType = %i' % s1

    else:
        print 'Table 1 unoccupied'
    clear_tables()
    checkFood()
    s2 = get_seat_two()
    if s2 != Blank.seat_2:
        if sushiTypes.has_key(s2):
            print 'table 2 is occupied and needs %s' % sushiTypes[s2]
            makeFood(sushiTypes[s2])
        else:
            print 'sushi not found!\n sushiType = %i' % s2

    else:
        print 'Table 2 unoccupied'
    checkFood()
    s3 = get_seat_three()
    if s3 != Blank.seat_3:
        if sushiTypes.has_key(s3):
            print 'table 3 is occupied and needs %s' % sushiTypes[s3]
            makeFood(sushiTypes[s3])
        else:
            print 'sushi not found!\n sushiType = %i' % s3

    else:
        print 'Table 3 unoccupied'
    checkFood()
    s4 = get_seat_four()
    if s4 != Blank.seat_4:
        if sushiTypes.has_key(s4):
            print 'table 4 is occupied and needs %s' % sushiTypes[s4]
            makeFood(sushiTypes[s4])
        else:
            print 'sushi not found!\n sushiType = %i' % s4

    else:
        print 'Table 4 unoccupied'
    clear_tables()
    checkFood()
    s5 = get_seat_five()
    if s5 != Blank.seat_5:
        if sushiTypes.has_key(s5):
            print 'table 5 is occupied and needs %s' % sushiTypes[s5]
            makeFood(sushiTypes[s5])
        else:
            print 'sushi not found!\n sushiType = %i' % s5

    else:
        print 'Table 5 unoccupied'
    checkFood()
    s6 = get_seat_six()
    if s6 != Blank.seat_6:
        if sushiTypes.has_key(s6):
            print 'table 1 is occupied and needs %s' % sushiTypes[s6]
            makeFood(sushiTypes[s6])
        else:
            print 'sushi not found!\n sushiType = %i' % s6

    else:
        print 'Table 6 unoccupied'
    clear_tables()


def main():
    # this method starts the game goes through the menus and    # then runs a loop of checkbulbs that will continue to run play through the game.    #startGame()   # while True:       # check_bubs()    get_all_seats()

if __name__ == '__main__':
    main()

Sushi Go Round Bot

1) Purpose:
The Purpose of this project was to create a bot that would play Sushi Go Round, learn the beginnings of Python and how to use Pycharm.This tutorial can be found at https://code.tutsplus.com/tutorials/how-to-build-a-python-bot-that-can-play-web-games--active-11117.I have put in multiple hours into this tutorial and as of right now everything is working like it is intended to. The code has minor differences from the tutorial in order to get my code to work on my machine.

2) Discussion of Solution:
A major problem I had was when I was trying to get the right sizes of the boxes to get the values of the sushi thought bubbles,  I followed the tutorial exactly to get the bottom right corner to create the box but that did not work on my machine. After double and triple checking my measurements for the top left corner in 2 different paint software, I looked at the images that printed out, I noticed that the images were only off by a few pixels and then I put the method of each seat into Main and then ran them individually and adjusted the images by 1 pixel in both the x and y-direction and found that instead of adding 63 to the x value and 16 to the y-value, if I added 62 to the x and 15 to the y then it worked. This was a minor problem that probably was caused by either a resolution difference between the machine used in the tutorial and my machine or due to a change in the way the website was laid out which is why the solution was so simple to fix but took me so long to realize that the pixels were only off by such a minute amount.  Below is the code and result of my code following what the tutorial said to do and then what I changed it to to get it to work as intended.








3) Testing Plan:
During the coding process, I learned that having a different zoom in Google Chrome or using a different size monitor will cause the bot to click on unintended spots on the website. After realizing this I always made sure to keep my laptop as my main display, have chrome’s zoom at 100%. While testing I also made sure to have print statements at every step when debugging and that printing out variable values and then comparing them to what they should be. Doing both of these help me speed up my debugging and made certain bugs easier to find and fix.  All measurements are based on an ASUS X550VX-MS71 15.6" Laptop running windows 10 and the game was opened in google chrome, 100% zoom, without scrolling down or anything like that.


4) Testing Documentation:

5) Commented Listings:
file:///C:/Users/adria/OneDrive/Documents/Thingy/html/index.html
Attached in Blackboard submission.

6) Quality:

I tried to use Doxygen for my commented listings but I have never done anything like this so I do not think I did it the right way. I made a youtube video for my Testing documentation and would’ve used it for a code walkthrough for the testing plan but I do not have a mic to record my voice and for it to be audible enough for a walkthrough.