from collections import namedtuple
from operator import attrgetter
from django.views.generic import TemplateView
PLAYERS_COUNT = 10
Fight = namedtuple('Fight', ['player_no', 'opponent', 'points'])
class Player:
name = ''
fights = None
result = 0.
def __init__(self, name='', fights=None):
self.name = name
self.fights = fights or []
self.result = sum(filter(None, self.fights))
class RankingTableView(TemplateView):
template_name = 'ranking_table.htm'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
fights = [
Fight(1, 4, 1.),
Fight(1, 8, 1.),
Fight(1, 2, 1.),
Fight(1, 6, 1.),
Fight(1, 3, 1.),
Fight(2, 7, 1.),
Fight(2, 3, 1.),
Fight(2, 1, 0.),
Fight(2, 9, 1.),
Fight(2, 5, 1.),
Fight(3, 6, 1.),
Fight(3, 2, 0.),
Fight(3, 9, 1.),
Fight(3, 5, 1.),
Fight(3, 1, 0.),
Fight(4, 1, 0.),
Fight(4, 5, 0.),
Fight(4, 7, 1.),
Fight(4, 8, 1.),
Fight(4, 6, 1.),
]
fight_data = [
[None]*PLAYERS_COUNT for _ in range(PLAYERS_COUNT)
]
for fight in fights:
player_index = fight.player_no - 1
opponent_index = fight.opponent - 1
fight_data[player_index][opponent_index] = fight.points
players = [Player(f'Боец №{i+1}', fights) for i, fights in enumerate(fight_data)]
players = sorted(players, key=attrgetter('result'), reverse=True)
table = [['№', 'Участник'] + list(range(1, PLAYERS_COUNT + 1)) + ['Результат', 'Место']]
for i, player in enumerate(players):
row = [i + 1, player.name] + player.fights + [player.result, i + 1]
table.append(row)
context['table'] = table
return context
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Ranking Table</title>
<style>
table {
border-collapse: collapse;
}
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<h1>Ranking Table</h1>
<table>
{% for row in table %}
<tr>
{% for column in row %}
<td>{{ column | default_if_none:'' }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
</body>
</html>