133 lines
4.5 KiB
Python
133 lines
4.5 KiB
Python
"""抽獎邏輯;代幣不足時丟出 db.NotEnoughCoins,整筆交易不會生效"""
|
||
import random
|
||
from collections import Counter
|
||
|
||
import db
|
||
|
||
# ===== 御神籤 =====
|
||
OMIKUJI_COST = 10
|
||
OMIKUJI = [ # (名稱, exp, 權重%)
|
||
("大吉", 130, 10),
|
||
("吉", 110, 20),
|
||
("中吉", 105, 15),
|
||
("小吉", 100, 15),
|
||
("末吉", 95, 15),
|
||
("凶", 85, 15),
|
||
("大凶", 70, 10),
|
||
]
|
||
|
||
|
||
def omikuji(user_id: int) -> tuple[str, int]:
|
||
with db.conn:
|
||
db.spend(user_id, OMIKUJI_COST)
|
||
name, exp, _ = random.choices(OMIKUJI, weights=[c[2] for c in OMIKUJI])[0]
|
||
db.add(user_id, exp=exp)
|
||
return name, exp
|
||
|
||
|
||
# ===== 幸運礦工 =====
|
||
MINER_MAX_TIMES = 500
|
||
MINER_POOLS = { # (獎勵, 權重)
|
||
"small": ([7, 22], [80, 20]),
|
||
"large": ([8, 350], [99, 1]),
|
||
"risk": ([9, 114514, 300000], [99997, 2, 1]),
|
||
}
|
||
MINER_ACHIEVEMENTS = {114514: "yaju", 300000: "win_all"}
|
||
GAMBLER_THRESHOLD = 1000
|
||
|
||
|
||
def miner(user_id: int, pool: str, times: int) -> tuple[int, Counter, list[str]]:
|
||
rewards, weights = MINER_POOLS[pool]
|
||
new_achievements = []
|
||
with db.conn:
|
||
db.spend(user_id, times)
|
||
log = random.choices(rewards, weights=weights, k=times)
|
||
total = sum(log)
|
||
db.add(user_id, exp=total)
|
||
db.conn.execute(
|
||
"UPDATE users SET miner_count = miner_count + ? WHERE id = ?", (times, user_id)
|
||
)
|
||
|
||
for reward, key in MINER_ACHIEVEMENTS.items():
|
||
if reward in log and db.grant_achievement(user_id, key):
|
||
new_achievements.append(key)
|
||
if db.get_user(user_id)["miner_count"] >= GAMBLER_THRESHOLD and db.grant_achievement(user_id, "gambler"):
|
||
new_achievements.append("gambler")
|
||
return total, Counter(log), new_achievements
|
||
|
||
|
||
# ===== 刮刮樂 =====
|
||
SCRATCH_COST = 5
|
||
# 對中幾個號碼 → exp;期望值約 46 exp/張 ≈ 9.2 exp/代幣
|
||
SCRATCH_REWARD = {0: 10, 1: 77, 2: 231, 3: 693}
|
||
|
||
|
||
def scratch(user_id: int) -> tuple[list[int], list[int], int, int]:
|
||
with db.conn:
|
||
db.spend(user_id, SCRATCH_COST)
|
||
opponent = random.sample(range(1, 61), 3)
|
||
player = random.sample(range(1, 61), 9)
|
||
hits = len(set(opponent) & set(player))
|
||
exp = SCRATCH_REWARD[hits]
|
||
db.add(user_id, exp=exp)
|
||
return opponent, player, hits, exp
|
||
|
||
|
||
# ===== 扭蛋 =====
|
||
GACHA_COST = 3
|
||
GACHA_SETS = { # 成就 key: (系列物品, 集齊獎勵)
|
||
"pc_gamer": (
|
||
["主機殼", "處理器", "主機板", "記憶體", "顯示卡", "塔扇", "電源供應器", "硬碟"],
|
||
{"exp": 480},
|
||
),
|
||
"shark_family": (
|
||
["白色鯊魚", "紅色鯊魚", "橘色鯊魚", "黃色鯊魚", "綠色鯊魚",
|
||
"藍色鯊魚", "紫色鯊魚", "灰色鯊魚", "黑色鯊魚", "彩色鯊魚"],
|
||
{"exp": 600},
|
||
),
|
||
"oshi": (
|
||
["作者的左手模型", "作者的右手模型", "作者的頭髮模型", "作者的左腳模型",
|
||
"作者的右腳模型", "作者的身體模型", "作者的頭模型"],
|
||
{"coin": 150},
|
||
),
|
||
"lazy": (
|
||
["床", "枕頭", "棉被", "冷氣", "椅子", "電腦桌", "筆電", "螢幕", "手機", "插座"],
|
||
{"exp": 600},
|
||
),
|
||
"crossdress": (
|
||
["假髮", "小褲褲", "上衣", "小裙子", "袖套", "絲襪", "褲襪", "蝴蝶結", "髮飾", "化妝品"],
|
||
{"exp": 600},
|
||
),
|
||
}
|
||
GACHA_SIDE = [30, 30, 25, 35] # 旁池 exp
|
||
GACHA_RARE = ["shut_up", "bottom"]
|
||
GACHA_RARE_RATE = 0.03
|
||
GACHA_POOL = [item for items, _ in GACHA_SETS.values() for item in items] + GACHA_SIDE
|
||
|
||
|
||
def gacha(user_id: int, draws: int) -> tuple[list[str], list[str]]:
|
||
lines = []
|
||
new_achievements = []
|
||
with db.conn:
|
||
db.spend(user_id, GACHA_COST * draws)
|
||
for _ in range(draws):
|
||
result = random.choice(GACHA_POOL)
|
||
if isinstance(result, int):
|
||
db.add(user_id, exp=result)
|
||
lines.append(f"{result} exp")
|
||
else:
|
||
count = db.add_item(user_id, result)
|
||
lines.append(f"{result}(持有 {count})")
|
||
|
||
if random.random() < GACHA_RARE_RATE:
|
||
key = random.choice(GACHA_RARE)
|
||
if db.grant_achievement(user_id, key):
|
||
new_achievements.append(key)
|
||
|
||
backpack = db.get_backpack(user_id)
|
||
for key, (items, reward) in GACHA_SETS.items():
|
||
if all(backpack.get(i, 0) > 0 for i in items) and db.grant_achievement(user_id, key):
|
||
db.add(user_id, **reward)
|
||
new_achievements.append(key)
|
||
return lines, new_achievements
|