def login(update, context): args = context.args telegram_id = update.effective_user.id if telegram_id == session.gm_telegram_id: update.message.reply_markdown('Sei già autenticato come GM.') return if telegram_id in [x.telegram_id for x in session.users]: update.message.reply_markdown('Sei già autenticato come giocatore.') return if len(args) == 0: update.message.reply_text('/login [token-giocatore]' \ ' [nome-personaggio] o /login [token-gm] per fare il login.') return if args[0] == session.player_token: # Autenticazione giocatore if len(args) == 1: update.message.reply_text( '/login [token-giocatore] [nome-personaggio]') return character_name = args[1] if character_name in [x.character.name for x in session.users]: other_user = [ x.name for x in session.users if x.character.name == character_name ][0] update.message.reply_markdown( 'Questo personaggio è già in uso da {}.'.format(other_user)) return character = session.get_character(character_name) if character is None: update.message.reply_markdown('Questo personaggio non esiste.') return username = update.message.from_user.first_name + ' ' + update.message.from_user.last_name logger.info('Authenticated player {} (ID: {})'.format( username, telegram_id)) user = User(telegram_id, username, character) session.users.append(user) update.message.reply_markdown( 'Autenticato come giocatore. Benvenuto/a, {}!'.format( character.name)) elif args[0] == session.gm_token: # Autenticazione GM logger.info('Authenticated GM (ID: {})'.format(telegram_id)) session.gm_telegram_id = telegram_id update.message.reply_markdown( 'Autenticato come GM. Benvenuto/a, master!') else: update.message.reply_markdown('Token errato.')
def character_status(update, context): args = context.args if len(args) == 0: message = session.state.character_summary else: character_name = context.args[0] character = session.get_character(character_name) if character is None: message = 'Giocatore non trovato.' else: message = character.status update.message.reply_markdown(message)
def del_action(update, context): args = context.args if len(args) == 0: update.message.reply_text('/del action [personaggio] |[azione]') return character_name = args.pop(0) character = session.get_character(character_name) if character is None: update.message.reply_markdown( 'Il personaggio "{}" non esiste.'.format(character_name)) return text = ' '.join(args) action_args = text.split('|') if len(action_args) != 2: update.message.reply_markdown( 'Usa |[azione] per indicare il nome dell\'azione.') return name = action_args[1] action = character.get_action(name) if action is None: update.message.reply_markdown( 'Il personaggio "{}" non ha l\'azione "{}".'.format( character_name, name)) return character.actions.remove(action) logger.info('Deleted action "{}" from character "{}"'.format( name, character_name)) update.message.reply_markdown( 'Rimosso l\'azione "{}" dal personaggio "{}"'.format( name, character_name))
def del_character(update, context): args = context.args if len(args) == 0: update.message.reply_text('/del character [personaggio]') return character_name = args[0] if character_name in session.get_active_character_names(): username = [x.name for x in session.users if x.character.name == character_name][0] update.message.reply_markdown('Il personaggio {} è al momento usato da {}'.format(character_name, username)) return character = session.get_character(character_name) if character is None: update.message.reply_markdown('Il personaggio {} non esiste'.format(character_name)) return session.state.characters.remove(character) logger.info('Removed character "{}".'.format(character_name)) update.message.reply_markdown('Il personaggio {} è stato rimosso'.format(character_name))
def add_effect(update, context): args = context.args if len(args) == 0: update.message.reply_text('/add effect [personaggio] |[nome]|[descrizione]|[attivo]|[durata]|[denaro-per-turno]') return character_name = args.pop(0) character = session.get_character(character_name) if character is None: update.message.reply_markdown('Il personaggio "{}" non esiste.'.format(character_name)) return text = ' '.join(args) effect_args = text.split('|') effect_args.pop(0) if len(effect_args) < 1 or len(effect_args) > 5: update.message.reply_text('Numero errato di argomenti effetto ([1-5], ne hai {}).'.format(len(effect_args))) return name = effect_args[0] if name in [x.name for x in character.effects]: update.message.reply_markdown('Il personaggio "{}" ha già l\'effetto "{}".'.format(character_name, name)) return if len(effect_args) >= 2: description = effect_args[1] else: description = None if len(effect_args) >= 3: active = effect_args[2] positive_answers = ['yes', 'si', 'y'] negative_answers = ['no', 'n'] if active in positive_answers: active = True elif active in negative_answers: active = False else: update.message.reply_markdown('"{}" non è un valore booleano valido.'.format(effect_args[2])) return else: active = True if len(effect_args) >= 4: try: duration = int(effect_args[3]) except ValueError: update.message.reply_markdown('"{}" non è un valore valido.'.format(effect_args[2])) return else: duration = -1 if len(effect_args) == 5: try: money_per_turn = int(effect_args[4]) except ValueError: update.message.reply_markdown('"{}" non è un valore valido.'.format(effect_args[2])) return else: money_per_turn = 0 effect = Effect(name, description=description, active=active, duration=duration, money_per_turn=money_per_turn) character.effects.append(effect) character.pending_messages.append('Hai un nuovo effetto:\n{}'.format(effect)) logger.info('Added to character {} the effect\n{}'.format(character_name, effect)) update.message.reply_markdown('Aggiunto al personaggio {} l\'effetto\n{}'.format(character_name, effect))
def add_action(update, context): args = context.args if len(args) == 0: update.message.reply_text( '/add action [personaggio] |[nome]|[usi]|[descrizione]|[condizioni]' ) return character_name = args.pop(0) character = session.get_character(character_name) if character is None: update.message.reply_markdown( 'Il personaggio "{}" non esiste.'.format(character_name)) return text = ' '.join(args) action_args = text.split('|') # Il primo argomento va ignorato action_args.pop(0) if len(action_args) < 2 or len(action_args) > 4: update.message.reply_markdown( 'Numero errato di argomenti azione ([2-4], ne hai {}).'.format( len(action_args))) return name = action_args[0] if name in [x.name for x in character.actions]: update.message.reply_markdown( 'Il personaggio "{}" ha già l\'azione "{}".'.format( character_name, name)) return try: uses = int(action_args[1]) except ValueError: update.message.reply_markdown('"{}" non è un valore valido.'.format( action_args[1])) return if len(action_args) >= 3: description = action_args[2] else: description = None if len(action_args) == 4: conditions = action_args[3] else: conditions = None action = Action(name, description, conditions=conditions, uses=uses) character.actions.append(action) character.pending_messages.append( 'Hai ricevuto una nuova azione:\n{}'.format(action)) logger.info('Added to character {} the action\n{}'.format( character_name, action)) update.message.reply_markdown( 'Aggiunta al personaggio {} l\'azione\n{}'.format( character_name, action))