From 0919b64937d635acb6d3c4f56403305726edd800 Mon Sep 17 00:00:00 2001 From: Nattiden <2682645990@qq.com> Date: Mon, 27 Feb 2023 20:19:41 +0800 Subject: [PATCH 1/9] coin --- modules/coin/__init__.py | 61 ++++++++++++++++++++++++++++++++++++++ modules/random/__init__.py | 3 +- 2 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 modules/coin/__init__.py diff --git a/modules/coin/__init__.py b/modules/coin/__init__.py new file mode 100644 index 00000000..455a8b43 --- /dev/null +++ b/modules/coin/__init__.py @@ -0,0 +1,61 @@ +import secrets +from core.builtins.message import MessageSession +from core.component import on_regex + +MAX_COIN_NUM = 20 +FACE_UP_RATE = 4975 # n/10000 +FACE_DOWN_RATE = 4975 +COIN_DROP_PLACES = ["地上","桌子上","桌子底下","门口","窗户外","月球"] # 硬币可能掉落的位置 + +dicergex = on_regex('coin_regex', + desc='打开后将在发送的聊天内容匹配以下信息时执行对应命令:\n' + '[丢/抛](n)[个/枚]硬币', developers=['Light-Beacon']) + +@dicergex.handle(r"[丢|抛]([0-9]*)?[个|枚]?硬币") +async def _(message: MessageSession): + groups = message.matched_msg.groups() + count = groups[0] if groups[0] else '1' + count = int(count) + await message.finish(await flipCoins(count)) + +async def flipCoins(count:int): + if count > MAX_COIN_NUM: + return f"发生错误:你最多只能抛 {MAX_COIN_NUM} 个硬币" + if FACE_UP_RATE + FACE_DOWN_RATE > 10000: + raise OverflowError("硬币概率总和超过最大概率") + 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 + head = f"你投了 {count} 个硬币," + if count == 1: + drop_place = COIN_DROP_PLACES[secrets.randbelow(len(COIN_DROP_PLACES))] + head += f"它掉到了{drop_place}...\n" + if faceUp: + return head + "...是正面!" + elif faceDown: + return head + "...是反面!" + else: + return head + "...它...它立起来了!" + else: + if not (stand or faceDown): + return "它们...\n...全是正面!" + if not (stand or faceUp): + return "它们...\n...全是反面!" + if not (faceUp or faceDown): + return "它们...\n...全都立起来了!?!?天哪!" + output = "其中...\n" + if faceUp: + output += f"{faceUp}个是正面," + if faceDown: + output += f"{faceDown}个是反面" + if stand: + output += f"...还有{stand}个立起来了!" + return output \ No newline at end of file diff --git a/modules/random/__init__.py b/modules/random/__init__.py index 910c3fbc..c7a4041b 100644 --- a/modules/random/__init__.py +++ b/modules/random/__init__.py @@ -3,8 +3,7 @@ import secrets from core.builtins import Bot from core.component import on_command -r = on_command('random', alias={'rand': 'random', 'rng': 'random', 'coin': 'random choice 正面 反面', - 'random coin': 'random choice 正面 反面'}, developers=['Dianliang233'], desc='随机数生成器(密码学安全)', ) +r = on_command('random', alias={'rand': 'random', 'rng': 'random'}, developers=['Dianliang233'], desc='随机数生成器(密码学安全)', ) @r.handle('number {生成区间内的随机整数}', ) From 7d191cfb84ac3bcda4b692beaaf5abd204589c85 Mon Sep 17 00:00:00 2001 From: Nattiden <2682645990@qq.com> Date: Mon, 27 Feb 2023 20:23:31 +0800 Subject: [PATCH 2/9] cnt 0 err --- modules/coin/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/modules/coin/__init__.py b/modules/coin/__init__.py index 455a8b43..2d02cb9a 100644 --- a/modules/coin/__init__.py +++ b/modules/coin/__init__.py @@ -21,6 +21,8 @@ async def _(message: MessageSession): async def flipCoins(count:int): if count > MAX_COIN_NUM: return f"发生错误:你最多只能抛 {MAX_COIN_NUM} 个硬币" + if count <= 0: + return f"你投了个空气,什么也没发生..." if FACE_UP_RATE + FACE_DOWN_RATE > 10000: raise OverflowError("硬币概率总和超过最大概率") faceUp = 0 From 842582b637f6af824bff38567d87213f07001ee9 Mon Sep 17 00:00:00 2001 From: Nattiden <2682645990@qq.com> Date: Mon, 27 Feb 2023 20:56:20 +0800 Subject: [PATCH 3/9] coin command --- modules/coin/__init__.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/modules/coin/__init__.py b/modules/coin/__init__.py index 2d02cb9a..cb442845 100644 --- a/modules/coin/__init__.py +++ b/modules/coin/__init__.py @@ -1,17 +1,27 @@ import secrets from core.builtins.message import MessageSession -from core.component import on_regex +from core.component import on_command,on_regex MAX_COIN_NUM = 20 FACE_UP_RATE = 4975 # n/10000 FACE_DOWN_RATE = 4975 COIN_DROP_PLACES = ["地上","桌子上","桌子底下","门口","窗户外","月球"] # 硬币可能掉落的位置 -dicergex = on_regex('coin_regex', +coin = on_command('coin', developers=['Light-Beacon'], desc='抛n枚硬币') + +@coin.handle('[] {投n枚硬币}',) +async def _(msg: MessageSession): + amount = msg.parsed_msg.get('', '1') + if not amount.isdigit(): + await msg.finish('发生错误:无效的硬币个数:' + amount) + else: + await msg.finish(await flipCoins(int(amount))) + +coinrgex = on_regex('coin_regex', desc='打开后将在发送的聊天内容匹配以下信息时执行对应命令:\n' '[丢/抛](n)[个/枚]硬币', developers=['Light-Beacon']) -@dicergex.handle(r"[丢|抛]([0-9]*)?[个|枚]?硬币") +@coinrgex.handle(r"[丢|抛]([0-9]*)?[个|枚]?硬币") async def _(message: MessageSession): groups = message.matched_msg.groups() count = groups[0] if groups[0] else '1' From 47f41d084641516afbf12fa181ed98546744ecfe Mon Sep 17 00:00:00 2001 From: Light Beacon <2682645990@qq.com> Date: Mon, 27 Feb 2023 21:24:30 +0800 Subject: [PATCH 4/9] text changes --- modules/coin/__init__.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/modules/coin/__init__.py b/modules/coin/__init__.py index cb442845..52ee2a07 100644 --- a/modules/coin/__init__.py +++ b/modules/coin/__init__.py @@ -2,8 +2,8 @@ import secrets from core.builtins.message import MessageSession from core.component import on_command,on_regex -MAX_COIN_NUM = 20 -FACE_UP_RATE = 4975 # n/10000 +MAX_COIN_NUM = 100 +FACE_UP_RATE = 4975 # n/10000 FACE_DOWN_RATE = 4975 COIN_DROP_PLACES = ["地上","桌子上","桌子底下","门口","窗户外","月球"] # 硬币可能掉落的位置 @@ -32,9 +32,9 @@ async def flipCoins(count:int): if count > MAX_COIN_NUM: return f"发生错误:你最多只能抛 {MAX_COIN_NUM} 个硬币" if count <= 0: - return f"你投了个空气,什么也没发生..." - if FACE_UP_RATE + FACE_DOWN_RATE > 10000: - raise OverflowError("硬币概率总和超过最大概率") + return f"你抛了个空气,什么也没发生..." + if FACE_UP_RATE + FACE_DOWN_RATE > 10000 or FACE_UP_RATE < 0 or FACE_DOWN_RATE < 0: + raise OverflowError("硬币概率错误") faceUp = 0 faceDown = 0 stand = 0 @@ -46,10 +46,10 @@ async def flipCoins(count:int): faceDown += 1 else: stand += 1 - head = f"你投了 {count} 个硬币," + head = f"你抛了 {count} 枚硬币," if count == 1: drop_place = COIN_DROP_PLACES[secrets.randbelow(len(COIN_DROP_PLACES))] - head += f"它掉到了{drop_place}...\n" + head += f"它掉到了{drop_place}......\n" if faceUp: return head + "...是正面!" elif faceDown: @@ -58,16 +58,16 @@ async def flipCoins(count:int): return head + "...它...它立起来了!" else: if not (stand or faceDown): - return "它们...\n...全是正面!" + return head + "它们...\n...全是正面!" if not (stand or faceUp): - return "它们...\n...全是反面!" + return head + "它们...\n...全是反面!" if not (faceUp or faceDown): - return "它们...\n...全都立起来了!?!?天哪!" - output = "其中...\n" + return head + "它们......\n...全都立起来了!?!?天哪!" + output = head + "其中......\n" if faceUp: - output += f"{faceUp}个是正面," + output += f"{faceUp}枚是正面," if faceDown: - output += f"{faceDown}个是反面" + output += f"{faceDown}枚是反面" if stand: output += f"...还有{stand}个立起来了!" return output \ No newline at end of file From b2dfe17fbf0dada9004bd5bb7eea78cb92bcae9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9A=E7=BE=85=E7=8B=BC?= Date: Mon, 27 Feb 2023 21:38:17 +0800 Subject: [PATCH 5/9] Update __init__.py --- modules/coin/__init__.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/modules/coin/__init__.py b/modules/coin/__init__.py index 52ee2a07..70c63dc2 100644 --- a/modules/coin/__init__.py +++ b/modules/coin/__init__.py @@ -55,19 +55,21 @@ async def flipCoins(count:int): elif faceDown: return head + "...是反面!" else: - return head + "...它...它立起来了!" + return head + "...它立起来了!" else: if not (stand or faceDown): return head + "它们...\n...全是正面!" if not (stand or faceUp): return head + "它们...\n...全是反面!" if not (faceUp or faceDown): - return head + "它们......\n...全都立起来了!?!?天哪!" + return head + "它们...\n...全都立起来了?!" output = head + "其中......\n" if faceUp: output += f"{faceUp}枚是正面," if faceDown: output += f"{faceDown}枚是反面" if stand: - output += f"...还有{stand}个立起来了!" - return output \ No newline at end of file + output += f"...还有{stand}枚立起来了!" + else: + output += f"。" + return output From 2d5984772452bb483d5e1438df22b638eb008aa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9A=E7=BE=85=E7=8B=BC?= Date: Mon, 27 Feb 2023 21:41:30 +0800 Subject: [PATCH 6/9] Update __init__.py --- modules/coin/__init__.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/coin/__init__.py b/modules/coin/__init__.py index 70c63dc2..da5be9b1 100644 --- a/modules/coin/__init__.py +++ b/modules/coin/__init__.py @@ -32,9 +32,9 @@ async def flipCoins(count:int): if count > MAX_COIN_NUM: return f"发生错误:你最多只能抛 {MAX_COIN_NUM} 个硬币" if count <= 0: - return f"你抛了个空气,什么也没发生..." + return f"发生错误:你抛了个空气,什么也没发生..." if FACE_UP_RATE + FACE_DOWN_RATE > 10000 or FACE_UP_RATE < 0 or FACE_DOWN_RATE < 0: - raise OverflowError("硬币概率错误") + raise OverflowError("发生错误:硬币概率错误") faceUp = 0 faceDown = 0 stand = 0 @@ -49,7 +49,7 @@ async def flipCoins(count:int): head = f"你抛了 {count} 枚硬币," if count == 1: drop_place = COIN_DROP_PLACES[secrets.randbelow(len(COIN_DROP_PLACES))] - head += f"它掉到了{drop_place}......\n" + head += f"它掉到了{drop_place}...\n" if faceUp: return head + "...是正面!" elif faceDown: @@ -63,7 +63,7 @@ async def flipCoins(count:int): return head + "它们...\n...全是反面!" if not (faceUp or faceDown): return head + "它们...\n...全都立起来了?!" - output = head + "其中......\n" + output = head + "其中...\n有" if faceUp: output += f"{faceUp}枚是正面," if faceDown: From 03ce0f3b65082522cf3a1ebd58e501669a50103a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9A=E7=BE=85=E7=8B=BC?= Date: Mon, 27 Feb 2023 21:51:57 +0800 Subject: [PATCH 7/9] Update __init__.py --- modules/coin/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/coin/__init__.py b/modules/coin/__init__.py index da5be9b1..210cb69c 100644 --- a/modules/coin/__init__.py +++ b/modules/coin/__init__.py @@ -10,6 +10,7 @@ COIN_DROP_PLACES = ["地上","桌子上","桌子底下","门口","窗户外"," coin = on_command('coin', developers=['Light-Beacon'], desc='抛n枚硬币') @coin.handle('[] {投n枚硬币}',) +@coin.handle() async def _(msg: MessageSession): amount = msg.parsed_msg.get('', '1') if not amount.isdigit(): From a9d39945c6c205868fc0cfc9bdeeee7f1b239f39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9A=E7=BE=85=E7=8B=BC?= Date: Mon, 27 Feb 2023 21:53:26 +0800 Subject: [PATCH 8/9] revert --- modules/coin/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/coin/__init__.py b/modules/coin/__init__.py index 210cb69c..da5be9b1 100644 --- a/modules/coin/__init__.py +++ b/modules/coin/__init__.py @@ -10,7 +10,6 @@ COIN_DROP_PLACES = ["地上","桌子上","桌子底下","门口","窗户外"," coin = on_command('coin', developers=['Light-Beacon'], desc='抛n枚硬币') @coin.handle('[] {投n枚硬币}',) -@coin.handle() async def _(msg: MessageSession): amount = msg.parsed_msg.get('', '1') if not amount.isdigit(): From 532de9c7f8b7ca549b10b4e997b3e023cbba8de7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9A=E7=BE=85=E7=8B=BC?= Date: Mon, 27 Feb 2023 22:32:09 +0800 Subject: [PATCH 9/9] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/coin/__init__.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/modules/coin/__init__.py b/modules/coin/__init__.py index da5be9b1..3e90efbe 100644 --- a/modules/coin/__init__.py +++ b/modules/coin/__init__.py @@ -9,7 +9,7 @@ COIN_DROP_PLACES = ["地上","桌子上","桌子底下","门口","窗户外"," coin = on_command('coin', developers=['Light-Beacon'], desc='抛n枚硬币') -@coin.handle('[] {投n枚硬币}',) +@coin.handle('[] {抛n枚硬币}',) async def _(msg: MessageSession): amount = msg.parsed_msg.get('', '1') if not amount.isdigit(): @@ -58,18 +58,18 @@ async def flipCoins(count:int): return head + "...它立起来了!" else: if not (stand or faceDown): - return head + "它们...\n...全是正面!" + return head + "它们...\n...全是正面!" if not (stand or faceUp): - return head + "它们...\n...全是反面!" + return head + "它们...\n...全是反面!" if not (faceUp or faceDown): return head + "它们...\n...全都立起来了?!" output = head + "其中...\n有" if faceUp: - output += f"{faceUp}枚是正面," + output += f" {faceUp} 枚是正面," if faceDown: - output += f"{faceDown}枚是反面" + output += f" {faceDown} 枚是反面" if stand: - output += f"...还有{stand}枚立起来了!" + output += f"...还有 {stand} 枚立起来了!" else: output += f"。" return output