2025-11-14 04:03:20 +00:00
|
|
|
import yaml
|
|
|
|
|
import json
|
|
|
|
|
import logging
|
2025-11-14 04:03:20 +00:00
|
|
|
import random
|
2025-11-14 04:03:20 +00:00
|
|
|
|
2025-11-14 04:03:21 +00:00
|
|
|
from db.queries import repeat_get, repeat_increment, repeat_clear, repeat_put
|
2025-11-14 04:03:20 +00:00
|
|
|
from settings import ORDERS_YML
|
2025-11-14 04:03:20 +00:00
|
|
|
|
2025-11-14 04:03:20 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
2025-11-14 04:03:20 +00:00
|
|
|
def pick_order(orders):
|
|
|
|
|
picked = random.choices(orders,
|
2026-03-04 23:33:17 +00:00
|
|
|
weights=[o.weight for o in orders],
|
2025-11-14 04:03:20 +00:00
|
|
|
k=1)[0]
|
|
|
|
|
|
|
|
|
|
result = []
|
|
|
|
|
|
2026-03-04 23:33:17 +00:00
|
|
|
result.append(picked.name)
|
2025-11-14 04:03:20 +00:00
|
|
|
|
2026-03-04 23:33:17 +00:00
|
|
|
for add_on in picked.add_ons:
|
|
|
|
|
if add_on.probability > random.random():
|
2026-03-07 18:38:44 +00:00
|
|
|
result.append(f"+ {add_on.name}")
|
2025-11-14 04:03:20 +00:00
|
|
|
|
2026-03-04 23:33:17 +00:00
|
|
|
return (result, picked.repeat)
|
2025-11-14 04:03:20 +00:00
|
|
|
|
2025-11-14 04:03:20 +00:00
|
|
|
def read_config():
|
|
|
|
|
with open(ORDERS_YML) as stream:
|
2025-11-14 04:03:20 +00:00
|
|
|
try:
|
|
|
|
|
orders = yaml.safe_load(stream)
|
|
|
|
|
except yaml.YAMLError as exc:
|
2025-11-14 04:03:20 +00:00
|
|
|
logger.error(exc)
|
|
|
|
|
return orders
|
2025-11-14 04:03:20 +00:00
|
|
|
|
2026-03-04 23:33:17 +00:00
|
|
|
def generate_order(orders_pool):
|
2025-11-14 04:03:20 +00:00
|
|
|
# Do we want to repeat?
|
2026-03-04 23:33:17 +00:00
|
|
|
if orders_pool.repeat.count() != 0:
|
|
|
|
|
repeat = orders_pool.repeat.get()
|
2025-11-14 04:03:21 +00:00
|
|
|
if repeat.probability > random.random():
|
2026-03-04 23:33:17 +00:00
|
|
|
repeat_increment(repeat.id)
|
2025-11-14 04:03:20 +00:00
|
|
|
return {
|
2025-11-14 04:03:21 +00:00
|
|
|
"orders": json.loads(repeat.orders),
|
|
|
|
|
"count": repeat.count
|
2025-11-14 04:03:20 +00:00
|
|
|
}
|
|
|
|
|
else:
|
2026-03-04 23:33:17 +00:00
|
|
|
repeat_clear(repeat.id)
|
2025-11-14 04:03:20 +00:00
|
|
|
|
2026-03-04 23:33:17 +00:00
|
|
|
if orders_pool.probability < random.random():
|
2025-11-14 04:03:20 +00:00
|
|
|
# No orders today
|
|
|
|
|
return { }
|
|
|
|
|
|
|
|
|
|
# Pick new orders
|
2026-03-04 23:33:17 +00:00
|
|
|
(result, repeat_p) = pick_order(orders_pool.orders)
|
2025-11-14 04:03:20 +00:00
|
|
|
|
|
|
|
|
# Log the repeat
|
|
|
|
|
if repeat_p > 0.0:
|
2026-03-04 23:33:17 +00:00
|
|
|
repeat_put(orders_pool.id, repeat_p, json.dumps(result))
|
2025-11-14 04:03:20 +00:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
"orders": result
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-05 03:01:35 +00:00
|
|
|
def generate_punishment(orders_pool):
|
|
|
|
|
# Pick new orders
|
|
|
|
|
(result, repeat_p) = pick_order(orders_pool.orders)
|
2025-11-14 04:03:20 +00:00
|
|
|
|
2026-03-05 03:01:35 +00:00
|
|
|
return {
|
|
|
|
|
"orders": result
|
|
|
|
|
}
|