Skip to content

Commit

Permalink
Historical ranking integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Edouard Bourque committed Apr 19, 2024
1 parent 1738199 commit c6514e7
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 35 deletions.
90 changes: 63 additions & 27 deletions clashstats/clanranking/templates/clanranking.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@tabler/icons-webfont@latest/dist/tabler-icons.min.css" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<style>
.custom-badge {
Expand Down Expand Up @@ -43,16 +44,16 @@ <h1 class='fw-bold text-warning'>Clan ranking</h1>
{% else %}
<p class='text-muted'>Updated {{ lastUpdatedMin }} minutes ago</p>
{% endif %}
<div class='d-flex justify-content-between'>
<div class='d-flex justify-content-end'>
<div class="btn-group mb-4 justify-content-center mx-3" 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>
<button type="button" class="btn btn-outline-primary mb-4" data-bs-toggle="modal" data-bs-target="#weeklyRanking">
Weekly ranking
Historical 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">
Expand All @@ -64,29 +65,43 @@ <h1 class="modal-title fs-5" id="weeklyRankingLabel">Weekly ranking</h1>
</div>
<div class="modal-body">
<div>
<p class='text-muted'>{% week_start %} - {% week_end %}<p>
<form id="dateForm" class='d-flex justify-content-between align-items-end'>
<div class="mb-3">
<label for="startDateInput" class="form-label">Start date</label>
<input type="date" class="form-control" id="startDate" name="start_date">
</div>
<div class="mb-3">
<label for="endDateInput" class="form-label">End date</label>
<input type="date" class="form-control" id="endDate" name="end_date">
</div>
<div class='mb-3'>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
<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 class='col border rounded' style='width:300px;' id="rankingTable">
<div id="rankingTable">
<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>
Expand Down Expand Up @@ -192,5 +207,26 @@ <h4>Clanmates stats</h4>
});
});
</script>
<script>
$(document).ready(function() {
$('#dateForm').on('submit', function(e) {
e.preventDefault();
var formData = $(this).serialize(); // Serialize the form data

$.ajax({
url: '{% url "clanranking" clantag="G9JVLC2C" %}',
type: 'POST',
data: formData,
headers: { 'X-Requested-With': 'XMLHttpRequest' },
success: function(response) {
$('#rankingTable').html(response);
},
error: function(xhr, status, error) {
console.error("Error occurred: " + error);
}
});
});
});
</script>
</body>
</html>
20 changes: 20 additions & 0 deletions clashstats/clanranking/templates/ranking_table.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{% load clanrankingtags %}
<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 start_date end_date 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>
Binary file not shown.
6 changes: 1 addition & 5 deletions clashstats/clanranking/templatetags/clanrankingtags.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@ def get_battle_stats(member):


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

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())
Expand All @@ -79,7 +76,6 @@ def get_weekly_ranking():
'name': member.tag.name,
'eloRating': member.eloRating
}

return mem_dict

@register.simple_tag
Expand Down
26 changes: 26 additions & 0 deletions clashstats/clanranking/views.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from django.http import HttpResponse
from django.template import loader
from django.template.loader import render_to_string
from django.views.decorators.csrf import csrf_exempt
from django.shortcuts import redirect
from django.db.models import Q
from django.utils import timezone
import requests
from .models import Members, Clans, Battles
from datetime import datetime, timedelta


def clanrankingsearch(request):
Expand Down Expand Up @@ -208,4 +210,28 @@ def clanranking(request, clantag):
"battles": Battles.objects.all(),
"lastUpdatedMin": round((tn - tu).total_seconds() / 60),
}

default_start_date = timezone.now() - timedelta(days=7)
default_end_date = timezone.now()

start_date = default_start_date
end_date = default_end_date

if request.method == 'POST':
start_date_str = request.POST.get('start_date')
end_date_str = request.POST.get('end_date')

start_date = datetime.strptime(start_date_str, '%Y-%m-%d') if start_date_str else default_start_date
end_date = datetime.strptime(end_date_str, '%Y-%m-%d') if end_date_str else default_end_date

contextDates = {
"start_date": start_date,
"end_date": end_date,
}

if request.headers.get('X-Requested-With') == 'XMLHttpRequest':
html = render_to_string('ranking_table.html', contextDates, request)
print(html)
return HttpResponse(html)

return HttpResponse(template.render(context, request))
Binary file modified clashstats/db.sqlite3
Binary file not shown.
3 changes: 0 additions & 3 deletions tested.py
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
from datetime import datetime

print(datetime.now())

0 comments on commit c6514e7

Please sign in to comment.