Archived
1
0
Fork 0
This repository has been archived on 2024-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
akari-bot/modules/coin/__init__.py

79 lines
2.9 KiB
Python
Raw Normal View History

2023-02-27 12:19:41 +00:00
import secrets
from core.builtins.message import MessageSession
2023-03-04 08:51:56 +00:00
from core.component import module
2023-03-01 13:30:11 +00:00
from .zhNum2Int import Zh2Int
2023-02-27 12:19:41 +00:00
2023-02-27 13:24:30 +00:00
MAX_COIN_NUM = 100
FACE_UP_RATE = 4975 # n/10000
2023-02-27 12:19:41 +00:00
FACE_DOWN_RATE = 4975
COIN_DROP_PLACES = ["地上","桌子上","桌子底下","门口","窗户外","月球"] # 硬币可能掉落的位置
2023-03-04 08:51:56 +00:00
coin = module('coin', developers=['Light-Beacon'], desc='抛n枚硬币', recommend_modules=['coin_regex'])
2023-02-27 12:56:20 +00:00
2023-03-04 08:51:56 +00:00
@coin.command('[<amount>] {抛n枚硬币}')
2023-02-27 12:56:20 +00:00
async def _(msg: MessageSession):
amount = msg.parsed_msg.get('<amount>', '1')
if not amount.isdigit():
await msg.finish('发生错误:无效的硬币个数:' + amount)
else:
await msg.finish(await flipCoins(int(amount)))
2023-03-04 08:51:56 +00:00
@coin.regex(r"[丢|抛](.*)?[个|枚]?硬币", desc='[丢/抛](n)[个/枚]硬币')
2023-02-27 12:19:41 +00:00
async def _(message: MessageSession):
groups = message.matched_msg.groups()
count = groups[0] if groups[0] else '1'
2023-03-01 13:30:11 +00:00
if count.isdigit():
count = int(count)
else:
try:
count = Zh2Int(count)
except ValueError as e:
await message.finish("发生错误:" + e.message)
2023-02-27 12:19:41 +00:00
await message.finish(await flipCoins(count))
async def flipCoins(count:int):
if count > MAX_COIN_NUM:
return f"发生错误:你最多只能抛 {MAX_COIN_NUM} 个硬币"
2023-02-27 12:23:31 +00:00
if count <= 0:
2023-02-27 13:41:30 +00:00
return f"发生错误:你抛了个空气,什么也没发生..."
2023-02-27 13:24:30 +00:00
if FACE_UP_RATE + FACE_DOWN_RATE > 10000 or FACE_UP_RATE < 0 or FACE_DOWN_RATE < 0:
2023-02-27 13:41:30 +00:00
raise OverflowError("发生错误:硬币概率错误")
2023-02-27 12:19:41 +00:00
faceUp = 0
faceDown = 0
stand = 0
for i in range(count):
randnum = secrets.randbelow(10000)
if randnum < FACE_UP_RATE:
faceUp += 1
elif randnum < FACE_UP_RATE + FACE_DOWN_RATE:
faceDown += 1
else:
stand += 1
2023-02-27 13:24:30 +00:00
head = f"你抛了 {count} 枚硬币,"
2023-02-27 12:19:41 +00:00
if count == 1:
drop_place = COIN_DROP_PLACES[secrets.randbelow(len(COIN_DROP_PLACES))]
2023-02-27 13:41:30 +00:00
head += f"它掉到了{drop_place}...\n"
2023-02-27 12:19:41 +00:00
if faceUp:
return head + "...是正面!"
elif faceDown:
return head + "...是反面!"
else:
2023-02-27 13:38:17 +00:00
return head + "...它立起来了!"
2023-02-27 12:19:41 +00:00
else:
if not (stand or faceDown):
2023-02-27 14:32:09 +00:00
return head + "它们...\n...全是正面!"
2023-02-27 12:19:41 +00:00
if not (stand or faceUp):
2023-02-27 14:32:09 +00:00
return head + "它们...\n...全是反面!"
2023-02-27 12:19:41 +00:00
if not (faceUp or faceDown):
2023-02-27 13:38:17 +00:00
return head + "它们...\n...全都立起来了?!"
2023-02-27 13:41:30 +00:00
output = head + "其中...\n"
2023-02-27 12:19:41 +00:00
if faceUp:
2023-02-27 14:32:09 +00:00
output += f" {faceUp} 枚是正面,"
2023-02-27 12:19:41 +00:00
if faceDown:
2023-03-01 09:47:54 +00:00
output += f"{faceDown} 枚是反面"
2023-02-27 12:19:41 +00:00
if stand:
2023-02-27 14:32:09 +00:00
output += f"...还有 {stand} 枚立起来了!"
2023-02-27 13:38:17 +00:00
else:
output += f""
return output