view gamelib/game_base.py @ 145:53277724645b

Science button juggling.
author Jeremy Thurgood <firxen@gmail.com>
date Fri, 11 May 2012 14:58:00 +0200
parents 245ef50de84d
children
line wrap: on
line source

import sys
import os


def get_subclasses(base_class, leaf_only=True):
    subclasses = []
    for cls in base_class.__subclasses__():
        if leaf_only and cls.__subclasses__():
            # Not a leaf class, and only want leaves
            continue
        if hasattr(cls, 'sanity_check'):
            cls.sanity_check()
        subclasses.append(cls)
    return subclasses


def get_save_filename():
    """Determine the base filename for auto saves"""
    app = "sypikslang"
    if sys.platform.startswith('win'):
        if "APPDATA" in os.environ:
            base = os.path.join(os.environ["APPDATA"], app)
        else:
            base = os.path.join(os.path.expanduser("~"), "." + app)
    elif 'XDG_DATA_HOME' in os.environ:
        base = os.path.join(os.environ["XDG_DATA_HOME"], app)
    else:
        base = os.path.join(os.path.expanduser("~"), ".local", "share", app)
    if not os.path.exists(base):
        os.makedirs(base, mode=0770)
    if os.path.isdir(base):
        return os.path.join(base, 'gamestate.json')
    print 'save game directory is not a directory %s' % base
    return None


class Science(object):
    NAME = None
    PREREQUISITES = ()
    ACQUISITION_CHANCE = 1.0
    SCIENCE_TYPE = None
    IMAGE_NAME = None

    def __init__(self, points=0):
        self.points = points

    def get_image_name(self):
        if self.IMAGE_NAME is not None:
            return self.IMAGE_NAME
        return type(self).__name__.lower()

    def spend_point(self):
        self.points += 1

    def can_spend(self, lab, spend):
        return True

    @classmethod
    def depends_on(self, sciences):
        for science_class, _ in self.PREREQUISITES:
            if any(isinstance(science, science_class) for science in sciences):
                return True
        return False

    @classmethod
    def save_name(cls):
        return "%s.%s" % (cls.SCIENCE_TYPE, cls.__name__)

    def save_data(self):
        return (self.save_name(), self.points)

    @classmethod
    def sanity_check(cls):
        for science, points in cls.PREREQUISITES:
            assert issubclass(science, Science)
            assert isinstance(points, int)