def setup_database(self): """ Sets up either a sqlite database or a GAE DataStore depending on configuration. """ datahandler = self._cfg.get('LOCAL', 'datahandler') if datahandler == 'sqlite': from AGSQLHandler import AGSQLHandler self._dh = AGSQLHandler(self._debug) elif datahandler == 'datastore': from AGDSHandler import AGDSHandler self._dh = AGDSHandler(self._debug) else: raise GoldFrameConfigException, "Unknown datahandler: %s" % datahandler
class Game(GoldFrame.GamePlugin): _gamedata = None _basepath = None _datafile = None _cfg = None assassin = None _datafile = 'assassinsgreed.dat' _alive_actions = { 'hidden': ['reroll'], 'active': ['assassinate', 'fight', 'heal', 'buy', 'collect', 'climb'], } _dead_actions = { 'active': ['reroll'], 'hidden': ['assassinate', 'fight', 'heal', 'buy', 'collect', 'climb'], } metadata = { 'name': "Assassin's Greed", 'gamekey': 'assassinsgreed', 'personal_hero': True, 'broadcast_actions': [], 'actions': [ { 'key': 'assassinate', 'name': 'Assassinate', 'description': 'Find a villain to assassinate.', 'img': '/images/assassinsgreed/icon-assassinate.png', 'tinyimg': '/images/assassinsgreed/tiny-icon-assassination.png', 'color': '#C30017', 'button': 'active', }, { 'key': 'fight', 'name': 'Fight', 'description': 'Find a villain to assassinate.', 'img': '/images/assassinsgreed/icon-fight.png', 'tinyimg': '/images/assassinsgreed/tiny-icon-fight.png', 'color': '#C30017', 'button': 'active', }, { 'key': 'heal', 'name': 'Heal', 'description': 'Heal to regain some health.', 'img': '/images/assassinsgreed/icon-rest.png', 'tinyimg': '/images/assassinsgreed/tiny-icon-health.png', 'color': '#004C7B', 'button': 'active', }, { 'key': 'buy', 'name': 'Buy', 'description': 'Buy items from a local merchant.', 'img': '/images/assassinsgreed/icon-buy.png', 'tinyimg': '/images/assassinsgreed/tiny-icon-buy.png', 'color': '#C30017', 'button': 'active', 'arguments': [ { 'type': 'list', 'key': 'item', 'name': 'Item', 'description': 'Item to buy', 'items': ['Potion', 'Smokebomb', 'Splinterbomb', 'Dagger'], }, { 'type': 'input', 'key': 'amount', 'name': 'Amount', 'description': 'Amount of items to buy', }, ] }, { 'key': 'collect', 'name': 'Collect Feathers', 'description': 'Search for feathers.', 'img': '/images/assassinsgreed/icon-collect.png', 'tinyimg': '/images/assassinsgreed/tiny-icon-feathers.png', 'color': '#E9B700', 'button': 'active', }, { 'key': 'climb', 'name': 'Climb', 'description': 'Climb a tower in the city.', 'img': '/images/assassinsgreed/icon-climb.png', 'tinyimg': '/images/assassinsgreed/tiny-icon-towers.png', 'color': '#351E00', 'button': 'active', }, { 'key': 'reroll', 'name': 'Reroll', 'description': 'Reroll a new assassin if the current is dead.', 'img': '/images/assassinsgreed/icon-reroll.png', 'tinyimg': '/images/assassinsgreed/tiny-icon-reroll.png', 'button': 'button', }, { 'key': 'stats', 'name': 'Stats', 'description': 'Update character sheet.', 'button': 'hidden', }, ], 'stats_img': '/images/assassinsgreed/icon-stats.png', 'stats': [ { 'key': 'name', 'name': 'Name', 'description': '', 'type': 'string', }, { 'key': 'strength', 'name': 'Strength', 'description': '', 'type': 'integer', 'img': '/images/assassinsgreed/tiny-icon-strength.png', }, { 'key': 'health', 'name': 'Health', 'description': '', 'type': 'integer', 'img': '/images/assassinsgreed/tiny-icon-health.png', }, { 'key': 'hurt', 'name': 'Hurt', 'description': '', 'type': 'integer', 'img': '/images/assassinsgreed/tiny-icon-hurt.png', }, { 'key': 'towers', 'name': 'Towers', 'description': '', 'type': 'integer', 'img': '/images/assassinsgreed/tiny-icon-towers.png', }, { 'key': 'kills', 'name': 'Kills', 'description': '', 'type': 'integer', 'img': '/images/assassinsgreed/tiny-icon-kills.png', }, { 'key': 'assassinations', 'name': 'Assassinations', 'description': '', 'type': 'integer', 'img': '/images/assassinsgreed/tiny-icon-assassinations.png', }, { 'key': 'feathers', 'name': 'Feathers', 'description': '', 'type': 'integer', 'img': '/images/assassinsgreed/tiny-icon-feathers.png', }, { 'key': 'potions', 'name': 'Potions', 'description': 'Health potions.', 'type': 'integer', 'img': '/images/assassinsgreed/tiny-icon-potions.png', }, { 'key': 'smokebombs', 'name': 'Smokebombs', 'description': 'Smokebombs', 'type': 'integer', 'img': '/images/assassinsgreed/tiny-icon-smokebombs.png', }, { 'key': 'splinterbombs', 'name': 'Splinterbombs', 'description': 'Splinterbombs', 'type': 'integer', 'img': '/images/assassinsgreed/tiny-icon-splinterbombs.png', }, { 'key': 'daggers', 'name': 'Daggers', 'description': 'Daggers', 'type': 'integer', 'img': '/images/assassinsgreed/tiny-icon-daggers.png', }, { 'key': 'alive', 'name': 'Alive', 'type': 'boolean', 'description': '', }, ], 'extra_info': { 'hurt_in_fight': { 'name': 'Hurt', 'cls': 'hurtInfo', }, 'hurt_by_action': { 'name': 'Hurt', 'cls': 'hurtInfo', }, 'healed': { 'name': 'Healed', 'cls': 'restInfo', }, 'gold': { 'name': 'Gold', 'cls': 'lootInfo', }, 'towers': { 'name': 'Towers', 'style': 'color: #351E00', }, 'feathers': { 'name': 'Feathers', 'style': 'color: #666', }, 'dinged': { 'name': 'Dinged', 'style': 'color: teal', }, 'item': { 'name': 'Item', 'style': 'color: green', }, 'total_price': { 'name': 'Price', 'style': 'color: mediumorchid', }, }, } _items = { 'Potion': { 'key': 'potions', 'price': 10 }, 'Smokebomb': { 'key': 'smokebombs', 'price': 25 }, 'Splinterbomb': { 'key': 'splinterbombs', 'price': 15 }, 'Dagger': { 'key': 'daggers', 'price': 10 }, } def template_charsheet(self): return """ <h1 id="nameValue" class="nameValue">{{ name }}</h1> <ul class="charsheetList"> <li class="statItem" id="strengthStatDiv"><img src="/images/assassinsgreed/tiny-icon-strength.png" width="16" height="16" alt="Strength" title="Strength" /><span class="statValue" id="strengthValue">{{ strength }}</span></li> <li class="statItem" id="healthStatDiv"><img src="/images/assassinsgreed/tiny-icon-health.png" width="16" height="16" alt="Health" title="Health" /><span class="statValue" id="hurthealthValue">{{ current_health }}/{{ health }}</span></li> <li class="statItem" id="towersStatDiv"><img src="/images/assassinsgreed/tiny-icon-towers.png" width="16" height="16" alt="Towers" title="Towers" /><span class="statValue" id="towersValue">{{ towers }}</span></li> <li class="statItem" id="assassinationsStatDiv"><img src="/images/assassinsgreed/tiny-icon-assassinations.png" width="16" height="16" alt="Assassinations" title="Assassinations" /><span class="statValue" id="assassinationsValue">{{ assassinations }}</span></li> <li class="statItem" id="killsStatDiv"><img src="/images/assassinsgreed/tiny-icon-kills.png" width="16" height="16" alt="Kills" title="Kills" /><span class="statValue" id="killsValue">{{ kills }}</span></li> <li class="statItem" id="feathersStatDiv"><img src="/images/assassinsgreed/tiny-icon-feathers.png" width="16" height="16" alt="Feathers" title="Feathers" /><span class="statValue" id="feathersValue">{{ feathers }}</span></li> <li class="statItem" id="goldStatDiv"><img src="/images/assassinsgreed/tiny-icon-gold.png" width="16" height="16" alt="Gold" title="Gold" /><span class="statValue" id="lootValue">{{ gold }}</span></li> </ul> """ def template_actionline(self): return "<li class='actionLine {{ cls }}' id='action_{{ id }}'>{{ line }}{{ extraInfo }}</li>" def setup(self): # Configure datahandler backend. #self.setup_database() # Read saved assassin. self.get_assassin(self._userid) @LogUsageCPU def setup_database(self): """ Sets up either a sqlite database or a GAE DataStore depending on configuration. """ datahandler = self._cfg.get('LOCAL', 'datahandler') if datahandler == 'sqlite': from AGSQLHandler import AGSQLHandler self._dh = AGSQLHandler(self._debug) elif datahandler == 'datastore': from AGDSHandler import AGDSHandler self._dh = AGDSHandler(self._debug) else: raise GoldFrameConfigException, "Unknown datahandler: %s" % datahandler @LogUsageCPU def get_assassin(self, userid=None): self.assassin = self._dh.get_alive_assassin(userid) def get_boss(self, lvl=None): if not lvl: lvl = self.assassin.towers or 1 boss = random.choice(self._gamedata['boss']) target = Target(lvl, boss, True) return target def get_target(self, lvl=None): if not lvl: lvl = self.assassin.towers or 1 targets = [] for target in self._gamedata['target']: if lvl >= target['lowlevel'] and (target['highlevel'] == 0 or lvl <= target['highlevel']): targets.append(target['name']) if targets: name = random.choice(targets) else: name = None # TODO: Bosses need to be supported. target = Target(self.assassin.towers, name) return target def play(self, command, asdict=False, arguments=None): # Load user assassin if needed. if self.assassin == None or self.assassin.userid != self._userid: self.get_assassin(self._userid) logging.info(arguments) # Handle action command. response = None actionbuttons = None if command in ['reroll']: response = self.action_reroll(arguments) actionbuttons = self._alive_actions elif not self.assassin or not self.assassin.alive: msg = self.get_text('nochampion') response = { 'message': msg, 'success': 0, } else: try: func = getattr(self, 'action_%s' % command) except AttributeError: return None else: response = func(arguments) self.save_data() if not self.assassin or not self.assassin.alive: actionbuttons = self._dead_actions return self.return_response(response, asdict, actionbuttons) def save_data(self): if self.assassin: self._dh.save_data(self.assassin) def get_alive_assassin(self): assassin = self._dh.get_alive_assassin() return assassin def action_reroll(self, aguments): if self.assassin and self.assassin.alive: response = { 'message': self.get_text('noreroll') % self.assassin.get_attributes(), 'success': 0, } return response else: # Reroll new assassin. self.assassin = Assassin(self._gamedata['assassin'], userid=self._userid) self.assassin.reroll() attribs = self.assassin.get_attributes() logging.info(attribs) msg = self.get_text('newassassin') try: msg = msg % attribs except KeyError, e: logging.error("Couldn't find a given text replacement: %s" % str(e)) response = { 'message': msg, 'data': { 'hero': attribs, } } return response