Skip to content

Commit

Permalink
Don't finish the battle if an acceptor's dick is too short
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonid Kozarin committed Dec 12, 2023
1 parent 78afa50 commit 4294daf
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 46 deletions.
7 changes: 3 additions & 4 deletions locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 3 additions & 4 deletions locales/ru.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ commands:
button: "Атаковать!"
errors:
no_args: "Вызови команду с числом сантиметров, которые готов поставить."
not_enough: "Пфф. Стрючок %{subject} слишком короткий для такой ставки!"
not_enough:
initiator: "Пфф. Стрючок инициатора слишком короткий для такой ставки!"
acceptor: "Твоя волына слишком коротка 😣"
same_person: "Нельзя биться с самим собой!"
subjects:
initiator: "инициатора"
acceptor: "принимающего соперника"
import:
description: "Импорт писюнов из других ботов"
result:
Expand Down
7 changes: 6 additions & 1 deletion src/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand All @@ -23,6 +23,11 @@ pub use promo::*;

pub type HandlerResult = Result<(), Box<dyn std::error::Error + Send + Sync>>;

pub enum CallbackResult {
EditMessage(String, Option<InlineKeyboardMarkup>),
ShowError(String),
}

pub fn ensure_lang_code(user: Option<&User>) -> String {
user
.and_then(|u| {
Expand Down
85 changes: 48 additions & 37 deletions src/handlers/pvp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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(())
}
Expand Down Expand Up @@ -247,20 +258,20 @@ 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<InlineKeyboardMarkup>)> {
async fn pvp_impl_attack(p: BattleParams, initiator: UserId, acceptor: UserInfo, bet: u32) -> anyhow::Result<CallbackResult> {
let chat_id_kind = p.chat_id.kind();
let (enough_initiator, enough_acceptor) = join!(
p.repos.dicks.check_dick(&chat_id_kind, initiator, bet),
p.repos.dicks.check_dick(&chat_id_kind, acceptor.uid, if p.features.check_acceptor_length { bet } else { 0 }),
);
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?;
Expand All @@ -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<T>(initiator: T, acceptor: T) -> (T, T) {
Expand Down

0 comments on commit 4294daf

Please sign in to comment.