Add save-game loading.
[naja.git] / naja / options.py
index 816d91ca3c29f87329a2b8f6b9428d899e851280..d37f1be1d459861b37db7ff5552d6290b44f1170 100644 (file)
@@ -1,5 +1,6 @@
 import optparse
 import os
+import sys
 
 from naja.attrdict import AttrDict
 from naja.constants import DEFAULTS
@@ -8,6 +9,41 @@ from naja.constants import DEFAULTS
 options = AttrDict()
 
 
+def check_min_max(option, value, min_value, max_value):
+    '''
+    Check value lies between min and max and raise OptionValueError if it does
+    not.
+    '''
+    if not (min_value <= value <= max_value):
+        raise optparse.OptionValueError(
+            "Value of %s should be between %s and %s but got: %r"
+            % (option.dest, min, max, value))
+
+
+def load_game(option, opt_str, value, parser):
+    '''
+    Load a save game and store it in parser.values.game_state.
+    '''
+    check_min_max(option, value, 0, 7)
+    # madness takes its toll ...
+    options.save_location = parser.values.save_location
+    from naja.scenes.load_save import SaveGameSlot
+    state = SaveGameSlot(value).load()
+    if state is None:
+        raise optparse.OptionValueError(
+            "Could not load game from slot %s" % (value,))
+    parser.values.game_state = state
+
+
+def load_deck(option, opt_str, value, parser):
+    '''
+    Create a new game for a specific deck  and store it in
+    parser.values.game_state.
+    '''
+    raise optparse.OptionalValueError(
+        "Deck loading not implemented.")
+
+
 def parse_args(args):
     '''
     Parse arguments and store them in the options dictionary.
@@ -28,8 +64,36 @@ 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")
+
+    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')
+        parser.add_option('--deck', default=None, action="callback",
+                          callback=load_deck,
+                          help='Start with a new game for a specific deck')
+        parser.add_option('--load', default=None, type=int, action="callback",
+                          callback=load_game,
+                          help='Start with a specific save game loaded (0-7)')
+
     opts, _ = parser.parse_args(args)
 
     for k in DEFAULTS:
         if getattr(opts, k, None) is not None:
             options[k] = getattr(opts, k)
+
+
+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)