def list_players(chat_id): game = storage.get_game(chat_id) if game is None: return telegram.send_message( chat_id, "There is no game in this channel.") message = "Players:" for player in game.users: message += str("\n{0}").format(player.username) telegram.send_message(chat_id, message)
def handle_config(update): user_id = dt.traverse(update, "message.from.id") user = storage.get_user(user_id) if user is None: return telegram.send_message( user_id, "You are not registered yet. Please do so with /start.") storage.update_user(user_id, state=UserState.config) telegram.send_message( user_id, "What would you like to change?", reply_markup={"keyboard": [[CONFIG_USERNAME]], "one_time_keyboard": True, "resize_keyboard": True})
def start_game(chat_id, user_id): game = storage.get_game(chat_id) user = storage.get_user(user_id) if game is None: return telegram.send_message( chat_id, "Trying to start non existing game!") if user is None: return telegram.send_message( chat_id, "You aren't registered yet.\n" + "Please send a '/start' to @Spyfall_Bot") if game.admin_user != user: return telegram.send_message( chat_id, "You are not the gamemaster.") if game.state != GameState.preparing.value: return telegram.send_message( chat_id, "This game is already running.") if len(game.users) < 3: return telegram.send_message( chat_id, "Not enough players.") storage.update_game(chat_id, state=GameState.running) telegram.send_message( chat_id, "Game started! Roles will be distributed...") distribute_roles(game)
def distribute_roles(game): location = locations.get_location() roles = location.roles player_list = list(game.users) random.shuffle(roles) random.shuffle(player_list) template = "Your role is: {}" for i, player in enumerate(player_list): if i == 0: player.role = "Spy" else: player.role = roles[i - 1] telegram.send_message( player.user_id, template.format(player.role))
def handle_plain(text, update): user_id = dt.traverse(update, "message.from.id") text = dt.traverse(update, "message.text") user = storage.get_user(user_id) if user is None: return if user.state == UserState.config and text == CONFIG_USERNAME: storage.update_user(user_id, state=UserState.config_username) return telegram.send_message( user_id, "Please send me your new username.", reply_markup={"hide_keyboard": True}) if user.state == UserState.config_username: storage.update_user(user_id, username=text, state=UserState.none) return telegram.send_message( user_id, "Updated your username to {}.".format(text))
def handle_register(update): user_id = dt.traverse(update, "message.from.id") username = dt.traverse(update, "message.from.username") existing = storage.get_user(user_id) if existing is not None: return telegram.send_message( user_id, "You are already registered.") storage.register_user(user_id, username) message = "Welcome. Thank you for registering." if username is None: message += "\nBe sure to set a username with /config." else: message += "\nYour username is: {}".format(username) telegram.send_message( user_id, message)
def end_game(chat_id, user_id): game = storage.get_game(chat_id) user = storage.get_user(user_id) if game is None: return telegram.send_message( chat_id, "Trying to end non existing game!") if user is None: return telegram.send_message( chat_id, "You aren't registered yet.\n" + "Please send a '/start' to @Spyfall_Bot") if game.admin_user != user: return telegram.send_message( chat_id, "You are not the gamemaster.") storage.update_game(chat_id, state=GameState.finished) telegram.send_message( chat_id, "Game ended!")
def create_game(chat_id, user_id): game = storage.get_game(chat_id) if game is not None: return telegram.send_message( chat_id, "There is already a game in this chat.") user = storage.get_user(user_id) if user is None: return telegram.send_message( chat_id, "You aren't registered yet.\n" + "Please send a '/start' to @Spyfall_Bot") if user.username is None: return telegram.send_message( chat_id, "You haven't set a username yet.\n" + "Please do so with /config.") storage.register_game(chat_id, user) telegram.send_message( chat_id, "Game created and joined.")
def player_join_game(chat_id, player_id): game = storage.get_game(chat_id) user = storage.get_user(player_id) if game is None: return telegram.send_message( chat_id, "There is no preparing game to join.") if user is None: return telegram.send_message( chat_id, "You aren't registered yet.\n" + "Please send me a private message (@Spyfall_Bot)") if user.username is None: return telegram.send_message( chat_id, "You haven't set a username yet.\n" + "Please do so with /config.") if game.state != GameState.preparing.value: return telegram.send_message( chat_id, "This game is already running.") if len(game.users) >= 8: return telegram.send_message( chat_id, "There are already 8 players in this game.") if user in game.users: return telegram.send_message( chat_id, "You already joined this game.") success = storage.join_game(chat_id, player_id) if success: return telegram.send_message( chat_id, "Player {} has joined the game.".format(user.username)) return telegram.send_message( chat_id, "You already joined another game.")
def handle_about(update): telegram.send_message( dt.traverse(update, "message.chat.id"), filehandler.get_doc(filehandler.Document.about))
def handle_unknown(update): return telegram.send_message( user_id, "Unknown command.")
def handle_cancel(update): user_id = dt.traverse(update, "message.from.id") storage.update_user(user_id, state=UserState.none) return telegram.send_message( user_id, "Reset your state.")