feat: improve mastermin usability

This commit is contained in:
s3lph 2024-11-17 23:09:45 +01:00
parent 4b5650555b
commit 6821f1e09c
Signed by: s3lph
GPG key ID: 0AA29A52FB33CFB5
2 changed files with 46 additions and 22 deletions

View file

@ -5,7 +5,10 @@
<meta http-equiv="refresh" content="3" /> <meta http-equiv="refresh" content="3" />
{% endif %} {% endif %}
<style> <style>
td { table {
border-collapse: collapse;
}
td.board {
background: #444444; background: #444444;
padding: 7pt; padding: 7pt;
} }
@ -45,7 +48,8 @@
<table border="0"> <table border="0">
{% if done %} {% if done %}
<tr> <tr>
<td>{{ attempt(solution) }}</td> <td>Solution</td>
<td class="board">{{ attempt(solution) }}</td>
</tr> </tr>
{% endif %} {% endif %}
<tr> <tr>
@ -60,11 +64,11 @@
{% for i in range(turn) %} {% for i in range(turn) %}
<tr> <tr>
<td>{{ i+1 }}</td> <td>{{ i+1 }}</td>
<td>{% if attempts | length >= i+1 %}{{ attempt(attempts[i]) }}{% endif %}</td> <td class="board">{% if attempts | length >= i+1 %}{{ attempt(attempts[i]) }}{% endif %}</td>
<td>{% if responses[player] | length >= i+1 %}{{ response(responses[player][i], false) }}{% endif %}</td> <td class="board">{% if responses[player] | length >= i+1 %}{{ response(responses[player][i], false) }}{% endif %}</td>
{% for p in players.values() %} {% for p in players.values() %}
{% if p.uuid == player %}{% continue %}{% endif %} {% if p.uuid == player %}{% continue %}{% endif %}
<td>{% if responses[p.uuid] | length >= i+1 %}{{ response(responses[p.uuid][i], true) }}{% endif %}</td> <td class="board">{% if responses[p.uuid] | length >= i+1 %}{{ response(responses[p.uuid][i], true) }}{% endif %}</td>
{% endfor %} {% endfor %}
</tr> </tr>
{% endfor %} {% endfor %}
@ -74,7 +78,9 @@
{% if winners | length == 0 %} {% if winners | length == 0 %}
Nobody won Nobody won
{% else %} {% else %}
{{ winners | join(', ') }} won! {% for w in winners %}
{{ players[w].name }}{% if not loop.last %}, {% endif %}
{% endfor %} won!
{% endif %} {% endif %}
Game Over! <a href="{{ baseurl }}/{{ player }}">Return to lobby</a> Game Over! <a href="{{ baseurl }}/{{ player }}">Return to lobby</a>
{% elif player not in played %} {% elif player not in played %}
@ -91,11 +97,11 @@
</form> </form>
<div id="colorlist"> <div id="colorlist">
{% for c in colors %} {% for c in colors %}
<span draggable="true" clickable="true" onclick="click(event, '{{ c.value.name }}', '{{ c.value.html }}')" ondragstart="drag(event, '{{ c.value.name }}', '{{ c.value.html }}')" style="color: {{ c.value.html }};">&#11044;</span> <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 %} {% endfor %}
</div> </div>
<script> <script>
let labels = document.getElementsByTagName('label'); let labels = Array.from(document.getElementsByTagName('label'));
for (let select of document.getElementsByTagName('select')) { for (let select of document.getElementsByTagName('select')) {
select.style.display = 'none'; select.style.display = 'none';
} }
@ -121,12 +127,20 @@
} }
function click(e, name, color) { function click(e, name, color) {
console.log(name);
labels[i].style.color = color; labels[i].style.color = color;
document.getElementById(labels[i].getAttribute('for')).value = name; document.getElementById(labels[i].getAttribute('for')).value = name;
labels[i].classList.remove('clickselect'); labels[i].classList.remove('clickselect');
i = (i+1) % 4; i = (i+1) % 4;
labels[i].classList.add('clickselect'); 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> </script>
{% else %} {% else %}
Please wait for the other players to finish their turn! Please wait for the other players to finish their turn!

View file

@ -15,15 +15,15 @@ class _Color:
class Color(enum.Enum): class Color(enum.Enum):
NONE = _Color('none', '#000000', '\033[30m') NONE = _Color('None', '#000000', '\033[30m')
RED = _Color('red', '#ff0000', '\033[31m') RED = _Color('Red', '#ff0000', '\033[31m')
GREEN = _Color('green', '#00aa00', '\033[32m') GREEN = _Color('Green', '#00aa00', '\033[32m')
BLUE = _Color('blue', '#0000ff', '\033[34m') BLUE = _Color('Blue', '#0000ff', '\033[34m')
YELLOW = _Color('yellow', '#ffff00', '\093[33m') YELLOW = _Color('Yellow', '#ffff00', '\093[33m')
WHITE = _Color('white', '#dddddd', '\033[97m') WHITE = _Color('White', '#dddddd', '\033[97m')
GREY = _Color('grey', '#888888', '\033[90m') GREY = _Color('Grey', '#888888', '\033[90m')
PINK = _Color('pink', '#ff0088', '\033[95m') PINK = _Color('Pink', '#ff0088', '\033[95m')
ORANGE = _Color('orange', '#ff8800', '\033[95m') ORANGE = _Color('Orange', '#ff8800', '\033[95m')
class ResponseColor(enum.Enum): class ResponseColor(enum.Enum):
@ -53,6 +53,7 @@ class Mastermind(Puzzle):
self._turn = 0 self._turn = 0
self._max_turn = 12 self._max_turn = 12
self._done = False self._done = False
self._allow_empty = False
self._urlbase = '/' self._urlbase = '/'
def add_player(self, uuid: UUID) -> None: def add_player(self, uuid: UUID) -> None:
@ -71,12 +72,13 @@ class Mastermind(Puzzle):
def begin(self, options, urlbase): def begin(self, options, urlbase):
self._urlbase = urlbase self._urlbase = urlbase
colors = list(Color) colors = list(Color)
if 'empty' not in options.getall('rules'): self._allow_empty = 'empty' in options.getall('rules')
if not self._allow_empty:
colors = colors[1:] colors = colors[1:]
if 'repeat' in options.getall('rules'): if 'repeat' in options.getall('rules'):
self._solution = random.choices(colors, 4) self._solution = random.choices(colors, k=4)
else: else:
self._solution = random.sample(colors, 4) self._solution = random.sample(colors, k=4)
self._turn = 1 self._turn = 1
def next_turn(self): def next_turn(self):
@ -89,11 +91,16 @@ class Mastermind(Puzzle):
# get counted as correct a second time. # get counted as correct a second time.
solution = [x for x in self._solution] solution = [x for x in self._solution]
response = [] response = []
checked = [False] * 4
for i, a in enumerate(attempt): for i, a in enumerate(attempt):
if a == self._solution[i]: if a == self._solution[i]:
response.append(ResponseColor.RED) response.append(ResponseColor.RED)
solution.remove(a) solution.remove(a)
elif a in solution: checked[i] = True
for i, a in enumerate(attempt):
if checked[i]:
continue
if a in solution:
response.append(ResponseColor.WHITE) response.append(ResponseColor.WHITE)
solution.remove(a) solution.remove(a)
else: else:
@ -116,7 +123,10 @@ class Mastermind(Puzzle):
a2 = Color[action.get('attempt2', 'NONE').upper()] a2 = Color[action.get('attempt2', 'NONE').upper()]
a3 = Color[action.get('attempt3', 'NONE').upper()] a3 = Color[action.get('attempt3', 'NONE').upper()]
a4 = Color[action.get('attempt4', 'NONE').upper()] a4 = Color[action.get('attempt4', 'NONE').upper()]
self._attempts[puuid].append([a1, a2, a3, a4]) attempt = [a1, a2, a3, a4]
if not self._allow_empty and Color.NONE in attempt:
return
self._attempts[puuid].append(attempt)
self._played.add(puuid) self._played.add(puuid)
except BaseException as e: except BaseException as e:
print(e) print(e)