diff --git a/target/functions.py b/target/functions.py index b3ec1fdab..90eac536d 100644 --- a/target/functions.py +++ b/target/functions.py @@ -89,5 +89,38 @@ def updateAttacks(player): return error +def updateRevives(player): + tId = player.tId + key = player.key + + error = False + req = apiCall('user', "", 'revives,timestamp', key) + if 'apiError' in req: + error = req + else: + revives = req.get("revives", dict({})) + + # needs to convert to dict if empty because if empty returns [] + if not len(revives): + revives = dict({}) + + # get database revives and delete 1 month old + player_revives = player.revive_set.all() + lastMonth = req.get("timestamp", 0) - 31 * 24 * 3600 + player_revives.filter(timestamp__lt=lastMonth).delete() + + for k, v in revives.items(): + if not len(player_revives.filter(tId=int(k))) and v.get("timestamp", 0) > lastMonth: + del revives[k]["reviver_id"] + del revives[k]["reviver_name"] + del revives[k]["reviver_faction"] + del revives[k]["reviver_factionname"] + player.revive_set.create(tId=k, **v) + + player.save() + + return error + + def convertElaspedString(str): return str.replace("minute", "min").replace("hour", "hr").replace("second", "sec") diff --git a/target/migrations/0001_initial.py b/target/migrations/0001_initial.py new file mode 100644 index 000000000..b8a10e122 --- /dev/null +++ b/target/migrations/0001_initial.py @@ -0,0 +1,30 @@ +# Generated by Django 3.0.1 on 2020-01-03 23:00 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('player', '0029_auto_20191227_1720'), + ] + + operations = [ + migrations.CreateModel( + name='Revive', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('tId', models.IntegerField(default=0)), + ('timestamp', models.IntegerField(default=0)), + ('target_id', models.IntegerField(default=0)), + ('target_name', models.CharField(default='target_name', max_length=32)), + ('target_faction', models.IntegerField(default=0)), + ('target_factionname', models.CharField(blank=True, default='target_factionname', max_length=32, null=True)), + ('paid', models.BooleanField(default=False)), + ('player', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='player.Player')), + ], + ), + ] diff --git a/target/models.py b/target/models.py index 71a836239..f9c498ed0 100644 --- a/target/models.py +++ b/target/models.py @@ -1,3 +1,13 @@ from django.db import models -# Create your models here. +from player.models import Player + +class Revive(models.Model): + player = models.ForeignKey(Player, on_delete=models.CASCADE) + tId = models.IntegerField(default=0) + timestamp = models.IntegerField(default=0) + target_id = models.IntegerField(default=0) + target_name = models.CharField(default="target_name", max_length=32) + target_faction = models.IntegerField(default=0) + target_factionname = models.CharField(default="target_factionname", null=True, blank=True, max_length=32) + paid = models.BooleanField(default=False) diff --git a/target/urls.py b/target/urls.py index 3c86c3482..019bc2ca1 100644 --- a/target/urls.py +++ b/target/urls.py @@ -11,6 +11,7 @@ re_path(r'^revives/$', views.revives, name='revives'), re_path(r'^toggleTarget/(?P\w+)$', views.toggleTarget, name='toggleTarget'), + re_path(r'^toggleRevive/$', views.toggleRevive, name='toggleRevive'), re_path(r'^updateNote/$', views.updateNote, name='updateNote'), re_path(r'^refresh/(?P\w+)$', views.refresh, name='refresh'), diff --git a/target/views.py b/target/views.py index a3ecf43d6..3fd91508d 100644 --- a/target/views.py +++ b/target/views.py @@ -29,6 +29,7 @@ from player.models import Player from chain.functions import BONUS_HITS from target.functions import updateAttacks +from target.functions import updateRevives from target.functions import convertElaspedString @@ -418,9 +419,9 @@ def revives(request): player.lastActionTS = int(timezone.now().timestamp()) player.save() - error = False - revives = dict({}) - # insert code here ^^ + error = updateRevives(player) + + revives = player.revive_set.all() context = {"player": player, "targetcat": True, "revives": revives, "view": {"revives": True}} if error: @@ -434,3 +435,25 @@ def revives(request): except Exception: return returnError() + + +def toggleRevive(request): + try: + if request.session.get('player') and request.method == "POST": + print('[view.target.toggleRevive] get player id from session and check POST') + tId = request.session["player"].get("tId") + player = Player.objects.filter(tId=tId).first() + + r = player.revive_set.filter(tId=request.POST.get("reviveId", 0)).first() + if r is not None: + r.paid = not r.paid + r.save() + + return render(request, 'target/revives-buttons.html', {"revive": r}) + + else: + message = "You might want to log in." if request.method == "POST" else "You need to post. Don\'t try to be a smart ass." + return returnError(type=403, msg=message) + + except Exception: + return returnError() diff --git a/templates/target.html b/templates/target.html index e2b5dd48e..df588ba25 100644 --- a/templates/target.html +++ b/templates/target.html @@ -33,6 +33,17 @@ reload.html(''); }); + // toggle revive from revives list button + $(document).on('click', 'a[id^="revives-list-toggle-"]', function(e){ + e.preventDefault(); + var reload = $(this).closest("td"); + var reviveId = $(this).attr("id").split("-").pop(); + reload.load( "/target/toggleRevive/", { + reviveId: reviveId, + csrfmiddlewaretoken: document.getElementsByName('csrfmiddlewaretoken')[0].value, + }).html(''); + }); + // refresh target from target list by clicking on the row $(document).on('click', 'tr[id^="target-list-refresh-"] > td:not(.dont-touch-me)', function(e){ e.preventDefault(); diff --git a/templates/target/revives-buttons.html b/templates/target/revives-buttons.html new file mode 100644 index 000000000..a2c408792 --- /dev/null +++ b/templates/target/revives-buttons.html @@ -0,0 +1,31 @@ +{% comment %} +Copyright 2019 kivou.2000607@gmail.com + +This file is part of yata. + + yata is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + yata is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with yata. If not, see . +{% endcomment %} + + +
{% csrf_token %} + + {% if revive.paid %} + + + {% else %} + + + {% endif %} + +
diff --git a/templates/target/revives.html b/templates/target/revives.html index c7b2e9172..28eb18fe9 100644 --- a/templates/target/revives.html +++ b/templates/target/revives.html @@ -20,7 +20,45 @@ {% load mathfilters %} {% load app_filters %} -

List of recent revives

+{% if revives %} + + + +

List of recent revives

-

I'm just being a troll here. I'm trying to get {{"kwartz"|playerURL:2002288|safe}} to code for YATA and it will be his first projet. Maybe if you push him he'll do it.

+ + + + + + + + + + {% for v in revives %} + + + + + + + {% endfor %} + +
TimeTargetFactionPaid
{{v.timestamp|ts2date}}{{v.target_name|playerURL:v.target_id|safe}}{{v.target_factionname|factionURL:v.target_faction|safe}} + {% include "target/revives-buttons.html" with revive=v %} +
+
+ +{% else %} +
+ It seems you didn't make any revives this month...
+{% endif %} diff --git a/templates/target/tutorial.html b/templates/target/tutorial.html index 18d7f3fa6..714195eb0 100644 --- a/templates/target/tutorial.html +++ b/templates/target/tutorial.html @@ -82,7 +82,7 @@

Attack list

Here you find the last 100 attacks you made and received.

Revives list

-

Soon™

+

Here you find all the revives you made the last 31 days. Each refresh of this page will load the last 100 revives from the API. To be sure to not miss a revive come here at least every 100 revives.
You can manually mark them as paid or not.

FAQ

diff --git a/yata/static/perso/css/target.css b/yata/static/perso/css/target.css index 6cafb65f8..973a163a9 100644 --- a/yata/static/perso/css/target.css +++ b/yata/static/perso/css/target.css @@ -28,7 +28,6 @@ table.target-categories td a:hover, table.target-categories td a:focus, table.target-categories td a:active { text-decoration: none; color: #447e9b; } - table.target-attacks tr.zero-respect { background:#ff00001e; } table.target-attacks tr.max-fair-fight { background:#d9ead3ee; } table.target-attacks tr.good-fair-fight { background:#d9ead3ae; } @@ -41,7 +40,6 @@ table.target-attacks td.c { width: 15%; text-align: left; } table.target-attacks td.d { width: 15%; text-align: center; } /* x2 */ table.target-attacks td.e { width: 5%; text-align: center; } - table.target-targets td.a { width: 7%; text-align: left; } /* x2 */ table.target-targets td.b { width: 10%; text-align: left; } table.target-targets td.c { width: 7%; text-align: center; } /* x3 */ @@ -51,11 +49,15 @@ table.target-targets td.f { width: 8%; text-align: center; } table.target-targets td.g { width: 3%; text-align: center; } /* x2 */ table.target-targets td.h { width: 14%; text-align: left; } -table.target-targets td.okay { color:#0B8E14; } -table.target-targets td.not-okay { color:#CC3434; } -table.target-targets i.not-okay { color:#aaa; } +table.target-targets td.okay { color:#0B8E14; } +table.target-targets td.not-okay { color:#CC3434; } +table.target-targets i.not-okay { color:#aaa; } table.target-targets td.status { cursor: default; } +table.target-revives td.a { width: 30%; text-align: center; } +table.target-revives td.b { width: 30%; text-align: left; } +table.target-revives td.c { width: 10%; text-align: center; } + input.target-list-note { height: 16px; width: 90%;