Slimes Remote

Credits: Programming, Python, QA

Slimes Remote is a Twitch.tv remote control application written in python/tkinter for the game Slimes. The application connects to Twitch.tv’s IRC server and allows players to send commands to the chat in order to perform actions in the game.

The code for Slimes Remote is PEP-8 compliant, open-sourced, and freely available from Github.

SlimesRemote.py
from Tkinter import *
import webbrowser
import tkSimpleDialog
import tkMessageBox
import socket


debug = False
loggedin = False
statusmsg = ""
s = socket.socket()
print ('Debugger set to: %s' % debug)


# Create a global for the statusmsg so it can be changed by functions
def printstatus():
    global statusmsg
    print statusmsg


def getversion():
    tkMessageBox.showwarning('Info!', 'Version: 1.0')


def github():
    webbrowser.open(url='https://github.com/acidxrain/SlimesRemote')


def about():
    tkMessageBox.showwarning('Info!', "Slimes Remote is a small python/"
                                      "tkinter application, written by Thomas "
                                      "Loupe, for the Twitch game Slimes.\n\nI"
                                      "f you\'d like to see more of Thomas\' "
                                      "projects, visit http://thomasloupe.com")


# Create the function for the submenu item to get auth token
def getauthtoken():
    if debug is True:
        webbrowser.open(url='http://www.twitchapps.com/tmi/')
        global statusmsg
        statusmsg = 'Opened URL successfully...'
        printstatus()
    else:
        webbrowser.open(url='http://www.twitchapps.com/tmi/')


# If user isn't logged in, let them know via dialog
def notconnected():
    tkMessageBox.showwarning('Info!', 'Please log in first via Actions menu!')


# Send the join command to the IRC channel to join the game
def joingame():
    # Don't send join command if the user hasn't logged into Twitch IRC first
    global loggedin
    if loggedin is True:
        chan = "#playslimes"
        msg = ("!join".encode("utf-8"))
        s.send("PRIVMSG {} :{}\r\n".format(chan, msg))
        global statusmsg
        statusmsg = "Joining Slimes game!"
        printstatus()
    else:
        notconnected()


def moveleft():
    # Check to see if user is logged in before allowing them to move left
    # If they enter an invalid int, don't accept it
    global loggedin
    if loggedin is True:
        movelvalue = tkSimpleDialog.askinteger('Move left', 'How far left? '
                                                            '(1-5):')
        if movelvalue < 1 or movelvalue > 5 or movelvalue == "":
            moveleft()
        else:
            chan = "#playslimes"
            msg = ("!movel %s".encode("utf-8") % movelvalue)
            s.send("PRIVMSG {} :{}\r\n".format(chan, msg))
            global statusmsg
            statusmsg = "Your slime is moving left %s units..." % movelvalue
            printstatus()
    else:
        notconnected()


def moveright():
    # Check to see if user is logged in before allowing them to move right
    # If they enter an invalid int, don't accept it
    global loggedin
    if loggedin is True:
        movervalue = tkSimpleDialog.askinteger('Move right', 'How far right? '
                                                             '(1-5):')
        if movervalue < 1 or movervalue > 5 or movervalue == "":
            moveright()
        else:
            chan = "#playslimes"
            msg = ("!mover %s".encode("utf-8") % movervalue)
            s.send("PRIVMSG {} :{}\r\n".format(chan, msg))
            global statusmsg
            statusmsg = "Your slime is moving right %s units..." % movervalue
            printstatus()
    else:
        notconnected()


def jumpleft():
    # Check to see if user is logged in before allowing them to jump left
    global loggedin
    if loggedin is True:
        chan = "#playslimes"
        msg = ("!jumpl".encode("utf-8"))
        s.send("PRIVMSG {} :{}\r\n".format(chan, msg))
        global statusmsg
        statusmsg = "Jumping your slime left..."
        printstatus()
    else:
        notconnected()


def jumpright():
    # Check to see if user is logged in before allowing them to jump right
    global loggedin
    if loggedin is True:
        chan = "#playslimes"
        msg = ("!jumpr".encode("utf-8"))
        s.send("PRIVMSG {} :{}\r\n".format(chan, msg))
        global statusmsg
        statusmsg = "Jumping your slime right..."
        printstatus()
    else:
        notconnected()


def aim():
    # Check if the user is logged in before allowing them to aim
    # If they enter an invalid int, don't accept it
    global loggedin
    if loggedin is True:
        aimdegree = tkSimpleDialog.askinteger('AIM', 'Set your aim (1-360):')
        if aimdegree < 1 or aimdegree > 360 or aimdegree == "":
            aim()
        else:
            chan = "#playslimes"
            msg = ("!aim %s".encode("utf-8") % aimdegree)
            s.send("PRIVMSG {} :{}\r\n".format(chan, msg))
            global statusmsg
            statusmsg = "Your slime is aiming at %s..." % aimdegree
            printstatus()
    else:
        notconnected()


def shoot():
    # Check if the user is logged in before allowing them to shoot
    # If they enter an invalid int, don't accept it
    global loggedin
    if loggedin is True:
        shotpower = tkSimpleDialog.askinteger('AIM', 'Shot power (1-3):')
        if shotpower < 1 or shotpower > 3 or shotpower == "":
            shoot()
        else:
            chan = "#playslimes"
            msg = ("!shoot %s".encode("utf-8") % shotpower)
            s.send("PRIVMSG {} :{}\r\n".format(chan, msg))
            global statusmsg
            statusmsg = "Your slime will shoot at %s..." % shotpower
            printstatus()
    else:
        notconnected()


def connect():
    # Gets nick and pass from user and opens a socket to send commands over IRC
    nick = tkSimpleDialog.askstring('Twitch Username', 'Twitch Username:')
    password = tkSimpleDialog.askstring('OAuthToken Prompt', 'OAuth Token:')
    if nick == "" or nick is None or password == "" or password is None:
        tkMessageBox.showwarning('Info!', 'Username and/or password was left '
                                          'blank!')
        connect()
    else:
        # Set socket mode to blocking to true to destroy messages
        s.setblocking(True)
        chan = "#playslimes"
        host = "irc.chat.twitch.tv"
        port = 6667
        # Connect to the IRC server, opening a socket
        s.connect((host, port))
        # Send our credentials to the server and login
        s.send("PASS {}\r\n".format(password).encode("utf-8"))
        s.send("NICK {}\r\n".format(nick).encode("utf-8"))
        s.send("JOIN {}\r\n".format(chan).encode("utf-8"))
        global statusmsg
        statusmsg = "Sending credentials to Twitch and logging you in..."
        global loggedin
        loggedin = True
        tkMessageBox.showwarning('Info!', 'Connected to Twitch.tv!')

# Tkinter window instantiation and geometry
root = Tk()
root.wm_title('Slimes Remote')
root.geometry('200x130')

# Create the tkinter label text for OAuthToken
menu = Menu(root)
menu1 = Menu(root)
entry_1 = Entry(root)
entry_2 = Entry(root)
root.config(menu=menu)

# Create the submenu items for the user to click
subMenu = Menu(menu)
menu.add_cascade(label='Actions', menu=subMenu)
subMenu.add_command(label='Get OAuth Token', command=getauthtoken)
subMenu.add_command(label='Connect', command=connect)

subMenu = Menu(menu1)
menu.add_cascade(label='Help', menu=subMenu)
subMenu.add_command(label='Version', command=getversion)
subMenu.add_command(label='Github', command=github)
subMenu.add_command(label='About', command=about)

# Create the tkinter buttons
button_1 = Button(root, text='Join Game', command=joingame)
button_1.grid(row=1, column=2)

button_2 = Button(root, text='Jump Left', command=jumpleft)
button_2.grid(row=3, column=1)

button_3 = Button(root, text='Jump Right', command=jumpright)
button_3.grid(row=3, column=3)

button_4 = Button(root, text='Move Left', command=moveleft)
button_4.grid(row=4, column=1)

button_5 = Button(root, text='Move Right', command=moveright)
button_5.grid(row=4, column=3)

button_6 = Button(root, text='Set Aim', command=aim)
button_6.grid(row=5, column=2)

button_7 = Button(root, text='Shoot!', command=shoot)
button_7.grid(row=6, column=2)

# Create the status bar that prints the debug information
# Code here

# Run the tkinter window
root.mainloop()