From d66f9f14dc49356a8d2013531510293d5f829139 Mon Sep 17 00:00:00 2001 From: Leonid Kozarin Date: Fri, 27 Sep 2024 00:30:21 +0300 Subject: [PATCH] Add a default attack option (#26) --- .env.example | 1 + Dockerfile | 1 + docker-compose.yml | 1 + src/config/app.rs | 3 +++ src/handlers/inline.rs | 5 +++-- src/handlers/pvp.rs | 27 +++++++++++++++------------ 6 files changed, 24 insertions(+), 14 deletions(-) diff --git a/.env.example b/.env.example index 240eb71..8b0498e 100644 --- a/.env.example +++ b/.env.example @@ -32,6 +32,7 @@ GROW_SHRINK_RATIO=0.5 GROWTH_DOD_BONUS_MAX=5 NEWCOMERS_GRACE_DAYS=7 TOP_LIMIT=10 +PVP_DEFAULT_BET=1 # Perks HELP_PUSSIES_COEF=0.01 diff --git a/Dockerfile b/Dockerfile index 2e44137..77be737 100644 --- a/Dockerfile +++ b/Dockerfile @@ -45,6 +45,7 @@ ARG HELP_ADMIN_CHANNEL_EN ARG HELP_GIT_REPO ARG CHATS_MERGING_ENABLED ARG TOP_UNLIMITED_ENABLED +ARG PVP_DEFAULT_BET ARG PVP_CHECK_ACCEPTOR_LENGTH ARG PVP_CALLBACK_LOCKS_ENABLED ARG PVP_STATS_SHOW diff --git a/docker-compose.yml b/docker-compose.yml index 586d2c2..770c4fe 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -17,6 +17,7 @@ services: - HELP_GIT_REPO - CHATS_MERGING_ENABLED - TOP_UNLIMITED_ENABLED + - PVP_DEFAULT_BET - PVP_CHECK_ACCEPTOR_LENGTH - PVP_CALLBACK_LOCKS_ENABLED - PVP_STATS_SHOW diff --git a/src/config/app.rs b/src/config/app.rs index 8545b06..efc1f54 100644 --- a/src/config/app.rs +++ b/src/config/app.rs @@ -12,6 +12,7 @@ pub struct AppConfig { pub top_limit: u16, pub loan_payout_ratio: f32, pub dod_rich_exclusion_ratio: Option, + pub pvp_default_bet: u16, pub announcements: AnnouncementsConfig, pub command_toggles: CachedEnvToggles, } @@ -30,6 +31,7 @@ impl AppConfig { let dod_rich_exclusion_ratio = get_optional_env_ratio("DOD_RICH_EXCLUSION_RATIO"); let chats_merging = get_env_value_or_default("CHATS_MERGING_ENABLED", false); let top_unlimited = get_env_value_or_default("TOP_UNLIMITED_ENABLED", false); + let pvp_default_bet = get_env_value_or_default("PVP_DEFAULT_BET", 1); let check_acceptor_length = get_env_value_or_default("PVP_CHECK_ACCEPTOR_LENGTH", false); let callback_locks = get_env_value_or_default("PVP_CALLBACK_LOCKS_ENABLED", true); let show_stats = get_env_value_or_default("PVP_STATS_SHOW", true); @@ -52,6 +54,7 @@ impl AppConfig { top_limit, loan_payout_ratio, dod_rich_exclusion_ratio, + pvp_default_bet, announcements: AnnouncementsConfig { max_shows: announcement_max_shows, announcements: [ diff --git a/src/handlers/inline.rs b/src/handlers/inline.rs index 455dc94..e8c1dc0 100644 --- a/src/handlers/inline.rs +++ b/src/handlers/inline.rs @@ -12,7 +12,7 @@ use teloxide::types::*; use teloxide::types::ParseMode::Html; use crate::config::AppConfig; use crate::domain::LanguageCode; -use crate::handlers::{build_pagination_keyboard, dick, dod, FromRefs, HandlerImplResult, HandlerResult, loan, stats, utils}; +use crate::handlers::{build_pagination_keyboard, dick, dod, FromRefs, HandlerImplResult, HandlerResult, loan, stats, utils, pvp}; use crate::handlers::utils::callbacks::CallbackDataWithPrefix; use crate::handlers::utils::Incrementor; use crate::handlers::utils::page::Page; @@ -103,7 +103,7 @@ pub async fn inline_handler(bot: Bot, query: InlineQuery, repos: Repositories, a let uid = query.from.id.0; let lang_code = LanguageCode::from_user(&query.from); let btn_label = t!("inline.results.button", locale = &lang_code); - let results: Vec = InlineCommand::iter() + let mut results: Vec = InlineCommand::iter() .map(|cmd| cmd.to_string()) .filter(|cmd| app_config.command_toggles.enabled(cmd)) .map(|key| { @@ -120,6 +120,7 @@ pub async fn inline_handler(bot: Bot, query: InlineQuery, repos: Repositories, a InlineQueryResult::Article(article) }) .collect(); + results.push(pvp::build_inline_keyboard_article_result(query.from.id, &lang_code, name, app_config.pvp_default_bet)); let mut answer = bot.answer_inline_query(query.id, results) .is_personal(true); diff --git a/src/handlers/pvp.rs b/src/handlers/pvp.rs index 99ce59a..a3d37a2 100644 --- a/src/handlers/pvp.rs +++ b/src/handlers/pvp.rs @@ -7,7 +7,7 @@ use teloxide::Bot; use teloxide::macros::BotCommands; use teloxide::payloads::AnswerInlineQuerySetters; use teloxide::requests::Requester; -use teloxide::types::{CallbackQuery, ChatId, ChosenInlineResult, InlineKeyboardButton, InlineKeyboardMarkup, InlineQuery, InlineQueryResultArticle, InputMessageContent, InputMessageContentText, Message, ParseMode, ReplyMarkup, User, UserId}; +use teloxide::types::{CallbackQuery, ChatId, ChosenInlineResult, InlineKeyboardButton, InlineKeyboardMarkup, InlineQuery, InlineQueryResult, InlineQueryResultArticle, InputMessageContent, InputMessageContentText, Message, ParseMode, ReplyMarkup, User, UserId}; use crate::handlers::{CallbackResult, HandlerResult, reply_html, send_error_callback_answer, utils}; use crate::{metrics, repo}; use crate::config::{AppConfig, BattlesFeatureToggles}; @@ -132,17 +132,7 @@ pub async fn inline_handler(bot: Bot, query: InlineQuery) -> HandlerResult { let bet: u16 = query.query.parse()?; let lang_code = LanguageCode::from_user(&query.from); let name = utils::get_full_name(&query.from); - - let title = t!("inline.results.titles.pvp", locale = &lang_code, bet = bet); - let text = t!("commands.pvp.results.start", locale = &lang_code, name = name, bet = bet); - let content = InputMessageContent::Text(InputMessageContentText::new(text).parse_mode(ParseMode::Html)); - let btn_label = t!("commands.pvp.button", locale = &lang_code); - let btn_data = BattleCallbackData::new(query.from.id, bet).to_data_string(); - let res = InlineQueryResultArticle::new("pvp", title, content) - .reply_markup(InlineKeyboardMarkup::new(vec![vec![ - InlineKeyboardButton::callback(btn_label, btn_data) - ]])) - .into(); + let res = build_inline_keyboard_article_result(query.from.id, &lang_code, name, bet); let mut answer = bot.answer_inline_query(query.id, vec![res]) .is_personal(true); @@ -153,6 +143,19 @@ pub async fn inline_handler(bot: Bot, query: InlineQuery) -> HandlerResult { Ok(()) } +pub(super) fn build_inline_keyboard_article_result(uid: UserId, lang_code: &LanguageCode, name: String, bet: u16) -> InlineQueryResult { + let title = t!("inline.results.titles.pvp", locale = lang_code, bet = bet); + let text = t!("commands.pvp.results.start", locale = lang_code, name = name, bet = bet); + let content = InputMessageContent::Text(InputMessageContentText::new(text).parse_mode(ParseMode::Html)); + let btn_label = t!("commands.pvp.button", locale = lang_code); + let btn_data = BattleCallbackData::new(uid, bet).to_data_string(); + InlineQueryResultArticle::new("pvp", title, content) + .reply_markup(InlineKeyboardMarkup::new(vec![vec![ + InlineKeyboardButton::callback(btn_label, btn_data) + ]])) + .into() +} + pub async fn inline_chosen_handler() -> HandlerResult { metrics::INLINE_COUNTER.finished(); Ok(())