def get_status_reply(update): games = get_all_games(update) player = get_player(update) if games: return slot_status_all(games, timezone=player.timezone_pytz) else: return "Create new game with /chettam"
def join(update, context): """Join current game""" query = update.callback_query player = get_player(update) game_id = re.search("[0-9]+", query.data).group(0) game = get_game(update.effective_chat.id, game_id=game_id) game.add_player(player, joined_at=dt.now(pytz.utc)) return refresh_main_page(update, context, query)
def leave(update, context): """Leave current game""" query = update.callback_query player = get_player(update) game_id = re.search("[0-9]+", query.data).group(0) game = get_game(update.effective_chat.id, game_id=game_id) remove_player_and_clean_game(context, game, player) return refresh_main_page(update, context, query)
def new_game(update, context): """Create new game""" query = update.callback_query player = get_player(update) timeslot = convert_to_dt(timeslot=query.data, timezone=player.timezone_pytz) create_game_and_add_player(update, context, player, timeslot) return refresh_main_page(update, context, query)
def set_user_nickname(update, context): nickname = update.message.text sanitized_nickname = nickname.strip().replace("\n", " ")[:30] player = get_player(update) player.csgo_nickname = sanitized_nickname player.save() update.message.reply_text( text=f'Your nickname was set to "{sanitized_nickname}".') return ConversationHandler.END
def set_user_timezone(update, context): query = update.callback_query new_tz = re.search("TZ_user_(.*)", query.data).group(1) player = get_player(update) player.timezone = new_tz player.save() query.answer() query.edit_message_text(text=f"Your TZ was set to {new_tz}") return MAIN_STATE
def user_nickname(update, context): query = update.callback_query player = get_player(update) reply = "" if player.csgo_nickname: reply += f'Your nickname is "{player.csgo_nickname}".' else: reply += f"You don't have nickname configured." reply += "\nReply to this message with your new nickname (30 chars max)." query.answer() query.edit_message_text(text=reply) return SECONDARY_STATE
def user_timezone(update, context): """Set current user's timezone""" query = update.callback_query player = get_player(update) keyboard = [ [ InlineKeyboardButton(f"{Emoji.check} {tz}, {code}", callback_data=f"TZ_user_{tz}") ] if tz == player.timezone else [InlineKeyboardButton(f"{tz}, {code}", callback_data=f"TZ_user_{tz}")] for tz, code in COMMON_TIMEZONES.items() ] query.answer() query.edit_message_text( text=f"Your timezone is {player.timezone}\nSet new timezone:", reply_markup=InlineKeyboardMarkup(keyboard), ) return MAIN_STATE
def call(update, context): """Mention all players about current game""" query = update.callback_query game_id = re.search("[0-9]+", query.data).group(0) game = get_game(update.effective_chat.id, game_id=game_id) player = get_player(update) query.answer() query.edit_message_text( text=slot_status(game, timezone=player.timezone_pytz), parse_mode=ParseMode.MARKDOWN, ) schedule_game_notification( context=context, update=update, game=game, message="go go!", ) remove_game_jobs(context, game, delay=0.1) return ConversationHandler.END
def hours_keyboard(update): """Returns keyboard with timeslots for new game""" player = get_player(update) chat = get_chat(update.effective_chat) timezone = player.timezone_pytz main_hours_dt = [ convert_to_dt(timeslot=f"{hour:02d}:00", timezone=timezone) for hour in chat.main_hours ] ts_games = get_all_games(update, ts_only=True) ts_filtered = [ timeslot.astimezone(timezone).strftime("%H:%M") for timeslot in main_hours_dt if timeslot not in ts_games and timeslot > dt.now(timezone) ] keyboard = [ InlineKeyboardButton(timeslot_time, callback_data=timeslot_time) for timeslot_time in ts_filtered ] return row_list_chunks(keyboard)
def get_chettam_data(update, context): """Reply message and keyboard for entry point""" games = get_all_games(update) player = get_player(update) keyboard = [] if games: reply = slot_status_all(games, timezone=player.timezone_pytz) for game in games: btn_row = [] if player in game.players: btn_text = f"{Emoji.cross} Leave" btn_callback = f"leave_{game.id}" else: btn_text = f"{Emoji.gun} Join" btn_callback = f"join_{game.id}" btn_row.append( InlineKeyboardButton( f"{get_time_header(game, player.timezone_pytz)}: {btn_text}", callback_data=btn_callback, )) if (game.slots > 1 and game_timediff(game, minutes=-30) and player in game.players and not get_assoc(game.id, player.id).in_queue): btn_row.append( InlineKeyboardButton(f"{Emoji.party} Call", callback_data=f"call_{game.id}")) keyboard.append(btn_row) else: reply = "Create new game below:" keyboard.append([ InlineKeyboardButton(f"{Emoji.fire} New", callback_data="pick_hour"), InlineKeyboardButton(f"{Emoji.check} Done", callback_data="status_conv"), ]) return reply, keyboard
def in_out(update, context, action, hard_args=None): """Ugliest function of them all""" args = hard_args if hard_args else context.args chat = get_chat(update.effective_chat) if args: player = get_player(update) if args[0].lower() == "all": for game in get_all_games(update): if action == "in" and player not in game.players: game.add_player(player, joined_at=dt.now(pytz.utc)) elif action == "out" and player in game.players: remove_player_and_clean_game(context, game, player) else: filtered_args = expand_hours( chat=chat, hours_list=[ argv for argv in args if re.search("^[0-9]+-[0-9]+$", argv) or re.search("^[0-9]+$", argv) ], ) for argv in filtered_args: timeslot = convert_to_dt( timeslot=f"{int(argv):02d}:00", timezone=player.timezone_pytz, ) game = get_game(chat_id=chat.id, timeslot=timeslot) if action == "in": if not game and timeslot > dt.now(pytz.utc): create_game_and_add_player(update, context, player, timeslot) elif game and player not in game.players: game.add_player(player, joined_at=dt.now(pytz.utc)) elif action == "out": if game and player in game.players: remove_player_and_clean_game(context, game, player)
def schedule_game_notification(context, update, game, message, when=0, auto=False): """Send a separate message""" player = get_player(update) tz = player.timezone_pytz def send_msg(ctx): if auto: prefix = "auto" else: prefix = player ctx.bot.send_message( chat_id=update.effective_chat.id, text= f"\[_{prefix}_] *{slot_time_header(game, timezone=tz)}*: {game.players_call_active} {message}", parse_mode=ParseMode.MARKDOWN, ) context.job_queue.run_once(send_msg, when=when, name=f"{game.id}_{when}")