152 lines
4.1 KiB
Python
Executable File
152 lines
4.1 KiB
Python
Executable File
import random
|
|
import sys
|
|
from botclient.botclient import Bot
|
|
from collections import defaultdict
|
|
|
|
GRID_TYPES = [ "latin_square", "concentric", "sator", "random", "vertical", "horizontal" ]
|
|
|
|
BLANK_P = {
|
|
0: 3,
|
|
1: 2,
|
|
2: 2,
|
|
3: 1,
|
|
4: 1,
|
|
5: 1,
|
|
6: 0,
|
|
7: 0,
|
|
8: 0,
|
|
9: 0,
|
|
}
|
|
|
|
class Grawlix(Bot):
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.ap.add_argument('-a', '--all', action='store_true', help="Dump a random grid of emojos")
|
|
self.ap.add_argument('-t', '--test', action='store_true', help="Dump emojo list")
|
|
|
|
def load_emojos(self):
|
|
self.emojos = []
|
|
self.groups = defaultdict(set)
|
|
with open(self.cf["emojos"], "r") as efh:
|
|
for line in efh:
|
|
parts = line.split()
|
|
self.emojos.append(parts[0])
|
|
for tag in parts[1:]:
|
|
self.groups[tag].add(parts[0])
|
|
self.tags = [ t for t in self.groups.keys() if not t == "blank" ]
|
|
|
|
|
|
def make_glyphset(self, n):
|
|
blanks_r = random.randrange(10)
|
|
blanks_n = BLANK_P[blanks_r]
|
|
emojos_n = n - blanks_n
|
|
if "groups" in self.cf and random.randrange(2) == 0:
|
|
tag = random.choice(self.cf["groups"])
|
|
glyphset = random.sample(list(self.groups[tag]), emojos_n)
|
|
else:
|
|
glyphset = random.sample(self.emojos, emojos_n)
|
|
blank = list(self.groups["blank"])[0]
|
|
glyphset += ( [ blank ] * blanks_n )
|
|
random.shuffle(glyphset)
|
|
return glyphset
|
|
|
|
def latin_square(self):
|
|
return [
|
|
[ 2, 1, 0, 4, 3 ],
|
|
[ 0, 4, 3, 2, 1 ],
|
|
[ 3, 2, 1, 0, 4 ],
|
|
[ 1, 0, 4, 3, 2 ],
|
|
[ 4, 3, 2, 1, 0 ],
|
|
]
|
|
|
|
def concentric(self):
|
|
return [
|
|
[ 0, 1, 2, 1, 0 ],
|
|
[ 1, 2, 3, 2, 1 ],
|
|
[ 2, 3, 4, 3, 2 ],
|
|
[ 1, 2, 3, 2, 1 ],
|
|
[ 0, 1, 2, 1, 0 ],
|
|
]
|
|
|
|
def sator(self):
|
|
return [
|
|
[ 0, 1, 2, 3, 4 ],
|
|
[ 1, 5, 6, 7, 3 ],
|
|
[ 2, 6, 8, 6, 2 ],
|
|
[ 3, 7, 6, 5, 1 ],
|
|
[ 4, 3, 2, 1, 0 ],
|
|
]
|
|
|
|
def random(self):
|
|
s = [ 0, 1, 2, 3, 4 ]
|
|
return [
|
|
[ random.choice(s) for i in range(5) ],
|
|
[ random.choice(s) for i in range(5) ],
|
|
[ random.choice(s) for i in range(5) ],
|
|
[ random.choice(s) for i in range(5) ],
|
|
[ random.choice(s) for i in range(5) ],
|
|
]
|
|
|
|
def vertical(self):
|
|
return [ [ 0, 1, 2, 3, 4 ] for i in range(5) ]
|
|
|
|
def horizontal(self):
|
|
return [ [ i, i, i, i, i ] for i in range(5) ]
|
|
|
|
def make_grid(self):
|
|
type = random.choice(GRID_TYPES)
|
|
if type == "latin_square":
|
|
return self.latin_square()
|
|
if type == "concentric":
|
|
return self.concentric()
|
|
if type == "sator":
|
|
return self.sator()
|
|
if type == "random":
|
|
return self.random()
|
|
if type == "vertical":
|
|
return self.vertical()
|
|
if type == "horizontal":
|
|
return self.horizontal()
|
|
|
|
def grid_glyphs(self, grid):
|
|
return max([max(row) for row in grid]) + 1
|
|
|
|
def render_row(self, row, glyphs):
|
|
return " ".join([glyphs[c] for c in row])
|
|
|
|
def render(self):
|
|
grid = self.make_grid()
|
|
n = self.grid_glyphs(grid)
|
|
glyphs = self.make_glyphset(n)
|
|
if self.args.dry_run:
|
|
print(glyphs)
|
|
rows = [ self.render_row(r, glyphs) for r in grid ]
|
|
return "\n".join(rows)
|
|
|
|
def header(self):
|
|
for r in range(20):
|
|
names = [ random.choice(self.emojos) for c in range(100) ]
|
|
files = [ f'<img src="grawlix_{n}.png" /> ' for n in names ]
|
|
print(''.join(files))
|
|
print('<br />')
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
g = Grawlix()
|
|
g.configure()
|
|
g.load_emojos()
|
|
if g.args.all:
|
|
g.header()
|
|
sys.exit(1)
|
|
if g.args.test:
|
|
print(g.tags)
|
|
print(g.cf)
|
|
sys.exit(1)
|
|
post = g.render()
|
|
g.wait()
|
|
g.post(post)
|
|
print(post)
|
|
|