2023-02-27 12:19:41 +00:00
|
|
|
import secrets
|
2023-04-19 07:41:39 +00:00
|
|
|
|
2023-02-27 12:19:41 +00:00
|
|
|
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
|
2023-04-30 03:30:59 +00:00
|
|
|
FACE_UP_RATE = 4994 # n/10000
|
2023-03-11 12:12:27 +00:00
|
|
|
FACE_DOWN_RATE = 4994
|
2023-03-05 11:15:44 +00:00
|
|
|
COIN_DROP_PLACES = ["地上", "桌子上", "桌子底下", "门口", "窗户外", "月球"] # 硬币可能掉落的位置
|
2023-02-27 12:19:41 +00:00
|
|
|
|
2023-04-16 01:39:50 +00:00
|
|
|
coin = module('coin', developers=['Light-Beacon'], desc='{coin.help.desc}')
|
2023-02-27 12:56:20 +00:00
|
|
|
|
2023-04-30 03:30:59 +00:00
|
|
|
|
2023-04-23 09:21:29 +00:00
|
|
|
@coin.command('{{coin.help}}')
|
|
|
|
async def _(msg: MessageSession):
|
|
|
|
await msg.finish(await flipCoins(1, msg))
|
2023-03-05 11:15:44 +00:00
|
|
|
|
2023-04-30 03:30:59 +00:00
|
|
|
|
2023-04-21 05:54:19 +00:00
|
|
|
@coin.command('[<amount>] {{coin.help}}')
|
2023-02-27 12:56:20 +00:00
|
|
|
async def _(msg: MessageSession):
|
2023-04-21 10:11:03 +00:00
|
|
|
amount = msg.parsed_msg.get('<amount>', '1')
|
2023-02-27 12:56:20 +00:00
|
|
|
if not amount.isdigit():
|
2023-04-16 01:39:50 +00:00
|
|
|
await msg.finish(msg.locale.t('coin.message.error.amount') + amount)
|
2023-02-27 12:56:20 +00:00
|
|
|
else:
|
2023-04-16 03:16:26 +00:00
|
|
|
await msg.finish(await flipCoins(int(amount), msg))
|
2023-02-27 12:56:20 +00:00
|
|
|
|
2023-03-05 11:15:44 +00:00
|
|
|
|
2023-03-11 11:33:33 +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)
|
2023-03-11 10:48:27 +00:00
|
|
|
except ValueError as ex:
|
2023-04-16 01:42:11 +00:00
|
|
|
await message.finish(message.locale.t("error") + str(ex))
|
2023-04-16 03:09:20 +00:00
|
|
|
await message.finish(await flipCoins(count, message))
|
2023-02-27 12:19:41 +00:00
|
|
|
|
2023-03-05 11:15:44 +00:00
|
|
|
|
2023-04-16 02:38:47 +00:00
|
|
|
async def flipCoins(count: int, msg):
|
2023-02-27 12:19:41 +00:00
|
|
|
if count > MAX_COIN_NUM:
|
2023-04-16 03:07:57 +00:00
|
|
|
return msg.locale.t("coin.message.error.max", max=MAX_COIN_NUM)
|
2023-02-27 12:23:31 +00:00
|
|
|
if count <= 0:
|
2023-04-16 03:07:57 +00:00
|
|
|
return msg.locale.t("coin.message.error.nocoin")
|
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-04-16 03:07:57 +00:00
|
|
|
raise OverflowError(msg.locale.t("coin.message.error.rate"))
|
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-05-21 03:48:33 +00:00
|
|
|
head = msg.locale.t("coin.message.prompt", count=count)
|
2023-02-27 12:19:41 +00:00
|
|
|
if count == 1:
|
|
|
|
drop_place = COIN_DROP_PLACES[secrets.randbelow(len(COIN_DROP_PLACES))]
|
2023-04-16 03:07:57 +00:00
|
|
|
head += msg.locale.t("coin.message.drop_place", drop_place=drop_place) + '\n'
|
2023-02-27 12:19:41 +00:00
|
|
|
if faceUp:
|
2023-04-16 02:38:47 +00:00
|
|
|
return head + msg.locale.t("coin.message.head")
|
2023-02-27 12:19:41 +00:00
|
|
|
elif faceDown:
|
2023-04-16 02:38:47 +00:00
|
|
|
return head + msg.locale.t("coin.message.tail")
|
2023-02-27 12:19:41 +00:00
|
|
|
else:
|
2023-04-16 02:38:47 +00:00
|
|
|
return head + msg.locale.t("coin.message.stand")
|
2023-02-27 12:19:41 +00:00
|
|
|
else:
|
|
|
|
if not (stand or faceDown):
|
2023-04-16 02:38:47 +00:00
|
|
|
return head + msg.locale.t("coin.message.head.all")
|
2023-02-27 12:19:41 +00:00
|
|
|
if not (stand or faceUp):
|
2023-04-16 02:38:47 +00:00
|
|
|
return head + msg.locale.t("coin.message.tail.all")
|
2023-02-27 12:19:41 +00:00
|
|
|
if not (faceUp or faceDown):
|
2023-04-16 02:38:47 +00:00
|
|
|
return head + msg.locale.t("coin.message.stand.all")
|
|
|
|
output = head + msg.locale.t("coin.message.mix")
|
2023-02-27 12:19:41 +00:00
|
|
|
if faceUp:
|
2023-04-16 02:38:47 +00:00
|
|
|
output += msg.locale.t("coin.message.mix.head", head=faceUp)
|
2023-02-27 12:19:41 +00:00
|
|
|
if faceDown:
|
2023-04-16 02:38:47 +00:00
|
|
|
output += msg.locale.t("coin.message.mix.tail", tail=faceDown)
|
2023-02-27 12:19:41 +00:00
|
|
|
if stand:
|
2023-04-16 02:38:47 +00:00
|
|
|
output += msg.locale.t("coin.message.mix.stand", stand=stand)
|
2023-02-27 13:38:17 +00:00
|
|
|
else:
|
2023-04-16 02:38:47 +00:00
|
|
|
output += msg.locale.t("coin.message.mix.end")
|
2023-02-27 13:38:17 +00:00
|
|
|
return output
|