Exemple #1
0
        return item
    

class GameItem(Item):
    """GameItem represents a "real" in-game item used by characters.
     
    GameItems are what players use in-game, and are 'copies' of their BuildItem
    counterparts.
    """
    db_table_name = 'game_item'
    db_columns = Item.db_columns + [
        Column('build_area'),
        Column('build_id'),
        Column('container', type="INTEGER", write=write_model, foreign_key=(db_table_name, 'dbid'), cascade="ON DELETE"),
        Column('owner', type="INTEGER", write=write_model, foreign_key=('player', 'dbid'), cascade='ON DELETE')
    ]
    def __init__(self, args={}, spawn_id=None):
        self.spawn_id = spawn_id
        Item.__init__(self, args)
    
    def load_extras(self):
        for key, value in ITEM_TYPES.items():
            row = self.world.db.select('* FROM %s WHERE game_item=?' % key,
                                       [self.dbid])
            if row:
                row[0]['game_item'] = self
                self.item_types[key] = value(row[0])
    

model_list.register(BuildItem)
model_list.register(GameItem)
Exemple #2
0
        else:
            return 'That exit is linked. You should unlink it before you set it to somewhere else.\n'

    def build_set_key(self, args):
        if not args:
            return 'Usage: set exit <direction> key <item-id> from area <area-name>\n'
        exp = r'([ ]+)?(?P<key_id>\d+)(([ ]+in)?([ ]+area)?([ ]+(?P<area_name>\w+)))?'
        match = re.match(exp, ''.join(args), re.I)
        if not match:
            return 'Usage: set key <item-id> from area <area-name>\n'
        key_id, area_name = match.group('key_id', 'area_name')
        if area_name:
            area = self.world.get_area(area_name)
            if not area:
                return 'That area doesn\'t exist.\n'
        else:
            area = self.room.area
        key = area.get_item(key_id)
        if not key:
            return 'That item doesn\'t exist.\n'
        self.key = key
        self.save()
        if self.linked_exit:
            self.to_room.exits[self.linked_exit].key = self.key
            self.to_room.exits[self.linked_exit].save()
        return '%s has been added as a key to room %s\'s %s exit.\n' % (
            key.name.capitalize(), self.room.id, self.direction)


model_list.register(RoomExit)
Exemple #3
0
    db_table_name = 'game_item'
    db_columns = Item.db_columns + [
        Column('build_area'),
        Column('build_id'),
        Column('container',
               type="INTEGER",
               write=write_model,
               foreign_key=(db_table_name, 'dbid'),
               cascade="ON DELETE"),
        Column('owner',
               type="INTEGER",
               write=write_model,
               foreign_key=('player', 'dbid'),
               cascade='ON DELETE')
    ]

    def __init__(self, args={}, spawn_id=None):
        self.spawn_id = spawn_id
        Item.__init__(self, args)

    def load_extras(self):
        for key, value in ITEM_TYPES.items():
            row = self.world.db.select('* FROM %s WHERE game_item=?' % key,
                                       [self.dbid])
            if row:
                row[0]['game_item'] = self
                self.item_types[key] = value(row[0])


model_list.register(BuildItem)
model_list.register(GameItem)
    def pop(self, index):
        """Remove and return the item at the given index for self.live.
        If the index given is invalid, return None.
        """
        if (index > len(self.live)) or (index < 0):
            return None
        group = self.live.pop(index)
        return (self.verify_item(group), group.get("price"))

    def reset_dead(self):
        """Set the self.dead list back to an empty list.
        """
        self.dead = []


model_list.register(MerchandiseList)


class Merchant(NpcAiPack):
    help = ("""<title>Merchant (Npc AI Pack)
The Merchant AI pack is meant to give NPCs the ability to become merchants.
    """)
    plural_map = {'plain': 'plain items'}
    plural_map.update(
        dict([(key, val.plural) for key, val in ITEM_TYPES.items()]))
    db_table_name = 'merchant'
    db_columns = Model.db_columns + [
        Column('npc',
               foreign_key=('npc', 'dbid'),
               null=False,
               type='INTEGER',
Exemple #5
0
        keyword. If nothing in the room matches the keyword, return None.
        """
        # check the items in the room first
        item = self.get_item_by_kw(keyword)
        if item: return item
        
        # then check the npcs in the room
        npc = self.get_npc_by_kw(keyword)
        if npc: return npc
        
        # then check the PCs in the room
        player = self.get_player(keyword)
        if player: return player
        
        # If we didn't match any of the above, return None
        return None
    
    def purge_room(self):
        """Delete all objects and npcs in this room."""
        # When npcs are loaded into the room, they're not saved to the db
        # so we can just wipe the memory instances of them
        self.npcs = []
        # The items in the room may have been dropped by a player (and would
        # therefore have been in the game_item db table). We need
        # to make sure we delete the item from the db if it has an entry.
        for i in range(len(self.items)):
            self.item_purge(self.items[0])
    

model_list.register(Room)
Exemple #6
0
        """Add an ai pack to this npc via BuildMode."""
        if not args:
            return 'Try: "add ai <ai-pack-name>", or type "help ai packs".'
        if args in self.ai_packs:
            return 'This npc (%s) already has that ai pack.' % self.name
        if args in NPC_AI_PACKS:
            self.new_ai(args)
            return "This npc (%s) is now a %s." % (self.name, args)
        else:
            return '"%s" is not a valid ai pack. See "help ai packs".' % args

    def build_remove_ai(self, ai_type, player=None):
        """Remove an ai pack from this npc via BuildMode."""
        if not ai_type:
            return 'Try: "remove ai <ai-pack-name>", or type "help ai packs".'
        if not ai_type in self.ai_packs:
            return 'This npc doesn\'t have the "%s" ai type.' % ai_type
        pack = self.ai_packs.pop(ai_type)
        pack.destruct()
        return 'Npc %s (%s) no longer has %s ai.' % (self.id, self.name,
                                                     ai_type)

    def new_ai(self, ai_pack, args={}):
        """Add a new ai pack to this npc."""
        args['npc'] = self
        pack = NPC_AI_PACKS[ai_pack](args)
        pack.save()
        self.ai_packs[ai_pack] = pack

model_list.register(Npc)
Exemple #7
0
        return 'Leave message set.'
    
    def build_set_entrance(self, message, player=None):
        """Set this portal's entrance message."""
        self.entrance_message = message
        self.save()
        return 'Entrance message set.'
    
    def build_set_emerge(self, message, player=None):
        """Set this portal's emerge message."""
        self.emerge_message = message
        self.save()
        return 'Emerge message set.'
    

class Book(ItemType):
    plural = 'books'
    pass


ITEM_TYPES = {'equippable': Equippable,
              'food': Food,
              'container': Container,
              'furniture': Furniture,
              'portal': Portal
              # 'book': Book
             }

for klass in ITEM_TYPES.values():
    model_list.register(klass)
Exemple #8
0
        self.spawn_object_area = val.area.name
    
    spawn_object = property(_get_spawn_obj, _set_spawn_obj)
    def add_nested_spawn(self, spawn):
        """Add another item to this object's "containee list"."""
        self.nested_spawns.append(spawn)
    
    def remove_nested_spawn(self, spawn):
        """Remove an item from this object's "containee list"."""
        if spawn in self.nested_spawns:
            self.nested_spawns.remove(spawn)
    
    # def destruct(self):
    #     world = World.get_world()
    #     if self.dbid:
    #         world.db.delete('FROM room_spawns WHERE dbid=?', [self.dbid])
    #         for spawn in self.nested_spawns:
    #             spawn.destruct()
    
    def spawn(self):
        """load the object"""
        new_object = self.spawn_object.load()
        new_object.spawn_id = self.get_spawn_id()
        for spawn in self.nested_spawns:
            container = new_object.item_types.get('container')
            container.item_add(spawn.spawn())
        return new_object
    

model_list.register(Spawn)
Exemple #9
0
        for effect in effect_list:
            effect.char = self
            if effect.name in self.effects:
                self.effects[effect.name].combine(effect)
                self.world.log.debug(effect.duration)
            else:
                self.effects[effect.name] = effect
                self.world.log.debug(effect.duration)
            
            self.effects[effect.name].begin()
    
    def effect_remove(self, effect):
        """Remove an effect from this player."""
        if effect.name in self.effects:
            del self.effects[effect.name]
    
    def death(self):
        # Send character to the default location, with 1 hp.
        self.hp = 1
        self.world.log.debug("%s has died." % self.fancy_name())
        self.update_output('You Died.')
        self.go(self.world.get_location(DEFAULT_LOCATION[0], DEFAULT_LOCATION[1]))
    
    def destruct(self):
        for item in self.inventory:
            item.destruct()
        Character.destruct(self)
    

model_list.register(Player)
Exemple #10
0
        Column('name', default='New Script'),
        Column('body', default=''),
        Column('id')
    ]        
    def __str__(self):
        string = (' Script %s in Area %s ' % (self.id, self.area.name)
                  ).center(50, '-') + '\n'
        body = '\n  '.join([line for line in self.body.split('\n')])
        if not body:
            body = 'Script is empty.'
        string += "Name: %s\nBody:\n  %s" % (self.name, body)
        string += '\n' + ('-' * 50)
        return string
        
    def build_set_name(self, name, player=None):
        """Set the name of this script item."""
        if not name:
            return 'Set the name of the the script to what?'
        self.name = name
        self.save()
        return 'Script %s\'s name has been set to "%s".' % (self.id, self.name)
    
    def build_set_body(self, body, player=None):
        """Set the body of this script item."""
        player.last_mode = player.mode
        player.mode = TextEditMode(player, self, 'body', self.body, 'script')
        return 'ENTERING TextEditMode: type "@help" for help.\n'
    

model_list.register(Script)
Exemple #11
0
        return script_list
    
    def new_script(self, script_dict=None):
        """Add a new script to this area's script list."""
        if script_dict:
            script_dict['area'] = self
            new_script = Script(script_dict)
        else:
            new_script = Script({'area': self, 'id': self.get_id('script')})
        new_script.save()
        self.scripts[str(new_script.id)] = new_script
        return new_script
    
    def get_script(self, script_id):
        """Return the script object associated with script_id (or None
        if the script doesn't exist)
        """
        return self.scripts.get(script_id)
    
    def destroy_script(self, script_id):
        s = self.get_script(script_id)
        if not s:
            return 'That script doesn\'t exist.'
        s.destruct()
        s.id = None
        del self.scripts[script_id]
        return 'Script "%s" has been successfully destroyed.' % script_id
    

model_list.register(Area)
Exemple #12
0
        self._script = s
        if s:
            self.script_id = s.id
            self.script_area = s.area.name

    script = property(_resolve_script, _set_script)

    def __str__(self):
        string = '%s' % self.event_trigger
        if self.condition:
            string += ' "%s"' % self.condition
        string += ' [script: %s, probability: %s%s]' % (self.script.id,
                                                        self.probability, '%')
        return string

    def get_args(self):
        """Build a dictionary of this event's attributes so that they can be
        passed as arguments to the event handler.
        """
        d = {}
        d['prototype'] = self.prototype
        d['script'] = self.script
        d['probability'] = self.probability
        d['event_trigger'] = self.event_trigger
        if self.condition:
            d['condition'] = self.condition
        return d


model_list.register(NPCEvent)
Exemple #13
0
        """Remove and return the item at the given index for self.live.
        If the index given is invalid, return None.
        """
        if (index > len(self.live)) or (index < 0):
            return None
        group = self.live.pop(index)
        return (self.verify_item(group), group.get("price"))
        
    
    def reset_dead(self):
        """Set the self.dead list back to an empty list.
        """
        self.dead = []
    

model_list.register(MerchandiseList)

class Merchant(NpcAiPack):
    help = (
    """<title>Merchant (Npc AI Pack)
The Merchant AI pack is meant to give NPCs the ability to become merchants.
    """
    )
    plural_map = {'plain':'plain items'}
    plural_map.update(dict([(key, val.plural) for key,val in ITEM_TYPES.items()]))
    db_table_name = 'merchant'
    db_columns = Model.db_columns + [
        Column('npc', foreign_key=('npc', 'dbid'), null=False, type='INTEGER',
               write=lambda npc: npc.dbid),
        Column('buyer', read=to_bool, default=True),
        Column('markup', read=read_float, type='NUMBER', default=1),
Exemple #14
0
            return 'That exit is linked. You should unlink it before you set it to somewhere else.\n'
    
    def build_set_key(self, args):
        if not args:
            return 'Usage: set exit <direction> key <item-id> from area <area-name>\n'
        exp = r'([ ]+)?(?P<key_id>\d+)(([ ]+in)?([ ]+area)?([ ]+(?P<area_name>\w+)))?'
        match = re.match(exp, ''.join(args), re.I)
        if not match:
            return 'Usage: set key <item-id> from area <area-name>\n'
        key_id, area_name = match.group('key_id', 'area_name')
        if area_name:
            area = self.world.get_area(area_name)
            if not area:
                return 'That area doesn\'t exist.\n'
        else:
            area = self.room.area
        key = area.get_item(key_id)
        if not key:
            return 'That item doesn\'t exist.\n'
        self.key = key
        self.save()
        if self.linked_exit:
            self.to_room.exits[self.linked_exit].key = self.key
            self.to_room.exits[self.linked_exit].save()
        return '%s has been added as a key to room %s\'s %s exit.\n' % (key.name.capitalize(),
                                                                        self.room.id,
                                                                        self.direction)
    

model_list.register(RoomExit)
Exemple #15
0
    def build_set_entrance(self, message, player=None):
        """Set this portal's entrance message."""
        self.entrance_message = message
        self.save()
        return 'Entrance message set.'

    def build_set_emerge(self, message, player=None):
        """Set this portal's emerge message."""
        self.emerge_message = message
        self.save()
        return 'Emerge message set.'


class Book(ItemType):
    plural = 'books'
    pass


ITEM_TYPES = {
    'equippable': Equippable,
    'food': Food,
    'container': Container,
    'furniture': Furniture,
    'portal': Portal
    # 'book': Book
}

for klass in ITEM_TYPES.values():
    model_list.register(klass)
Exemple #16
0
        script_list.append('-'.center(50, '-'))
        return script_list

    def new_script(self, script_dict=None):
        """Add a new script to this area's script list."""
        if script_dict:
            script_dict['area'] = self
            new_script = Script(script_dict)
        else:
            new_script = Script({'area': self, 'id': self.get_id('script')})
        new_script.save()
        self.scripts[str(new_script.id)] = new_script
        return new_script

    def get_script(self, script_id):
        """Return the script object associated with script_id (or None
        if the script doesn't exist)
        """
        return self.scripts.get(script_id)

    def destroy_script(self, script_id):
        s = self.get_script(script_id)
        if not s:
            return 'That script doesn\'t exist.'
        s.destruct()
        s.id = None
        del self.scripts[script_id]
        return 'Script "%s" has been successfully destroyed.' % script_id

model_list.register(Area)
Exemple #17
0
        if s:
            self.script_id = s.id
            self.script_area = s.area.name
    
    script = property(_resolve_script, _set_script)
    
    def __str__(self):
        string = '%s' % self.event_trigger
        if self.condition:
            string += ' "%s"' % self.condition
        string += ' [script: %s, probability: %s%s]' % (self.script.id,
                                                              self.probability,
                                                              '%')
        return string
    
    def get_args(self):
        """Build a dictionary of this event's attributes so that they can be
        passed as arguments to the event handler.
        """
        d = {}
        d['prototype'] = self.prototype
        d['script'] = self.script
        d['probability'] = self.probability
        d['event_trigger'] = self.event_trigger
        if self.condition:
            d['condition'] = self.condition
        return d
    

model_list.register(NPCEvent)
Exemple #18
0
        """Add an ai pack to this npc via BuildMode."""
        if not args:
            return 'Try: "add ai <ai-pack-name>", or type "help ai packs".'
        if args in self.ai_packs:
            return "This npc (%s) already has that ai pack." % self.name
        if args in NPC_AI_PACKS:
            self.new_ai(args)
            return "This npc (%s) is now a %s." % (self.name, args)
        else:
            return '"%s" is not a valid ai pack. See "help ai packs".' % args

    def build_remove_ai(self, ai_type, player=None):
        """Remove an ai pack from this npc via BuildMode."""
        if not ai_type:
            return 'Try: "remove ai <ai-pack-name>", or type "help ai packs".'
        if not ai_type in self.ai_packs:
            return 'This npc doesn\'t have the "%s" ai type.' % ai_type
        pack = self.ai_packs.pop(ai_type)
        pack.destruct()
        return "Npc %s (%s) no longer has %s ai." % (self.id, self.name, ai_type)

    def new_ai(self, ai_pack, args={}):
        """Add a new ai pack to this npc."""
        args["npc"] = self
        pack = NPC_AI_PACKS[ai_pack](args)
        pack.save()
        self.ai_packs[ai_pack] = pack


model_list.register(Npc)