def load(): """Setups the database upon sp load. Makes sure there are heroes on the server, restarts the game and setups the database file. Raises: NotImplementedError: When there are no heroes """ heroes = Hero.get_subclasses() if not heroes: raise NotImplementedError('No heroes on the server.') if not cfg.starting_heroes: raise NotImplementedError('No starting heroes set.') for cid in cfg.starting_heroes: if not find_element(heroes, 'cid', cid): raise ValueError('Invalid starting hero cid: {0}'.format(cid)) # Setup database setup() # Restart the game engine_server.server_command('mp_restartgame 1\n') # Send a message to everyone other_messages['Plugin Loaded'].send()
def _buy_hero_categories_build_callback(menu, player_index): """Buy Hero Categories menu's build_callback function.""" player = Player(player_index) menu.entities = [] for hero_cls in Hero.get_subclasses(): if find_element(player.heroes, 'cid', hero_cls.cid): continue elif (hero_cls.allowed_players and player.steamid not in hero_cls.allowed_players): continue menu.entities.append(hero_cls) menu.clear() categories = dict() for entity in menu.entities: if entity.category not in categories: categories[entity.category] = [entity] else: categories[entity.category].append(entity) for category in categories: menu.append(PagedOption('{category} ({size})'.format( category=category, size=len(categories[category]) ), categories[category] ))
def __init__(self, index): """Initializes a new player instance. Args: index: Index of the player """ super().__init__(index) # Create player's data dict if self.userid not in Player._data: Player._data[self.userid] = { 'gold': 0, 'hero': None, 'heroes': [], 'restrictions': set() } # Load player's data load_player_data(self) # Make sure the player gets his starting heroes heroes = Hero.get_subclasses() for cid in starting_heroes: hero_cls = find_element(heroes, 'cid', cid) if hero_cls and not find_element(self.heroes, 'cid', cid): self.heroes.append(hero_cls()) # Make sure the player has a hero if not self.hero: self.hero = self.heroes[0]
def load_player_data(player): """Loads player's data from the database. Args: player: player whose data to load """ with closing(connection.cursor()) as cursor: cursor.execute("SELECT gold, hero_cid FROM players WHERE steamid=?", (player.steamid, )) gold, current_hero_cid = cursor.fetchone() or (0, None) player.gold = gold # Load player's heroes cursor.execute("SELECT cid, level, exp FROM heroes WHERE steamid=?", (player.steamid, )) heroes = cursor.fetchall() # Load heroes data hero_classes = Hero.get_subclasses() for cid, level, exp in heroes: hero_cls = find_element(hero_classes, 'cid', cid) if hero_cls: hero = hero_cls(level, exp) load_hero_data(player.steamid, hero) player.heroes.append(hero) if cid == current_hero_cid: player.hero = hero
def _buy_hero_categories_build_callback(menu, player_index): """Buy Hero Categories menu's build_callback function.""" player = Player(player_index) menu.entities = [] for hero_cls in Hero.get_subclasses(): if find_element(player.heroes, 'cid', hero_cls.cid): continue elif (hero_cls.allowed_players and player.steamid not in hero_cls.allowed_players): continue menu.entities.append(hero_cls) menu.clear() categories = dict() for entity in menu.entities: if entity.category not in categories: categories[entity.category] = [entity] else: categories[entity.category].append(entity) for category in categories: menu.append( PagedOption( '{category} ({size})'.format(category=category, size=len(categories[category])), categories[category]))
def load_player_data(player): """Loads player's data from the database. Args: player: player whose data to load """ with closing(connection.cursor()) as cursor: cursor.execute("SELECT gold, hero_cid FROM players WHERE steamid=?", (player.steamid,)) gold, current_hero_cid = cursor.fetchone() or (0, None) player.gold = gold # Load player's heroes cursor.execute("SELECT cid, level, exp FROM heroes WHERE steamid=?", (player.steamid,)) heroes = cursor.fetchall() # Load heroes data hero_classes = Hero.get_subclasses() for cid, level, exp in heroes: hero_cls = find_element(hero_classes, "cid", cid) if hero_cls: hero = hero_cls(level, exp) load_hero_data(player.steamid, hero) player.heroes.append(hero) if cid == current_hero_cid: player.hero = hero