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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
import asyncio
import random
import typing
from abc import ABC, abstractmethod
from datetime import datetime, timedelta
from enum import Enum, auto
import discord
from redbot.core import Config
from redbot.core.bot import Red
from redbot.core.config import Group
from . import constants, utils
from .recipe_provider import DEFAULT_RECIPE_PROVIDER
class GamePhase(Enum):
INACTIVE = auto()
SIGNUP = auto()
RUNNING = auto()
FINISHED = auto()
class OngoingGame(ABC):
participants: typing.List[discord.User]
channel: discord.TextChannel
phase: GamePhase
signup_message: discord.Message
def __init__(self, bot: Red, config: Config, channel: discord.TextChannel):
self.participants = []
self.bot = bot
self.config: Group = config.guild(channel.guild)
self.channel = channel
self.phase = GamePhase.INACTIVE
async def start_signup(self):
self.phase = GamePhase.SIGNUP
embed = discord.Embed(
title="Signups opened for new game of Minecraft trivia",
description="React to this message in order to join. You have 30 seconds to signup.",
)
embed.timestamp = datetime.now()
self.signup_message = await self.channel.send(embed=embed)
await self.signup_message.add_reaction(constants.POSITIVE_REACTION)
await asyncio.sleep(await self.config.join_timeout())
embed.description = "Signups are now closed. Wait for the game to finish to start a new one."
await self.signup_message.edit(embed=embed)
self.participants = await utils.get_participants((await self.channel.fetch_message(self.signup_message.id)).reactions)
await self.start_game()
async def start_game(self):
self.phase = GamePhase.RUNNING
await self.gameloop()
async def conclude_game(self):
self.phase = GamePhase.FINISHED
embed = discord.Embed(
title="Minecraft Trivia Finished",
description=self.leaderboard(),
)
await self.channel.send(embed=embed)
async def gameloop(self):
for i in range(2):#todo
await self.single_round(i)
await self.conclude_game()
async def wait_for_participant_messages(self, consumer: typing.Callable[[discord.Message], typing.Awaitable[bool]]):
"""
Calls consumer for all participant messages in this channel. consumer(mes) should return True if this is the final accepted message. Consumer is a callable
"""
def check(mes: discord.Message):
if mes.channel.id != self.channel.id:
return False
if mes.author.id not in map(lambda x: x.id, self.participants):
return False
return True
until = datetime.now() + timedelta(seconds=await self.config.guess_timeout())
while True:
try:
mes = await self.bot.wait_for('message', check=check, timeout=(until - datetime.now()).total_seconds())
if await consumer(mes):
return
except asyncio.TimeoutError:
return
async def wait_for_correct_answer(self, answer_filter: typing.Callable[[str], bool]) -> typing.Optional[discord.User]:
u = None
async def check(mes: discord.Message):
nonlocal u
if answer_filter(mes.content):
u = mes.author
return True
await self.wait_for_participant_messages(check)
return u
@abstractmethod
def leaderboard(self) -> str:
pass
@abstractmethod
async def single_round(self, round_number: int):
pass
class PointBasedGame(OngoingGame, ABC):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.points: typing.Dict[discord.User, int] = {}
async def conclude_game(self):
async with self.config.total_scores() as total_scores:
for user, points in self.points.items():
uid = str(user.id)
if uid not in total_scores:
total_scores[uid] = 0
total_scores[uid] += points
async with self.config.high_scores() as high_scores:
for user, points in self.points.items():
uid = str(user.id)
if uid not in high_scores:
high_scores[uid] = points
else:
high_scores[uid] = max(high_scores[uid], points)
return await super().conclude_game()
@property
def ranks(self) -> typing.List[typing.Tuple[int, typing.Tuple[discord.User, int]]]:
return utils.create_leaderboard(self.points)
async def start_game(self):
for u in self.participants:
self.points[u] = 0
return await super().start_game()
def leaderboard(self) -> str:
return utils.format_leaderboard(self.ranks)
class XDGame(PointBasedGame):
async def single_round(self, round_number: int):
await self.channel.send("xd?")
def answer_filter(msg: str) -> bool:
return msg.casefold() == "xd".casefold()
round_winner = await self.wait_for_correct_answer(answer_filter)
self.points[round_winner] += 1
await self.channel.send(f"{round_winner.mention} won this round")
class CraftingGame(PointBasedGame):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.used_recipes: typing.List[int] = []
def random_recipe(self):
index = -1
while index in self.used_recipes or index < 0:
index = random.randint(0, len(DEFAULT_RECIPE_PROVIDER.recipes) - 1)
self.used_recipes.append(index)
return DEFAULT_RECIPE_PROVIDER.recipes[index]
@staticmethod
def get_name(obj: str) -> str:
return DEFAULT_RECIPE_PROVIDER.get_all_names(obj)[-1]
async def single_round(self, round_number: int):
recipe = self.random_recipe()
items_left_to_find = recipe.ingredients.copy()
embed = discord.Embed(title=f"RECIPE REQUIRED: {self.get_name(recipe.result)}")
embed.description = "Items found so far:"
message = await self.channel.send(embed=embed)
async def check(msg: discord.Message):
inp = msg.content.casefold().replace(' ', '')
for ingredient in items_left_to_find:
found = False
for item in ingredient.allowed_items:
names = DEFAULT_RECIPE_PROVIDER.get_all_names(item)
name_correct = False
for name in names:
if inp == name.casefold().replace(' ', ''):
name_correct = True
if not name_correct:
continue
embed.description += f"\n{names[-1]} ({ingredient.count}) found by {msg.author.mention}"
await message.edit(embed=embed)
self.points[msg.author] += 1
items_left_to_find.remove(ingredient)
found = True
break
if found:
break
return len(items_left_to_find) == 0
await self.wait_for_participant_messages(check)
for ingredient in items_left_to_find:
item = ingredient.allowed_items[0]
embed.description += f"\n{self.get_name(item)} ({ingredient.count}) - Not Found"
embed.description += "\n\nDone"
await message.edit(embed=embed)
|