Trysdyn

Copy Kitty Marathon auto-splitter

Feb 6th, 2015
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.19 KB | None | 0 0
  1. # Copy Kitty 1.8 IGT auto-splitter
  2. # Utilizes LiveSplit and its Server component to auto-split a Copy Kitty run
  3. # Built for version 1.8 Turbo.
  4.  
  5. # Caveat: This script does not detect the final split at the end of a run. That must be handled manually. Sorry!
  6.  
  7. import time
  8. import struct
  9. import socket
  10. from watchdog.observers import Observer
  11. from watchdog.events import FileSystemEventHandler
  12.  
  13.  
  14. class BokiHandler(FileSystemEventHandler):
  15.     # Data locations in the bytestream in the savefile
  16.     handle = {"stage": 4, "second": 8, "minute": 10, "hour": 12}
  17.  
  18.     # Stages that come after bosses
  19.     bosses = [27, 37, 47, 57, 72, 82, 92, 103, 112, 127]
  20.  
  21.     def on_modified(self, event):
  22.         # If the marathon savefile got edited, parse it
  23.         if event.src_path.endswith("boki.kittymarathon"):
  24.             data = self.parse_savefile(event.src_path)
  25.  
  26.             # If we didn't beat a boss stage, stop processing
  27.             if not data["stage"] in self.bosses:
  28.                 return None
  29.  
  30.             # From here on is where you'd do whatever you want to handle
  31.             # the data. I make LiveSplit begin a new split.
  32.  
  33.             # Figure out how many seconds in gametime we're in
  34.             sec = ((data["hour"] * 3600) +
  35.                    (data["minute"] * 60) +
  36.                    data["second"])
  37.  
  38.             # When the first boss is beaten, start the timer
  39.             # This makes RTA timing useless but IGT is better anyway
  40.             if data["stage"] == self.bosses[0]:
  41.                 s.send("starttimer\r\n")
  42.  
  43.             # Send the split command to livesplit
  44.             print "Sending split at %02i:%02i:%02i" % (data["hour"],
  45.                                                        data["minute"],
  46.                                                        data["second"])
  47.             s.send("setgametime %i\r\n" % sec)
  48.             s.send("split\r\n")
  49.  
  50.     def parse_savefile(self, filename):
  51.         # Datafile is 39 4-byte ints, parse as such
  52.         with open(filename, 'rb') as f:
  53.             data = struct.unpack(">" + "H" * 39, f.read())
  54.  
  55.         # Use the information on handle to construct a data dict
  56.         packed_data = {}
  57.         for k, v in self.handle.items():
  58.             packed_data[k] = data[v]
  59.  
  60.         # Return said dict
  61.         return packed_data
  62.  
  63.  
  64. if __name__ == "__main__":
  65.     s = socket.socket()
  66.  
  67.     # LiveSplit specific stuff. If you're doing something else with this
  68.     # script, do initial setup here.
  69.  
  70.     try:
  71.         s.connect(("localhost", 16834))
  72.         print "Connected to LiveSplit"
  73.     except:
  74.         print "Could not connect to LiveSplit. Is it running?"
  75.  
  76.     # End LiveSplit specific stuff
  77.  
  78.     # Here we set a hook to fire BokiHandler.on_modified when files
  79.     # in CopyKitty's data dir change.
  80.     i = "C:/Users/Trysdyn/AppData/Local/Apps/2.0/CopyKitty/"
  81.     event_handler = BokiHandler()
  82.     observer = Observer()
  83.     observer.schedule(event_handler, path=i, recursive=False)
  84.     observer.start()
  85.  
  86.     # Now we check once a second for a ctrl-C to terminate
  87.     try:
  88.         while True:
  89.             time.sleep(1)
  90.     except KeyboardInterrupt:
  91.         observer.stop()
  92.         s.close()
  93.     observer.join()
Advertisement
Add Comment
Please, Sign In to add comment