def iterate(self, context: Optional[CallbackContext] = None): session = database.get_session() for language in Language.ENGLISH, Language.TURKISH, Language.ITALIAN: todays_mwe = get_todays_mwe(language) positive_together_count = session.query(Submission)\ .filter(Submission.category == SubmissionCategory.POSITIVE_TOGETHER)\ .filter(Submission.mwe == todays_mwe)\ .count() negative_together_count = session.query(Submission)\ .filter(Submission.category == SubmissionCategory.NEGATIVE_TOGETHER)\ .filter(Submission.mwe == todays_mwe)\ .count() diff = abs(positive_together_count - negative_together_count) if self.buffed_category[language] is None: if diff >= 15: if positive_together_count < negative_together_count: self.buffed_category[ language] = SubmissionCategory.POSITIVE_TOGETHER notification_manager.send_idioms_worth_more(context) else: self.buffed_category[ language] = SubmissionCategory.NEGATIVE_TOGETHER notification_manager.send_non_idioms_worth_more( context) else: if diff < 10: self.buffed_category[language] = None
def _process_review_achievements(user: User, update: Update): session = database.get_session() todays_mwe = get_todays_mwe(user.language) user_review_count_today = session.query(Review)\ .filter(and_(Review.mwe == todays_mwe, Review.user == user)).count() if user_review_count_today == 10: award_achievement(user, AchievementType.REVIEW_LVL_1) update.message.reply_sticker(ACHIEVEMENT_STICKER) update.message.reply_html( user.language.get(Token.REVIEW_LVL_1_ACH_CONGRATS_MSG)) if user_review_count_today == 20: award_achievement(user, AchievementType.REVIEW_LVL_2) update.message.reply_sticker(ACHIEVEMENT_STICKER) update.message.reply_html( user.language.get(Token.REVIEW_LVL_2_ACH_CONGRATS_MSG)) if user_review_count_today == 40: award_achievement(user, AchievementType.REVIEW_LVL_3) update.message.reply_sticker(ACHIEVEMENT_STICKER) update.message.reply_html( user.language.get(Token.REVIEW_LVL_3_ACH_CONGRATS_MSG)) if user_review_count_today == 80: award_achievement(user, AchievementType.REVIEW_LVL_4) update.message.reply_sticker(ACHIEVEMENT_STICKER) update.message.reply_html( user.language.get(Token.REVIEW_LVL_4_ACH_CONGRATS_MSG)) if user_review_count_today == 160: award_achievement(user, AchievementType.REVIEW_LVL_5) update.message.reply_sticker(ACHIEVEMENT_STICKER) update.message.reply_html( user.language.get(Token.REVIEW_LVL_5_ACH_CONGRATS_MSG))
def submission_contains_todays_mwe(user: User, submission: str) -> bool: todays_mwe = get_todays_mwe(user.language) if parser.get_sentence_count(submission) != 1: return False try: parsed = parser.parse(submission, todays_mwe) return parsed.contains_mwe(todays_mwe) except Exception as ex: mwelog.exception(str(ex)) return False
def _send_submission_to_review(user: User, update: Update, context: CallbackContext): if not user.viewed_review_help: reply_to(user, update, user.language.get(Token.REVIEW_HELP_MESSAGE_1)) time.sleep(4) reply_to(user, update, user.language.get(Token.REVIEW_HELP_MESSAGE_2)) time.sleep(4) user.viewed_review_help = True update_user(user) todays_mwe = get_todays_mwe(user.language) submissions = get_submissions_to_review(todays_mwe, user) if len(submissions) > 0: submission: Submission = submissions[0] context.user_data["submission"] = submission if "review_count" not in context.user_data: context.user_data["review_count"] = 0 parsed = parser.parse(submission.value, submission.mwe) review_example = submission.value for index in reversed(sorted(submission.mwe_indices)): start_index = parsed.token_positions[index][0] end_index = parsed.token_positions[index][1] review_example = review_example[: end_index] + "</u></b>" + review_example[ end_index:] review_example = review_example[: start_index] + "<b><u>" + review_example[ start_index:] if submission.category == SubmissionCategory.POSITIVE_SEPARATED or \ submission.category == SubmissionCategory.POSITIVE_TOGETHER: review_question = user.language.get(Token.REVIEW_QUESTION_POSITIVE) \ % (review_example, _get_word_list_str_from_submission(submission)) reply_html(user, update, review_question, Keyboard.review_keyboard(user.language)) else: review_question = user.language.get(Token.REVIEW_QUESTION_NEGATIVE) \ % (review_example, _get_word_list_str_from_submission(submission)) reply_html(user, update, review_question, Keyboard.review_keyboard(user.language)) else: if "review_count" in context.user_data: reply_to(user, update, user.language.get(Token.NO_SUB_LEFT_TO_REVIEW), Keyboard.main(user)) else: reply_to(user, update, user.language.get(Token.NO_SUBMISSIONS), Keyboard.main(user)) clear_state(context) unmute_user(user.id) _safe_delete_context_data(context, "submission") _safe_delete_context_data(context, "review_count")
def submit_message_handler(user: User, update: Update, context: CallbackContext) -> None: """ Gets the submission text from user """ submission_value = update.message.text sentence_count = parser.get_sentence_count(submission_value) if sentence_count != 1: reply_to( user, update, user.language.get(Token.PLEASE_ENTER_ONE_SENTENCE) % sentence_count) return todays_mwe = get_todays_mwe(user.language) try: parsed = parser.parse(submission_value, todays_mwe) except Exception as ex: mwelog.error(ex) reply_to(user, update, user.language.get(Token.SUBMISSION_CONTAINS_ERROR), reply_markup=Keyboard.remove()) return context.user_data["parsed"] = parsed if not parsed.contains_mwe(todays_mwe): reply_to(user, update, user.language.get(Token.SUBMISSION_DOES_NOT_CONTAIN_MWE) % todays_mwe.name, reply_markup=Keyboard.remove()) return # Duplicate check # submission_hash = get_submission_hash(doc) # if session.query(Submission).filter(Submission.hash == submission_hash).count() > 0: # reply_to(user, update, # get_language_token(user.language, Token.DUPLICATE_SUBMISSION)) # return # Find MWE position submission_mwe_lemmas = parsed.get_mwe_tokens(todays_mwe) submission_mwe_lemmas_str = ", ".join(submission_mwe_lemmas[:-1]) submission_mwe_lemmas_str += "</u></b> %s <b><u>%s" % (user.language.get( Token.AND), submission_mwe_lemmas[-1]) context.user_data["sub_state"] = "choosing_category" reply_html( user, update, user.language.get(Token.DOES_WORDS_FORM_SPECIAL_MEANING) % submission_mwe_lemmas_str, Keyboard.submission_category(user.language))
def scoreboard_handler(user: User, update: Update, context: CallbackContext): scoreboard.send_to_user(user, update) todays_mwe = get_todays_mwe(user.language) session = database.get_session() submission_count_now = session.query(Submission).filter( Submission.mwe == todays_mwe).count() if submission_count_now < 100: update.message.reply_text( user.language.get(Token.TODAYS_TARGET) % (100 - submission_count_now)) if user.became_champion and user.email is None: update.message.reply_text(user.language.get(Token.CHAMP_BUT_NO_EMAIL)) update.message.reply_text(user.language.get(Token.SURVEY_MESSAGE))
def send_i_need_x_examples(self, context: CallbackContext): self._clear_old_notifications() language = mwexpress_config.language todays_mwe = get_todays_mwe(language) session = database.get_session() submission_count_now = session.query(Submission).filter( Submission.mwe == todays_mwe).count() if submission_count_now < 100: for user in get_all_users(): self._send_notification( context, user.id, user.language.get(Token.TODAYS_TARGET) % (100 - submission_count_now), NotificationType.I_NEED_X_EXAMPLES, SAD_RAIN_STICKER) time.sleep(0.3)
def start_submit_handler(user: User, update: Update, context: CallbackContext) -> None: """ Starts the submission cycle for user. """ todays_mwe = get_todays_mwe(user.language) context.user_data["sub_state"] = "typing_example" if not user.viewed_submission_help: reply_html(user, update, user.language.get(Token.SUBMISSION_HELP_MESSAGE_1)) time.sleep(7) user.viewed_submission_help = True update_user(user) reply_html(user, update, user.language.get(Token.PLEASE_ENTER_EXAMPLE) % _get_word_list_str_from_mwe(todays_mwe), reply_markup=Keyboard.remove())
def send_game_starting_message_to_all() -> None: mwelog.info("Sending game started message to all users") unmute_everyone() all_users = get_all_users() for user in all_users: try: mwexpress_bot.bot.send_sticker(user.id, GOOD_MORNING_STICKER) send_message_to_user(mwexpress_bot.bot, user, user.language.get(Token.GAME_STARTED)) todays_mwe = get_todays_mwe(user.language) send_message_to_user( mwexpress_bot.bot, user, user.language.get(Token.TODAYS_MWE_REPLY_TEXT) % (todays_mwe.name, todays_mwe.meaning), reply_markup=Keyboard.main(user), parse_mode=ParseMode.HTML) time.sleep(0.3) except Exception as ex: mwelog.exception(str(ex)) mwelog.info("Sent game started message to all users")
def todays_mwe_handler(user: User, update: Update): now = datetime.now().time() if mwexpress_config.start_time <= now <= mwexpress_config.end_time: todays_mwe = get_todays_mwe(user.language) update.message.reply_text( text=user.language.get(Token.TODAYS_MWE_REPLY_TEXT) % (todays_mwe.name, todays_mwe.meaning), parse_mode=ParseMode.HTML, reply_markup=Keyboard.main(user)) if submission_scores.buffed_category[user.language] is not None: notification_messages = { SubmissionCategory.POSITIVE_TOGETHER: Token.POS_TOG_WORTH_MORE, SubmissionCategory.POSITIVE_SEPARATED: Token.POS_SEP_WORTH_MORE, SubmissionCategory.NEGATIVE_TOGETHER: Token.NEG_TOG_WORTH_MORE } update.message.reply_html( user.language.get(notification_messages[ submission_scores.buffed_category[user.language]])) if not user.viewed_todays_mwe_help: time.sleep(3) update.message.reply_text(text=user.language.get( Token.TODAYS_MWE_HELP_MESSAGE_1), parse_mode=ParseMode.MARKDOWN, reply_markup=Keyboard.main(user)) time.sleep(5) update.message.reply_text(text=user.language.get( Token.TODAYS_MWE_HELP_MESSAGE_2), parse_mode=ParseMode.HTML, reply_markup=Keyboard.main(user)) user.viewed_todays_mwe_help = True update_user(user) else: reply_to(user, update, user.language.get(Token.GAME_HOURS_FINISHED) % mwexpress_config.start_time.hour, reply_markup=Keyboard.main(user))
def submit_category_handler(user: User, update: Update, context: CallbackContext) -> None: available_inputs = [ user.language.get(Token.FORMS_SPECIAL_MEANING), user.language.get(Token.DOES_NOT_FORM_SPECIAL_MEANING), user.language.get(Token.CANCEL) ] if update.message.text not in available_inputs: reply_to(user, update, user.language.get(Token.ENTER_VALID_MWE_CATEGORY), Keyboard.submission_category(user.language)) return if update.message.text == user.language.get(Token.CANCEL): _clear_context(context) unmute_user(user.id) reply_to(user, update, user.language.get(Token.SUBMISSION_CANCELLED), Keyboard.main(user)) return parsed = context.user_data["parsed"] todays_mwe = get_todays_mwe(user.language) positive = update.message.text == user.language.get( Token.FORMS_SPECIAL_MEANING) submission = add_submission(user, parsed, todays_mwe, positive) unmute_user(user.id) _clear_context(context) reply_to( user, update, user.language.get(Token.THANKS_FOR_SUBMISSION) % (get_random_congrats_message(user.language), submission.points), Keyboard.main(user)) if random.random() < 0.75: time.sleep(1) send_hint_message(user, update, context) session = database.get_session() submission_count_now = session.query(Submission).filter( Submission.mwe == todays_mwe).count() if submission_count_now == 1: # award first submission award_achievement(user, AchievementType.FIRST_SUBMISSION) update.message.reply_sticker(ACHIEVEMENT_STICKER) update.message.reply_html( user.language.get(Token.FIRST_SUB_ACH_CONGRATS_MSG)) # if submission_count_now < 100: # update.message.reply_text(user.language.get(Token.TODAYS_TARGET) % (100 - submission_count_now)) now = datetime.now() start_datetime = datetime.combine(now, mwexpress_config.start_time) start_diff = now - start_datetime if start_diff.total_seconds() < 1800: # award early bird if not user_has_achievement(user, AchievementType.EARLY_BIRD): award_achievement(user, AchievementType.EARLY_BIRD) update.message.reply_sticker(ACHIEVEMENT_STICKER) update.message.reply_html( user.language.get(Token.EARLY_BIRD_ACH_CONGRATS_MSG)) today_sub_count_by_user = session.query(Submission)\ .filter(and_(Submission.mwe == todays_mwe, Submission.user == user)).count() if today_sub_count_by_user == 5: award_achievement(user, AchievementType.SUB_LVL_1) update.message.reply_sticker(ACHIEVEMENT_STICKER) update.message.reply_html( user.language.get(Token.SUB_LVL_1_ACH_CONGRATS_MSG)) if today_sub_count_by_user == 10: award_achievement(user, AchievementType.SUB_LVL_2) update.message.reply_sticker(ACHIEVEMENT_STICKER) update.message.reply_html( user.language.get(Token.SUB_LVL_2_ACH_CONGRATS_MSG)) if today_sub_count_by_user == 20: award_achievement(user, AchievementType.SUB_LVL_3) update.message.reply_sticker(ACHIEVEMENT_STICKER) update.message.reply_html( user.language.get(Token.SUB_LVL_3_ACH_CONGRATS_MSG)) if today_sub_count_by_user == 40: award_achievement(user, AchievementType.SUB_LVL_4) update.message.reply_sticker(ACHIEVEMENT_STICKER) update.message.reply_html( user.language.get(Token.SUB_LVL_4_ACH_CONGRATS_MSG)) if today_sub_count_by_user == 70: award_achievement(user, AchievementType.SUB_LVL_5) update.message.reply_sticker(ACHIEVEMENT_STICKER) update.message.reply_html( user.language.get(Token.SUB_LVL_5_ACH_CONGRATS_MSG)) submission_scores.iterate(context)