Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update target.html #574

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion faction/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1272,7 +1272,9 @@ def getWarStatus(self):
war["them"]["faction_id"] = int(fid)
self.warAgainst = fid
break

# if you want to debug this
# you can return a crafted dictionary like so:
# return {'them': {'faction_id': 9528}, 'us': {'faction_id': 41363}}
return war

def updateFactionTargets(self):
Expand Down
27 changes: 25 additions & 2 deletions faction/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@
RevivesReport,
SpyDatabase,
Territory,
FactionTarget,
)
from target.models import Target
from target.models import Target, TargetInfo
from yata.bulkManager import BulkUpdateManager
from yata.handy import (
apiCall,
Expand Down Expand Up @@ -4670,10 +4671,23 @@ def fightclub(request):
# SECTION: war
def rankedWar(request):
try:

if request.session.get("player"):
player = getPlayer(request.session["player"].get("tId"))
factionId = player.factionId
if 'remove_or_add' in request.POST:
# get the target list
new_target = request.POST.get('target_id')
add_target = request.POST.get('remove_or_add') == '1'
# get the target id of the ranked war page target
new_targets = FactionTarget.objects.filter(target_id=new_target).values_list('target_id', flat=True)
if not add_target:
player.targetinfo_set.filter(target_id__in=new_targets).delete()
else:
for selected_target in new_targets:
target_object, created = TargetInfo.objects.get_or_create(player=player, target_id=selected_target)
if created:
target_object.getTarget(update=True)
return JsonResponse({'success': 'Target added successfully'}, )

# get page
page = "faction/content-reload.html" if request.method == "POST" else "faction.html"
Expand All @@ -4687,11 +4701,16 @@ def rankedWar(request):

error = False

player_targets = FactionTarget.objects.filter(
target_id__in=TargetInfo.objects.filter(player=player).values_list('target_id', flat=True)
).values_list('target_id', flat=True)

context = {
"player": player,
"faction": faction,
"factioncat": True,
"view": {"war": True},
"player_targets": player_targets,
}
if error:
selectError = "apiErrorSub" if request.method == "POST" else "apiError"
Expand Down Expand Up @@ -4754,6 +4773,9 @@ def wartargets(request):
return render(request, "yata/error.html", context)

targets = faction.updateFactionTargets()
player_targets = FactionTarget.objects.filter(
target_id__in=TargetInfo.objects.filter(player=player).values_list('target_id', flat=True)
).values_list('target_id', flat=True)

return render(
request,
Expand All @@ -4762,6 +4784,7 @@ def wartargets(request):
"player": player,
"faction": faction,
"targets": targets,
"player_targets": player_targets,
},
)

Expand Down
15 changes: 14 additions & 1 deletion player/templatetags/app_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,23 @@ def playerURLShort(value, arg):

@register.filter(name="cleanhtml")
def cleanhtml(raw_html):
import re
cleanr = re.compile("<.*?>")
cleantext = re.sub(cleanr, "", raw_html)
return cleantext


@register.filter(name='clean_travel_status')
def clean_travel_status(raw_html):
cleanr = re.compile("<.*?>")
cleantext = re.sub(cleanr, "", raw_html)
clean_travel_r = re.compile(r"^(?:Returning to Torn|Traveling)")

if clean_travel_r.match(raw_html):
travel_text = re.sub(clean_travel_r, "", cleantext).strip()
if travel_text[0].islower():
travel_text = travel_text[0].upper() + travel_text[1:]
return travel_text

return cleantext


Expand Down
11 changes: 9 additions & 2 deletions target/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from django.shortcuts import redirect, render
from django.views.decorators.csrf import csrf_exempt

from faction.models import Faction
from faction.models import Faction, FactionTarget
from player.models import Key, Player
from target.functions import getTargets, updateAttacks, updateRevives
from yata.handy import apiCall, getFaction, getPlayer, returnError
Expand Down Expand Up @@ -228,8 +228,15 @@ def attack(request):

else:
returnError(type=403, msg="Unknown request")
faction = getFaction(player.factionId)

context = {"v": attack, "targets": getTargets(player), "ts": int(time.time())}
faction_targets = FactionTarget.objects.filter(target_id__in=faction.getTargetsId())
context = {
"v": attack,
"targets": getTargets(player),
"ts": int(time.time()),
"faction_targets": faction_targets.values_list("target_id", flat=True),
}
return render(request, "target/attacks/button-target.html", context)

else:
Expand Down
3 changes: 3 additions & 0 deletions templates/faction/war/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ <h2 class="title rounded-top px-2 py-2">
<h2 class="title rounded-top px-2 py-2">
<div class="d-flex flex-wrap align-items-center">
<div class="px-2 me-auto"><i class="fas fa-crosshairs"></i>&nbsp;Target list</div>
<div class="px-2">
<a id="targets-update" href=""><i class="fas fa-crosshairs fa-icon-inline" title="Add to targets list"></i>&nbsp;Update targets</a>
</div>
<div class="px-2">
<a id="faction-targets-refresh" href=""><i class="fas fa-sync fa-icon-inline" title="Refresh targets"></i>&nbsp;Refresh</a>
</div>
Expand Down
8 changes: 7 additions & 1 deletion templates/faction/war/target.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{% load i18n %}
{% comment %}
Copyright 2019 [email protected]

Expand Down Expand Up @@ -47,7 +48,7 @@
</div>
</td>
{% if target.status_color == "blue" %}
<td class='text-start player-status-{{target.status_color}}' title="{% if target.status_details %}{{target.status_details|cleanhtml}}{% endif %}" data-val="">{{target.status_description}}</td>
<td class='text-start player-status-{{target.status_color}}' title="{% if target.status_details %}{{target.status_details|cleanhtml}}{% else %}{% trans 'Destination unavailable at this time' %}{% endif %}" data-val="">{% if target.status_details %}{{target.status_details|clean_travel_status}}{% else %}{{target.status_description}}{% endif %}</td>
{% else %}
<td class='text-start status player-status-{{target.status_color}}' title="{% if target.status_details %}{{target.status_details|cleanhtml}}{% endif %}" data-val="{{target.status_until}}">{{target.status_description}}</td>
{% endif %}
Expand All @@ -56,4 +57,9 @@
<td class="text-center dont-touch-me" data-val="{{target.target_id}}">
{% include "faction/war/dibs.html" %}
</td>
<td class="text-center dont-touch-me" data-val="{% if target.target_id in player_targets %}1{% else %}0{% endif %}" >
<a class="add-target" data-val="{{ target.target_id }}">
<i class="fas {% if target.target_id in player_targets %}fa-toggle-on{% else %}fa-toggle-off{% endif %}" title=""></i>
</a>
</td>
{% endif %}
30 changes: 28 additions & 2 deletions templates/faction/war/targets.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,45 @@
9: function(node, table, cellIndex) { return $(node).attr("data-val"); },
10: function(node, table, cellIndex) { return $(node).attr("data-val"); },
11: function(node, table, cellIndex) { return $(node).attr("data-val"); },
14: function(node, table, cellIndex) {return $(node).attr("data-val"); },
},
cssAsc: 'up',
cssDesc: 'down',
sortList: [[11,0],[7,1],[6,0]]
sortList: [[11,0],[7,1],[6,0],[14,0]]
});

var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
return new bootstrap.Tooltip(tooltipTriggerEl)
})

$('.add-target').on('click tap', function (e) {
e.preventDefault();

var target_id = $(this).attr('data-val');
var csrf_token = getCookie('csrftoken');
var faction_id = "{{ faction.tId }}";
var removeOrAdd = $(this).find('i').hasClass('fa-toggle-off') ? 1 : 0;
$(this).find('i').toggleClass('fa-toggle-off').toggleClass('fa-toggle-on');

const params = new URLSearchParams();
params.set('target_id', target_id);
params.set('faction_id', faction_id);
params.set('csrfmiddlewaretoken', csrf_token);
params.set('remove_or_add', ''+removeOrAdd)
$.ajax({
type: 'POST',
url: './?rank_target=1',
data: params.toString(),
success: function(data) {
// do something to show success
}
});
});
});
</script>

<div class="table-responsive">

<table id="faction-targets" class="table table-hover table-striped table-borderless tablesorter faction-targets">
<thead>
<tr>
Expand All @@ -74,6 +99,7 @@
<th class="f" title="Status of the target">Status</th>
<th class="g" title="Time since last update">Update</th>
<th class="h" title="Call dibs">Dibs</th>
<th class="i" style="width: 2.5vw;" title="Add target">A</th>
</thead>
<tbody>
{% for target in targets %}
Expand Down
2 changes: 2 additions & 0 deletions templates/target/targets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
// console.log($(node).attr("data-col") + $(node).find("input.target-list-note").val() + "->" + a + "-" + b);
return a + "-" + b; },
12: function(node, table, cellIndex) { return $(node).attr("data-val"); },
15: function(node, table, cellIndex) { return $(node).attr("data-val"); },
},
cssAsc: 'up',
cssDesc: 'down',
Expand Down Expand Up @@ -83,6 +84,7 @@ <h2 class="title rounded-top px-2 py-2">
<th class="j" title="Attack target">A</th>
<th class="j" title="Delete target">D</th>
<th class="j" title="Faction target">F</th>
<th class="j" title="Faction target">RW</th>
</thead>
<tbody>
{% for targetId, target in targets.items %}
Expand Down
7 changes: 7 additions & 0 deletions templates/target/targets/line.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@
<!-- add to faction -->
{% include "target/targets/faction.html" %}
</td>
<td class="text-center" data-val="{% if targetId in factionTargets %}1{% else %}0{% endif %}">
{% if targetId in faction_targets %}
<i class="fas fa-check-circle text-success" title="Target is in ranked war targets"></i>
{% else %}
<i class="fas fa-xmark-circle text-danger" title="Target is not in ranked war targets"></i>
{% endif %}
</td>
{% else %}
<td colspan="3"><span class="error"><i class="far fa-dizzy fa-icon-inline"></i>No recorded id. Will be deleted soon.</span></td>
{% endif %}
Expand Down