From 4294dafdc0f0e9ccfb839d9b9ca91a4429a289f6 Mon Sep 17 00:00:00 2001 From: Leonid Kozarin Date: Tue, 12 Dec 2023 02:24:34 +0300 Subject: [PATCH] Don't finish the battle if an acceptor's dick is too short --- locales/en.yml | 7 ++-- locales/ru.yml | 7 ++-- src/handlers/mod.rs | 7 +++- src/handlers/pvp.rs | 85 +++++++++++++++++++++++++-------------------- 4 files changed, 60 insertions(+), 46 deletions(-) diff --git a/locales/en.yml b/locales/en.yml index 0a3febc..3b4f908 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -32,11 +32,10 @@ commands: button: "Attack!" errors: no_args: "Call the command with a number of centimeters you're willing to bet." - not_enough: "Meh. The %{subject}'s dick is not long enough for such a big bet!" + not_enough: + initiator: "Meh. The initiator's dick is not long enough for such a big bet!" + acceptor: "Your gun is not long enough 😣" same_person: "You cannot fight with yourself!" - subjects: - initiator: "initiator" - acceptor: "acceptor" import: description: "Import dicks from other bots" result: diff --git a/locales/ru.yml b/locales/ru.yml index aec9bfd..fd3337c 100644 --- a/locales/ru.yml +++ b/locales/ru.yml @@ -32,11 +32,10 @@ commands: button: "ΠΡ‚Π°ΠΊΠΎΠ²Π°Ρ‚ΡŒ!" errors: no_args: "Π’Ρ‹Π·ΠΎΠ²ΠΈ ΠΊΠΎΠΌΠ°Π½Π΄Ρƒ с числом сантимСтров, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ Π³ΠΎΡ‚ΠΎΠ² ΠΏΠΎΡΡ‚Π°Π²ΠΈΡ‚ΡŒ." - not_enough: "ΠŸΡ„Ρ„. Π‘Ρ‚Ρ€ΡŽΡ‡ΠΎΠΊ %{subject} слишком ΠΊΠΎΡ€ΠΎΡ‚ΠΊΠΈΠΉ для Ρ‚Π°ΠΊΠΎΠΉ ставки!" + not_enough: + initiator: "ΠŸΡ„Ρ„. Π‘Ρ‚Ρ€ΡŽΡ‡ΠΎΠΊ ΠΈΠ½ΠΈΡ†ΠΈΠ°Ρ‚ΠΎΡ€Π° слишком ΠΊΠΎΡ€ΠΎΡ‚ΠΊΠΈΠΉ для Ρ‚Π°ΠΊΠΎΠΉ ставки!" + acceptor: "Ввоя Π²ΠΎΠ»Ρ‹Π½Π° слишком ΠΊΠΎΡ€ΠΎΡ‚ΠΊΠ° 😣" same_person: "НСльзя Π±ΠΈΡ‚ΡŒΡΡ с самим собой!" - subjects: - initiator: "ΠΈΠ½ΠΈΡ†ΠΈΠ°Ρ‚ΠΎΡ€Π°" - acceptor: "ΠΏΡ€ΠΈΠ½ΠΈΠΌΠ°ΡŽΡ‰Π΅Π³ΠΎ сопСрника" import: description: "Π˜ΠΌΠΏΠΎΡ€Ρ‚ писюнов ΠΈΠ· Π΄Ρ€ΡƒΠ³ΠΈΡ… Π±ΠΎΡ‚ΠΎΠ²" result: diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs index a158d5a..d777082 100644 --- a/src/handlers/mod.rs +++ b/src/handlers/mod.rs @@ -11,7 +11,7 @@ use std::borrow::ToOwned; use teloxide::Bot; use teloxide::payloads::SendMessage; use teloxide::requests::{JsonRequest, Requester}; -use teloxide::types::{Message, User}; +use teloxide::types::{InlineKeyboardMarkup, Message, User}; use teloxide::types::ParseMode::Html; pub use dick::*; @@ -23,6 +23,11 @@ pub use promo::*; pub type HandlerResult = Result<(), Box>; +pub enum CallbackResult { + EditMessage(String, Option), + ShowError(String), +} + pub fn ensure_lang_code(user: Option<&User>) -> String { user .and_then(|u| { diff --git a/src/handlers/pvp.rs b/src/handlers/pvp.rs index 20f8cbb..1d0e158 100644 --- a/src/handlers/pvp.rs +++ b/src/handlers/pvp.rs @@ -10,7 +10,7 @@ use teloxide::macros::BotCommands; use teloxide::payloads::{AnswerCallbackQuerySetters, AnswerInlineQuerySetters}; use teloxide::requests::Requester; use teloxide::types::{CallbackQuery, ChatId, ChosenInlineResult, InlineKeyboardButton, InlineKeyboardMarkup, InlineQuery, InlineQueryResultArticle, InputMessageContent, InputMessageContentText, Message, MessageId, ParseMode, ReplyMarkup, User, UserId}; -use crate::handlers::{ensure_lang_code, HandlerResult, reply_html, utils}; +use crate::handlers::{CallbackResult, ensure_lang_code, HandlerResult, reply_html, utils}; use crate::{metrics, repo}; use crate::config::{AppConfig, BattlesFeatureToggles}; use crate::repo::{ChatIdPartiality, Repositories}; @@ -171,33 +171,44 @@ pub async fn callback_handler(bot: Bot, query: CallbackQuery, repos: Repositorie return Ok(()) } - let (text, keyboard) = pvp_impl_attack(params, initiator, query.from.into(), bet).await?; - - let answer_req_fut = bot.answer_callback_query(query.id).into_future(); - let (answer_resp, edit_resp) = match &edit_params { - EditMessageTextParams::Chat(chat_id, message_id) => { - let mut edit_req = bot.edit_message_text(*chat_id, message_id.clone(), text); - edit_req.parse_mode.replace(ParseMode::Html); - edit_req.reply_markup = keyboard; - join( - answer_req_fut, - edit_req.into_future().map_ok(|_| ()) - ).await + let attack_result = pvp_impl_attack(params, initiator, query.from.into(), bet).await?; + let answer_req = bot.answer_callback_query(query.id); + match attack_result { + CallbackResult::EditMessage(text, keyboard) => { + let answer_req_fut = answer_req.into_future(); + let (answer_resp, edit_resp) = match &edit_params { + EditMessageTextParams::Chat(chat_id, message_id) => { + let mut edit_req = bot.edit_message_text(*chat_id, message_id.clone(), text); + edit_req.parse_mode.replace(ParseMode::Html); + edit_req.reply_markup = keyboard; + join( + answer_req_fut, + edit_req.into_future().map_ok(|_| ()) + ).await + } + EditMessageTextParams::Inline { inline_message_id } => { + let mut edit_req = bot.edit_message_text_inline(inline_message_id, text); + edit_req.parse_mode.replace(ParseMode::Html); + edit_req.reply_markup = keyboard; + join( + answer_req_fut, + edit_req.into_future().map_ok(|_| ()) + ).await + } + }; + answer_resp?; + if edit_resp.is_err() { + log::error!("couldn't edit the message ({chat_id}, {edit_params:?}): {}", edit_resp.unwrap_err()) + } } - EditMessageTextParams::Inline { inline_message_id } => { - let mut edit_req = bot.edit_message_text_inline(inline_message_id, text); - edit_req.parse_mode.replace(ParseMode::Html); - edit_req.reply_markup = keyboard; - join( - answer_req_fut, - edit_req.into_future().map_ok(|_| ()) - ).await + CallbackResult::ShowError(error) => { + answer_req + .text(error) + .show_alert(true) + .await?; } - }; - answer_resp?; - if edit_resp.is_err() { - log::error!("couldn't edit the message ({chat_id}, {edit_params:?}): {}", edit_resp.unwrap_err()) } + metrics::CMD_PVP_COUNTER.inline.inc(); Ok(()) } @@ -247,12 +258,12 @@ pub(crate) async fn pvp_impl_start(p: BattleParams, initiator: UserInfo, bet: u3 ]]); (text, Some(keyboard)) } else { - (t!("commands.pvp.errors.not_enough", locale = &p.lang_code), None) + (t!("commands.pvp.errors.not_enough.initiator", locale = &p.lang_code), None) }; Ok(data) } -async fn pvp_impl_attack(p: BattleParams, initiator: UserId, acceptor: UserInfo, bet: u32) -> anyhow::Result<(String, Option)> { +async fn pvp_impl_attack(p: BattleParams, initiator: UserId, acceptor: UserInfo, bet: u32) -> anyhow::Result { let chat_id_kind = p.chat_id.kind(); let (enough_initiator, enough_acceptor) = join!( p.repos.dicks.check_dick(&chat_id_kind, initiator, bet), @@ -260,7 +271,7 @@ async fn pvp_impl_attack(p: BattleParams, initiator: UserId, acceptor: UserInfo, ); let (enough_initiator, enough_acceptor) = (enough_initiator?, enough_acceptor?); - let text = if enough_initiator && enough_acceptor { + let result = if enough_initiator && enough_acceptor { let acceptor_uid = acceptor.clone().into(); let (winner, loser) = choose_winner(initiator, acceptor_uid); let (loser_res, winner_res) = p.repos.dicks.move_length(&p.chat_id, loser, winner, bet).await?; @@ -269,22 +280,22 @@ async fn pvp_impl_attack(p: BattleParams, initiator: UserId, acceptor: UserInfo, let loser_info = get_user_info(&p.repos.users, loser, &acceptor).await?; let main_part = t!("commands.pvp.results.finish", locale = &p.lang_code, winner_name = winner_info.name, winner_length = winner_res.new_length, loser_length = loser_res.new_length); - if let (Some(winner_pos), Some(loser_pos)) = (winner_res.pos_in_top, loser_res.pos_in_top) { + let text = if let (Some(winner_pos), Some(loser_pos)) = (winner_res.pos_in_top, loser_res.pos_in_top) { let winner_pos = t!("commands.pvp.results.position.winner", locale = &p.lang_code, name = winner_info.name, pos = winner_pos); let loser_pos = t!("commands.pvp.results.position.loser", locale = &p.lang_code, name = loser_info.name, pos = loser_pos); format!("{main_part}\n\n{winner_pos}\n{loser_pos}") } else { main_part - } - } else { - let subject = if enough_acceptor { - t!("commands.pvp.subjects.initiator", locale = &p.lang_code) - } else { - t!("commands.pvp.subjects.acceptor", locale = &p.lang_code) }; - t!("commands.pvp.errors.not_enough", locale = &p.lang_code, subject = subject) + CallbackResult::EditMessage(text, None) + } else if enough_acceptor { + let text = t!("commands.pvp.errors.not_enough.initiator", locale = &p.lang_code); + CallbackResult::EditMessage(text, None) + } else { + let text = t!("commands.pvp.errors.not_enough.acceptor", locale = &p.lang_code); + CallbackResult::ShowError(text) }; - Ok((text, None)) + Ok(result) } fn choose_winner(initiator: T, acceptor: T) -> (T, T) {