[fractale module] finished module.

This commit is contained in:
Suwako Moriya 2019-03-04 01:25:09 +01:00
parent 333ac8b732
commit 0fc44eef2f

View File

@ -1,7 +1,9 @@
# dummy module
import discord
import random
import fractale.source.main
from PIL import Image
class MainClass():
def __init__(self, client, modules, owners, prefix):
self.client = client
@ -16,32 +18,32 @@ class MainClass():
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"}
"von_koch_curve_flake": {"Type": "Figures", "Max": ((5000, 5000), 5000, 10, 16777215, 16777215, 5000), "Min": ((0, 0), 0, 1, 0, 0, 0),
"Default": "(2500 2500) 2000 5 #81ff65 #191919 2",
"Indication": "(départ) (arrivée) longueur iterations couleur_trait couleur_fond stroke",
"ParseData": "pfixxi"},
"von_koch_curve": {"Type": "Figures", "Max": ((5000, 5000), (5000, 5000), 10, 16777215, 16777215, 5000), "Min": ((0, 0), (0, 0), 1, 0, 0, 0),
"Default": "(0 2500) (5000 2500) 5 #81ff65 #191919 2",
"Indication": "(départ) (arrivée) iterations couleur_trait couleur_fond stroke", "ParseData": "ppixxi"},
"blanc_manger": {"Type": "Figures", "Max": ((5000, 5000), (5000, 5000), 10, 16777215, 16777215, 5000), "Min": ((0, 0), (0, 0), 1, 0, 0, 0),
"Default": "(1000 1000) (4000 4000) 7 #81ff65 #191919 2",
"Indication": "(départ) (arrivée) iterations couleur_trait couleur_fond stroke", "ParseData": "ppixxi"},
"dragon": {"Type": "Lsystem", "Max": ((5000, 5000), 2500, 19, 16777215, 16777215, 5000), "Min": ((0, 0), 1, 1, 0, 0, 0),
"Default": "(2500 2500) 4 18 #81ff65 #191919 2", "Indication": "(origine) longueur iterations couleur_trait couleur_fond stroke",
"ParseData": "pfixxi"},
"sierpinski_triangle": {"Type": "Lsystem", "Max": ((5000, 5000), 2500, 11, 16777215, 16777215, 5000), "Min": ((0, 0), 0, 1, 0, 0, 0),
"Default": "(0 0) 10 9 #81ff65 #191919 2", "Indication": "(origine) longueur iterations couleur_trait couleur_fond stroke",
"ParseData": "pfixxi"},
"fractal_plant": {"Type": "Lsystem", "Max": ((5000, 5000), 2500, 8, 16777215, 16777215, 5000), "Min": ((0, 0), 0, 1, 0, 0, 0),
"Default": "(0 2500) 6.5 8 #81ff65 #191919 2", "Indication": "(origine) longueur iterations couleur_trait couleur_fond stroke",
"ParseData": "pfixxi"},
"koch_curve_right_angle": {"Type": "Lsystem", "Max": ((5000, 5000), 2500, 9, 16777215, 16777215, 5000), "Min": ((0, 0), 0, 1, 0, 0, 0),
"Default": "(0 5000) 2.25 7 #81ff65 #191919 2",
"Indication": "(origine) longueur iterations couleur_trait couleur_fond stroke",
"ParseData": "pfixxi"},
"fractal_binary_tree": {"Type": "Lsystem", "Max": ((5000, 5000), 2500, 15, 16777215, 16777215, 5000), "Min": ((0, 0), 0, 1, 0, 0, 0),
"Default": "(0 0) 1.2 12 #81ff65 #191919 2", "Indication": "(origine) longueur iterations couleur_trait couleur_fond stroke",
"ParseData": "pfixxi"}
}
self.help = """\
</prefix>fractale info <fractale>
@ -62,27 +64,48 @@ class MainClass():
if len(args) == 1:
await self.modules['help'][1].send_help(message.channel, self)
elif len(args) == 2:
pass
if args[1] in self.fractals.keys():
await self.send_fractal(message, args[1]+' '+' '.join(['*']*len(self.fractals[args[1]]['ParseData'])))
elif len(args) == 3:
if args[1] == "" and args[2] in self.fractals.keys():
if args[1] == "info" and args[2] in self.fractals.keys():
description = """\
La fractale {nom} attend les arguments suivant :
`{arguments}`
avec le type suivant:
`{type}`
Les nombres décimaux sont des nombres à virgules les nombres entiers n'en ont pas. **(mettre un point à la place de la virgule)**
Attention, les coordonnées des points doivent être entre parentheses et son considérés comme un unique argument.
Attention, les coordonnées des points doivent être entre parentheses et sont considérées comme un unique argument.
Gardez aussi en tête que **l'image a pour résolution 5000*5000.**
Les arguments valent par défaut :
`{defaut}`""".format(nom=args[2])
`{defaut}`""".format(nom=args[2], arguments=self.fractals[args[2]]['Indication'], type=' '.join(["point decimal entier hexadecimal".split(" ")["pfix".index(ch)] for ch in self.fractals[args[2]]['ParseData']]), defaut=self.fractals[args[2]]['Default'])
await message.channel.send(
embed=discord.Embed(title="[%s] - Infos : *%s*" % (self.name, args[2]), description=""))
embed=discord.Embed(title="[%s] - Infos : *%s*" % (self.name, args[2]), description=description, color=self.color))
else:
await self.modules['help'][1].send_help(message.channel, self)
# parsed_data=self.parse(message.content[len("%sfractale "%self.prefix):])))
elif args[1] in self.fractals.keys():
await self.send_fractal(message, ' '.join(args[1:]))
else:
await self.modules['help'][1].send_help(message.channel, self)
async def send_fractal(self, message, command):
return # TODO Coder la fonction
parsed_data=self.parse(command)
if parsed_data["Success"]:
res=parsed_data["Result"]
tmpstr="/tmp/%s.png"%random.randint(1,10000000)
im=Image.new('RGB', (5000,5000), tuple([int(i, 16) for i in map(''.join, zip(*[iter(hex(res[-2])[2:].ljust(6,'0'))]*2))]))
fig = eval("fractale.source.main."+self.fractals[command.split(' ')[0]]["Type"]+"(im=im)")
if self.fractals[command.split(' ')[0]]["Type"]=="Lsystem":
fig.state.x,fig.state.y=res[0]
#fig.state.color,fig.state.width=tuple([int(i, 16) for i in map(''.join, zip(*[iter(hex(res[-3])[2:].ljust(6,'0'))]*2))]),res[-1]
eval("fig."+command.split(' ')[0])(*res[1:-3],color=tuple([int(i, 16) for i in map(''.join, zip(*[iter(hex(res[-3])[2:].ljust(6,'0'))]*2))]), width=res[-1])
else:
eval("fig."+command.split(' ')[0])(*res[:-3],color=tuple([int(i, 16) for i in map(''.join, zip(*[iter(hex(res[-3])[2:].ljust(6,'0'))]*2))]), width=res[-1])
im.save(tmpstr)
await message.channel.send(file=discord.File(tmpstr))
else:
await message.channel.send(parsed_data["Message"])
def parse(self, inp):
retDic = {"Success": False, "Message": "", "Result": ()}
@ -181,5 +204,15 @@ Les arguments valent par défaut :
parsed_args.append(
self.parse(inp.split(' ')[0] + ' ' + fractal['Default'])['Result'][len(parsed_args)])
i += 1
for i,element in enumerate(parsed_args):
print(element,fractal['Max'][i])
if element>fractal['Max'][i]:
retDic.update({"Success": False,
"Message": "Les valeurs sont trop grandes (%s > %s)"%(str(element),str(fractal['Max'][i]))})
return retDic
elif element<fractal['Min'][i]:
retDic.update({"Success": False,
"Message": "Les valeurs sont trop petites (%s < %s)"%(str(element),str(fractal['Min'][i]))})
return retDic
retDic.update({"Success": True, "Result": parsed_args})
return (retDic)