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

86 lines
3.1 KiB
Python
Raw Normal View History

2023-02-27 12:19:41 +00:00
import secrets
2023-04-19 07:41:39 +00:00
2023-05-21 09:11:39 +00:00
from config import Config
from core.builtins import Bot
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-06-07 09:22:46 +00:00
MAX_COIN_NUM = int(Config('coin_limit'))
2023-07-11 16:39:52 +00:00
FACE_UP_RATE = int(Config('coin_faceup_rate')) # n/10000
FACE_DOWN_RATE = int(Config('coin_facedown_rate'))
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: Bot.MessageSession):
2023-04-23 09:21:29 +00:00
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}}')
async def _(msg: Bot.MessageSession, amount: int = 1):
2023-06-09 09:59:57 +00:00
await msg.finish(await flipCoins(amount, msg))
2023-02-27 12:56:20 +00:00
2023-03-05 11:15:44 +00:00
2023-07-11 18:12:37 +00:00
@coin.regex(r"[丢抛]([^个|個|枚]*)?[个個枚]?硬[币幣]", desc='{coin.help.regex.desc}')
async def _(message: Bot.MessageSession):
2023-02-27 12:19:41 +00:00
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-07-11 16:39:52 +00:00
if FACE_UP_RATE + FACE_DOWN_RATE > 10000 or FACE_UP_RATE < 0 or FACE_DOWN_RATE < 0 or MAX_COIN_NUM <= 0:
2023-07-13 04:26:10 +00:00
raise OverflowError(msg.locale.t("error.config"))
2023-07-11 02:44:42 +00:00
if count > MAX_COIN_NUM:
2023-05-21 10:24:03 +00:00
return msg.locale.t("coin.message.error.out_of_range", max=count_max)
2023-05-23 01:55:36 +00:00
if count == 0:
2023-04-16 03:07:57 +00:00
return msg.locale.t("coin.message.error.nocoin")
2023-05-23 01:55:36 +00:00
if count < 0:
return msg.locale.t("coin.message.error.amount")
2023-02-27 12:19:41 +00:00
faceUp = 0
faceDown = 0
stand = 0
for i in range(count):
randnum = secrets.randbelow(10000)
2023-07-11 02:44:42 +00:00
if randnum < FACE_UP_RATE:
2023-02-27 12:19:41 +00:00
faceUp += 1
2023-07-11 02:44:42 +00:00
elif randnum < FACE_UP_RATE + FACE_DOWN_RATE:
2023-02-27 12:19:41 +00:00
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:
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-05-21 07:49:51 +00:00
return head + msg.locale.t("coin.message.all.head")
2023-02-27 12:19:41 +00:00
if not (stand or faceUp):
2023-05-21 07:49:51 +00:00
return head + msg.locale.t("coin.message.all.tail")
2023-02-27 12:19:41 +00:00
if not (faceUp or faceDown):
2023-05-21 07:49:51 +00:00
return head + msg.locale.t("coin.message.all.stand")
2023-04-16 02:38:47 +00:00
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-05-21 08:17:20 +00:00
elif faceDown:
2023-07-07 06:27:01 +00:00
output += msg.locale.t("coin.message.mix.tail2", tail=faceDown)
2023-05-21 08:13:22 +00:00
if faceUp and faceDown:
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-05-21 08:06:50 +00:00
else:
2023-06-26 13:18:54 +00:00
output += msg.locale.t("message.end")
2023-02-27 13:38:17 +00:00
return output