Update coffee route and template to include score tracking
This commit is contained in:
parent
6b4f61879b
commit
431dda453b
2 changed files with 17 additions and 9 deletions
|
|
@ -33,9 +33,9 @@ def generate_solvable_matrix(level, seed_clicks=None):
|
||||||
@main.route("/12648243")
|
@main.route("/12648243")
|
||||||
def coffee():
|
def coffee():
|
||||||
level = request.args.get('level', 0, int)
|
level = request.args.get('level', 0, int)
|
||||||
|
score = request.args.get('score', 0, int)
|
||||||
matrix, clicked = generate_solvable_matrix(level)
|
matrix, clicked = generate_solvable_matrix(level)
|
||||||
best_score = len([x for x in clicked if not (len(clicked) % 2)])
|
return render_template("coffee.html", matrix=matrix, level=level, clicked=clicked, score=score)
|
||||||
return render_template("coffee.html", matrix=matrix, level=level, clicked=clicked, best_score=best_score)
|
|
||||||
|
|
||||||
@main.route("/playground")
|
@main.route("/playground")
|
||||||
def playground():
|
def playground():
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,11 @@
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block precontent %}
|
{% block precontent %}
|
||||||
<div id="best_score"></div>
|
{{ toolbars.render_toolbar(
|
||||||
|
id = 'score',
|
||||||
|
left = '<span id="best_score">Loading...</span>' | safe,
|
||||||
|
right= ('<span id="current_score">Score: ' + score|string + '</span>') | safe
|
||||||
|
) }}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
|
|
@ -27,6 +31,7 @@
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block script %}
|
{% block script %}
|
||||||
|
var score = {{ score }};
|
||||||
const gridSize = {{ level + 3 }};
|
const gridSize = {{ level + 3 }};
|
||||||
var clickOrder = {};
|
var clickOrder = {};
|
||||||
var clickCounter = 0;
|
var clickCounter = 0;
|
||||||
|
|
@ -43,9 +48,7 @@
|
||||||
.reduce((n, arr) => n + (arr.length & 1), 0);
|
.reduce((n, arr) => n + (arr.length & 1), 0);
|
||||||
|
|
||||||
|
|
||||||
document.getElementById('best_score').textContent = `Best Score: ${bestScore}`;
|
document.getElementById('best_score').textContent = `Perfect Clicks: ${bestScore}`;
|
||||||
console.log(clickOrder, bestScore);
|
|
||||||
|
|
||||||
|
|
||||||
Object.entries(clickOrder).forEach(([key, value]) => {
|
Object.entries(clickOrder).forEach(([key, value]) => {
|
||||||
const light = document.querySelector(`#light-${key}`);
|
const light = document.querySelector(`#light-${key}`);
|
||||||
|
|
@ -71,8 +74,13 @@
|
||||||
light.addEventListener('click', function() {
|
light.addEventListener('click', function() {
|
||||||
const [x, y] = this.id.split('-').slice(1).map(Number);
|
const [x, y] = this.id.split('-').slice(1).map(Number);
|
||||||
const checkbox = document.querySelector(`#checkbox-${x}-${y}`);
|
const checkbox = document.querySelector(`#checkbox-${x}-${y}`);
|
||||||
|
++score;
|
||||||
|
document.getElementById('current_score').textContent = `Score: ${score}`;
|
||||||
|
|
||||||
checkbox.click();
|
// Toggle manually
|
||||||
|
checkbox.checked = !checkbox.checked;
|
||||||
|
// Fire a non-bubbling change so it won't climb back to .light
|
||||||
|
checkbox.dispatchEvent(new Event('change'));
|
||||||
updateLights();
|
updateLights();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
@ -99,10 +107,10 @@
|
||||||
const allUnchecked = Array.from(document.querySelectorAll('.form-check-input')).every(cb => !cb.checked);
|
const allUnchecked = Array.from(document.querySelectorAll('.form-check-input')).every(cb => !cb.checked);
|
||||||
if (allChecked && !window.__alreadyNavigated && {{ level }} < 51) {
|
if (allChecked && !window.__alreadyNavigated && {{ level }} < 51) {
|
||||||
window.__alreadyNavigated = true;
|
window.__alreadyNavigated = true;
|
||||||
location.href = "{{ url_for('main.coffee', level=level + 1) }}";
|
location.href = `{{ url_for('main.coffee', level=level + 1) | safe }}&score=${score - bestScore}`;
|
||||||
} else if (allUnchecked && !window.__alreadyNavigated && {{ level }} > -2) {
|
} else if (allUnchecked && !window.__alreadyNavigated && {{ level }} > -2) {
|
||||||
window.__alreadyNavigated = true;
|
window.__alreadyNavigated = true;
|
||||||
location.href = "{{ url_for('main.coffee', level=level - 1) }}";
|
location.href = `{{ url_for('main.coffee', level=level - 1) | safe }}&score=${score + bestScore}`;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue