def on_win(self, transport, chat, user): Score(name=user.name, turns=user.turn, money=user.money, score=50*user.turn + user.money).save() transport.send_message( chat, self.messages['you_won'].format( user.turn, user.money, 50*user.turn + user.money)) self.highscores(transport, chat)
def start(self, transport, chat, user): user.current_location = 'home_sweet_home' location = self.load_location(user) user.save() transport.send_message( chat, self.messages['story'] + ' ' + location.description(user), keyboard=self.make_location_keyboard(user, location))
def highscores(self, transport, chat): highscores = Score.objects.order_by('-score')[:10] transport.send_message( chat, self.messages['highscores'].format( '\n'.join( self.messages['highscore'].format( i, transport.formatter.bold(score.name.replace('*', r'\*').replace('_', r'\_')), score.turns, score.money, score.score) for i, score in enumerate(highscores))))
def on_turn(self, transport, chat, text, user): user.turn += 1 action_result = None if user.current_npc is not None: npc = self.load_npc(user) npc_phrase, phrases = npc.talk(text, user) action_result = '*{}:* {}'.format(npc.name, npc_phrase) if phrases is None: user.current_npc = None action_result += '\n\n' else: user.save() return transport.send_message(chat, action_result, keyboard=phrases) location = self.load_location(user) new_location = None if action_result is None and self.messages['show_inventory'] == text: action_result = self.show_inventory(transport, user) if action_result is None: for npc_id in location.npcs(user): npc = npcs.NPCS[npc_id](self.messages) if self.messages['talk'].format(npc.name) == text: user.current_npc = npc.key npc = self.load_npc(user) npc_phrase, phrases = npc.greeting(user) action_result = '*{}:* {}'.format(npc.name, npc_phrase) if phrases is None: user.current_npc = None action_result += '\n\n' else: user.save() return transport.send_message(chat, action_result, keyboard=phrases) break if action_result is None: for i, o in enumerate(location.state['objects']): if self.messages['take'].format(o) == text: action_result = self.messages['took'].format(o) user.inventory.append(o) del location.state['objects'][i] break if action_result is None: new_location, action_result = location.handle_action(text, user) user.locations[location.key] = location.state if new_location: user.current_location = new_location location = self.load_location(user) user.save() transport.send_message( chat, action_result + ' ' + location.description(user), keyboard=self.make_location_keyboard(user, location))
def handle_intro(self, transport, message, user): user.in_intro = False user.save() if not user.name: buttons = [] buttons.append(message.user.first_name) if message.user.username: buttons.append(message.user.username) transport.send_message( message.chat, self.messages['ask_name'], keyboard=buttons) else: self.start(transport, message.chat, user)
def on_start(self, transport, chat, user): user.turn = 0 user.in_intro = True user.locations = {} user.current_location = 'home_sweet_home' user.inventory = [] user.current_npc = None user.know_about_generator = False user.filled_request = False user.electrician_went_check = False user.burned = False user.win = False user.money = 100 user.save() transport.send_message( chat, self.messages['intro'], keyboard=self.keyboards['intro'])
def on_help(self, transport, chat): transport.send_message( chat, self.messages['help'])