Skip to content

Commit

Permalink
Merge pull request #5 from edbourque0/auto-refresh
Browse files Browse the repository at this point in the history
Auto refresh and UX improvements integration
  • Loading branch information
edbourque0 authored Apr 18, 2024
2 parents 48c7e32 + 1df9fab commit f47c13f
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 15 deletions.
15 changes: 15 additions & 0 deletions clashstats/clanranking/autorefresh.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import requests
import time

while True:
url = 'http://192.168.2.39:8000/clanrefresh/G9JVLC2C'
try:
response = requests.get(url)
if response.status_code == 200:
print("View called successfully.")
else:
print(f"Error calling view: {response.status_code}")
except requests.exceptions.RequestException as e:
print(f"Request failed: {e}")
print(f"Waiting for 1 hour.")
time.sleep(3600)
19 changes: 19 additions & 0 deletions clashstats/clanranking/migrations/0006_clans_lastupdated.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 5.0.4 on 2024-04-18 18:04

import django.utils.timezone
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("clanranking", "0005_delete_lastupdated"),
]

operations = [
migrations.AddField(
model_name="clans",
name="lastUpdated",
field=models.DateTimeField(default=django.utils.timezone.now),
),
]
2 changes: 2 additions & 0 deletions clashstats/clanranking/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.db import models
from django.utils import timezone


class Clans(models.Model):
Expand All @@ -11,6 +12,7 @@ class Clans(models.Model):
clanWarTrophies = models.PositiveIntegerField()
requiredTrophies = models.PositiveIntegerField()
donationsPerWeek = models.PositiveIntegerField()
lastUpdated = models.DateTimeField(default=timezone.now)


class Members(models.Model):
Expand Down
36 changes: 25 additions & 11 deletions clashstats/clanranking/templates/clanranking.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,8 @@
{% progressive_web_app_meta %}
<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://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<script>
document.addEventListener("DOMContentLoaded", function() {
var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
return new bootstrap.Tooltip(tooltipTriggerEl);
});
});
</script>
<style>
.custom-badge {
background-color: #d63384;
Expand Down Expand Up @@ -44,9 +37,15 @@
<div class='container py-5'>
<div class='text-center'>
<h1 class='fw-bold text-warning'>Clan ranking</h1>
</div>
<div class='text-center'>
<h4 class='p-3 fw-bold'><span class="badge text-bg-primary">{{ clan.name }}</span></h4>
{% if lastUpdatedMin == 0 %}
<p class='text-muted'>Just updated</p>
{% 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>
</div>
{% for member in members %}
<div class='mb-3 border border-secondary border-3 rounded p-3'>
Expand Down Expand Up @@ -117,5 +116,20 @@ <h4>Clanmates stats</h4>
</div>
{% endfor %}
</div>
<script>
document.addEventListener("DOMContentLoaded", function() {
var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
return new bootstrap.Tooltip(tooltipTriggerEl);
});

var refreshButton = document.getElementById('refreshButton');
refreshButton.addEventListener('click', function(event) {
this.innerHTML = '<span class="spinner-border spinner-border-sm" aria-hidden="true"></span>';
this.classList.add('disabled'); // Use Bootstrap's 'disabled' class to disable the button
this.setAttribute('aria-disabled', 'true'); // Optionally set aria-disabled for accessibility
});
});
</script>
</body>
</html>
1 change: 1 addition & 0 deletions clashstats/clanranking/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@
urlpatterns = [
path("clanranking/<slug:clantag>", views.clanranking, name="clanranking"),
path("clanrankingsearch", views.clanrankingsearch, name="clanrankingsearch"),
path("clanrefresh/<slug:clantag>", views.clanrefresh, name="clanrefresh"),
]
19 changes: 15 additions & 4 deletions clashstats/clanranking/views.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from django.http import HttpResponse
from django.template import loader
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


def clanrankingsearch(request):
Expand All @@ -16,7 +17,7 @@ def clanrankingsearch(request):


@csrf_exempt
def clanranking(request, clantag):
def clanrefresh(request, clantag):
# Request variables
APIKEY = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiIsImtpZCI6IjI4YTMxOGY3LTAwMDAtYTFlYi03ZmExLTJjNzQzM2M2Y2NhNSJ9.eyJpc3MiOiJzdXBlcmNlbGwiLCJhdWQiOiJzdXBlcmNlbGw6Z2FtZWFwaSIsImp0aSI6IjRiODJiNzlmLTg5NzAtNDllYi05NGEyLWEyZDAzNzU5MjIyNSIsImlhdCI6MTcxMjk1MzUyMSwic3ViIjoiZGV2ZWxvcGVyLzNkNjhmM2MyLWM4ZmItNDAyYy0zZTU4LTk0YjIzMGY1Y2IzZCIsInNjb3BlcyI6WyJyb3lhbGUiXSwibGltaXRzIjpbeyJ0aWVyIjoiZGV2ZWxvcGVyL3NpbHZlciIsInR5cGUiOiJ0aHJvdHRsaW5nIn0seyJjaWRycyI6WyI3NC4xMi4xNjkuMjMwIiwiMTczLjE4Mi4xNTQuMjQ2IiwiMjA5LjE3MS4xNjIuMTQ1IiwiMjA3LjE2Ny4yMTYuODkiXSwidHlwZSI6ImNsaWVudCJ9XX0.YvF3ojCgj7r7KdBBCoSufuTnVj6Qd0wz6YaRWfdSet6QPIZOn3HSlXlJUlyLLfUQUQi0G6nfL3MPleE_CTnn2w"
headers = {
Expand All @@ -27,8 +28,6 @@ def clanranking(request, clantag):
)

rawclan = c.json()
# -----------------

for memint, member in enumerate(rawclan["memberList"]):
if Members.objects.filter(tag=member["tag"][1:]).exists() == False:
Members.objects.update_or_create(
Expand Down Expand Up @@ -58,6 +57,9 @@ def clanranking(request, clantag):
requiredTrophies=rawclan["requiredTrophies"],
donationsPerWeek=rawclan["donationsPerWeek"],
)

else:
Clans.objects.filter(tag=rawclan["tag"]).update(lastUpdated=timezone.now())

for member in Members.objects.all():
b = requests.get(
Expand Down Expand Up @@ -190,12 +192,21 @@ def clanranking(request, clantag):
Members.objects.filter(tag=battle.team1Tag).update(eloRating=newRanking_t)
Members.objects.filter(tag=battle.opponent1Tag).update(eloRating=newRanking_o)

return redirect(f'/clanranking/{clantag}')


@csrf_exempt
def clanranking(request, clantag):
template = loader.get_template("clanranking.html")
tu = Clans.objects.get(tag=f"#{clantag}").lastUpdated
tn = timezone.now()
context = {
"clan": Clans.objects.get(tag=f"#{clantag}"),
"currentclantag": Clans.objects.get(tag=f"#{clantag}").tag,
"members": Members.objects.filter(~Q(eloRating=1000))
.order_by("-eloRating")
.all(),
"battles": Battles.objects.all(),
"lastUpdatedMin": round((tn - tu).total_seconds() / 60),
}
return HttpResponse(template.render(context, request))
1 change: 1 addition & 0 deletions clashstats/clashstats/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,4 @@
]
PWA_APP_DIR = 'ltr'
PWA_APP_LANG = 'en-US'

Binary file modified clashstats/db.sqlite3
Binary file not shown.
12 changes: 12 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
amqp==5.2.0
asgiref==3.8.1
billiard==4.2.0
black==24.4.0
certifi==2024.2.2
charset-normalizer==3.3.2
click==8.1.7
click-didyoumean==0.3.1
click-plugins==1.1.1
click-repl==0.3.0
Django==5.0.4
django-pwa==1.1.0
idna==3.6
kombu==5.3.7
mypy-extensions==1.0.0
packaging==24.0
pathspec==0.12.1
platformdirs==4.2.0
prompt-toolkit==3.0.43
pyinstrument==4.6.2
python-dateutil==2.9.0.post0
requests==2.31.0
six==1.16.0
sqlparse==0.4.4
tzdata==2024.1
urllib3==2.2.1
vine==5.1.0
wcwidth==0.2.13
3 changes: 3 additions & 0 deletions tested.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from datetime import datetime

print(datetime.now())

0 comments on commit f47c13f

Please sign in to comment.