From 79d1bbbc464f458566750ebc5af966a70455e268 Mon Sep 17 00:00:00 2001 From: luxick Date: Fri, 18 Oct 2019 22:00:52 +0200 Subject: [PATCH] Bug fixes and improved handling of event editor. --- estusshots/choices.py | 9 +++- estusshots/estus-shots.ts | 17 ------- estusshots/static/js/estus-shots.js | 38 ++++++++++++++ estusshots/static/js/estus-shots.js.map | 2 +- estusshots/static/js/estus-shots.ts | 68 +++++++++++++++++++++++++ estusshots/templates/event_editor.html | 23 ++++++--- estusshots/views/enemies.py | 10 ++-- estusshots/views/episodes.py | 2 +- estusshots/views/events.py | 1 + estusshots/views/players.py | 15 +++--- static/js/estus-shots.js | 37 ++++++++++++++ tsconfig.json | 5 +- 12 files changed, 183 insertions(+), 44 deletions(-) delete mode 100644 estusshots/estus-shots.ts create mode 100644 estusshots/static/js/estus-shots.ts create mode 100644 static/js/estus-shots.js diff --git a/estusshots/choices.py b/estusshots/choices.py index 5fe954b..6b99f6b 100644 --- a/estusshots/choices.py +++ b/estusshots/choices.py @@ -1,4 +1,4 @@ -from estusshots.orm import new_session, EventType, Season, Player, Drink, Enemy +from estusshots.orm import new_session, EventType, Season, Player, Drink, Enemy, Episode def event_choices(): @@ -26,6 +26,13 @@ def player_choice(): return [(p.id, p.name) for p in players] +def player_choice_for_episode(episode: Episode): + """ + Create a list of ids and names for players of this episode + """ + return [(p.id, p.name) for p in episode.players] + + def drink_choice(): """ Query database for a list of all available drinks to select from diff --git a/estusshots/estus-shots.ts b/estusshots/estus-shots.ts deleted file mode 100644 index 55d3840..0000000 --- a/estusshots/estus-shots.ts +++ /dev/null @@ -1,17 +0,0 @@ -class EditorModule{ - - setCurrentTime = (elemId: string) => { - const elem = document.getElementById(elemId) as HTMLInputElement; - if (!elem) return; - elem.value = this.currentTimeHHMM() - }; - - currentTimeHHMM = () => { - const d = new Date(); - const hours = (d.getHours()<10 ? '0' : '') + d.getHours(); - const minutes = (d.getMinutes()<10 ? '0' : '') + d.getMinutes(); - return `${hours}:${minutes}`; - }; -} - -const editorModule = new EditorModule(); diff --git a/estusshots/static/js/estus-shots.js b/estusshots/static/js/estus-shots.js index 078b191..595db45 100644 --- a/estusshots/static/js/estus-shots.js +++ b/estusshots/static/js/estus-shots.js @@ -17,4 +17,42 @@ var EditorModule = (function () { return EditorModule; }()); var editorModule = new EditorModule(); +HTMLElement.prototype.addClass = function (cssClass) { + if (!this.classList.contains(cssClass)) { + this.classList.add(cssClass); + } +}; +HTMLElement.prototype.removeClass = function (cssClass) { + if (this.classList.contains(cssClass)) { + console.log(this.classList); + this.classList.remove(cssClass); + } +}; +var EventEditor; +(function (EventEditor) { + EventEditor.updateControls = function () { + var typeSelect = document.getElementById("event_type"); + var selectedType = typeSelect.options[typeSelect.selectedIndex].value; + var penalties = document.querySelector(".penalty-container"); + var player = document.getElementById("player_row"); + var enemy = document.getElementById("enemy_row"); + switch (selectedType) { + case "0": + penalties.addClass("d-none"); + player.addClass("d-none"); + enemy.addClass("d-none"); + break; + case "1": + penalties.removeClass("d-none"); + player.removeClass("d-none"); + enemy.removeClass("d-none"); + break; + case "2": + penalties.addClass("d-none"); + player.removeClass("d-none"); + enemy.removeClass("d-none"); + break; + } + }; +})(EventEditor || (EventEditor = {})); //# sourceMappingURL=estus-shots.js.map \ No newline at end of file diff --git a/estusshots/static/js/estus-shots.js.map b/estusshots/static/js/estus-shots.js.map index c273638..b047306 100644 --- a/estusshots/static/js/estus-shots.js.map +++ b/estusshots/static/js/estus-shots.js.map @@ -1 +1 @@ -{"version":3,"file":"estus-shots.js","sourceRoot":"","sources":["../../estus-shots.ts"],"names":[],"mappings":"AAAA;IAAA;QAAA,iBAcC;QAZC,mBAAc,GAAG,UAAC,MAAc;YAC9B,IAAM,IAAI,GAAG,QAAQ,CAAC,cAAc,CAAC,MAAM,CAAqB,CAAC;YACjE,IAAI,CAAC,IAAI;gBAAE,OAAO;YAClB,IAAI,CAAC,KAAK,GAAG,KAAI,CAAC,eAAe,EAAE,CAAA;QACrC,CAAC,CAAC;QAEF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K,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;gBACzB,MAAM;YAEN,KAAK,GAAG;gBACN,SAAS,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC;gBAChC,MAAM,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC;gBAC7B,KAAK,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC;gBAC5B,MAAM;YAER,KAAK,GAAG;gBACN,SAAS,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;gBAC7B,MAAM,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC;gBAC7B,KAAK,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC;gBAC5B,MAAM;SACX;IACH,CAAC,CAAA;AAEH,CAAC,EAhCS,WAAW,KAAX,WAAW,QAgCpB"} \ No newline at end of file diff --git a/estusshots/static/js/estus-shots.ts b/estusshots/static/js/estus-shots.ts new file mode 100644 index 0000000..e3b63b1 --- /dev/null +++ b/estusshots/static/js/estus-shots.ts @@ -0,0 +1,68 @@ +class EditorModule{ + + setCurrentTime = (elemId: string) => { + const elem = document.getElementById(elemId) as HTMLInputElement; + if (!elem) return; + elem.value = this.currentTimeHHMM() + }; + + currentTimeHHMM = () => { + const d = new Date(); + const hours = (d.getHours()<10 ? '0' : '') + d.getHours(); + const minutes = (d.getMinutes()<10 ? '0' : '') + d.getMinutes(); + return `${hours}:${minutes}`; + }; +} + +const editorModule = new EditorModule(); + +interface HTMLElement { + addClass(cssClass: string): void; + removeClass(cssClass: string): void; +} + +HTMLElement.prototype.addClass = function(cssClass: string): void { + if (!this.classList.contains(cssClass)){ + this.classList.add(cssClass); + } +}; +HTMLElement.prototype.removeClass = function(cssClass: string): void { + if (this.classList.contains(cssClass)){ + console.log(this.classList); + this.classList.remove(cssClass); + } +}; + +namespace EventEditor { + + export let updateControls = () => { + const typeSelect = document.getElementById("event_type") as HTMLSelectElement; + const selectedType = typeSelect.options[typeSelect.selectedIndex].value; + + const penalties = document.querySelector(".penalty-container") as HTMLDivElement; + const player = document.getElementById("player_row") as HTMLDivElement; + const enemy = document.getElementById("enemy_row") as HTMLDivElement; + + switch (selectedType) { + // Pause + case "0": + penalties.addClass("d-none"); + player.addClass("d-none"); + enemy.addClass("d-none"); + break; + // Death + case "1": + penalties.removeClass("d-none"); + player.removeClass("d-none"); + enemy.removeClass("d-none"); + break; + // Victory + case "2": + penalties.addClass("d-none"); + player.removeClass("d-none"); + enemy.removeClass("d-none"); + break; + } + } + +} diff --git a/estusshots/templates/event_editor.html b/estusshots/templates/event_editor.html index 82d4c0a..98ce5c1 100644 --- a/estusshots/templates/event_editor.html +++ b/estusshots/templates/event_editor.html @@ -4,6 +4,12 @@ {% block title %}{{ model.page_title }} {{ super() }}{% endblock %} {% block page %} + +

{{ model.form_title }}

@@ -27,7 +33,7 @@ {{ form.event_type.label(class_="form-control-label") }}
- {{ form.event_type(class_="form-control") }} + {{ form.event_type(class_="form-control", onchange="EventEditor.updateControls()") }}
@@ -47,7 +53,7 @@ -
+
{{ form.player.label(class_="form-control-label") }}
@@ -56,7 +62,7 @@
-
+
{{ form.enemy.label(class_="form-control-label") }}
@@ -75,14 +81,19 @@
+

Penalties

{% for penalty in form.penalties %} {{ penalty.hidden_tag() }} -
+
{{ penalty.penalty_id }} {{ penalty.player_id }} {{ penalty.player }} - - {{ penalty.drink }} +
+ +
+
+ {{ penalty.drink(class_="form-control") }} +
{% endfor %}
diff --git a/estusshots/views/enemies.py b/estusshots/views/enemies.py index 60cd38e..530bd05 100644 --- a/estusshots/views/enemies.py +++ b/estusshots/views/enemies.py @@ -40,24 +40,20 @@ def enemy_edit(enemy_id: int): 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": - db = orm.new_session() - enemy = db.query(Enemy).filter(Enemy.id == enemy_id).first() - - form = forms.EnemyForm() 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(): - db = orm.new_session() - enemy = db.query(Enemy).filter(Enemy.id == enemy_id).first() if not enemy: enemy = Enemy() db.add(enemy) diff --git a/estusshots/views/episodes.py b/estusshots/views/episodes.py index c05b008..0dc044c 100644 --- a/estusshots/views/episodes.py +++ b/estusshots/views/episodes.py @@ -86,4 +86,4 @@ def episode_edit(season_id: int, episode_id: int): 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)) + return redirect(url_for("episode_detail", season_id=season.id, episode_id=episode.id)) diff --git a/estusshots/views/events.py b/estusshots/views/events.py index 52fb283..5634e8d 100644 --- a/estusshots/views/events.py +++ b/estusshots/views/events.py @@ -60,6 +60,7 @@ def event_new(s_id: int, ep_id: int): 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"]) diff --git a/estusshots/views/players.py b/estusshots/views/players.py index f4f3233..e6a6b0d 100644 --- a/estusshots/views/players.py +++ b/estusshots/views/players.py @@ -26,27 +26,26 @@ def player_edit(player_id: int): 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": - db = orm.new_session() - player = db.query(Player).filter(Player.id == player_id).first() - - form = forms.PlayerForm() 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(): - db = orm.new_session() - player = db.query(Player).filter(Player.id == player_id).first() + if not player: + player = Player() + db.add(player) player.populate_from_form(form) db.commit() return redirect("/player") diff --git a/static/js/estus-shots.js b/static/js/estus-shots.js new file mode 100644 index 0000000..9186faf --- /dev/null +++ b/static/js/estus-shots.js @@ -0,0 +1,37 @@ +var EditorModule = /** @class */ (function () { + function EditorModule() { + var _this = this; + this.setCurrentTime = function (elemId) { + var elem = document.getElementById(elemId); + if (!elem) + return; + elem.value = _this.currentTimeHHMM(); + }; + this.currentTimeHHMM = function () { + var d = new Date(); + var hours = (d.getHours() < 10 ? '0' : '') + d.getHours(); + var minutes = (d.getMinutes() < 10 ? '0' : '') + d.getMinutes(); + return hours + ":" + minutes; + }; + } + return EditorModule; +}()); +var editorModule = new EditorModule(); +var EventEditor; +(function (EventEditor) { + EventEditor.updateControls = function () { + var typeSelect = document.getElementById("event_type"); + var selectedType = typeSelect.options[typeSelect.selectedIndex].value; + switch (selectedType) { + case "0": + console.log("Pause"); + break; + case "1": + break; + case "2": + break; + default: + console.log("Unknown event type: " + selectedType); + } + }; +})(EventEditor || (EventEditor = {})); diff --git a/tsconfig.json b/tsconfig.json index 7277d56..c51a877 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,10 +4,9 @@ "noImplicitAny": true, "removeComments": true, "preserveConstEnums": true, - "sourceMap": true, - "outDir": "./estusshots/static/js" + "sourceMap": true }, "include": [ "**/*.ts" - ], + ] }