-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
place.py
238 lines (214 loc) · 12.4 KB
/
place.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
import discord
from discord.ext import commands
from PIL import Image, ImageDraw
import re
import io
from util_database import myclient
import asyncio
width, height = 500, 500
mycol = myclient["place"]["pixels"]
async def draw_image(x: int, y: int, zoom: float) -> io.BytesIO:
canvas = Image.new("RGB", (width, height), color="black")
draw = ImageDraw.Draw(canvas)
all_pixels = await find_all_pixels()
for pixel in all_pixels:
await asyncio.to_thread(draw.point, (pixel['x'], pixel['y']), fill=rgb_string_to_tuple(pixel['color']))
zoomed_canvas = zoom_canvas(canvas, zoom, (x, y))
resized_canvas = resize_image(zoomed_canvas)
image_buffer = io.BytesIO()
resized_canvas.save(image_buffer, format="PNG")
image_buffer.seek(0)
return image_buffer
async def PlaceEmbed(x: int, y: int, z: float, ctx: commands.Context, status: str) -> discord.Embed:
d = await find_pixel(x, y)
e = discord.Embed(title=f"({x}, {y}) [{z}x]", description=f"{d['author']}: {d['color']}",
color=rgb_tuple_to_hex(rgb_string_to_tuple(d['color'])))
if ctx.author.avatar: e.set_author(name=ctx.author, icon_url=ctx.author.avatar.url)
else: e.set_author(name=ctx.author)
e.set_footer(text=status)
return e
async def PLACE(ctx: commands.Context, x: str, y: str, z: str):
params = f"```-place [x: <0-{width-1}>, y: <0-{height-1}>, zoom:<16x>]```"
msg = await ctx.reply("Drawing canvas…")
if x and y:
if x.isdigit() and y.isdigit():
if int(x) > -1 and int(x) < width and int(y) > -1 and int(y) < height: pass
else: return await ctx.reply(f"Must be {width}x{height}")
else: return await ctx.reply(f"Must be integer and {width}x{height}")
else: x, y = 0, 0
if z:
z = extract_number(z)
if not z: return await ctx.reply("Invalid zoom format.\nTry `2x` or `2`.")
else: z = 16
file = discord.File(await draw_image(int(x), int(y), z), filename=f"{x}x{y}.png")
await msg.edit(content="r/place")
await ctx.reply(view=ViewPlace(int(x), int(y), z, ctx), file=file,
embed=await PlaceEmbed(int(x), int(y), z, ctx, "Idle"))
def zoom_canvas(canvas, zoom_multiplier, center_pixel):
# Calculate the region to crop based on the scale factor and center pixel
cropped_width = int(width / zoom_multiplier)
cropped_height = int(height / zoom_multiplier)
left = center_pixel[0] - cropped_width // 2
top = center_pixel[1] - cropped_height // 2
right = left + cropped_width
bottom = top + cropped_height
# Crop the region around the center pixel
cropped_image = canvas.crop((left, top, right, bottom))
# Resize the cropped image to the original size
zoomed_image = cropped_image.resize((width, height), Image.BOX)
return zoomed_image
def resize_image(canvas):
resized_image = canvas.resize((1000, 1000), Image.BOX)
return resized_image
def rgb_string_to_tuple(rgb_string):
# Use regular expression to extract three groups of digits, with or without "#"
match = re.match(r"#?(\w{2})(\w{2})(\w{2})", rgb_string)
if match:
# Convert the hexadecimal digits to integers using base 16
red = int(match.group(1), 16)
green = int(match.group(2), 16)
blue = int(match.group(3), 16)
return (red, green, blue)
else:
# Check if the input is in the format "255, 255, 255"
match = re.match(r"(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})", rgb_string)
if match:
red = int(match.group(1))
green = int(match.group(2))
blue = int(match.group(3))
if 0 <= red <= 255 and 0 <= green <= 255 and 0 <= blue <= 255:
return (red, green, blue)
return None
def rgb_tuple_to_hex(rgb_tuple):
# Check if the RGB values are integers within the valid range (0-255)
red, green, blue = rgb_tuple
if not (0 <= red <= 255 and 0 <= green <= 255 and 0 <= blue <= 255):
raise ValueError("Invalid RGB values. Each value should be between 0 and 255.")
# Convert the RGB values to their hexadecimal representation and combine them into a single integer
hex_color_code = (red << 16) + (green << 8) + blue
return hex_color_code
def extract_number(input_str):
pattern = r'^(-?\d+(\.\d+)?)(x.*)?$'
match = re.match(pattern, input_str)
if match:
if match.group(1):
number = float(match.group(1)) if '.' in match.group(1) else int(match.group(1))
return 1 if number <= 0 else number
return None
class ViewPlace(discord.ui.View):
def __init__(self, x: int, y: int, z: float, ctx: commands.Context):
super().__init__(timeout=None)
if x-1 > -1:
self.add_item(ButtonChoice(x, y, z, ctx, 0, "◀️", "LEFT"))
if y+1 < height:
self.add_item(ButtonChoice(x, y, z, ctx, 0, "🔽", "DOWN"))
if y-1 > -1:
self.add_item(ButtonChoice(x, y, z, ctx, 0, "🔼", "UP"))
if x+1 < width:
self.add_item(ButtonChoice(x, y, z, ctx, 0, "▶️", "RIGHT"))
if x-10 > -1:
self.add_item(ButtonChoice(x, y, z, ctx, 1, "⏪", "LEFTLEFT"))
if y+10 < height:
self.add_item(ButtonChoice(x, y, z, ctx, 1, "⏬", "DOWNDOWN"))
if y-10 > -1:
self.add_item(ButtonChoice(x, y, z, ctx, 1, "⏫", "UPUP"))
if x+10 < width:
self.add_item(ButtonChoice(x, y, z, ctx, 1, "⏩", "RIGHTRIGHT"))
self.add_item(ButtonChoice(x, y, z, ctx, 2, "🪧", "PLACE"))
self.add_item(ButtonChoice(x, y, z, ctx, 2, "🧭", "LOCATE"))
self.add_item(ButtonChoice(x, y, z, ctx, 2, "🔍", "ZOOM"))
class ButtonChoice(discord.ui.Button):
def __init__(self, x: int, y: int, z: float, ctx: commands.Context, r: int, e: str, l: str):
self.x, self.y, self.z, self.ctx, self.l = x, y, z, ctx, l
labels = ["PLACE", "ZOOM", "LOCATE"]
if not l in labels: l = None
super().__init__(label=l, emoji=e, row=r)
async def callback(self, interaction: discord.Interaction):
if interaction.user != self.ctx.author:
return await interaction.response.send_message(f"{self.ctx.author.mention} is using this view. Use `-place` to create your own.",
ephemeral=True)
data = await find_pixel(self.x, self.y)
if self.l == "PLACE": return await interaction.response.send_modal(ModalPlace(self.x, self.y, self.z, self.ctx, data))
if self.l == "ZOOM": return await interaction.response.send_modal(ModalZoom(self.x, self.y, self.z, self.ctx))
if self.l == "LOCATE": return await interaction.response.send_modal(ModalLocate(self.x, self.y, self.z, self.ctx))
if self.l == "LEFTLEFT": self.x += -10
if self.l == "LEFT": self.x += -1
if self.l == "RIGHTRIGHT": self.x += 10
if self.l == "RIGHT": self.x += 1
if self.l == "UPUP": self.y += -10
if self.l == "UP": self.y += -1
if self.l == "DOWNDOWN": self.y += 10
if self.l == "DOWN": self.y += 1
await interaction.response.defer()
await interaction.message.remove_attachments(interaction.message.attachments[0])
await interaction.message.edit(embed=await PlaceEmbed(self.x, self.y, self.z, self.ctx, f"Processing {self.l}, syncing…"),
view=None)
f = discord.File(await draw_image(self.x, self.y, self.z), filename=f"{self.x}x{self.y}.png")
await interaction.message.edit(embed=await PlaceEmbed(self.x, self.y, self.z, self.ctx, f"Synced"), view=None)
await interaction.followup.send(view=ViewPlace(self.x, self.y, self.z, self.ctx), file=f,
embed=await PlaceEmbed(self.x, self.y, self.z, self.ctx, "Idle"))
class ModalPlace(discord.ui.Modal):
def __init__(self, x: int, y: int, z: float, ctx: commands.Context, data):
super().__init__(title="Place")
self.i = discord.ui.TextInput(label=f"Color ({data['color']})")
self.add_item(self.i)
self.x, self.y, self.z, self.ctx, = x, y, z, ctx
async def on_submit(self, interaction: discord.Interaction):
col = rgb_string_to_tuple(self.i.value)
if not col: return await interaction.response.send_message("Invalid color format.\nMust be `#00ff00`", ephemeral=True)
await update_pixel(self.x, self.y, interaction.user.name, self.i.value)
await interaction.response.defer()
await interaction.message.remove_attachments(interaction.message.attachments[0])
await interaction.message.edit(embed=await PlaceEmbed(self.x, self.y, self.z, self.ctx,
f"Placing {col}, syncing…"), view=None)
f = discord.File(await draw_image(self.x, self.y, self.z), filename=f"{self.x}x{self.y}.png")
await interaction.message.edit(embed=await PlaceEmbed(self.x, self.y, self.z, self.ctx, f"Synced"), view=None)
await interaction.followup.send(view=ViewPlace(self.x, self.y, self.z, self.ctx), file=f,
embed=await PlaceEmbed(self.x, self.y, self.z, self.ctx, "Idle"))
class ModalZoom(discord.ui.Modal):
def __init__(self, x: int, y: int, z: float, ctx: commands.Context):
super().__init__(title="Zoom")
self.i = discord.ui.TextInput(label=f"Value ({z}x)")
self.add_item(self.i)
self.x, self.y, self.z, self.ctx, = x, y, z, ctx
async def on_submit(self, interaction: discord.Interaction):
self.z = extract_number(self.i.value)
if not self.z: return await interaction.response.send_message("Invalid zoom format.\nTry `2x` or `2`.", ephemeral=True)
await interaction.response.defer()
await interaction.message.remove_attachments(interaction.message.attachments[0])
await interaction.message.edit(embed=await PlaceEmbed(self.x, self.y, self.z, self.ctx, f"Zooming {self.z}x, syncing…"), view=None)
f = discord.File(await draw_image(self.x, self.y, self.z), filename=f"{self.x}x{self.y}.png")
await interaction.message.edit(embed=await PlaceEmbed(self.x, self.y, self.z, self.ctx, f"Synced"), view=None)
await interaction.followup.send(view=ViewPlace(self.x, self.y, self.z, self.ctx), file=f,
embed=await PlaceEmbed(self.x, self.y, self.z, self.ctx, "Idle"))
class ModalLocate(discord.ui.Modal):
def __init__(self, x: int, y: int, z: float, ctx: commands.Context):
super().__init__(title="Locate")
self.iX = discord.ui.TextInput(label="x")
self.iY = discord.ui.TextInput(label="y")
self.add_item(self.iX)
self.add_item(self.iY)
self.x, self.y, self.z, self.ctx, = x, y, z, ctx
async def on_submit(self, interaction: discord.Interaction):
self.x, self.y = locate_integer(self.iX.value, self.iY.value)
if self.x == -1 or self.y == -1: return await interaction.response.send_message("Invalid coordinates.", ephemeral=True)
await interaction.response.defer()
await interaction.message.remove_attachments(interaction.message.attachments[0])
await interaction.message.edit(embed=await PlaceEmbed(self.x, self.y, self.z, self.ctx,
f"Locating ({self.x}, {self.y}), syncing…"), view=None)
f = discord.File(await draw_image(self.x, self.y, self.z), filename=f"{self.x}x{self.y}.png")
await interaction.message.edit(embed=await PlaceEmbed(self.x, self.y, self.z, self.ctx, f"Synced"), view=None)
await interaction.followup.send(view=ViewPlace(self.x, self.y, self.z, self.ctx), file=f,
embed=await PlaceEmbed(self.x, self.y, self.z, self.ctx, "Idle"))
def locate_integer(x: str, y: str):
if x.isdigit() and y.isdigit():
if int(x) > -1 and int(x) < width and int(y) > -1 and int(y) < height: return int(x), int(y)
return -1, -1
# database handling
async def find_all_pixels():
cursor = mycol.find()
return await cursor.to_list(None)
async def find_pixel(x, y):
return await mycol.find_one({'x': x, 'y': y})
async def update_pixel(x, y, name, value):
await mycol.update_one({"x": x, "y": y}, {"$set": {"author": name, "color": value}})