webgames/templates/mastermind/mastermind.html.j2

149 lines
4.3 KiB
Django/Jinja

<!DOCTYPE html>
<html>
<head>
{% if not done and player in played %}
<meta http-equiv="refresh" content="3" />
{% endif %}
<style>
table {
border-collapse: collapse;
}
td.board {
background: #444444;
padding: 7pt;
}
.attempt {
font-size: 14pt;
}
.response {
font-size: 7pt;
}
.other {
opacity: 0.7;
}
.clickselect {
text-shadow: 0 0 5pt #4030ff;
}
</style>
</head>
<body>
{% macro attempt(a) %}
<div class="attempt">
{% for i in range(4) %}
<span style="color: {{ a[i].value.html }};">&#11044;</span>
{% endfor %}
</div>
{% endmacro %}
{% macro response(r, other) %}
<div class="response {% if other %}other{% endif %}">
{% for i in range(4) %}
<span style="color: {{ r[i].value.html }};">&#11044;</span>
{#{% if i == 1 %}<br>{% endif %}#}
{% endfor %}
</div>
{% endmacro %}
<table border="0">
{% if done %}
<tr>
<td>Solution</td>
<td class="board">{{ attempt(solution) }}</td>
</tr>
{% endif %}
<tr>
<th>Turn</th>
<th>Your guess</th>
<th>{{ players[player].name }}</th>
{% for p in players.values() %}
{% if p.uuid == player %}{% continue %}{% endif %}
<th>{{ p.name }}</th>
{% endfor %}
</tr>
{% for i in range(turn) %}
<tr>
<td>{{ i+1 }}</td>
<td class="board">{% if attempts | length >= i+1 %}{{ attempt(attempts[i]) }}{% endif %}</td>
<td class="board">{% if responses[player] | length >= i+1 %}{{ response(responses[player][i], false) }}{% endif %}</td>
{% for p in players.values() %}
{% if p.uuid == player %}{% continue %}{% endif %}
<td class="board">{% if responses[p.uuid] | length >= i+1 %}{{ response(responses[p.uuid][i], true) }}{% endif %}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
{% if done %}
{% if winners | length == 0 %}
Nobody won
{% else %}
{% for w in winners %}
{{ players[w].name }}{% if not loop.last %}, {% endif %}
{% endfor %} won!
{% endif %}
Game Over! <a href="{{ baseurl }}/{{ player }}">Return to lobby</a>
{% elif player not in played %}
<form name="controls" method="POST">
{% for i in range(4) %}
<label ondragover="allowDrop(event)" ondrop="drop(event)" for="mastermind-controls-attempt{{ i+1 }}">&#11044;</label>
<select name="attempt{{ i+1 }}" id="mastermind-controls-attempt{{ i+1 }}">
{% for c in colors %}
<option name="{{ c.name }}">{{ c.value.name }}</option>
{% endfor %}
</select>
{% endfor %}
<input type="submit" value="Submit" name="action" />
</form>
<div id="colorlist">
{% for c in colors %}
<span class="clickdrag" draggable="true" clickable="true" data-name="{{ c.value.name }}" data-color="{{ c.value.html }}" ondragstart="drag(event, '{{ c.value.name }}', '{{ c.value.html }}')" style="color: {{ c.value.html }};">&#11044;</span>
{% endfor %}
</div>
<script>
let labels = Array.from(document.getElementsByTagName('label'));
for (let select of document.getElementsByTagName('select')) {
select.style.display = 'none';
}
labels[0].classList.add('clickselect');
i = 0;
function allowDrop(e) {
e.preventDefault();
}
function drag(e, name, color) {
e.dataTransfer.setData('name', name);
e.dataTransfer.setData('color', color);
}
function drop(e) {
e.preventDefault();
console.log(e.dataTransfer.getData('color'));
console.log(e.dataTransfer.getData('name'));
e.target.style.color = e.dataTransfer.getData('color');
document.getElementById(e.target.getAttribute('for')).value = e.dataTransfer.getData('name');
console.log(e.target);
}
function click(e, name, color) {
console.log(name);
labels[i].style.color = color;
document.getElementById(labels[i].getAttribute('for')).value = name;
labels[i].classList.remove('clickselect');
i = (i+1) % 4;
labels[i].classList.add('clickselect');
}
document.onclick= e => {
if (e.target.classList.contains('clickdrag')) {
e.preventDefault();
console.log('hi');
click(e, e.target.getAttribute('data-name'), e.target.getAttribute('data-color'));
}
};
</script>
{% else %}
Please wait for the other players to finish their turn!
{% endif %}
</body>
</html>