2021-04-09 10:17:38 +02:00
|
|
|
import random
|
|
|
|
|
|
|
|
|
|
|
|
class Promotioner:
|
|
|
|
"""
|
|
|
|
Promotioner is used to select promotion randomly based on weights of every promotion.
|
|
|
|
"""
|
2022-09-02 17:44:56 +02:00
|
|
|
def __init__(self, promotions: list[dict], default_promotion_index: int = 0):
|
2021-04-09 10:17:38 +02:00
|
|
|
self.promotions = promotions
|
2022-09-02 17:44:56 +02:00
|
|
|
self.default_promotion_index = default_promotion_index
|
2021-04-09 10:17:38 +02:00
|
|
|
self.partial_sums: list = [self.promotions[0]['weight']]
|
|
|
|
for promotion in self.promotions[1:]:
|
|
|
|
self.partial_sums.append(promotion['weight'] + self.partial_sums[-1])
|
|
|
|
|
|
|
|
def choose_promotion(self, language: str = 'en') -> str:
|
|
|
|
pivot = random.randrange(self.partial_sums[-1])
|
|
|
|
for partial_sum, promotion in zip(self.partial_sums, self.promotions):
|
|
|
|
if partial_sum <= pivot:
|
|
|
|
continue
|
|
|
|
if language in promotion['texts']:
|
|
|
|
return promotion['texts'][language]
|
2022-09-02 17:44:56 +02:00
|
|
|
elif promotion.get('local', False):
|
|
|
|
default_promotion = self.promotions[self.default_promotion_index]
|
|
|
|
if language in default_promotion['texts']:
|
|
|
|
return default_promotion['texts'][language]
|
|
|
|
return default_promotion['texts']['en']
|
|
|
|
else:
|
|
|
|
return promotion['texts']['en']
|