2019-03-02 14:38:37 +01:00
|
|
|
# dummy module
|
|
|
|
import os
|
2019-02-26 16:31:43 +01:00
|
|
|
import random
|
2019-03-02 14:38:37 +01:00
|
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
|
|
|
2019-02-26 16:31:43 +01:00
|
|
|
import discord
|
2019-03-02 14:38:37 +01:00
|
|
|
import fractale.source.main
|
2019-02-26 19:00:08 +01:00
|
|
|
from PIL import Image
|
2019-03-02 14:38:37 +01:00
|
|
|
|
|
|
|
|
2019-02-26 16:31:43 +01:00
|
|
|
class MainClass():
|
|
|
|
def __init__(self, client, modules, owners, prefix):
|
|
|
|
self.client = client
|
|
|
|
self.modules = modules
|
|
|
|
self.owners = owners
|
|
|
|
self.prefix = prefix
|
2019-03-02 14:38:37 +01:00
|
|
|
self.events = ['on_message'] # events list
|
|
|
|
self.command = "%sfractale" % self.prefix # command prefix (can be empty to catch every single messages)
|
2019-02-26 16:31:43 +01:00
|
|
|
|
2019-03-02 14:38:37 +01:00
|
|
|
self.name = "Fractale"
|
|
|
|
self.description = "Module de génération de fractales"
|
|
|
|
self.interactive = True
|
|
|
|
self.color = 0x78ffc3
|
|
|
|
self.fractals = {
|
|
|
|
"von_koch_curve_flake": {"Type": "Figures", "Max": ((5000, 5000), 5000, 10), "Min": ((0, 0), 0, 1),
|
|
|
|
"Default": "(2500 2500) 2000 5 #ffffff 1",
|
|
|
|
"Indication": "(départ) (arrivée) longueur iterations color bg stroke",
|
|
|
|
"ParseData": "pfixi"},
|
|
|
|
"von_koch_curve": {"Type": "Figures", "Max": ((5000, 5000), (5000, 5000), 10), "Min": ((0, 0), (0, 0), 1),
|
|
|
|
"Default": "(0 2500) (5000 2500) 5 #ffffff 1",
|
|
|
|
"Indication": "(départ) (arrivée) iterations color bg stroke", "ParseData": "ppixi"},
|
|
|
|
"blanc_manger": {"Type": "Figures", "Max": ((5000, 5000), (5000, 5000), 10), "Min": ((0, 0), (0, 0), 1),
|
|
|
|
"Default": "(1000 1000) (4000 4000) 7 #ffffff 1",
|
|
|
|
"Indication": "(départ) (arrivée) iterations color bg stroke", "ParseData": "ppixi"},
|
|
|
|
"dragon": {"Type": "Lsystem", "Max": ((5000, 5000), 2500, 19), "Min": ((0, 0), 1, 1),
|
|
|
|
"Default": "(2500 2500)", "Indication": "(origine) longueur iterations color bg stroke",
|
|
|
|
"ParseData": "pfixi"},
|
|
|
|
"sierpinski_triangle": {"Type": "Lsystem", "Max": ((5000, 5000), 2500, 11), "Min": ((0, 0), 0, 1),
|
|
|
|
"Default": "(0 0)", "Indication": "(origine) longueur iterations color bg stroke",
|
|
|
|
"ParseData": "pfixi"},
|
|
|
|
"fractal_plant": {"Type": "Lsystem", "Max": ((5000, 5000), 2500, 8), "Min": ((0, 0), 0, 1),
|
|
|
|
"Default": "(0 2500)", "Indication": "(origine) longueur iterations color bg stroke",
|
|
|
|
"ParseData": "pfixi"},
|
|
|
|
"koch_curve_right_angle": {"Type": "Lsystem", "Max": ((5000, 5000), 2500, 9), "Min": ((0, 0), 0, 1),
|
|
|
|
"Default": "(0 5000)",
|
|
|
|
"Indication": "(origine) longueur iterations color bg stroke",
|
|
|
|
"ParseData": "pfixi"},
|
|
|
|
"fractal_binary_tree": {"Type": "Lsystem", "Max": ((5000, 5000), 2500, 15), "Min": ((0, 0), 0, 1),
|
|
|
|
"Default": "(0 0)", "Indication": "(origine) longueur iterations color bg stroke",
|
|
|
|
"ParseData": "pfixi"}
|
|
|
|
}
|
|
|
|
self.help = """\
|
2019-02-26 19:00:08 +01:00
|
|
|
</prefix>fractale [fractale] [nombre d'itérations]
|
|
|
|
=> Génère une image fractale. (Si on met le nombre d'itérations, on doit mettre le nom de la fractale.)
|
|
|
|
|
2019-02-28 17:03:42 +01:00
|
|
|
-> Valeurs possible pour [fractale]
|
2019-02-27 02:49:02 +01:00
|
|
|
```..: Toutes les fractales:
|
2019-03-02 14:38:37 +01:00
|
|
|
%s```""" % '\n'.join(['......: %s' % t for t in self.fractals.keys()])
|
2019-02-28 17:03:42 +01:00
|
|
|
|
|
|
|
def parse(self, inp):
|
2019-03-02 14:38:37 +01:00
|
|
|
retDic = {"Success": False, "Message": "", "Result": ()}
|
|
|
|
# Parsing the fractal name and storing the corresponding dic into a variable
|
2019-02-28 17:03:42 +01:00
|
|
|
try:
|
2019-03-02 14:38:37 +01:00
|
|
|
fractal = self.fractals[inp.split(' ')[0]]
|
2019-02-28 17:03:42 +01:00
|
|
|
except KeyError:
|
2019-03-02 14:38:37 +01:00
|
|
|
retDic.update({"Success": False, "Message": "La fractale %s n'existe pas." % inp.split(' ')[0]})
|
|
|
|
return (retDic)
|
|
|
|
arg = ' '.join(inp.split(' ')[1:]) # Stuff after the fractal name
|
|
|
|
# checking for incoherent parentheses usage
|
|
|
|
parentheses_count = 0
|
|
|
|
for i, char in enumerate(arg):
|
|
|
|
if char == '(':
|
|
|
|
parentheses_count += 1
|
|
|
|
elif char == ')':
|
|
|
|
parentheses_count -= 1
|
|
|
|
if not (-1 < parentheses_count < 2):
|
|
|
|
retDic.update({"Success": False,
|
|
|
|
"Message": "Usage invalide de parentheses au charactère numéro %s (à partir d'après le nom de la fractale)." % i})
|
|
|
|
return (retDic)
|
2019-02-28 17:03:42 +01:00
|
|
|
|
2019-03-02 14:38:37 +01:00
|
|
|
# Here, we have a coherent parentheses usage
|
2019-02-28 17:03:42 +01:00
|
|
|
if ',' in arg:
|
2019-03-02 14:38:37 +01:00
|
|
|
retDic.update({"Success": False,
|
|
|
|
"Message": "Les virgules n'ont pas leur place dans les paramètres de génération. Il ne doit y avoir que des espaces uniques."})
|
|
|
|
return (retDic)
|
2019-02-28 17:03:42 +01:00
|
|
|
|
2019-03-02 14:38:37 +01:00
|
|
|
# parsing the fractal
|
|
|
|
args = arg.replace(')', '').replace('(', '').split(' ')
|
|
|
|
parsed_args = []
|
|
|
|
i = 0
|
|
|
|
for parse in fractal['ParseData']:
|
|
|
|
if parse == 'p':
|
|
|
|
if args[i] != '*':
|
2019-02-28 17:03:42 +01:00
|
|
|
try:
|
|
|
|
int(args[i])
|
2019-03-02 14:38:37 +01:00
|
|
|
int(args[i + 1])
|
2019-02-28 17:03:42 +01:00
|
|
|
except:
|
2019-03-02 14:38:37 +01:00
|
|
|
retDic.update({"Success": False,
|
|
|
|
"Message": "Les valeurs ne sont pas du bon type. (nombre entiers attendus pour les coordonnées d'un point)"})
|
|
|
|
return (retDic)
|
|
|
|
parsed_args.append((int(args[i]), int(args[i + 1])))
|
|
|
|
i += 2
|
2019-02-28 17:03:42 +01:00
|
|
|
else:
|
2019-03-02 14:38:37 +01:00
|
|
|
parsed_args.append(
|
|
|
|
self.parse(inp.split(' ')[0] + ' ' + fractal['Default'])['Result'][len(parsed_args)])
|
|
|
|
i += 1
|
2019-02-28 17:03:42 +01:00
|
|
|
|
2019-03-02 14:38:37 +01:00
|
|
|
elif parse == 'f':
|
|
|
|
if args[i] != '*':
|
2019-02-28 17:03:42 +01:00
|
|
|
try:
|
|
|
|
float(args[i])
|
|
|
|
except:
|
2019-03-02 14:38:37 +01:00
|
|
|
retDic.update({"Success": False,
|
|
|
|
"Message": "Les valeurs ne sont pas du bon type. (Nombre à virgule flottante attendu (mettre un point pour la virgule))"})
|
|
|
|
return (retDic)
|
2019-02-28 17:03:42 +01:00
|
|
|
parsed_args.append(float(args[i]))
|
2019-03-02 14:38:37 +01:00
|
|
|
i += 1
|
2019-02-28 17:03:42 +01:00
|
|
|
else:
|
2019-03-02 14:38:37 +01:00
|
|
|
parsed_args.append(
|
|
|
|
self.parse(inp.split(' ')[0] + ' ' + fractal['Default'])['Result'][len(parsed_args)])
|
|
|
|
i += 1
|
2019-02-28 17:03:42 +01:00
|
|
|
|
2019-03-02 14:38:37 +01:00
|
|
|
elif parse == 'i':
|
|
|
|
if args[i] != '*':
|
2019-02-28 17:03:42 +01:00
|
|
|
try:
|
|
|
|
int(args[i])
|
|
|
|
except:
|
|
|
|
print(args[i])
|
|
|
|
print(i)
|
2019-03-02 14:38:37 +01:00
|
|
|
retDic.update({"Success": False,
|
|
|
|
"Message": "Les valeurs ne sont pas du bon type. (Nombre entier attendu)"})
|
|
|
|
return (retDic)
|
2019-02-28 17:03:42 +01:00
|
|
|
parsed_args.append(int(args[i]))
|
2019-03-02 14:38:37 +01:00
|
|
|
i += 1
|
2019-02-28 17:03:42 +01:00
|
|
|
else:
|
2019-03-02 14:38:37 +01:00
|
|
|
parsed_args.append(
|
|
|
|
self.parse(inp.split(' ')[0] + ' ' + fractal['Default'])['Result'][len(parsed_args)])
|
|
|
|
i += 1
|
2019-02-28 17:03:42 +01:00
|
|
|
|
2019-03-02 14:38:37 +01:00
|
|
|
elif parse == 'x':
|
|
|
|
if args[i] != '*':
|
2019-02-28 17:03:42 +01:00
|
|
|
try:
|
|
|
|
if '#' in args[i]:
|
2019-03-02 14:38:37 +01:00
|
|
|
int(args[i].replace('#', '0x'), 16)
|
2019-02-28 17:03:42 +01:00
|
|
|
else:
|
|
|
|
raise
|
|
|
|
except:
|
2019-03-02 14:38:37 +01:00
|
|
|
retDic.update({"Success": False,
|
|
|
|
"Message": "Les valeurs ne sont pas du bon type. (Valeur hexadécimale attendue)"})
|
|
|
|
return (retDic)
|
|
|
|
parsed_args.append(int(args[i].replace('#', '0x'), 16))
|
|
|
|
i += 1
|
2019-02-28 17:03:42 +01:00
|
|
|
else:
|
2019-03-02 14:38:37 +01:00
|
|
|
parsed_args.append(
|
|
|
|
self.parse(inp.split(' ')[0] + ' ' + fractal['Default'])['Result'][len(parsed_args)])
|
|
|
|
i += 1
|
|
|
|
retDic.update({"Success": True, "Result": parsed_args})
|
|
|
|
return (retDic)
|
2019-02-28 17:03:42 +01:00
|
|
|
|
2019-02-26 16:31:43 +01:00
|
|
|
async def on_message(self, message):
|
2019-03-02 14:38:37 +01:00
|
|
|
# await message.channel.send(str(self.parse(message.content[len("%sfractale "%self.prefix):])))
|
|
|
|
# ħere
|
|
|
|
args = message.content.split(" ")
|
|
|
|
tmpstr = "/tmp/%s.png" % random.randint(1, 10000000)
|
|
|
|
im = Image.new('RGB', (5000, 5000), (0, 0, 0)) # here
|
|
|
|
|
|
|
|
fig = fractale.source.main.Figures(im=im) # here
|
|
|
|
if len(args) == 1:
|
|
|
|
await self.client.loop.run_in_executor(ThreadPoolExecutor(), fig.von_koch_curve_flake,
|
|
|
|
*((2500, 2500), 2000, 5))
|
|
|
|
elif args[1].lower() == "blanc_manger":
|
|
|
|
iterations = 7
|
|
|
|
if len(args) > 2 and int(args[2]) <= 20:
|
|
|
|
iterations = int(args[2])
|
|
|
|
await self.client.loop.run_in_executor(ThreadPoolExecutor(), fig.blanc_manger,
|
|
|
|
*((1000, 1000), (4000, 4000), iterations))
|
|
|
|
elif args[1].lower() == "von_koch_curve_flake":
|
|
|
|
iterations = 5
|
|
|
|
if len(args) > 2 and int(args[2]) <= 7:
|
|
|
|
iterations = int(args[2])
|
|
|
|
await self.client.loop.run_in_executor(ThreadPoolExecutor(), fig.von_koch_curve_flake,
|
|
|
|
*((2500, 2500), 2000, iterations))
|
|
|
|
elif args[1].lower() == "von_koch_curve":
|
|
|
|
iterations = 5
|
|
|
|
if len(args) > 2 and int(args[2]) <= 7:
|
|
|
|
iterations = int(args[2])
|
|
|
|
await self.client.loop.run_in_executor(ThreadPoolExecutor(), fig.von_koch_curve,
|
|
|
|
*((0, 2500), (5000, 2500), iterations))
|
2019-02-27 03:06:57 +01:00
|
|
|
else:
|
|
|
|
await self.modules['help'][1].send_help(message.channel, self)
|
2019-03-02 14:38:37 +01:00
|
|
|
return # here
|
|
|
|
im.save(tmpstr) # here
|
2019-02-26 16:31:43 +01:00
|
|
|
await message.channel.send(file=discord.File(tmpstr))
|
2019-03-02 14:38:37 +01:00
|
|
|
os.remove(tmpstr) # here
|