battleman.py: Added code to write pickle data for the battle... no way to read it back in yet though.

This commit is contained in:
Anna Rose 2012-03-29 17:07:18 -04:00
parent 593c9d2937
commit af3988cc1a

View File

@ -14,7 +14,9 @@
from dice import Dice from dice import Dice
import cPickle as pickle
import sys import sys
import os.path
class CombatGroup(): class CombatGroup():
@ -465,9 +467,21 @@ class Battle():
return None return None
battle = Battle() 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(): 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. # hard-coding test cases for now.
# Eventually, use a state-saving text file that's easy to edit # 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)) battle.add_group(CombatGroup("Adele", [Combatant("Adele", hp=26, pc=True, surges=8, sw=1)], 2))
@ -479,6 +493,7 @@ def main():
battle.add_group(CombatGroup("Orcs of Baz", [Combatant("Orc", hp=32), Combatant("Orc", hp=32)], 1)) battle.add_group(CombatGroup("Orcs of Baz", [Combatant("Orc", hp=32), Combatant("Orc", hp=32)], 1))
print "Welcome to 4e Battle Manager.\n" print "Welcome to 4e Battle Manager.\n"
print battle print battle
while True: while True:
@ -532,6 +547,22 @@ def do_prompt():
elif comm == 'q': elif comm == 'q':
sys.exit(0) 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(): def do_help():
print("""Possible commands: print("""Possible commands: