Add enemy creator.
This commit is contained in:
32
app.py
32
app.py
@@ -213,12 +213,42 @@ def enemies():
|
|||||||
'columns': [
|
'columns': [
|
||||||
('id', 'ID'),
|
('id', 'ID'),
|
||||||
('name', 'Enemy Name'),
|
('name', 'Enemy Name'),
|
||||||
('vol', 'Is Boss')
|
('boss', 'Is Boss')
|
||||||
],
|
],
|
||||||
'controls': [('edit', 'Edit')]
|
'controls': [('edit', 'Edit')]
|
||||||
}
|
}
|
||||||
return render_template('enemies.html', model=model)
|
return render_template('enemies.html', model=model)
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/newenemy', methods=['GET'])
|
||||||
|
@authorize
|
||||||
|
def new_enemy(preselect_season=None):
|
||||||
|
sql, args = db.load_season()
|
||||||
|
db_seasons = db.query_db(sql, args, cls=models.Season)
|
||||||
|
db_seasons = sorted(db_seasons, key=lambda s: s.code)
|
||||||
|
|
||||||
|
view_seasons = [(s.id, f'{s.code} - {s.game}') for s in db_seasons]
|
||||||
|
view_seasons.insert(0, (None, 'No Season'))
|
||||||
|
model = {
|
||||||
|
'boss': True,
|
||||||
|
'seasons': view_seasons,
|
||||||
|
'select_season': preselect_season
|
||||||
|
}
|
||||||
|
return render_template('editenemy.html', model=model)
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/saveenemy', methods=['POST'])
|
||||||
|
@authorize
|
||||||
|
def save_enemy():
|
||||||
|
valid_enemy = models.Enemy.from_form(request.form)
|
||||||
|
sql, args = db.save_enemy(valid_enemy)
|
||||||
|
error = db.update_db(sql, args)
|
||||||
|
|
||||||
|
if 'continue' not in request.form:
|
||||||
|
return redirect('/enemies')
|
||||||
|
last_selection = valid_enemy.season_id
|
||||||
|
return new_enemy(preselect_season=last_selection)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app.run()
|
app.run()
|
||||||
|
|||||||
15
db.py
15
db.py
@@ -126,7 +126,19 @@ def load_enemies(id=None):
|
|||||||
return enemies
|
return enemies
|
||||||
|
|
||||||
|
|
||||||
def save_season_query(season):
|
def save_enemy(enemy: models.Enemy):
|
||||||
|
if not enemy.id:
|
||||||
|
sql = 'insert into enemy values (?, ?, ?, ?)'
|
||||||
|
args = (None, enemy.name, enemy.boss, enemy.season_id)
|
||||||
|
else:
|
||||||
|
sql = 'update enemy ' \
|
||||||
|
'set name=?, boss=?, season_id=? ' \
|
||||||
|
'where id==?'
|
||||||
|
args = (enemy.name, enemy.boss, enemy.season_id)
|
||||||
|
return sql, args
|
||||||
|
|
||||||
|
|
||||||
|
def save_season_query(season: models.Season):
|
||||||
if not season.id:
|
if not season.id:
|
||||||
sql = 'insert into season values (?, ?, ?, ?, ?, ?)'
|
sql = 'insert into season values (?, ?, ?, ?, ?, ?)'
|
||||||
args = (None,
|
args = (None,
|
||||||
@@ -156,3 +168,4 @@ def load_season(id=None):
|
|||||||
args = (id, )
|
args = (id, )
|
||||||
sql += ' order by season.start'
|
sql += ' order by season.start'
|
||||||
return sql, args
|
return sql, args
|
||||||
|
|
||||||
|
|||||||
13
models.py
13
models.py
@@ -52,6 +52,7 @@ class Enemy:
|
|||||||
id: int
|
id: int
|
||||||
name: str
|
name: str
|
||||||
boss: bool
|
boss: bool
|
||||||
|
season_id: int
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_form(cls, form):
|
def from_form(cls, form):
|
||||||
@@ -63,11 +64,19 @@ class Enemy:
|
|||||||
raise AttributeError(INVALID_STR.format('name'))
|
raise AttributeError(INVALID_STR.format('name'))
|
||||||
name = str(name)
|
name = str(name)
|
||||||
|
|
||||||
boss = form.get('boss', '')
|
boss = form.get('boss', False)
|
||||||
if boss not in [True, False, 'True', 'False']:
|
if boss not in [True, False, 'True', 'False']:
|
||||||
raise AttributeError(INVALID_STR.format('boss'))
|
raise AttributeError(INVALID_STR.format('boss'))
|
||||||
|
boss = bool(boss)
|
||||||
|
|
||||||
self = cls(id=id, name=name, boss=boss)
|
season = form.get('season', None)
|
||||||
|
if season not in ['None', None]:
|
||||||
|
try:
|
||||||
|
season = int(season)
|
||||||
|
except ValueError:
|
||||||
|
raise AttributeError(INVALID_STR.format('boss'))
|
||||||
|
|
||||||
|
self = cls(id=id, name=name, boss=boss, season_id=season)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
24
schema.sql
24
schema.sql
@@ -47,27 +47,11 @@ create table if not exists enemy
|
|||||||
constraint enemy_pk
|
constraint enemy_pk
|
||||||
primary key autoincrement,
|
primary key autoincrement,
|
||||||
name text not null,
|
name text not null,
|
||||||
boss integer not null
|
boss integer not null,
|
||||||
|
season_id integer,
|
||||||
|
|
||||||
|
foreign key (season_id) references season(id)
|
||||||
);
|
);
|
||||||
|
|
||||||
create unique index if not exists enemy_id_uindex
|
create unique index if not exists enemy_id_uindex
|
||||||
on enemy (id);
|
on enemy (id);
|
||||||
|
|
||||||
create table if not exists season_enemy
|
|
||||||
(
|
|
||||||
id integer not null
|
|
||||||
constraint season_enemy_pk
|
|
||||||
primary key autoincrement,
|
|
||||||
season_id integer
|
|
||||||
constraint season_enemy_season_id_fk
|
|
||||||
references season,
|
|
||||||
enemy_id integer
|
|
||||||
constraint season_enemy_enemy_id_fk
|
|
||||||
references enemy
|
|
||||||
);
|
|
||||||
|
|
||||||
create unique index if not exists season_enemy_id_uindex
|
|
||||||
on season_enemy (id);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
76
templates/editenemy.html
Normal file
76
templates/editenemy.html
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
{% extends "base.html" %}
|
||||||
|
{% set active_page = "enemies" %}
|
||||||
|
{% block title %}Enemies{% endblock %}
|
||||||
|
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
<form class="needs-validation" novalidate action="/saveenemy" method="post">
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="id">ID</label>
|
||||||
|
<input name="id"
|
||||||
|
type="text"
|
||||||
|
class="form-control"
|
||||||
|
id="id"
|
||||||
|
value="{{ model['id'] }}"
|
||||||
|
readonly>
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="season">Season</label>
|
||||||
|
<select name="season"
|
||||||
|
type="range"
|
||||||
|
class="form-control"
|
||||||
|
id="season">
|
||||||
|
{% for id, display in model.seasons %}
|
||||||
|
<option value="{{ id }}" {% if id == model.select_season %} selected {% endif %}>
|
||||||
|
{{ display }}
|
||||||
|
</option>
|
||||||
|
{% endfor %}
|
||||||
|
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="name">Enemy Name</label>
|
||||||
|
<input name="name"
|
||||||
|
type="text"
|
||||||
|
class="form-control"
|
||||||
|
id="name"
|
||||||
|
placeholder="Enter name..."
|
||||||
|
value="{{ model['game'] }}"
|
||||||
|
required>
|
||||||
|
</div>
|
||||||
|
<div class="form-group form-check">
|
||||||
|
<input name="boss"
|
||||||
|
type="checkbox"
|
||||||
|
class="form-check-input"
|
||||||
|
id="boss"
|
||||||
|
value="True"
|
||||||
|
{% if model['boss'] %}
|
||||||
|
checked
|
||||||
|
{% endif %}>
|
||||||
|
<label class="form-check-label" for="newPlayerAnon">This enemy is a boss</label>
|
||||||
|
</div>
|
||||||
|
<button type="submit" class="btn btn-primary">Submit</button>
|
||||||
|
<button type="submit" class="btn btn-primary" name="continue" value="true">Submit and Continue</button>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
// Example starter JavaScript for disabling form submissions if there are invalid fields
|
||||||
|
(function() {
|
||||||
|
'use strict';
|
||||||
|
window.addEventListener('load', function() {
|
||||||
|
// Fetch all the forms we want to apply custom Bootstrap validation styles to
|
||||||
|
let forms = document.getElementsByClassName('needs-validation');
|
||||||
|
// Loop over them and prevent submission
|
||||||
|
let validation = Array.prototype.filter.call(forms, function (form) {
|
||||||
|
form.addEventListener('submit', function (event) {
|
||||||
|
if (form.checkValidity() === false) {
|
||||||
|
event.preventDefault();
|
||||||
|
event.stopPropagation();
|
||||||
|
}
|
||||||
|
form.classList.add('was-validated');
|
||||||
|
}, false);
|
||||||
|
});
|
||||||
|
}, false);
|
||||||
|
})();
|
||||||
|
</script>
|
||||||
|
{% endblock %}
|
||||||
Reference in New Issue
Block a user