Files
henan-backend/olympus-py/_make_templates.py
T
2026-09-21 19:21:35 +08:00

72 lines
3.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""给每箱 × 每身份建"默认配比"模板并置为生效。
为什么需要:箱子的一个 cycle 抽满后(drawn ≥ N),Java 会
cycleRepository.resetCycle() + distributionService.refillFromConfig(boxId, audience)
而 refillFromConfig 是**按 (boxId, audience) 查生效模板**(is_active=1,取 id 最大的一条):
- 有生效模板 → 按模板 items_json 重写 count(writeDistribution),并按固定池反推回写 total_ornaments
- 没有模板 → 只把 used 归零(配比不变)
所以要让"抽完按模板重填",必须给每个 (box, audience) 都有一条生效模板。
用法:
python _make_templates.py --dry # 只看会写什么
python _make_templates.py # 写入
"""
import argparse
import json
from collections import defaultdict
import olympus_db as db
from olympus.config import AUDIENCES
TEMPLATE_NAME = '默认配比'
def build_rows():
rows = []
with db.get_conn() as conn:
cur = conn.cursor()
cur.execute('SELECT box_id, box_type, bet, rtp FROM game_olympus_box ORDER BY box_id')
boxes = cur.fetchall()
cur.execute('''SELECT box_id, ornament_id, audience, count
FROM game_olympus_box_ornament ORDER BY box_id, audience, ornament_id''')
cfg = defaultdict(dict)
for box_id, orn, aud, cnt in cur.fetchall():
cfg[(int(box_id), aud)][int(orn)] = int(cnt)
for box_id, box_type, bet, rtp in boxes:
for aud in AUDIENCES:
items = cfg.get((int(box_id), aud), {})
if not items:
print('!! box%s %s 无配比,跳过' % (box_id, aud))
continue
items_json = json.dumps(
[{'ornamentId': k, 'count': v} for k, v in sorted(items.items())],
ensure_ascii=False, separators=(',', ':'))
rows.append((int(box_id), aud, TEMPLATE_NAME, sum(items.values()), items_json))
return rows
def main():
ap = argparse.ArgumentParser()
ap.add_argument('--dry', action='store_true')
args = ap.parse_args()
rows = build_rows()
print('待写模板 %d 条(%d 箱 × %d 身份)' % (len(rows), len(rows) // len(AUDIENCES), len(AUDIENCES)))
if args.dry:
print('样例:', rows[0][:4], rows[0][4][:80], '...')
return
with db.get_conn() as conn:
cur = conn.cursor()
# 先取消原有生效模板,再 upsert(表上有 uk_box_aud_name 唯一键)
cur.execute('''UPDATE game_olympus_box_template SET is_active=0''')
cur.executemany('''INSERT INTO game_olympus_box_template
(box_id, audience, name, total_rounds, items_json, is_active, create_time)
VALUES (%s,%s,%s,%s,%s,1,NOW())
ON DUPLICATE KEY UPDATE total_rounds=VALUES(total_rounds),
items_json=VALUES(items_json), is_active=1, create_time=NOW()''', rows)
cur.execute('''SELECT COUNT(*), SUM(is_active) FROM game_olympus_box_template''')
print('模板表: 总行数/生效行数 =', cur.fetchone())
if __name__ == '__main__':
main()