ypergen

- take a break from javascript

Back to documentation

warning: 🔊

Show sourcesshoot_em_up.py
from hypergen.imports import *

from random import choice
from time import time

from django.templatetags.static import static
from django.views.decorators.cache import never_cache

from website.templates2 import base_example_template

@never_cache
@liveview(perm=NO_PERM_REQUIRED)
def shoot_em_up(request):
    with base_example_template(__file__):
        script("""
            function play(url) {
                new Audio(url).play()
            }
        """)

        template(start_time=time())

@action(perm=NO_PERM_REQUIRED, target_id="shoot-em-up")
def fire(request, start_time, hits, target_num):
    template(start_time, hits=hits + 1, target_num=target_num)
    command("play", static("website/gun.mp3"))

def template(start_time=None, hits=0, target_num=-1):
    target_num = choice(list(set(range(0, 5)) - {target_num}))

    with div(id="shoot-em-up"):
        for i in range(0, 5):
            if i == target_num:
                img(
                    id="fire",
                    src=static("website/target.svg"),
                    onmousedown=callback(fire, start_time, hits, target_num),
                )
            else:
                img(src=static("website/duck.svg"))

        if hits:
            rate = round(hits / (time() - start_time), 2)
            div(b("hits: "), hits)
            div(b("rate: "), rate, "/s")
        else:
            div(b("warning:"), "🔊", sep=" ")
shoot_em_up_alt2.py
from hypergen.imports import *
from hypergen.incubation import SessionStore

from random import choice
from time import time

from django.templatetags.static import static
from django.views.decorators.cache import never_cache

from website.templates2 import doc_base_template

settings = dict(perm=NO_PERM_REQUIRED, base_template=doc_base_template(__file__, "shoot-em-up"))

class State(SessionStore):
    def reset(self):
        self.target_num = -1
        self.hits = 0
        self.start_time = time()

state = State()

@never_cache
@liveview(**settings)
def shoot_em_up_alt(request):
    if request.method == "GET":
        script("""
            function play(url) {
                new Audio(url).play()
            }
        """)
        state.reset()

    state.target_num = choice(list(set(range(0, 5)) - {state.target_num}))

    for i in range(0, 5):
        if i == state.target_num:
            img(id="fire_alt", src=static("website/target.svg"), onmousedown=callback(fire_alt))
        else:
            img(src=static("website/duck.svg"))

    if state.hits:
        rate = round(state.hits / (time() - state.start_time), 2)
        div(b("hits: "), state.hits)
        div(b("rate: "), rate, "/s")
    else:
        div(b("warning:"), "🔊", sep=" ")

@action(base_view=shoot_em_up_alt, **settings)
def fire_alt(request):
    state.hits += 1
    command("play", static("website/gun.mp3"))
shoot_em_up_alt.py
from hypergen.imports import *

from random import choice
from time import time

from django.templatetags.static import static
from django.views.decorators.cache import never_cache

from website.templates2 import doc_base_template

def init_appstate():
    return {"target_num": -1, "hits": 0, "start_time": time()}

settings = dict(
    perm=NO_PERM_REQUIRED,
    base_template=doc_base_template(__file__, "shoot-em-up"),
    appstate=init_appstate,
)

@never_cache
@liveview(**settings)
def shoot_em_up_alt(request):
    if request.method == "GET":
        script("""
            function play(url) {
                new Audio(url).play()
            }
        """)
        context.hypergen.appstate = init_appstate()

    context.hypergen.appstate["target_num"] = choice(
        list(set(range(0, 5)) - {context.hypergen.appstate["target_num"]}))

    for i in range(0, 5):
        if i == context.hypergen.appstate["target_num"]:
            img(id="fire_alt", src=static("website/target.svg"), onmousedown=callback(fire_alt))
        else:
            img(src=static("website/duck.svg"))

    if context.hypergen.appstate["hits"]:
        rate = round(context.hypergen.appstate["hits"] / (time() - context.hypergen.appstate["start_time"]), 2)
        div(b("hits: "), context.hypergen.appstate["hits"])
        div(b("rate: "), rate, "/s")
    else:
        div(b("warning:"), "🔊", sep=" ")

@action(base_view=shoot_em_up_alt, **settings)
def fire_alt(request):
    context.hypergen.appstate["hits"] += 1
    command("play", static("website/gun.mp3"))