Skip to content

Commit

Permalink
大喜利機能で2つ回答が設定できるように改修(✕✕がお題にある場合のみ)
Browse files Browse the repository at this point in the history
  • Loading branch information
tetsuya-ki committed Feb 11, 2021
1 parent 29e5444 commit 837032b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
18 changes: 14 additions & 4 deletions cogs/gamecog.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ async def start_ohgiriGame(self, ctx, win_point=5):
await self.startOhgiri(ctx, win_point)

@ohgiriGame.command(aliases=['a','sen','send','ans','kaitou'], description='回答者がお題に提出する回答を設定')
async def answer(self, ctx, card_id=None):
async def answer(self, ctx, card_id=None, second_card_id=None):
"""
回答者が回答として提出するカードを設定
- ans_number: 回答として設定する値(数字で指定)
Expand All @@ -516,13 +516,19 @@ async def answer(self, ctx, card_id=None):
# コマンド実行者が所持しているかチェック
elif card_id not in self.ohgiriGames.members[ctx.author].cards:
await ctx.send(f'{card_id}{ctx.author.display_name}の所持しているカードではありません!')
elif self.ohgiriGames.required_ans_num == 1 and second_card_id is not None:
await ctx.send(f'お題で2つ設定するように指定がないので、回答は1つにしてください!')
elif self.ohgiriGames.required_ans_num == 2 and second_card_id is None:
await ctx.send('2つめの引数`second_card_id`が設定されていません!(もう一つ数字を設定してください)')
elif self.ohgiriGames.required_ans_num == 2 and second_card_id not in self.ohgiriGames.members[ctx.author].cards:
await ctx.send(f'{second_card_id}{ctx.author.display_name}の所持しているカードではありません!')
else:
logger.debug('回答を受け取ったよ!')
# 既に回答したメンバーから再度回答を受けた場合、入れ替えた旨お知らせする
if self.ohgiriGames.members[ctx.author].answered:
await ctx.send(f'{ctx.author.mention} 既に回答を受け取っていたため、そちらのカードと入れ替えますね!')
# カードの受領処理
self.ohgiriGames.receive_card(card_id, ctx.author)
self.ohgiriGames.receive_card(card_id, ctx.author, second_card_id)
# 回答者が出そろった場合、場に出す(親は提出できないので引く)
if (len(self.ohgiriGames.members) - 1) == len(self.ohgiriGames.field):
self.ohgiriGames.show_answer()
Expand Down Expand Up @@ -604,7 +610,7 @@ async def startOhgiri(self, ctx, win_point):
# 参加者と手札の数を設定
await self.ohgiriGames.setting(make_team.vc_members, 12, win_point)
self.ohgiriGames.shuffle()
msg = f'お題が提供されるので**「親」はお題を声に出して読み上げ**てください("○○"は「まるまる」と読む)。ほかのプレイヤーは読み上げられた**お題に相応しいと思う回答**を`/o ans <数字>`で選びます。\n'\
msg = f'お題が提供されるので**「親」はお題を声に出して読み上げ**てください("○○"は「まるまる」、"✕✕"は「ばつばつ」と読む)。ほかのプレイヤーは読み上げられた**お題に相応しいと思う回答**を`/o ans <数字>`で選びます。\n'\
+ f'全員が回答したら、**「親」はもっとも秀逸な回答**を`/o choice <番号>`で選択します。「親」から選ばれたプレイヤーは1点もらえます。ただし、山札から1枚カードが混ざっており、それを選択すると親はポイントが減算されます。\n'\
+ f'今回のゲームの勝利点は{self.ohgiriGames.win_point}点です。'
await ctx.send(msg)
Expand All @@ -619,7 +625,11 @@ async def dealAndNextGame(self, ctx):
# DMで回答カードを示す
for player in self.ohgiriGames.members:
await self.send_ans_dm(ctx, player, odai_msg)
await ctx.send(f'カードを配りました。DMをご確認ください。{self.ohgiriGames.description}\n親は{self.ohgiriGames.house.display_name}です!')

msg = f'カードを配りました。DMをご確認ください。{self.ohgiriGames.description}\n親は{self.ohgiriGames.house.display_name}です!'
if self.ohgiriGames.required_ans_num == 2:
msg += '\n(回答は**2つ**設定するようにしてください! 例:`/o ans 1 2`'
await ctx.send(msg)

async def send_ans_dm(self, ctx, player: discord.member, odai_msg:discord.message=None):
dm_msg = ''
Expand Down
37 changes: 30 additions & 7 deletions cogs/modules/ohgiri.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ def __init__(self):
self.answered = False

class Answer:
def __init__(self, card_id, member):
def __init__(self, card_id, member, second_card_id=None):
self.card_id = card_id # 回答カード配列の配列番号
self.member = member # 回答者
self.answer_index = None # 画面にある番号(画面に表示する前にまとめて採番して設定するのでinitではNone)
self.second_card_id = second_card_id # 2つ目の回答カード配列の配列番号

class Ohgiri:
"""
Expand All @@ -38,6 +39,7 @@ def __init__(self):
self.deck_odai = [] # デッキ(お題)
self.deck_ans = [] # デッキ(回答)
self.odai = None # 場におかれているお題
self.required_ans_num = 1 # 必要な回答数
self.field = [] # 場におかれている回答
self.discards_odai = [] # 捨て札(お題)
self.discards_ans = [] # 捨て札(回答)
Expand Down Expand Up @@ -112,12 +114,17 @@ def deal(self):
"""
self.turn = self.turn + 1
self.description = ''
self.hands = []
self.field = []

# 場に置かれるお題をひく
self.odai = self.deck_odai.pop()

# お題にXXがあるかチェック
if '✕✕' in self.odai:
self.required_ans_num = 2
else:
self.required_ans_num = 1

# お題の山札がなくなった場合の処理
if len(self.deck_odai) == 0:
self.retern_discards_to_deck('お題カード', self.discards_odai, self.deck_odai)
Expand All @@ -142,35 +149,44 @@ def retern_discards_to_deck(self, name, target_discards, target_deck):
target_discards = []
self.shuffle()

def receive_card(self, card_id, member):
def receive_card(self, card_id, member, second_card_id=None):
"""
メンバーからカードを受け取ったときの処理
受け取ったカードを場に出す
メンバーの手持ちから受領したカードを除去
cardNum {Int}
member self.membersから取り出すキー
second_card_id {Int}
"""
# 回答済のメンバーからカードを受け取った場合は、場に出されたカードとそのカードを入れ替える
if self.members[member].answered:
for answer in self.field:
if answer.member == member:
self.members[member].cards.append(answer.card_id)
if answer.second_card_id is not None:
self.members[member].cards.append(answer.second_card_id)
break
self.field = [answer for answer in self.field if answer.member != member]

# 回答済に設定する
self.members[member].answered = True
self.field.append(Answer(card_id, member))
if second_card_id is None:
self.field.append(Answer(card_id, member))
else:
self.field.append(Answer(card_id, member, second_card_id))

# 受信したカード以外のカードをユーザに返す
self.members[member].cards = [users_card_id for users_card_id in self.members[member].cards if users_card_id != card_id]
self.members[member].cards = [users_card_id for users_card_id in self.members[member].cards if (users_card_id != card_id and users_card_id != second_card_id)]

def show_answer(self):
"""
山札からカードを1枚加え、ランダムに混ぜた上で、回答を表示
"""
# 山札からカードを引いてダミーの回答を作る
self.field.append(Answer(self.deck_ans.pop(), 'dummy'))
if self.required_ans_num == 1:
self.field.append(Answer(self.deck_ans.pop(), 'dummy'))
else:
self.field.append(Answer(self.deck_ans.pop(), 'dummy', self.deck_ans.pop()))

# 場に出た回答に画面表示用のランダムな番号を設定する。
random_field = random.sample(self.field, len(self.field))
Expand All @@ -179,7 +195,10 @@ def show_answer(self):

self.description = ''
for sorted_answer in sorted(random_field, key=lambda answer: answer.answer_index):
self.description += f'{str(sorted_answer.answer_index)}: {str(self.odai).replace("〇〇", "||" + self.ans_dict[sorted_answer.card_id] + "||")}\n'
description_text = f'{str(sorted_answer.answer_index)}: {str(self.odai).replace("〇〇", "||" + self.ans_dict[sorted_answer.card_id] + "||")}\n'
if self.required_ans_num == 2:
description_text = description_text.replace("✕✕", "||" + self.ans_dict[sorted_answer.second_card_id] + "||")
self.description += description_text

def choose_answer(self, answer_index):
"""
Expand All @@ -206,6 +225,8 @@ def choose_answer(self, answer_index):

# 回答と回答者を入れたメッセージをwinCardsListに入れ、説明文に追加
win_word = f'{str(self.odai).replace("〇〇", "**" + self.ans_dict[choosen_answer.card_id] + "**")} ({choosen_member_display_name}さん)\n'
if self.required_ans_num == 2:
win_word = win_word.replace("✕✕", "**" + self.ans_dict[choosen_answer.second_card_id] + "**")
self.winCardsList.append(win_word)
self.description += '> ' + win_word

Expand All @@ -214,6 +235,8 @@ def choose_answer(self, answer_index):
self.odai = ''
for answer in self.field:
self.discards_ans.append(str(answer.card_id))
if answer.second_card_id is not None:
self.discards_ans.append(str(answer.second_card_id))

# 勝利判定
if choosen_answer.member != 'dummy' and self.members[choosen_answer.member].point >= self.win_point:
Expand Down

0 comments on commit 837032b

Please sign in to comment.