X-Git-Url: https://git.ctpug.org.za/?a=blobdiff_plain;f=naja%2Foptions.py;h=498a979d8ecae6299cbf4d5d280b89bd6bfb2a46;hb=HEAD;hp=816d91ca3c29f87329a2b8f6b9428d899e851280;hpb=eccb327f1260b9aa981755840e097d5b23af05a9;p=naja.git diff --git a/naja/options.py b/naja/options.py index 816d91c..498a979 100644 --- a/naja/options.py +++ b/naja/options.py @@ -1,5 +1,6 @@ import optparse import os +import sys from naja.attrdict import AttrDict from naja.constants import DEFAULTS @@ -8,6 +9,35 @@ from naja.constants import DEFAULTS options = AttrDict() +def load_game(parser, slot_num): + ''' + Load a save game and store it in parser.values.game_state. + ''' + from naja.scenes.load_save import SaveGameSlot + if not (0 <= slot_num <= 7): + parser.error("--load accepts a slot number from 0 to 7.") + state = SaveGameSlot(slot_num).load() + if state is None: + raise parser.error( + "Could not load game from slot %s" % (slot_num,)) + options.game_state = state + + +def load_deck(parser, deck): + ''' + Create a new game for a specific deck and store it in + parser.values.game_state. + ''' + from naja.gamestate import GameState + try: + state = GameState.new(deck=deck) + except: + if options.debug: + raise + parser.error("Could not load deck %r" % (deck,)) + options.game_state = state + + def parse_args(args): ''' Parse arguments and store them in the options dictionary. @@ -28,8 +58,43 @@ def parse_args(args): dest='music', action='store_false', default=True, help='Disable music (but not sound)') + parser.add_option("--save-location", default=_get_default_save_location(), + dest="save_location", help="Saved game location") + + parser.add_option('--deck', default=None, + help='Start with a new game for a specific deck' + ' (bypassing the menu).') + + parser.add_option('--load', default=None, type=int, + help='Start with a specific save game loaded' + ' (bypassing the menu). Slot values are 0-7.') + + if options.debug: + parser.add_option('--initial-bits', type=int, + help='Initial player bits') + parser.add_option('--cheat-enabled', default=False, + action='store_true', + help='For those too lazy to type the KONAMI code') + opts, _ = parser.parse_args(args) for k in DEFAULTS: if getattr(opts, k, None) is not None: options[k] = getattr(opts, k) + + if opts.load is not None: + load_game(parser, opts.load) + if opts.deck is not None: + load_deck(parser, opts.deck) + + +def _get_default_save_location(): + """Return a default save game location.""" + app = "naja" + if sys.platform.startswith("win"): + if "APPDATA" in os.environ: + return os.path.join(os.environ["APPDATA"], app) + return os.path.join(os.path.expanduser("~"), "." + app) + elif 'XDG_DATA_HOME' in os.environ: + return os.path.join(os.environ["XDG_DATA_HOME"], app) + return os.path.join(os.path.expanduser("~"), ".local", "share", app)