Esempio n. 1
0
class Local(TextObject):

    def __init__(self, title, description):
        TextObject.__init__(self, title, description)
        self._locals = dict()
        self.commandmanager = CommandManager()

    @property
    def title(self):
        return self.name

    @title.setter
    def title(self, value):
        self.name = value

    def __eq__(self, other):
        return self.title == other.title

    def __ne__(self, other):
        return self.title != other.title

    def addLocal(self, direction, local):
        if direction.lower() not in DIRECTIONS:
            raise DirectionNotFoundException()
        if direction.lower() in self._locals:
            raise LocalAlreadyImplementException()
        self._locals[direction.lower()] = local
        if direction not in self._locals:
            local.addLocal(oppositedirection(direction), self)

    def getlocal(self, direction):
        if direction.lower() not in DIRECTIONS:
            return DIRECTION_NOT_VALID
        if direction.lower() not in self._locals:
            return DIRECTION_NOT_PERMITED
        return self._locals[direction.lower()]

    def addcommand(self, idcommand, command):
        self.commandmanager.addcommand(idcommand, command)

    def execute(self, command, arg):
        return self.commandmanager.execute(command, arg)
 def __init__(self, game, world, player, factory):
     self.player = player
     self.playerinventory = Inventory()
     self.playercommandmanager = CommandManager()
     self.world = world
     self.factory = factory
     self.framework = None
     self.playercommandmanager.addcommand(CommandConst.INV, Inv(None, self, self.framework))
     self._game = game
     self._currentLocal = None
     self._endingLocals = list()
Esempio n. 3
0
 def __init__(self, title, description):
     TextObject.__init__(self, title, description)
     self._locals = dict()
     self.commandmanager = CommandManager()
class Controller:
    """Control the interation between the game and the rest of the framework"""
    def __init__(self, game, world, player, factory):
        self.player = player
        self.playerinventory = Inventory()
        self.playercommandmanager = CommandManager()
        self.world = world
        self.factory = factory
        self.framework = None
        self.playercommandmanager.addcommand(CommandConst.INV, Inv(None, self, self.framework))
        self._game = game
        self._currentLocal = None
        self._endingLocals = list()

    @property
    def currentlocal(self):
        """Define the local where the player is"""
        return self._currentLocal

    @currentlocal.setter
    def currentlocal(self, value):
        if isinstance(value, Local):
            self._currentLocal = value
        else:
            raise IncorrectTypeException('Local')

    def addendinglocal(self, local):
        """Add the places where the game end"""
        self._endingLocals.append(local)

    def isendinglocal(self, local):
        """Check if a local is a ending place
        if is the game should stop"""
        return local in self._endingLocals

    def getlocal(self, title):
        """Return the local based in the title"""
        return self.world.getlocal(title)

    def additem(self, item):
        self.playerinventory.add(item)

    def hasitem(self, item):
        return item in self.playerinventory

    def removeitem(self, item):
        self.playerinventory.remove(item)

    def takeitem(self,item):
        return self.player.takeitem(item)

    def quantitem(self):
        return len(self.playerinventory)

    def inventory(self):
        return str(self.playerinventory)

    def getlocal(self,name):
        return self.world.getlocal(name)

    def endgame(self, message=None):
        self._game.endgame(message)

    def addcommand(self, local, idcommand, command):
        """Add a command in a local"""
        mylocal = self.world.getlocal(local) if isinstance(local, str) else local
        mycommand = command(mylocal, self,self.framework)
        mylocal.addcommand(idcommand, mycommand)

    def execute(self, command, args):
        """Execute the command"""
        return self.playercommandmanager.execute(command, args) if self.playercommandmanager.hascommand(
                command) else self.currentlocal.execute(command, args)