Skip to content

Commit

Permalink
Add a default attack option (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonid Kozarin committed Sep 26, 2024
1 parent 3e6fdda commit 0ccee30
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 14 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/config/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct AppConfig {
pub top_limit: u16,
pub loan_payout_ratio: f32,
pub dod_rich_exclusion_ratio: Option<Ratio>,
pub pvp_default_bet: u16,
pub announcements: AnnouncementsConfig,
pub command_toggles: CachedEnvToggles,
}
Expand All @@ -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);
Expand All @@ -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: [
Expand Down
5 changes: 3 additions & 2 deletions src/handlers/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<InlineQueryResult> = InlineCommand::iter()
let mut results: Vec<InlineQueryResult> = InlineCommand::iter()
.map(|cmd| cmd.to_string())
.filter(|cmd| app_config.command_toggles.enabled(cmd))
.map(|key| {
Expand All @@ -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);
Expand Down
27 changes: 15 additions & 12 deletions src/handlers/pvp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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);
Expand All @@ -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(())
Expand Down

0 comments on commit 0ccee30

Please sign in to comment.