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,
|
|
|
|
|
weights=[o['weight'] for o in orders],
|
|
|
|
|
k=1)[0]
|
|
|
|
|
|
|
|
|
|
result = []
|
|
|
|
|
repeat = 0.0
|
|
|
|
|
|
|
|
|
|
if('text' in picked):
|
|
|
|
|
result.append(picked['text'])
|
|
|
|
|
|
|
|
|
|
if('repeat' in picked):
|
|
|
|
|
repeat = picked['repeat']
|
|
|
|
|
|
|
|
|
|
if('add' in picked):
|
|
|
|
|
for addition in picked['add']:
|
|
|
|
|
if addition['probability'] > random.random():
|
|
|
|
|
result.append(addition['text'])
|
|
|
|
|
|
|
|
|
|
if('pick' in picked):
|
|
|
|
|
(new_result, new_repeat) = pick_order(picked['pick'])
|
|
|
|
|
result += new_result
|
|
|
|
|
if new_repeat > 0.0:
|
|
|
|
|
repeat = new_repeat
|
|
|
|
|
|
|
|
|
|
return (result, repeat)
|
|
|
|
|
|
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
|
|
|
|
2025-11-14 04:03:20 +00:00
|
|
|
def generate_order():
|
|
|
|
|
# Do we want to repeat?
|
2025-11-14 04:03:21 +00:00
|
|
|
repeat = repeat_get()
|
2025-11-14 04:03:20 +00:00
|
|
|
if repeat is not None:
|
2025-11-14 04:03:21 +00:00
|
|
|
if repeat.probability > random.random():
|
|
|
|
|
repeat_increment()
|
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:
|
2025-11-14 04:03:21 +00:00
|
|
|
repeat_clear()
|
2025-11-14 04:03:20 +00:00
|
|
|
|
|
|
|
|
orders_config = read_config()
|
2025-11-14 04:03:20 +00:00
|
|
|
|
2025-11-14 04:03:20 +00:00
|
|
|
if orders_config['orders_probability'] < random.random():
|
2025-11-14 04:03:20 +00:00
|
|
|
# No orders today
|
|
|
|
|
return { }
|
|
|
|
|
|
|
|
|
|
# Pick new orders
|
|
|
|
|
(result, repeat_p) = pick_order(orders_config['orders'])
|
|
|
|
|
|
|
|
|
|
# Log the repeat
|
|
|
|
|
if repeat_p > 0.0:
|
2025-11-14 04:03:21 +00:00
|
|
|
repeat_put(repeat_p, json.dumps(result))
|
2025-11-14 04:03:20 +00:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
"orders": result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def generate_punishment():
|
|
|
|
|
orders_config = read_config()
|
|
|
|
|
|
|
|
|
|
(result, repeat_p) = pick_order(orders_config['punishments'])
|
|
|
|
|
|
|
|
|
|
return result
|