-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordle_.py
410 lines (359 loc) · 17.7 KB
/
wordle_.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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import discord
from discord import app_commands
from discord.ext import commands
import json
import random
from PIL import Image, ImageDraw, ImageFont
from io import BytesIO
from util_database import myclient
import pymongo
from util_discord import command_check, description_helper, get_guild_prefix
mycol = myclient["games"]["wordle"]
font = ImageFont.truetype("./res/font/LibreFranklin-Bold.ttf", size=75)
colors = ["#787c7e", "#e9c342", "#77a76a"] # gray, yellow, green
possible_modes = ["all", "me", "hardcore", "lead", "rank", "global"]
def read_json_file(file_path):
with open(file_path, 'r') as json_file:
data = json.load(json_file)
return data
def add_player(p) -> dict:
return {"score": 0, "name": p, "host": False, "confirm": -1}
def id2e(id: str) -> str:
if id == "INPUT": return "📔"
if id == "LEAVE": return "💩"
if id == "NEXT": return "🩲"
if id == "UPDATE": return "💽"
def game_reset(dead: dict, settings: dict, history: list):
settings["step"] = 0
settings["result"] = -1
history.clear()
dead["yellow"].clear()
dead["green"].clear()
dead["gray"].clear()
def keys(d: dict) -> str:
text = ""
for key, value in d.items():
text += f"\n{value['name']}: {value['score']}"
return text
def c_state(r: int):
if r == 1: return 0x00ff00
elif r == 0: return 0xff0000
return None
def QuizEmbed(settings: dict, index: int, words: list, players: dict) -> discord.Embed:
c = c_state(settings["result"])
e = discord.Embed(color=c)
e.set_footer(text=f"{index+1}/{len(words)}")
e.set_author(name=keys(players))
return e
def check_and_push(arg: str, dead: dict, real: str):
# consumables
real_temp = list(real)
fake_temp = list(arg)
# green
index = 0
for c in fake_temp:
if c == real_temp[index]:
if not c in dead["green"]: dead["green"].append(c)
if c in dead["yellow"]: dead["yellow"].remove(c)
real_temp[index] = ""
fake_temp[index] = "_"
elif not c in real_temp:
if not c in dead["gray"]: dead["gray"].append(c)
index+=1
# yellow
index = 0
for c in fake_temp:
if c in real_temp and c not in dead["green"]: # blunder?
if not c in dead["yellow"]: dead["yellow"].append(c)
real_temp.remove(c)
index+=1
def button_confirm(d, k) -> bool:
d[k]["confirm"]+=1
if d[k]["confirm"] < 1:
return True
return False
def format_hearts(dead: dict) -> str:
return f":green_heart: {dead['green']}\n:yellow_heart: {dead['yellow']}\n:grey_heart: {dead['gray']}"
def draw_rounded_rectangle(draw: ImageDraw.ImageDraw, position: tuple, size: tuple, radius: int, color: str):
x, y = position
width, height = size
# Adjust dimensions to avoid 1 pixel excess
width -= 1
height -= 1
draw.rectangle(
[(x, y + radius), (x + width, y + height - radius)],
fill=color,
)
draw.rectangle(
[(x + radius, y), (x + width - radius, y + height)],
fill=color,
)
draw.pieslice(
[(x, y), (x + radius * 2, y + radius * 2)],
start=180,
end=270,
fill=color,
)
draw.pieslice(
[(x + width - radius * 2, y), (x + width, y + radius * 2)],
start=270,
end=360,
fill=color,
)
draw.pieslice(
[(x, y + height - radius * 2), (x + radius * 2, y + height)],
start=90,
end=180,
fill=color,
)
draw.pieslice(
[(x + width - radius * 2, y + height - radius * 2), (x + width, y + height)],
start=0,
end=90,
fill=color,
)
def wordle_image(history: list, real: str) -> discord.File:
img = Image.new('RGBA', (500, 600))
draw = ImageDraw.Draw(img)
x, y, size = 0, 0, 100
for i in range(5):
for j in range(6):
draw_rounded_rectangle(draw, (i*100, j*100), (size, size), 25, colors[0])
if history:
for word in history:
# consumables
real_temp = list(real)
fake_temp = list(word)
# first pass
x, index = 0, 0
for c in fake_temp:
if c == real_temp[index]:
draw_rounded_rectangle(draw, (x, y), (size, size), 25, colors[2])
real_temp[index] = ""
fake_temp[index] = "_"
# elif not c in real_temp:
# draw_rounded_rectangle(draw, (x, y), (size, size), 25, colors[0])
x+=size
index+=1
# second pass
x = 0
for c in fake_temp:
if c in real_temp:
draw_rounded_rectangle(draw, (x, y), (size, size), 25, colors[1])
real_temp.remove(c)
x+=size
x = 0
for c in word:
draw.text((x+50, y+50), c, fill="white", anchor="mm", font=font)
x+=size
y+=size
# return everything all at once
img_byte_array = BytesIO()
img.save(img_byte_array, format='PNG')
img_byte_array.seek(0)
return discord.File(img_byte_array, 'wordle.png')
class QuizView(discord.ui.View):
def __init__(self, ctx: commands.Context, words: list, index: int, dead: dict, settings: dict, players: dict, history: list):
super().__init__(timeout=None)
possible_games = True
if history and history[len(history)-1] == words[index]["word"].upper():
if index+1 < len(words):
self.add_item(ButtonChoice("NEXT", ctx, words, index, dead, settings, players, history))
else: possible_games = False
else:
self.add_item(ButtonChoice("INPUT", ctx, words, index, dead, settings, players, history))
if possible_games:
self.add_item(ButtonChoice("LEAVE", ctx, words, index, dead, settings, players, history))
self.add_item(ButtonChoice("UPDATE", ctx, words, index, dead, settings, players, history))
class ButtonChoice(discord.ui.Button):
def __init__(self, id: str, ctx: commands.Context, words: list, index: int, dead: dict, settings: dict, players: dict, history: list):
super().__init__(label=id, emoji=id2e(id))
self.id, self.ctx, self.words, self.index, self.dead, self.settings, self.players, self.history = id, ctx, words, index, dead, settings, players, history
async def callback(self, interaction: discord.Interaction):
# get host
host_id = None
a = False
for k, v in self.players.items():
if v["host"]:
host_id = k
a = True
if not a:
k = next(iter(self.players))
self.players[k]["host"] = True
# solo lock
if self.settings["mode"] != "all" and interaction.user.id != host_id:
return await interaction.response.send_message(content=f"<@{host_id}> is playing this game. Use `{await get_guild_prefix(self.ctx)}wordle` to create your own game.",
ephemeral=True)
# register player choice
if not interaction.user.id in self.players: self.players[interaction.user.id] = add_player(interaction.user)
# buttons
if self.id == "LEAVE":
a = False
keys_to_remove = []
for k, v in self.players.items():
if k == interaction.user.id:
keys_to_remove.append(k)
a = True
if not a: return await interaction.response.send_message(content="Just stop.", ephemeral=True)
if button_confirm(self.players, interaction.user.id):
return await interaction.response.send_message(content=f"Hey <@{interaction.user.id}>, press the button again to confirm.",
ephemeral=True)
for k in keys_to_remove: del self.players[k]
if self.players:
await interaction.response.edit_message(content=f"Someone left.\n{format_hearts(self.dead)}",
embed=QuizEmbed(self.settings, self.index, self.words, self.players),
view=QuizView(self.ctx, self.words, self.index, self.dead, self.settings, self.players, self.history))
else:
return await interaction.response.edit_message(content=f"You left.\n{self.words[self.index]['word'].upper()}", embed=None, view=None)
if self.id == "INPUT": # removing return was hot
return await interaction.response.send_modal(MyModal(self.ctx, self.words, self.index, self.dead, self.settings, self.players, self.history))
if self.id == "NEXT":
game_reset(self.dead, self.settings, self.history)
await interaction.response.edit_message(view=None)
await interaction.followup.send(content=f"New game.",
embed=QuizEmbed(self.settings, self.index+1, self.words, self.players),
file=wordle_image(self.history, self.words[self.index+1]["word"].upper()),
view=QuizView(self.ctx, self.words, self.index+1, self.dead, self.settings, self.players, self.history))
if self.id == "UPDATE":
if interaction.user.id != host_id:
return await interaction.response.send_message(f"Only <@{host_id}> can press this button.", ephemeral=True)
await interaction.response.edit_message(view=None)
await interaction.followup.send(content=f"Message updated.\n{format_hearts(self.dead)}",
embed=QuizEmbed(self.settings, self.index, self.words, self.players),
file=wordle_image(self.history, self.words[self.index]["word"].upper()),
view=QuizView(self.ctx, self.words, self.index, self.dead, self.settings, self.players, self.history))
await interaction.delete_original_response()
class MyModal(discord.ui.Modal):
def __init__(self, ctx: commands.Context, words: list, index: int, dead: dict, settings: dict, players: dict, history: list):
super().__init__(title="Enter credit card details")
self.i = discord.ui.TextInput(label="Expiry Date")
self.add_item(self.i)
self.ctx, self.words, self.index, self.dead, self.settings, self.players, self.history = ctx, words, index, dead, settings, players, history
async def on_submit(self, interaction: discord.Interaction):
i, word = self.i.value.upper(), self.words[self.index]["word"].upper()
# you don't belong here
if len(i) != 5:
return await interaction.response.send_message(content="hey, 5 letter words only pls.", ephemeral=True)
await interaction.response.edit_message(view=None)
self.history.append(i)
check_and_push(i, self.dead, word)
if i == word: # you win
# TODO: leaderboard stats (win/loss, register server on first input)
user_data = await mycol.find_one({"user": interaction.user.id})
if not user_data:
await mycol.insert_one({
"user": interaction.user.id,
"servers": [interaction.guild_id],
"score": 6-self.settings["step"],
"plays": 1
})
else:
await mycol.update_one(
{"user": interaction.user.id},
{
"$addToSet": {"servers": interaction.guild_id},
"$inc": {"score": 6-self.settings["step"]},
"$inc": {"plays": 1}
}
)
self.settings["result"] = 1
self.players[interaction.user.id]["score"] += 1
await interaction.followup.send(embed=QuizEmbed(self.settings, self.index, self.words, self.players),
view=QuizView(self.ctx, self.words, self.index, self.dead, self.settings, self.players, self.history),
file=wordle_image(self.history, word))
else:
self.settings["step"] += 1
if self.settings["step"] != 6: # in-game
await interaction.followup.send(embed=QuizEmbed(self.settings, self.index, self.words, self.players), content=format_hearts(self.dead),
view=QuizView(self.ctx, self.words, self.index, self.dead, self.settings, self.players, self.history),
file=wordle_image(self.history, word))
else: # you lose
self.settings["result"] = 0
await interaction.followup.send(embed=QuizEmbed(self.settings, self.index, self.words, self.players),
content=f"GAME OVER!\n{word}", file=wordle_image(self.history, word))
await interaction.delete_original_response()
async def brag_embed(server_scores, ctx: commands.Context, global_lead: bool) -> discord.Embed:
e = discord.Embed(color=0x00ff00, title=ctx.guild if not global_lead else "GLOBAL", description="wordle prototype")
index, limit = 0, 10
for user_data in server_scores:
index+=1
bot: commands.Bot = ctx.bot
user = bot.get_user(user_data['user'])
user_name = user.name if user else "???"
e.add_field(name=f"{index}. {user_name}",
value=f"Score: {user_data['score']} ({user_data['plays'] if user_data.get('plays') else '?'} plays)",
inline=False)
if index == limit: break
return e
async def brag_function(ctx: commands.Context, mode: str, optional: str):
try:
if not optional or int(optional): pass
except: return await ctx.reply("⁉️")
user_id = ctx.author.id if not optional else int(optional)
if not ctx.guild: return await ctx.reply(content="this is a server-only command.")
cursor = mycol.find({"servers": ctx.guild.id}).sort("score", pymongo.DESCENDING)
server_scores = await cursor.to_list(None)
user_data = await mycol.find_one({"user": user_id, "servers": ctx.guild.id})
if not user_data or not server_scores:
return await ctx.reply(content="🤨")
if mode == "rank":
count = 0
for user_data in server_scores:
count+=1
if user_data['user'] == user_id:
try:
if optional: member = await ctx.guild.fetch_member(user_id)
except discord.NotFound: return await ctx.reply(content="🤨")
return await ctx.reply(f"{ctx.author if not optional else member}\nRANK: #{count}, SCORE: {user_data['score']}")
if mode == "lead":
return await ctx.reply(embed=await brag_embed(server_scores, ctx, False))
if mode == "global":
cursor = mycol.find().sort("score", pymongo.DESCENDING)
global_scores = await cursor.to_list(None)
return await ctx.reply(embed=await brag_embed(global_scores, ctx, True))
async def wordle_game(ctx: commands.Context, mode: str, count: str):
if await command_check(ctx, "word", "games"): return await ctx.reply("command disabled", ephemeral=True)
params = f"```{await get_guild_prefix(ctx)}wordle [stats: <rank/lead/global> OR mode: <all/hardcore/me> count: <1-50>]```"
if mode in ["lead", "rank", "global"]:
return await brag_function(ctx, mode, count)
if mode:
modes = ["all", "me", "hardcore"]
if mode in modes: pass
else: return await ctx.reply(content="Mode not found.\n"+params)
else: mode = "me"
synsets_data = read_json_file("./res/dict/synsets_wordle.json")
if count:
if count.isdigit():
if int(count) > 0 and int(count) <= len(synsets_data): pass
else: return await ctx.reply(content=f"Must be greater than 0 and less than or equal to {len(synsets_data)}."+params)
else: return await ctx.reply(content="Not a valid integer.\n"+params)
else: count = 1
random.shuffle(synsets_data)
words = synsets_data if mode == "hardcore" else synsets_data[:int(count)]
players = {}
players[ctx.author.id] = add_player(ctx.author)
players[ctx.author.id]["host"] = True
dead = {"yellow": [], "green": [], "gray": []}
settings = {"step": 0, "mode": mode, "result": -1}
history = []
await ctx.reply(file=wordle_image(history, words[0]["word"].upper()),
embed=QuizEmbed(settings, 0, words, players), view=QuizView(ctx, words, 0, dead, settings, players, history))
async def mode_auto(interaction: discord.Interaction, current: str) -> list[app_commands.Choice[str]]:
return [
app_commands.Choice(name=mode, value=mode) for mode in possible_modes if current.lower() in mode.lower()
]
class CogWord(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command() # alias
async def word(self, ctx: commands.Context, mode: str=None, count: str=None):
await wordle_game(ctx, mode, count)
@commands.hybrid_command(description=f'{description_helper["emojis"]["games"]} {description_helper["games"]["wordle"]}')
@app_commands.autocomplete(mode=mode_auto)
@app_commands.describe(count="Set count (Must be a valid integer)", mode="Set game mode")
@app_commands.allowed_installs(guilds=True, users=True)
@app_commands.allowed_contexts(guilds=True, dms=True, private_channels=True)
async def wordle(self, ctx: commands.Context, mode: str=None, count: str=None):
await wordle_game(ctx, mode, count)
async def setup(bot: commands.Bot):
await bot.add_cog(CogWord(bot))