diff options
| author | levlam <levlam@telegram.org> | 2024-10-23 13:51:12 +0300 |
|---|---|---|
| committer | levlam <levlam@telegram.org> | 2024-10-23 13:51:12 +0300 |
| commit | 4b8dd3b9de53dd408eda5fc5d5776c94cb2d3857 (patch) | |
| tree | f220e2f931690fe0762d258471f29169d316ee3e /td/telegram/Premium.cpp | |
| parent | 02b7d872b886b6dad14d1794a8397ba3c1c266db (diff) | |
Add premiumGiftCodePaymentOption.discount_percentage.
Diffstat (limited to 'td/telegram/Premium.cpp')
| -rw-r--r-- | td/telegram/Premium.cpp | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/td/telegram/Premium.cpp b/td/telegram/Premium.cpp index 4131be2e1..3f5e788bd 100644 --- a/td/telegram/Premium.cpp +++ b/td/telegram/Premium.cpp @@ -402,6 +402,21 @@ class GetPremiumGiftCodeOptionsQuery final : public Td::ResultHandler { } auto results = result_ptr.move_as_ok(); + td::remove_if(results, [](const telegram_api::object_ptr<telegram_api::premiumGiftCodeOption> &payment_option) { + return payment_option->users_ <= 0 || payment_option->months_ <= 0 || payment_option->amount_ <= 0; + }); + auto get_monthly_price = [](const telegram_api::object_ptr<telegram_api::premiumGiftCodeOption> &payment_option) { + return static_cast<double>(payment_option->amount_) / static_cast<double>(payment_option->months_); + }; + FlatHashMap<int32, double> max_prices; + for (auto &result : results) { + auto &max_price = max_prices[result->users_]; + auto price = get_monthly_price(result); + if (price > max_price) { + max_price = price; + } + } + vector<td_api::object_ptr<td_api::premiumGiftCodePaymentOption>> options; for (auto &result : results) { if (result->store_product_.empty()) { @@ -409,9 +424,10 @@ class GetPremiumGiftCodeOptionsQuery final : public Td::ResultHandler { } else if (result->store_quantity_ <= 0) { result->store_quantity_ = 1; } + double relative_price = get_monthly_price(result) / max_prices[result->users_]; options.push_back(td_api::make_object<td_api::premiumGiftCodePaymentOption>( - result->currency_, result->amount_, result->users_, result->months_, result->store_product_, - result->store_quantity_)); + result->currency_, result->amount_, static_cast<int32>(100 * (1.0 - relative_price)), result->users_, + result->months_, result->store_product_, result->store_quantity_)); } promise_.set_value(td_api::make_object<td_api::premiumGiftCodePaymentOptions>(std::move(options))); |
