Skip to content

Commit

Permalink
UI improvements and weekly ranking
Browse files Browse the repository at this point in the history
  • Loading branch information
Edouard Bourque committed Apr 19, 2024
1 parent f47c13f commit 1738199
Show file tree
Hide file tree
Showing 13 changed files with 210 additions and 48 deletions.
4 changes: 2 additions & 2 deletions clashstats/battlelog/templates/battlelog.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active fw-bold" aria-current="page" href="/playerstatssearch">Player stats</a>
<a class="nav-link active" aria-current="page" href="/playerstatssearch">Player stats</a>
</li>
<li class="nav-item">
<a class="nav-link active fw-bold" aria-current="page" href="/clanrankingsearch"> Clan ranking</a>
<a class="nav-link active" aria-current="page" href="/clanrankingsearch"> Clan ranking</a>
</li>
</ul>
</div>
Expand Down
4 changes: 2 additions & 2 deletions clashstats/battlelog/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active fw-bold" aria-current="page" href="/playerstatssearch">Player stats</a>
<a class="nav-link active" aria-current="page" href="/playerstatssearch">Player stats</a>
</li>
<li class="nav-item">
<a class="nav-link active fw-bold" aria-current="page" href="/clanrankingsearch"> Clan ranking</a>
<a class="nav-link active" aria-current="page" href="/clanrankingsearch"> Clan ranking</a>
</li>
</ul>
</div>
Expand Down
4 changes: 2 additions & 2 deletions clashstats/battlelog/templates/playerstatssearch.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active fw-bold" aria-current="page" href="/playerstatssearch">Player stats</a>
<a class="nav-link active" aria-current="page" href="/playerstatssearch">Player stats</a>
</li>
<li class="nav-item">
<a class="nav-link active fw-bold" aria-current="page" href="/clanrankingsearch"> Clan ranking</a>
<a class="nav-link active" aria-current="page" href="/clanrankingsearch"> Clan ranking</a>
</li>
</ul>
</div>
Expand Down
38 changes: 38 additions & 0 deletions clashstats/clanranking/migrations/0007_weekbattles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Generated by Django 5.0.4 on 2024-04-19 18:31

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("clanranking", "0006_clans_lastupdated"),
]

operations = [
migrations.CreateModel(
name="weekBattles",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("eloRating", models.PositiveIntegerField(default=1000)),
(
"tag",
models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="weekBattleTag",
to="clanranking.members",
),
),
],
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 5.0.4 on 2024-04-19 18:32

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("clanranking", "0007_weekbattles"),
]

operations = [
migrations.RenameModel(
old_name="weekBattles",
new_name="weeklyRanking",
),
]
7 changes: 7 additions & 0 deletions clashstats/clanranking/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,10 @@ class Battles(models.Model):
loser1Tag = models.ForeignKey(
Members, on_delete=models.SET_NULL, null=True, related_name="looser1tag"
)


class weeklyRanking(models.Model):
tag = models.ForeignKey(
Members, on_delete=models.SET_NULL, null=True, related_name="weekBattleTag"
)
eloRating = models.PositiveIntegerField(default=1000)
91 changes: 76 additions & 15 deletions clashstats/clanranking/templates/clanranking.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<!doctype html>
<html lang="en" data-bs-theme='dark'>
{% load pwa %}
{% load clanrankingtags %}
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
Expand All @@ -25,10 +26,10 @@
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active fw-bold" aria-current="page" href="/playerstatssearch">Player stats</a>
<a class="nav-link active" aria-current="page" href="/playerstatssearch">Player stats</a>
</li>
<li class="nav-item">
<a class="nav-link active fw-bold" aria-current="page" href="/clanrankingsearch"> Clan ranking</a>
<a class="nav-link active" aria-current="page" href="/clanrankingsearch"> Clan ranking</a>
</li>
</ul>
</div>
Expand All @@ -42,17 +43,71 @@ <h1 class='fw-bold text-warning'>Clan ranking</h1>
{% else %}
<p class='text-muted'>Updated {{ lastUpdatedMin }} minutes ago</p>
{% endif %}
<h4 class="fw-bold d-inline-block my-auto"><span class="badge text-bg-primary">{{ clan.name }}</span></h4>
<div class="mb-3 text-end">
<a id='refreshButton' href="/clanrefresh/{{ clan.tag|slice:"1:" }}" class="btn btn-outline-primary" role="button"><i class="bi bi-arrow-clockwise" style='-webkit-text-stroke: 1px;'></i></a>
<div class='d-flex justify-content-between'>
<div>
<button type="button" class="btn btn-outline-primary mb-4" data-bs-toggle="modal" data-bs-target="#weeklyRanking">
Weekly ranking
</button>
</div>
<div class="btn-group mb-4 justify-content-center" role="group" aria-label="Basic example">
<button type="button" class="btn btn-primary pe-none">{{ clan.name }}</button>
<a id='refreshButton' href="/clanrefresh/{{ clan.tag|slice:"1:" }}" class="btn btn-outline-primary" role="button"><i class="bi bi-arrow-clockwise" style='-webkit-text-stroke: 1px;'></i></a>
</div>
</div>
<!-- Modal -->
<div class="modal fade" id="weeklyRanking" tabindex="-1" aria-labelledby="weeklyRankingLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title fs-5" id="weeklyRankingLabel">Weekly ranking</h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div>
<p class='text-muted'>{% week_start %} - {% week_end %}<p>
<div class='container-fluid'>
<div class='row d-flex justify-content-center'>
<div class='col border rounded' style='width:300px;'>
<table class="table table-striped">
<thead>
<tr>
<th class='text-warning' scope="col">#</th>
<th scope="col">Player</th>
<th scope="col">Elo</th>
</tr>
</thead>
<tbody>
{% get_weekly_ranking as weekly_ranking %}
{% for index, member_info in weekly_ranking.items %}
<tr>
<th class='text-warning' scope="row">{{ index }}</th>
<td>{{ member_info.name }}</td>
<td><span class="badge text-bg-light">{{ member_info.eloRating }}</span></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
{% for member in members %}
<div class='mb-3 border border-secondary border-3 rounded p-3'>
<div class='row'>
<div class='col-12 col-md-4 mb-3'>
<h1><a class="link-opacity-100-hover link-underline-dark text-warning mb-4" href="/battlelog/{{ member.tag }}">#{{ forloop.counter }} <span class='text-light'>{{ member.name }}</span></a></h1>
<h5 class='mb-3'><span class="badge text-bg-light">{{ member.eloRating }}</span></h5>
<div class='d-flex justify-content-between align-items-center'>
<div>
<h1><a class="link-opacity-100-hover link-underline-dark text-warning mb-4" href="/battlelog/{{ member.tag }}">#{{ forloop.counter }} <span class='text-light'>{{ member.name }}</span></a></h1>
</div>
<div>
<span class="badge text-bg-light fs-6">{{ member.eloRating }}</span>
</div>
</div>
<h5><i class="ti ti-trophy"></i> {{ member.trophies }} trophies</h5>
<h5><i class="ti ti-rosette"></i> XP level {{ member.expLevel }}</h5>
<h5><i class="ti ti-building-stadium"></i> {{ member.arena }}</h5>
Expand All @@ -65,18 +120,24 @@ <h5><i class="ti ti-math-x-divide-y-2"></i> Win / game ratio: {{ member.wgratio
<div class='border rounded p-3 mb-3'>
<h4>Recent battles</h4>
<div class='overflow-auto' style='height:270px;'>
{% load clanrankingtags %}
{% get_combined_battles member as combined_battles %}
{% for battle in combined_battles %}
<div class="border rounded mb-2">
<div class="p-2">
<div class="mb-2">
<h5>
{{ battle.winner1Tag.name }} <span class="text-warning">vs</span> {{ battle.loser1Tag.name }}
</h5>
<span class='text-muted'>{{ battle.battleTime }}</span>
<p><i class="ti ti-award"></i> Winner: {{ battle.winner1Tag.name }}</p>
<p><i class="ti ti-skull"></i> Loser: {{ battle.loser1Tag.name }}</p>
<div>
<div class='d-flex justify-content-between'>
<div>
<h5>{{ battle.winner1Tag.name }} <span class="text-warning">vs</span> {{ battle.loser1Tag.name }}</h5>
</div>
<div>
{% if battle.winner1Tag.name == member.name %}
<span class="badge text-bg-success">Won</span>
{% else %}
<span class="badge text-bg-danger">Lost</span>
{% endif %}
</div>
</div>
<p class='text-muted'>{{ battle.battleTime }}</p>
</div>
</div>
</div>
Expand Down
4 changes: 2 additions & 2 deletions clashstats/clanranking/templates/clanrankinghome.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active fw-bold" aria-current="page" href="/playerstatssearch">Player stats</a>
<a class="nav-link active" aria-current="page" href="/playerstatssearch">Player stats</a>
</li>
<li class="nav-item">
<a class="nav-link active fw-bold" aria-current="page" href="/clanrankingsearch"> Clan ranking</a>
<a class="nav-link active" aria-current="page" href="/clanrankingsearch"> Clan ranking</a>
</li>
</ul>
</div>
Expand Down
Binary file not shown.
86 changes: 63 additions & 23 deletions clashstats/clanranking/templatetags/clanrankingtags.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from django import template

from django.utils import timezone
register = template.Library()
from django.db import models
from clanranking.models import Battles
from clanranking.models import Battles, Members, weeklyRanking
from datetime import timedelta, datetime


@register.simple_tag
Expand All @@ -15,15 +16,23 @@ def get_combined_battles(member):

@register.simple_tag
def get_battle_stats(member):
# Initialize a dictionary to hold the stats.
stats = {}
# Initialize a dictionary to hold the stats for each member.
all_members = Members.objects.exclude(tag=member.tag)
stats = {
other_member.name: {
"wins": 0,
"losses": 0,
"eloExpectation": round(1 / (1 + 10 ** ((other_member.eloRating - member.eloRating) / 400)) * 100)
}
for other_member in all_members
}

# Query all battles involving the member.
involved_battles = Battles.objects.filter(
models.Q(team1Tag=member)
| models.Q(team2Tag=member)
| models.Q(opponent1Tag=member)
| models.Q(opponent2Tag=member)
models.Q(team1Tag=member) |
models.Q(team2Tag=member) |
models.Q(opponent1Tag=member) |
models.Q(opponent2Tag=member)
).distinct()

for battle in involved_battles.order_by("-battleTime"):
Expand All @@ -35,18 +44,49 @@ def get_battle_stats(member):
opponent = battle.winner1Tag
result = "losses"

opponent_name = opponent.name
# Initialize opponent's stats in the dictionary if not already present.
if opponent_name not in stats:
# It seems the intention is to calculate expectation against opponent1Tag, but you might need to adjust based on actual opponent
eloExp = 1 / (1 + 10 ** ((opponent.eloRating - member.eloRating) / 400))
stats[opponent_name] = {
"wins": 0,
"losses": 0,
"eloExpectation": round(eloExp * 100),
}

# Increment the win or loss count.
stats[opponent_name][result] += 1

return stats
# Use opponent's name to update stats.
if opponent.name in stats:
stats[opponent.name][result] += 1

# Sort the dictionary by wins in descending order
sorted_stats = dict(sorted(stats.items(), key=lambda item: item[1]['eloExpectation'], reverse=True))

return sorted_stats


@register.simple_tag
def get_weekly_ranking():
week_start = datetime.now() - timedelta(weeks=1)
week_end = datetime.now()

weeklyRanking.objects.all().delete()
for member in Members.objects.all():
weeklyRanking.objects.create(tag=Members.objects.filter(tag=member.tag).get())

for battle in Battles.objects.filter(battleTime__range=[week_start, week_end]):
expectation_t = 1 / (1 + 10 ** ((battle.opponent1Tag.eloRating - battle.team1Tag.eloRating) / 400))
expectation_o = 1 - expectation_t
sAt = 1 if battle.team1Tag == battle.winner1Tag else 0
sAo = 1 if battle.opponent1Tag == battle.winner1Tag else 0
newRanking_t = battle.team1Tag.eloRating + 32 * (sAt - expectation_t)
newRanking_o = battle.opponent1Tag.eloRating + 32 * (sAo - expectation_o)
weeklyRanking.objects.filter(tag=battle.team1Tag.tag).update(eloRating=newRanking_t)
weeklyRanking.objects.filter(tag=battle.opponent1Tag.tag).update(eloRating=newRanking_o)

mem_dict = {}
for index, member in enumerate(weeklyRanking.objects.filter(~models.Q(eloRating=1000)).order_by('-eloRating')):
mem_dict[index + 1] = {
'name': member.tag.name,
'eloRating': member.eloRating
}

return mem_dict

@register.simple_tag
def week_start():
date = datetime.now() - timedelta(weeks=1)
return date.strftime("%m/%d/%Y")

@register.simple_tag
def week_end():
return datetime.now().strftime("%m/%d/%Y")
1 change: 0 additions & 1 deletion clashstats/clanranking/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ def clanrefresh(request, clantag):
Members.objects.filter(tag=member.tag).update(eloRating=1000)

for battle in Battles.objects.all():
# Team 1 expectations
expectation_t = 1 / (
1
+ 10 ** ((battle.opponent1Tag.eloRating - battle.team1Tag.eloRating) / 400)
Expand Down
2 changes: 1 addition & 1 deletion clashstats/clashstats/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"django.contrib.staticfiles",
"battlelog",
"clanranking",
'pwa',
"pwa",
]

MIDDLEWARE = [
Expand Down
Binary file modified clashstats/db.sqlite3
Binary file not shown.

0 comments on commit 1738199

Please sign in to comment.