Skip to content

Commit

Permalink
feat: Support multiple Parade Miis in the underground
Browse files Browse the repository at this point in the history
  • Loading branch information
noahpistilli committed Jul 5, 2024
1 parent 677b291 commit 0986a55
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 18 deletions.
117 changes: 115 additions & 2 deletions templates/room_action.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,13 @@
</div>

{{ form.has_mascot.label }}: {{ form.has_mascot(class_="block") }}
{{ form.mii.label(class_="label") }} {{ form.mii(class_="input block") }}
{{ form.mii_msg.label(class_="label") }} {{ form.mii_msg(class="textarea") }}

{{ form.mii }}
<br>
<button type="button" class="button is-info" id="addNewField">Add Mii</button>
<button type="button" class="button is-danger" id="removeField">Remove Mii</button>
<br>
<br>
{{ form.intro_msg.label(class_="label") }} {{ form.intro_msg(class_="textarea") }}
{{ form.news.label(class_="label") }} {{ form.news(class_="input block") }}
{{ form.contact.label(class_="label") }} {{ form.contact(class_="input block") }}
Expand All @@ -63,4 +68,112 @@
</p>
<p>{{ form.submit(class_="button is-success block") }}</p>
</form>

<script>
var fieldNum = 0;
function createMiiPair() {
// Mii ID
var newLabel = document.createElement("label")
newLabel.className = "label"
newLabel.htmlFor = "mii-" + fieldNum;
newLabel.textContent = "Mii ID"

var newTextArea = document.createElement("input");
newTextArea.className = "input";
newTextArea.id = "mii-" + fieldNum;
newTextArea.name = "mii-" + fieldNum;
fieldNum++;

// Mii Message
var newLabelName = document.createElement("label")
newLabelName.className = "label"
newLabelName.htmlFor = "mii-" + fieldNum;
newLabelName.textContent = "Mii Message"

var newTextAreaName = document.createElement("textarea");
newTextAreaName.className = "textarea";
newTextAreaName.id = "mii-" + fieldNum;
newTextAreaName.name = "mii-" + fieldNum;
fieldNum++;

document.getElementById("mii").appendChild(newLabel);
document.getElementById("mii").appendChild(newTextArea);
document.getElementById("mii").appendChild(document.createElement("br"));

document.getElementById("mii").appendChild(newLabelName);
document.getElementById("mii").appendChild(newTextAreaName);
document.getElementById("mii").appendChild(document.createElement("br"));

doDisableButtons()
}

function doDisableButtons() {
if (fieldNum === 2) {
document.getElementById("removeField").disabled = true;
} else if (fieldNum === 6) {
document.getElementById("addNewField").disabled = true;
} else {
document.getElementById("addNewField").disabled = false;
document.getElementById("removeField").disabled = false;
}
}

document.getElementById("addNewField").addEventListener("click", createMiiPair);
document.getElementById("removeField").addEventListener("click", function(){
// Remove last field pair
for (var i = 0; i < 6; i++) {
document.getElementById("mii").removeChild(document.getElementById("mii").lastChild);
}

// Finally decrement the fieldNum
fieldNum -= 2;
console.log(fieldNum);

doDisableButtons();
})

{% if miis|length == 0 %}
document.addEventListener("DOMContentLoaded", createMiiPair);
{% else %}
document.addEventListener("DOMContentLoaded", function () {
{% for mii in miis %}
// Mii ID
var newLabel = document.createElement("label")
newLabel.className = "label"
newLabel.htmlFor = "mii-" + fieldNum;
newLabel.textContent = "Mii ID"

var newTextArea = document.createElement("input");
newTextArea.className = "input";
newTextArea.id = "mii-" + fieldNum;
newTextArea.name = "mii-" + fieldNum;
newTextArea.value = "{{ mii.mii_id }}";
fieldNum++;

// Mii Message
var newLabelName = document.createElement("label")
newLabelName.className = "label"
newLabelName.htmlFor = "mii-" + fieldNum;
newLabelName.textContent = "Mii Message"

var newTextAreaName = document.createElement("textarea");
newTextAreaName.className = "textarea";
newTextAreaName.id = "mii-" + fieldNum;
newTextAreaName.name = "mii-" + fieldNum;
newTextAreaName.value = `{{ mii.mii_msg }}`
fieldNum++;

document.getElementById("mii").appendChild(newLabel);
document.getElementById("mii").appendChild(newTextArea);
document.getElementById("mii").appendChild(document.createElement("br"));

document.getElementById("mii").appendChild(newLabelName);
document.getElementById("mii").appendChild(newTextAreaName);
document.getElementById("mii").appendChild(document.createElement("br"));

doDisableButtons()
{% endfor %}
});
{% endif %}
</script>
{% endblock %}
3 changes: 1 addition & 2 deletions theunderground/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class CategoryForm(FlaskForm):


class RoomForm(FlaskForm):
mii = StringField("Mii ID")
mii = FieldList(StringField())
bgm = SelectField(
"Background Music",
choices=RoomBGMTypes.choices(),
Expand All @@ -101,7 +101,6 @@ class RoomForm(FlaskForm):
category_logo = FileField("Category Logo")
has_mascot = BooleanField("Mascot Enabled")
intro_msg = TextAreaField("Intro Message", validators=[DataRequired()])
mii_msg = TextAreaField("Mii Message", validators=[DataRequired()])
news = StringField("Company", validators=[DataRequired(), Length(max=41)])
# PostgreSQL treats an empty string ('') separately from NULL (None in Python).
# https://stackoverflow.com/a/21853689
Expand Down
63 changes: 49 additions & 14 deletions theunderground/rooms.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def list_room():
def edit_room(room_id):
form = RoomForm()

mii = RoomMiis.query.filter_by(room_id=room_id).first()
if not mii:
miis = RoomMiis.query.filter_by(room_id=room_id).all()
if not miis:
return exceptions.NotFound()

room = Rooms.query.filter_by(room_id=room_id).first()
Expand All @@ -47,9 +47,35 @@ def edit_room(room_id):
if form.category_logo.data:
NormalCategoryAsset(room.room_id + 30000).encode(form.category_logo)

mii.mii_id = form.mii.data
mii.mii_msg = form.mii_msg.data
db.session.add(mii)
received_length = len(form.mii.data) / 2
original_length = RoomMiis.query.filter_by(room_id=room_id).count()
if received_length < original_length:
# If the edited credits is less than what is in the database, we must delete the removed.
RoomMiis.query.filter_by(room_id=room_id).where(
RoomMiis.seq > received_length
).delete()

# We can handle any edits now.
seq = 1
for i in range(0, int(received_length) * 2, 2):
# Query the row
data = RoomMiis.query.filter_by(room_id=room_id).filter_by(seq=seq).first()
if not data:
# Brand new row.
db_mii = RoomMiis(
room_id=room_id,
mii_id=form.mii.data[i],
mii_msg=form.mii.data[i + 1],
seq=seq,
)

db.session.add(db_mii)
else:
data.mii_id = form.mii.data[i]
data.mii_msg = form.mii.data[i + 1]

seq += 1

db.session.commit()

room.bgm = form.bgm.data
Expand All @@ -64,8 +90,6 @@ def edit_room(room_id):
else:
# Populate our form.
# This is long and unwieldy...
form.mii.data = mii.mii_id
form.mii_msg.data = mii.mii_msg
form.bgm.data = room.bgm
form.has_mascot.data = room.mascot
form.intro_msg.data = room.intro_msg
Expand All @@ -75,7 +99,12 @@ def edit_room(room_id):
rooms = Rooms.query.order_by(Rooms.room_id.asc()).all()

return render_template(
"room_action.html", form=form, room_id=room_id, action="Edit", rooms=rooms
"room_action.html",
form=form,
room_id=room_id,
action="Edit",
rooms=rooms,
miis=miis,
)


Expand All @@ -101,12 +130,18 @@ def create_room():
db.session.commit()

# Next, add our Mii.
mii = RoomMiis(
room_id=room.room_id,
mii_id=form.mii.data,
mii_msg=form.mii_msg.data,
)
db.session.add(mii)
seq = 1
for i in range(0, len(form.mii), 2):
# Brand new row.
db_mii = RoomMiis(
room_id=room.room_id,
mii_id=form.mii.data[i],
mii_msg=form.mii.data[i + 1],
seq=seq,
)
db.session.add(db_mii)
seq += 1

db.session.commit()

# Finally, handle room data - our room logo, and parade banner.
Expand Down

0 comments on commit 0986a55

Please sign in to comment.