Skip to content

Commit

Permalink
live chain status
Browse files Browse the repository at this point in the history
  • Loading branch information
Kivou-2000607 committed Jun 29, 2019
1 parent cfa672c commit c18134c
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 81 deletions.
22 changes: 12 additions & 10 deletions chain/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,24 +141,26 @@ def live(request):
page = 'chain/content-reload.html' if request.method == 'POST' else 'chain.html'

# get live chain and next bonus
liveChain = apiCall('faction', factionId, 'chain', key, sub='chain')
if 'apiError' in liveChain:
req = apiCall('faction', factionId, 'chain,timestamp', key)
if 'apiError' in req:
player.chainInfo = "N/A"
player.factionId = 0
player.factionAA = False
# player.lastUpdateTS = int(timezone.now().timestamp())
player.save()
selectError = 'apiErrorSub' if request.method == 'POST' else 'apiError'
context = {'player': player, selectError: liveChain["apiError"] + " We can't check your faction so you don't have access to this section."}
context = {'player': player, selectError: req["apiError"] + " We can't check your faction so you don't have access to this section."}
return render(request, page, context)

liveChain = req.get("chain")
liveChain["timestamp"] = req.get("timestamp", 0)
activeChain = bool(int(liveChain['current']) > 9)
print("[view.chain.index] live chain: {}".format(activeChain))
liveChain["nextBonus"] = 10
for i in BONUS_HITS:
liveChain["nextBonus"] = i
if i >= int(liveChain["current"]):
break
# liveChain["nextBonus"] = 10
# for i in BONUS_HITS:
# liveChain["nextBonus"] = i
# if i >= int(liveChain["current"]):
# break

faction = Faction.objects.filter(tId=factionId).first()
if faction is None:
Expand Down Expand Up @@ -213,12 +215,12 @@ def live(request):
a, b, _, _, _ = stats.linregress(x[-20:], y[-20:])
print("[view.chain.index] linreg a={} b={}".format(a, b))
a = max(a, 0.00001)
ETA = timestampToDate(int((liveChain["nextBonus"] - b) / a))
ETA = timestampToDate(int((liveChain["max"] - b) / a))
graph['info']['ETALast'] = ETA
graph['info']['regLast'] = [a, b]

a, b, _, _, _ = stats.linregress(x, y)
ETA = timestampToDate(int((liveChain["nextBonus"] - b) / a))
ETA = timestampToDate(int((liveChain["max"] - b) / a))
graph['info']['ETA'] = ETA
graph['info']['reg'] = [a, b]

Expand Down
4 changes: 2 additions & 2 deletions player/templatetags/app_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def ts2date(timestamp, fmt=None):
return d.strftime(fmt)


@register.filter(name='ts2hhmmss')
def ts2mmss(timestamp):
@register.filter(name='ts2time')
def ts2time(timestamp):
d = timestamp // 86400
h = (timestamp - 86400 * d) // 3600 % 24
m = (timestamp - 3600 * h) // 60 % 60
Expand Down
2 changes: 1 addition & 1 deletion templates/bazaar/item.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
{% if seconds > 599 %}
> 10 mins
{% else %}
{{seconds|ts2hhmmss}}
{{seconds|ts2time}}
{% endif %}
{% endwith %}
</a>
Expand Down
2 changes: 1 addition & 1 deletion templates/chain/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ <h2 class="title">List of chains</h2>
<td class="b">-</td>
{% endif %}

<td class="c">{{chain.end|sub:chain.start|ts2hhmmss}}</td>
<td class="c">{{chain.end|sub:chain.start|ts2time}}</td>
<td class="d" title="{{chain.reportNHits}}/{{chain.nHits}}"><b>{{chain.nHits}}</b></td>
<td class="d" ><b>{{chain.respect|floatformat:2}}</b></td>
<td class="d" ><b>{{chain.respect|div:chain.nHits|floatformat:2}}</b></td>
Expand Down
93 changes: 81 additions & 12 deletions templates/chain/live.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,86 @@
along with yata. If not, see <https://www.gnu.org/licenses/>.
{% endcomment %}

<div>
<h2 class="title">Chain status</h2>

{% if liveChain.current %}
<div class="module">
Active chain: {{liveChain.current}} / {{liveChain.nextBonus}}
</div>
{% else %}
<div class="module">
No active chain
</div>
{% endif %}
{% load mathfilters %}
{% load app_filters %}

<script>
// refresh timer target update
window.setInterval(function(){
$(".timeout").each(function() {
var timeRefresh = $.trim($(this).text());
var splitRefresh = timeRefresh.replace("Timeout: ", "").split(" ");
var sRefresh = 0;
if (splitRefresh.length == 2) {
sRefresh = parseInt(splitRefresh[0]);
} else if (splitRefresh.length == 4) {
sRefresh = parseInt(splitRefresh[2]) + 60 * parseInt(splitRefresh[0]);
} else if (splitRefresh.length == 6) {
sRefresh = parseInt(splitRefresh[4]) + 60 * parseInt(splitRefresh[2]) + 3600 * parseInt(splitRefresh[0]);
}

sRefresh = Math.max(sRefresh-1, 0);
mRefresh = Math.floor(sRefresh / 60);
sRefresh = sRefresh % 60;
if (mRefresh) {
spad = ("0"+sRefresh.toString()).slice(-2);
$(this).html("Timeout: "+mRefresh.toString()+" mins "+spad+" s");
} else {
$(this).html("Timeout: "+sRefresh.toString()+" s");
}
if(!sRefresh) {
$(this).removeClass("timeout")
}
});
}, 1000);

</script>

<div>
{% if liveChain.current %}
<div class="module rounded">
<table class="center">
<thead>
<tr><th colspan="2">Live chain status {{currentTimestamp}}</th></tr>
</thead>
{% if liveChain.cooldown %}
<tr>
<td>Chain started {{currentTimestamp|sub:liveChain.start|ts2time}} ago</td>
<td>Chain status: <span class="error">cooldown</span></td>
</tr>
<tr>
<td>End of cooldown in {{liveChain.cooldown|ts2time}}</td>
<td>Hits: {{liveChain.current}} / {{liveChain.max}} (x{{liveChain.modifier}})</td>
</tr>
<tr>
<td colspan="2">
<div class="live-bar cooldown" title="{{liveChain.cooldown|ts2time}} left over the {{liveChain.current|mul:10|ts2time}}">
<span class="strips" style="width: {{liveChain.cooldown|div:liveChain.current|mul:10|floatformat:0}}%;"></span>
</div>
</td>
</tr>
{% else %}
<tr>
<td>Chain started {{currentTimestamp|sub:liveChain.start|ts2time}} ago</td>
<td>Chain status: <span class="valid">active</span></td>
</tr>
<tr>
<td class="timeout">Timeout: {{liveChain.timeout|add:currentTimestamp|sub:liveChain.timestamp|ts2time}}</td>
<td> Hits: {{liveChain.current}} / {{liveChain.max}} (x{{liveChain.modifier}})</td>
</tr>
<tr>
<td colspan="3">
<div class="live-bar" title="{{liveChain.current}} / {{liveChain.max}}">
<span class="strips" style="width: {{liveChain.current|div:liveChain.max|mul:100|floatformat:0}}%;"></span>
</div>
</td>
</tr>
{% endif %}
</table>
</div>
{% else %}
<div class="module">
No active chain
</div>
{% endif %}
</div>
2 changes: 1 addition & 1 deletion templates/chain/report-table.html
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
<tbody>
<tr>
<td colspan="2"><b>From</b> {{chain.start|ts2date}} <b>to</b> {{chain.end|ts2date}}</td>
<td colspan="2"><b>Chain time:</b> {{chain.end|sub:chain.start|ts2hhmmss}}</td>
<td colspan="2"><b>Chain time:</b> {{chain.end|sub:chain.start|ts2time}}</td>
</tr>
<tr>
<td colspan="2"><b>Hits / Attacks:</b> {{chain.reportNHits}} / {{chain.nAttacks}} &nbsp;&nbsp; ( {{chain.reportNHits|div:chain.nAttacks|mul:"100"|floatformat:0}}%)</td>
Expand Down
2 changes: 1 addition & 1 deletion templates/target/targets-line.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
{% if seconds > 7199 %}
> 2 hrs
{% else %}
{{seconds|ts2hhmmss}}
{{seconds|ts2time}}
{% endif %}
{% endwith %}
</td>
Expand Down
4 changes: 4 additions & 0 deletions yata/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ def news(request):
else:
print("[yata.context_processors.news] out")
return {}


def currentTimestamp(request):
return {"currentTimestamp": int(timezone.now().timestamp())}
12 changes: 7 additions & 5 deletions yata/handy.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,19 @@

def apiCall(section, id, selections, key, sub=None):
import requests
# # DEBUG live chain
# if selections == "chain" and section == "faction":
# DEBUG live chain
# if selections == "chain,timestamp" and section == "faction":
# from django.utils import timezone
# print("[yata.function.apiCall] DEBUG chain/faction")
# chain = dict({"chain": {"current": 10,
# "timeout": 65,
# chain = dict({"timestamp": int(timezone.now().timestamp())-4,
# "chain": {"current": 76,
# "timeout": 7,
# "max": 100,
# "modifier": 0.75,
# "cooldown": 0,
# "start": 1555211268
# }})
# return chain[sub] if sub is not None else chain
# return chain

# DEBUG API error
# return dict({"apiError": "API error code 42: debug error."})
Expand Down
1 change: 1 addition & 0 deletions yata/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'yata.context_processors.news',
'yata.context_processors.currentTimestamp',
],
},
},
Expand Down
53 changes: 5 additions & 48 deletions yata/static/perso/css/chain.css
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@ table.chain-armory-breakdown td.a { width: 50%; text-align: left; } /* x1 */
table.chain-armory-breakdown td.b { width: 25%; text-align: center; } /* x2 */


.progress-bar {
.live-bar {
background-color: lightgray;
border-radius: 4px;
width: 150px;
width: 100%;
height: 14px;
display: inline-block;
}

.progress-bar > span {
.live-bar > span {
display: block;
height: 100%;
border-radius: 4px;
Expand All @@ -118,7 +118,7 @@ table.chain-armory-breakdown td.b { width: 25%; text-align: center; } /* x2 */
font-weight: bold;
}

.progress-bar > span.strips:after {
.live-bar > span.strips:after {
content: "";
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
Expand All @@ -139,49 +139,6 @@ table.chain-armory-breakdown td.b { width: 25%; text-align: center; } /* x2 */
overflow: hidden;
}

.blue > span {
background-color: #7d93c4;
}

.violet > span {
.cooldown > span {
background-color: #c493c4;
}

@keyframes refresh {
0% {width: 100%; background-color: #f1a165;}
100% {width: 0%; background-color: #ff0000;}
}

.refresh > span {
animation: refresh 30s linear;
-webkit-animation: refresh 30s linear;
}

@keyframes refresh-content {
0% {content: "30s"}
10% {content: "27s"}
20% {content: "24s"}
30% {content: "21s"}
40% {content: "18s"}
50% {content: "15s"}
60% {content: "12s"}
70% {content: "9s"}
80% {content: "6s"}
90% {content: "3s"}
100% {content: "0s"}
}

.refresh > span.strips:after {
animation: refresh-content 30s linear;
-webkit-animation: refresh-content 30s linear;
}

@keyframes move-strips {
0% {width: 100%;}
100% {width: 0%;}
}

.move-strips > span.strips:after {
animation: move-strips 30s linear;
-webkit-animation: move-strips 30s linear;
}

0 comments on commit c18134c

Please sign in to comment.