import tempfile
import os
import sys
+import gi
+gi.require_version('Rsvg', '2.0')
+from gi.repository import Rsvg
import math
import random as rand
(fd, filename) = tempfile.mkstemp(suffix=".svg", prefix="busart", dir=OUTPUT_DIR);
os.close(fd)
-os.chmod(filename, 0o644);
+os.chmod(filename, 0o644)
+
+if len(sys.argv) < 2:
+ sys.stderr.write("This script requires a single argument (value, 1 - 4)")
+ sys.exit(1)
+
+user = int(sys.argv[1])
+stderr = sys.stderr
# Also delete our import for some more safety
del tempfile
del os
del sys
+del gi
# Note: We'll let the following "safe" imports remain:
#
'yellow3': (0xcd/0xff, 0xcd/0xff, 0x00/0xff),
'yellow4': (0x8b/0xff, 0x8b/0xff, 0x00/0xff),
'yellowgreen': (0x9a/0xff, 0xcd/0xff, 0x32/0xff)
-};
+}
+
+hint = [
+ "WAT", "ER ", "PLA", "NET"
+]
+
+code = [
+ "304", "570", "325", "6"
+]
+
+r = 0
+g = 0
+b = 0
+a = 1.0
with cairo.SVGSurface(filename, 512, 512) as surface:
+
cr = cairo.Context(surface);
+ cr.set_font_size(190)
+ cr.select_font_face("sans", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
+ # Setup RSVG rendering
+ rsvg = Rsvg.Handle()
+
+ answer_shown = False
+
del cairo
+ del Rsvg
+ def random_within(max):
+ return max * random()
+
+ def wiggle():
+ delta = 200
+ cr.rel_curve_to(-delta/2 + random_within(delta),
+ -delta/2 + random_within(delta),
+ -delta/2 + random_within(delta),
+ -delta/2 + random_within(delta),
+ -delta/2 + random_within(delta),
+ -delta/2 + random_within(delta))
+
def arc(x, y, r, a1, a2):
- cr.arc(x, y, r, a1, a2);
+ cr.arc(x, y, r, a1, a2)
def circle(x, y, r):
- cr.arc(x, y, r, 0, 2 * math.pi);
+ cr.arc(x, y, r, 0, 2 * math.pi)
def fill():
- cr.fill();
+ cr.fill()
+
+ def stroke():
+ cr.stroke()
def set_color(color):
+ global r, g, b, a
if color in COLORS:
- (r,g,b) = COLORS[color];
+ (r,g,b) = COLORS[color]
else:
(r,g,b) = (0,0,0)
- cr.set_source_rgb(r, g, b)
+ cr.set_source_rgba(r, g, b, a)
- cr.set_line_width(6);
- exec(input)
+ def set_random_color():
+ global r, g, b, a
+ (r,g,b) = rand.choice(list(COLORS.values()))
+ cr.set_source_rgba(r, g, b, a)
+
+ def set_opacity(opacity):
+ global a
+ a = opacity
+ cr.set_source_rgba(r, g, b, a)
+
+ def move_to(x, y):
+ cr.move_to(x, y)
+
+ def line_to(x, y):
+ cr.line_to(x, y)
+
+ def line(x, y, dx, dy):
+ cr.move_to(x, y)
+ cr.rel_line_to(dx, dy)
+
+ def set_line_width(width):
+ cr.set_line_width(width)
-web_file = filename.removeprefix(OUTPUT_DIR_PREFIX);
+ def show_answer():
+ global answer_shown
+ answer_shown = True
+ # After the submitted code, draw the solution in white, (so it will
+ # only be clearly visible if they've made their drawing big enough).
+ cr.set_source_rgb(1, 1, 1)
+ cr.move_to(0,250)
+ cr.show_text(hint[user])
+ cr.set_font_size(240)
+ cr.move_to(0,480)
+ cr.show_text(code[user])
+
+ def fingers():
+ show_answer()
+ svg = rsvg.new_from_file("hand.svg")
+ cr.scale(0.2,0.2)
+ for i in range(16):
+ cr.save()
+ cr.translate(i%4 * 500, i/4 * 500)
+ cr.rotate(2 * math.pi * random())
+ svg.render_cairo(cr)
+ cr.restore()
+
+ def mouths():
+ show_answer()
+ svg = rsvg.new_from_file("mouth.svg")
+ cr.scale(0.5,0.5)
+ for i in range(16):
+ cr.save()
+ cr.translate(i%4 * 150, i/4 * 150)
+ cr.rotate(2 * math.pi * random())
+ svg.render_cairo(cr)
+ cr.restore()
+
+ def eyes():
+ show_answer()
+ svg = rsvg.new_from_file("eye.svg")
+ cr.scale(0.2,0.2)
+ for i in range(16):
+ cr.save()
+ cr.translate(i%4 * 600, i/4 * 600)
+ cr.rotate(2 * math.pi * random())
+ svg.render_cairo(cr)
+ cr.restore()
+
+ # Run the submitted code
+ exec(input)
-print(web_file);
+ # If the answer hasn't been shown yet, show it now
+ if not answer_shown:
+ show_answer()
+web_file = filename.removeprefix(OUTPUT_DIR_PREFIX)
+print(web_file)