85 lines
2.7 KiB
Python
85 lines
2.7 KiB
Python
from __future__ import annotations
|
|
|
|
from models.config import BotConfig, RateConfig
|
|
from models.pricing import CookieDonationInfo, PricingResult
|
|
|
|
|
|
def find_rate_for_cookie(all_time: int, year: int, rates: dict[str, RateConfig]):
|
|
best = None
|
|
best_price = -1.0
|
|
for key, rate in rates.items():
|
|
donate = year if rate.type == "year" else all_time
|
|
if rate.min <= donate <= rate.max and rate.price > best_price:
|
|
best_price = rate.price
|
|
best = (key, rate, donate)
|
|
if best:
|
|
return best
|
|
return (None, None, 0)
|
|
|
|
|
|
def calc_avg_price(donates: list[int], cfg: BotConfig) -> float:
|
|
if not donates:
|
|
return 0.0
|
|
avg = sum(donates) / len(donates)
|
|
price = avg / 1000.0
|
|
price = max(cfg.avg_min_price, min(cfg.avg_max_price, price))
|
|
return round(price, 2)
|
|
|
|
|
|
def price_cookie_batch(
|
|
donates: list[CookieDonationInfo],
|
|
cfg: BotConfig,
|
|
) -> tuple[list[PricingResult], bool, float]:
|
|
priced: list[PricingResult] = []
|
|
unpriced: list[CookieDonationInfo] = []
|
|
|
|
for item in donates:
|
|
key, rate, donate_used = find_rate_for_cookie(item.all_time, item.year, cfg.rates)
|
|
if rate:
|
|
priced.append(
|
|
PricingResult(
|
|
cookie=item.cookie,
|
|
user_id=item.user_id,
|
|
username=item.username,
|
|
all_time=item.all_time,
|
|
year=item.year,
|
|
price=rate.price,
|
|
rate_key=key,
|
|
rate_name=rate.name,
|
|
donate_used=donate_used,
|
|
)
|
|
)
|
|
elif item.all_time > 0:
|
|
unpriced.append(item)
|
|
|
|
all_donate_infos = [item for item in donates if item.all_time > 0]
|
|
total_donate_cookies = len(all_donate_infos)
|
|
|
|
avg_used = False
|
|
avg_price = 0.0
|
|
if total_donate_cookies >= cfg.avg_min_cookies:
|
|
avg_price = calc_avg_price([item.all_time for item in all_donate_infos], cfg)
|
|
avg_total = avg_price * total_donate_cookies
|
|
piece_total = sum(item.price for item in priced)
|
|
if avg_total > piece_total:
|
|
avg_used = True
|
|
|
|
payable: list[PricingResult] = []
|
|
if avg_used:
|
|
for item in all_donate_infos:
|
|
payable.append(
|
|
PricingResult(
|
|
cookie=item.cookie,
|
|
user_id=item.user_id,
|
|
username=item.username,
|
|
all_time=item.all_time,
|
|
year=item.year,
|
|
price=avg_price,
|
|
is_avg=True,
|
|
)
|
|
)
|
|
else:
|
|
payable.extend(priced)
|
|
|
|
return payable, avg_used, avg_price
|