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:
parent
593c9d2937
commit
af3988cc1a
33
battleman.py
33
battleman.py
|
@ -14,7 +14,9 @@
|
|||
|
||||
|
||||
from dice import Dice
|
||||
import cPickle as pickle
|
||||
import sys
|
||||
import os.path
|
||||
|
||||
|
||||
class CombatGroup():
|
||||
|
@ -465,9 +467,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))
|
||||
|
@ -479,8 +493,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()
|
||||
|
||||
|
@ -532,6 +547,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:
|
||||
|
|
Loading…
Reference in New Issue
Block a user