-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathmodels.py
74 lines (63 loc) · 2.85 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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/>.
import json
from django.db import models
from django.utils import timezone
from awards.functions import HONORS_UNREACH, computeRarity
from setup.functions import randomKey
from yata.handy import apiCall
class AwardsData(models.Model):
timestamp = models.IntegerField(default=0)
apicall = models.TextField(default="{}")
hofHistogram = models.TextField(default="{}")
def __str__(self):
return "Awards Data #{}".format(self.pk)
def updateApiCall(self):
req = apiCall("torn", "", "medals,honors", randomKey())
if "apiError" in req:
print(req["apiError"])
else:
self.timestamp = int(timezone.now().timestamp())
popTotal = 0
for awardType in ["honors", "medals"]:
to_del = []
for k, v in req[awardType].items():
circulation = int(req[awardType][k].get("circulation", 0))
if v.get("type") in [1]:
to_del.append(k)
elif awardType == "medals" and v["type"] == "CRM" and int(k) < 241:
# crimes 1.0
to_del.append(k)
else:
if circulation > 1:
popTotal += 1.0 / computeRarity(circulation)
if awardType in ["honors"]:
# req[awardType][k]["img"] = "https://awardimages.torn.com/{}.png".format(d.get(int(k), 0))
req[awardType][k]["unreach"] = 1 if int(k) in HONORS_UNREACH else 0
# elif awardType in ["medals"]:
# req[awardType][k]["img"] = "{}".format(k)
for k in to_del:
del req[awardType][k]
for awardType in ["honors", "medals"]:
for k, v in req[awardType].items():
req[awardType][k]["id"] = int(k)
if v["circulation"] > 1:
req[awardType][k]["rScore"] = 100.0 / computeRarity(v["circulation"]) / popTotal
self.apicall = json.dumps(req)
self.save()
def loadAPICall(self):
return json.loads(self.apicall)