Beispiel #1
0
def player_choice():
    """
    Query database for a list of available players to bind them to a select box
    """
    db = new_session()
    players = sorted(db.query(Player).all(), key=lambda x: x.name)
    return [(p.id, p.name) for p in players]
Beispiel #2
0
def player_edit(player_id: int):
    model = forms.GenericFormModel(
        page_title="Players",
        form_title=f"Edit Player",
        post_url=f"/player/{player_id}/edit",
    )
    db = orm.new_session()
    player = db.query(Player).filter(Player.id == player_id).first()
    form = forms.PlayerForm()

    # Edit Existing Player
    if request.method == "GET":
        form.player_id.data = player.id
        form.anonymize.data = player.anon
        form.real_name.data = player.real_name
        form.alias.data = player.alias
        form.hex_id.data = player.hex_id
        model.form_title = f'Edit Player "{player.name}"'
        return render_template("generic_form.html", model=model, form=form)
    # Save POSTed data
    else:
        form = forms.PlayerForm()
        if form.validate_on_submit():
            if not player:
                player = Player()
                db.add(player)
            player.populate_from_form(form)
            db.commit()
            return redirect("/player")

        model.form_title = "Incorrect Data"
        return render_template("generic_form.html", model=model, form=form)
Beispiel #3
0
def season_edit(season_id: int):
    model = forms.GenericFormModel(
        page_title="Seasons",
        form_title="Edit Season",
        post_url=f"/season/edit/{season_id}",
    )
    db = orm.new_session()
    season = db.query(Season).get(season_id)
    form = forms.SeasonForm()

    if request.method == "GET":
        form.season_id.process_data(season.id)
        form.code.process_data(season.code)
        form.game_name.process_data(season.game)
        form.description.process_data(season.description)
        form.start.process_data(season.start)
        form.end.process_data(season.end)

        model.form_title = f"Edit Season '{season.code}: {season.game}'"
        return render_template("generic_form.html", model=model, form=form)
    else:
        if not form.validate_on_submit():
            model.errors = form.errors
            return render_template("generic_form.html", model=model, form=form)

        if not season:
            season = Season()
            db.add(season)

        season.populate_from_form(form)
        db.commit()
        return redirect(url_for("season_list"))
Beispiel #4
0
def enemy_edit(enemy_id: int):
    model = forms.GenericFormModel(
        page_title="Enemies",
        form_title="Edit Enemy",
        post_url=f"/enemy/{enemy_id}/edit",
    )
    db = orm.new_session()
    enemy = db.query(Enemy).get(enemy_id)
    form = forms.EnemyForm()

    if request.method == "GET":
        form.season_id.data = enemy.season_id if enemy.season_id else -1
        form.name.data = enemy.name
        form.is_boss.data = enemy.boss
        form.enemy_id.data = enemy_id
        model.form_title = f'Edit Enemy "{enemy.name}"'
        return render_template("generic_form.html", model=model, form=form)
    else:
        form = forms.EnemyForm()
        if form.validate_on_submit():
            if not enemy:
                enemy = Enemy()
                db.add(enemy)
            enemy.populate_from_form(form)
            db.commit()
            if form.submit_continue_button.data:
                return redirect(url_for("enemy_new", preselect=form.season_id.data))
            return redirect(url_for("enemy_list"))

        model.form_title = "Incorrect Data"
        return render_template("generic_form.html", model=model, form=form)
Beispiel #5
0
def drink_choice():
    """
    Query database for a list of all available drinks to select from
    """
    db = new_session()
    drinks = db.query(Drink).order_by(Drink.name).all()
    choices = [(d.id, d.name) for d in drinks]
    choices.insert(0, (-1, "None"))
    return choices
Beispiel #6
0
def drink_list():
    db = orm.new_session()
    drinks = db.query(Drink).order_by(Drink.name).all()
    model = {
        "drinks": drinks,
        "columns": [("name", "Drink Name"), ("vol", "Alcohol %")],
        "controls": [("edit", "Edit")],
    }
    return render_template("drink_list.html", model=model)
Beispiel #7
0
def season_choices():
    """
    Query the database for available seasons.
    This returns a list of tuples with the season ID and a display string.
    """
    db = new_session()
    seasons = db.query(Season).order_by(Season.code).all()
    choices = [(s.id, f"{s.code}: {s.game}") for s in seasons]
    choices.insert(0, (-1, "No Season"))
    return choices
Beispiel #8
0
def player_list():
    db = orm.new_session()
    players = db.query(Player)
    model = {
        "player_list": players,
        "columns": [
            ("name", "Player Name"),
            ("alias", "Alias"),
            ("hex_id", "Hex ID"),
        ],
    }
    return render_template("player_list.html", model=model)
Beispiel #9
0
def enemy_choice_for_season(season_id: int):
    """
    Query database for all available enemies in this season
    """
    db = new_session()
    season: Season = db.query(Season).get(season_id)
    season_enemies = [
        enemy for enemy in season.enemies if not enemy.is_defeated
    ]
    global_enemies = db.query(Enemy).filter(Enemy.season_id == -1).all()
    if not season and not global_enemies:
        return []
    combined = global_enemies + season_enemies
    return [(e.id, e.name) for e in combined]
Beispiel #10
0
def drink_edit(drink_id: int):
    db = orm.new_session()
    drink = db.query(Drink).filter(Drink.id == drink_id).first()
    form = forms.DrinkForm()
    form.drink_id.data = drink.id
    form.name.data = drink.name
    form.vol.data = drink.vol

    model = forms.GenericFormModel(
        page_title="Edit Drink",
        form_title=f'Edit Drink "{drink.name}"',
        post_url="/drink/save",
    )

    return render_template("generic_form.html", model=model, form=form)
Beispiel #11
0
def season_overview(season_id: int):
    db = orm.new_session()
    season = db.query(Season).filter(Season.id == season_id).first()

    infos = {
        "Number": season.code,
        "Game": season.game,
        "Start Date": season.start,
        "End Date": season.end if season.end else "Ongoing",
    }
    model = {
        "title": f"{season.code} {season.game}",
        "season_info": infos,
        "episodes": season.episodes,
    }
    return render_template("season_overview.html", model=model)
Beispiel #12
0
def drink_save():
    form = forms.DrinkForm()
    if form.validate_on_submit():
        drink_id = int(form.drink_id.data) if form.drink_id.data else None
        db = orm.new_session()
        if drink_id:
            drink = db.query(Drink).filter(Drink.id == drink_id).first()
        else:
            drink = Drink()
            db.add(drink)
        drink.populate_from_form(form)
        err = db.commit()
        return redirect("/drink")

    model = forms.GenericFormModel(page_title="Drinks",
                                   form_title="Edit Drink",
                                   post_url="/drink/save")
    return render_template("generic_form.html", model=model, form=form)
Beispiel #13
0
def episode_detail(season_id: int, episode_id: int):
    db = orm.new_session()
    episode: Episode = db.query(Episode).get(episode_id)
    deaths = [
        event for event in episode.events if event.type == orm.EventType.Death
    ]
    victories = [
        event for event in episode.events
        if event.type == orm.EventType.Victory
    ]
    model = {
        "title": f"{episode.season.code}{episode.code}",
        "episode": episode,
        "season": episode.season,
        "players": episode.players,
        "deaths": sorted(deaths, key=by_time),
        "victories": sorted(victories, key=by_time)
    }

    return render_template("episode_details.html", model=model)
Beispiel #14
0
def episode_edit(season_id: int, episode_id: int):
    model = forms.GenericFormModel(
        page_title="Edit Episode",
        form_title="Edit Episode",
        post_url=f"/season/{season_id}/episode/{episode_id}/edit",
    )
    form = forms.EpisodeForm()
    db = orm.new_session()
    episode: Episode = db.query(Episode).get(episode_id)
    if request.method == "GET":
        form.season_id.data = episode.season_id
        form.episode_id.data = episode.id
        form.code.data = episode.code
        form.date.data = episode.date
        form.start.data = episode.start
        form.end.data = episode.end
        form.title.data = episode.title
        form.players.data = [p.id for p in episode.players]
        model.form_title = f"Edit Episode '{episode.code}: {episode.title}'"
        return render_template("generic_form.html", model=model, form=form)
    else:
        if not form.validate_on_submit():
            model.errors = form.errors
            return render_template("generic_form.html", model=model, form=form)
        if not episode:
            episode = Episode()
            db.add(episode)
        season: Season = db.query(Season).get(season_id)
        episode.populate_from_form(form)
        episode.season = season
        player_ids = list(form.players.data)
        players = db.query(Player).filter(Player.id.in_(player_ids)).all()
        episode.players = players
        errors = db.commit()
        if errors:
            model.errors = {"Error saving episode": [errors]}
            return render_template("generic_form.html", model=model, form=form)
        return redirect(
            url_for("episode_detail",
                    season_id=season.id,
                    episode_id=episode.id))
Beispiel #15
0
def event_new(s_id: int, ep_id: int):
    model = {
        "page_title": "New Event",
        "form_title": "Create New Event",
        "post_url": f"/season/{s_id}/episode/{ep_id}/event/null/edit",
    }
    db = new_session()
    episode: Episode = db.query(Episode).get(ep_id)

    form = forms.EventForm()
    form.episode_id.data = ep_id
    form.enemy.choices = choices.enemy_choice_for_season(s_id)
    form.player.choices = choices.player_choice_for_episode(episode)
    form.event_type.data = EventType.Death.value

    Penalty = namedtuple("Penalty",
                         ["penalty_id", "player_id", "player", "drink"])
    for player in episode.players:
        form.penalties.append_entry(Penalty(None, player.id, player.name, 1))

    return render_template("event_editor.html", model=model, form=form)
Beispiel #16
0
def event_edit(s_id: int, ep_id: int, ev_id: int):
    model = {
        "page_title": "Edit Event",
        "form_title": "Edit Event",
        "post_url": f"/season/{s_id}/episode/{ep_id}/event/{ev_id}/edit",
    }
    db = new_session()
    event: Event = db.query(Event).get(ev_id)
    form = forms.EventForm()
    form.enemy.choices = choices.enemy_choice_for_season(s_id)

    if request.method == "GET":
        form.episode_id.process_data(event.episode_id)
        form.event_type.process_data(event.type.value)
        form.enemy.process_data(event.enemy_id)
        form.player.process_data(event.player_id)
        form.time.process_data(event.time)
        form.comment.process_data(event.comment)
        Penalty = namedtuple("Penalty",
                             ["penalty_id", "player_id", "player", "drink"])
        for penalty in event.penalties:
            form.penalties.append_entry(
                Penalty(penalty.id, penalty.player_id, penalty.player.name,
                        penalty.drink_id))
        return render_template("event_editor.html", model=model)
    else:
        if not form.validate_on_submit():
            model["errors"] = form.errors
            return render_template("event_editor.html", model=model, form=form)
        if not event:
            event = Event()
            db.add(event)
        event.populate_from_form(form)
        # Different event types fill different fields
        func = event_type_map[event.type]
        func(event, db, form)
        db.commit()
        return redirect(f"/season/{s_id}/episode/{ep_id}")
Beispiel #17
0
def season_list():
    db = orm.new_session()
    seasons = db.query(Season).order_by(Season.code).all()
    model = {"seasons": seasons}
    return render_template("season_list.html", model=model)
Beispiel #18
0
def episode_list(season_id: int):
    db = orm.new_session()
    season = db.query(Season).filter(Season.id == season_id).first()
    model = {"season_id": season.id, "season_code": season.code}
    return render_template("episode_list.html", model=model)
Beispiel #19
0
def enemy_list():
    db = orm.new_session()
    enemies = db.query(Enemy).options(subqueryload(Enemy.season)).order_by(Enemy.name).all()
    model = {"enemies": enemies}
    return render_template("enemies.html", model=model)