Skip to content

Commit

Permalink
Add last revives in target
Browse files Browse the repository at this point in the history
  • Loading branch information
Kivou-2000607 committed Jan 3, 2020
1 parent b845f27 commit c6a7b99
Show file tree
Hide file tree
Showing 10 changed files with 191 additions and 12 deletions.
33 changes: 33 additions & 0 deletions target/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
30 changes: 30 additions & 0 deletions target/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -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')),
],
),
]
12 changes: 11 additions & 1 deletion target/models.py
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions target/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
re_path(r'^revives/$', views.revives, name='revives'),

re_path(r'^toggleTarget/(?P<targetId>\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<targetId>\w+)$', views.refresh, name='refresh'),
Expand Down
29 changes: 26 additions & 3 deletions target/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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:
Expand All @@ -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()
11 changes: 11 additions & 0 deletions templates/target.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@
reload.html('<i class="fas fa-spinner fa-pulse"></i>');
});

// 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('<i class="fas fa-spinner fa-pulse"></i>');
});

// 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();
Expand Down
31 changes: 31 additions & 0 deletions templates/target/revives-buttons.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{% comment %}
Copyright 2019 [email protected]

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 <https://www.gnu.org/licenses/>.
{% endcomment %}

<!-- toggle -->
<form style="display: inline;">{% csrf_token %}
<a id="revives-list-toggle-{{revive.tId}}" href="{% url 'target:toggleRevive' %}">
{% if revive.paid %}
<i class="fas fa-toggle-on" title="Click if revive not paid for"></i>
<input class="paid" type="hidden" value="1">
{% else %}
<i class="fas fa-toggle-off" title="Click if revive paid for"></i>
<input class="paid" type="hidden" value="0">
{% endif %}
</a>
</form>
42 changes: 40 additions & 2 deletions templates/target/revives.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,45 @@
{% load mathfilters %}
{% load app_filters %}

<h2 class="title">List of recent revives</span></h2>
{% if revives %}

<script>
$(document).ready(function() {
$("#target-revives").tablesorter({
textExtraction: {
3: function(node, table, cellIndex) { return $(node).find("input.paid").val(); },
},
cssAsc: 'up', cssDesc: 'down', sortList: [[0,1]]});
});
</script>

<h2 class="title">List of recent revives</h2>
<div class="module">
<p>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. <i class="far fa-kiss-wink-heart"></i></p>
<table id="target-revives" class="center trshadow target-revives">
<thead>
<tr>
<th>Time</th>
<th>Target</th>
<th>Faction</th>
<th>Paid</th>
</thead>
<tbody>
{% for v in revives %}
<tr>
<td class="a">{{v.timestamp|ts2date}}</td>
<td class="b">{{v.target_name|playerURL:v.target_id|safe}}</td>
<td class="b">{{v.target_factionname|factionURL:v.target_faction|safe}}</td>
<td class="c">
{% include "target/revives-buttons.html" with revive=v %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>

{% else %}
<div class="module rounded">
<span class="error">It seems you didn't make any revives this month...</span>
</div>
{% endif %}
2 changes: 1 addition & 1 deletion templates/target/tutorial.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ <h3>Attack list</h3>
<p>Here you find the last 100 attacks you made and received.</p>

<h3>Revives list</h3>
<p>Soon&trade;</p>
<p>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.<br/>You can manually mark them as paid or not.</p>


<h3>FAQ</h3>
Expand Down
12 changes: 7 additions & 5 deletions yata/static/perso/css/target.css
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -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 */
Expand All @@ -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%;
Expand Down

0 comments on commit c6a7b99

Please sign in to comment.