This repository has been archived by the owner on Apr 27, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanual_judge.py
118 lines (97 loc) · 4.33 KB
/
manual_judge.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"""
全手動 †JUDGEMENT SYSTEM† の処理
クラス化する必要はなかった気がするすまん (繰り返される歴史)
"""
import discord
import asyncio
import random
from lib.util import Singleton
class ManualJudge(Singleton):
"""
!jd / !judge コマンドの処理をするクラス
"""
_MANUAL_JUDGE_MESSAGE = {
"invalid-argument":"引数の形式がまちがっているよ...?",
"invalid-command":"引数は少なくとも1つは必要だよ...?",
"too-many-judges":"回数は1回から30回にしてね",
"no-such-error":"そんなエラーはないよ...?"
}
def __init__(self,abc_emojis : dict):
"""
初期化を行う ABCの絵文字を保持しているだけ
Parameters
----------
abc_emojis: dict<str:discord.Emoji>
ABCの絵文字の辞書
keyは以下の通り:
"AC","WA","CE","TLE"
"""
self.abc_emoji_dict = abc_emojis
async def checker(self,commands : list, channel : discord.channel):
"""
!jd / !judge コマンドの書式を確認して実行の可否を判断する関数
ただし実際は可否を判断しているだけで実行する機能自体はない
エラーメッセージは送信するようになっている
Parameters
----------
commands: list<str>
ユーザーが実際に入力したコマンドが格納されているリスト
!プレフィックスを消して空白で区切ったやつ
Returns
----------
allow_or_not: bool
コマンドに問題がないかを格納している
問題があればTrue問題がなければFalse
"""
# I.第二引数がint型にキャスト可能かどうか
try:
int(commands[1]) # 変更可能性あり
except:
await channel.send(ManualJudge._MANUAL_JUDGE_MESSAGE["invalid-argument"])
return True
# II.第二引数が0より大きく30以下かどうか
if not (0 < int(commands[1]) <= 30):
await channel.send(ManualJudge._MANUAL_JUDGE_MESSAGE["too-many-judges"])
return True
# 此処から先は任意で引数の有無が決まるのでチェックするかどうかを判定
if len(commands) >= 3:
# III.第三引数が正しいオプションか判定
if not (commands[2] in self.abc_emoji_dict.keys()):
await channel.send(ManualJudge._MANUAL_JUDGE_MESSAGE["no-such-error"])
return True
return False
async def call(self,commands : list, channel : discord.channel):
"""
!jd / !judge コマンドを受け取って処理をする関数
----------
commands: list<str>
ユーザーが実際に入力したコマンドが格納されているリスト
!プレフィックスを消して空白で区切ったやつ
"""
# checkerに通す ダメならブロック
if (await self.checker(commands, channel)):
return
is_all_error = True if "-all" in commands else False
if len(commands) >= 3:
mode = commands[2].upper() if(commands[2].upper() in self.abc_emoji_dict.keys() and len(commands) >= 3) else "AC"
else:
mode = "AC"
await channel.send("***†HARACHO ONLINE JUDGEMENT SYSTEM†***")
await asyncio.sleep(1)
if is_all_error:
for n in range(1,int(commands[1])+1):
await channel.send("```\n"+str(n)+"/"+commands[1]+" "+(mode)+"\n```")
if n == int(commands[1]):
break
await asyncio.sleep(random.uniform(0, 4))
else:
error_occurs_at = random.sample(range(1,int(commands[1])+1),random.randint(1,int(commands[1])+1))
for n in range(1,int(commands[1])+1):
if n in error_occurs_at:
await channel.send("```\n"+str(n)+"/"+commands[1]+" "+(mode)+"\n```")
else:
await channel.send("```\n"+str(n)+"/"+commands[1]+" "+"AC"+"\n```")
if n == int(commands[1]):
break
await asyncio.sleep(random.uniform(0, 4))
await channel.send(self.abc_emoji_dict[mode])