diff --git a/battleman.py b/battleman.py index 56311bc..2d00da3 100755 --- a/battleman.py +++ b/battleman.py @@ -14,7 +14,9 @@ from dice import Dice +import cPickle as pickle import sys +import os.path class CombatGroup(): @@ -547,9 +549,21 @@ class Battle(): return None + battle = Battle() +### This is the pickling jar +battle_pickle = None +bp_io_failed = False +BP_FILE = os.path.expanduser('~/.config/4etools/battleman/battle.pickle') +### + + def main(): + # Make sure config directory exists + if not os.path.exists(os.path.dirname(BP_FILE)): + os.makedirs(os.path.dirname(BP_FILE)) + # hard-coding test cases for now. # Eventually, use a state-saving text file that's easy to edit battle.add_group(CombatGroup("Adele", [Combatant("Adele", hp=26, pc=True, surges=8, sw=1)], 2)) @@ -561,8 +575,9 @@ def main(): battle.add_group(CombatGroup("Orcs of Baz", [Combatant("Orc", hp=32), Combatant("Orc", hp=32)], 1)) print "Welcome to 4e Battle Manager.\n" - print battle + print battle + while True: do_prompt() @@ -616,6 +631,22 @@ def do_prompt(): elif comm == 'q': sys.exit(0) + # Re-pickle and write if changed after every query. It's cheap + # and we only have to run at user-speed anyway + global battle_pickle + + old_bp = battle_pickle + battle_pickle = pickle.dumps(battle) + + if old_bp != battle_pickle: + try: + with open(BP_FILE, 'w') as f: + f.write(battle_pickle) + except: + if not bp_io_failed: + print("Warning: can't write the battle pickle. Resuming later will fail.") + bp_io_failed = True + def do_help(): print("""Possible commands: