Skip to content

Instantly share code, notes, and snippets.

@starthal
Last active August 29, 2015 13:56
Show Gist options
  • Save starthal/9128329 to your computer and use it in GitHub Desktop.
Save starthal/9128329 to your computer and use it in GitHub Desktop.
Cookie Clicker Control Module
#!/usr/bin/env python
# Cookie Clicker Python Control module
# Author: starthal
# Version 1.0
# This requires the use of Firefox and the extension "Remote Control"
# https://addons.mozilla.org/en-US/firefox/addon/remote-control/
# Be sure to turn the remote on using the toolbar button!
# Cookie Clicker "API" reference: http://cookieclicker.wikia.com/wiki/Cheating
import socket
import sys
import time
TCP_IP = '127.0.0.1'
TCP_PORT = 32000 # You can change this port in the FF Remote Control settings
SCROLL_AMOUNT = 150 # Amount to scroll on scroll_up and scroll_down commands
# Dict of building IDs.
BLDGS = {
'cursor': 0,
'grandma': 1,
'farm': 2,
'factory': 3,
'mine': 4,
'shipment': 5,
'lab': 6,
'portal': 7,
'time': 8,
'antimatter': 9,
'prism': 10,
}
# Backs up the save string (use before a reset)
def backup_save(save_str):
save_f = open('twitch_cc_save_backup.txt', 'a')
save_f.write(time.asctime() + '\n' + save_str + '\n\n')
save_f.close()
# Connect to FF-Remote
def init_control():
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((TCP_IP, TCP_PORT))
except:
print('Could not connect. Make sure FF Remote Control is running on port 32000')
sock.close()
sys.exit(1)
print('Connected to Firefox')
return sock
def deinit_control(sock):
sock.close()
def click_cookie(sock):
sock.send('Game.ClickCookie()')
def click_golden(sock):
sock.send('Game.goldenCookie.click()')
def click_reindeer(sock):
sock.send('Game.seasonPopup.click()')
def buy_building(sock, bldg_name):
bldg_id = BLDGS[bldg_name]
#print('Building id is {0}'.format(bldg_id))
sock.send('Game.ObjectsById[{0}].buy()'.format(bldg_id))
def sell_building(sock, bldg_name):
bldg_id = BLDGS[bldg_name]
#print('Building id is {0}'.format(bldg_id))
sock.send('Game.ObjectsById[{0}].sell()'.format(bldg_id))
def buy_upgrade(sock, upgrade_index):
# First upgrade has index 0
sock.send('Game.UpgradesInStore[{0}].buy()'.format(upgrade_index))
def pop_all_wrinklers(sock)
sock.send('Game.CollectWrinklers()')
def scroll_down(sock):
sock.send('document.getElementById("sectionMiddle").scrollTop+={0}'.format(SCROLL_AMOUNT))
def scroll_up(sock):
sock.send('document.getElementById("sectionMiddle").scrollTop-={0}'.format(SCROLL_AMOUNT))
def toggle_stats(sock):
sock.send('Game.ShowMenu("stats")')
def soft_reset(sock):
# Export save.
# The indexing [11:-3] strips the JSON wrapper
sock.send('Game.WriteSave(1)')
save_str = sock.recv(1024)[11:-3]
backup_save(save_str)
# Passing the argument 1 bypasses the confirmation dialog
sock.send('Game.Reset(1)')
# Main routine for testing
if __name__ == "__main__":
s = init_control()
while True:
print('Clicking cookie')
click_cookie(s)
time.sleep(2)
# print('Buying farm')
# buy_building(s, 'farm')
# time.sleep(5)
print ('Scrolling down')
scroll_down(s)
time.sleep(2)
# Clean up
deinit_control(s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment