def list(request, type): try: tId = request.session["player"].get("tId") if request.session.get('player') else -1 player = getPlayer(tId) awardsPlayer, awardsTorn, error = player.getAwards() userInfo = awardsPlayer.get('userInfo') summaryByType = awardsPlayer.get('summaryByType') pinnedAwards = awardsPlayer.get('pinnedAwards') if type in AWARDS_CAT: awards, awardsSummary = createAwards(awardsTorn, userInfo, type) graph = [] graph2 = [] for type, honors in awards.items(): for k, h in honors.items(): # if h.get("rarity", "Unknown Rarity") not in ["Unknown Rarity"]: if h.get("circulation", 0) > 0: if h.get("awardType") in ["Honor"]: graph.append([h.get("name", "?"), h.get("circulation", 0), int(h.get("achieve", 0)), h.get("img", ""), h.get("rScore", 0), h.get("unreach", 0)]) elif h.get("awardType") in ["Medal"]: graph2.append([h.get("name", "?"), h.get("circulation", 0), int(h.get("achieve", 0)), h.get("img", ""), h.get("rScore", 0), h.get("unreach", 0)]) graph = sorted(graph, key=lambda x: -x[1]) graph2 = sorted(graph2, key=lambda x: -x[1]) context = {"player": player, "view": {"awards": True}, "pinnedAwards": pinnedAwards, "awardscat": True, "awards": awards, "awardsSummary": awardsSummary, "summaryByType": summaryByType, "graph": graph, "graph2": graph2} if error: selectError = 'apiErrorSub' if request.method == 'POST' else 'apiError' context.update({selectError: error['apiError']}) page = 'awards/content-reload.html' if request.method == 'POST' else "awards.html" return render(request, page, context) else: awards = awardsPlayer.get('awards') graph = [] for k, h in sorted(awardsTorn.get("honors").items(), key=lambda x: x[1]["circulation"], reverse=True): if h.get("circulation", 0) > 0: graph.append([h.get("name", "?"), h.get("circulation", 0), int(h.get("achieve", 0)), h.get("img", ""), h.get("rScore", 0), h.get("unreach", 0)]) graph2 = [] for k, h in sorted(awardsTorn.get("medals").items(), key=lambda x: x[1]["circulation"], reverse=True): if h.get("circulation", 0) > 0: graph2.append([h.get("name", "?"), h.get("circulation", 0), int(h.get("achieve", 0)), h.get("img", ""), h.get("rScore", 0), h.get("unreach", 0)]) context = {"player": player, "view": {"awards": True}, "pinnedAwards": pinnedAwards, "awardscat": True, "awards": awards, "summaryByType": summaryByType, "graph": graph, "graph2": graph2} page = 'awards/content-reload.html' if request.method == 'POST' else "awards.html" return render(request, page, context) except Exception as e: return returnError(exc=e, session=request.session)
def getAwards(self, userInfo=dict({}), force=False): from awards.models import AwardsData from awards.functions import AWARDS_CAT from awards.functions import createAwards # get torn awards awardsTorn = AwardsData.objects.first().loadAPICall() error = False if self.tId > 0: if not len(userInfo): dbInfo = self.tmpreq_set.filter(type="awards").first() if dbInfo is not None: userInfo = json.loads(dbInfo.req) else: userInfo = dict({}) error = { 'apiError': "Your data can't be found in the database." } if not len(userInfo) or force: req = apiCall( 'user', '', 'personalstats,crimes,education,battlestats,workstats,perks,gym,networth,merits,profile,medals,honors,icons,bars,weaponexp,hof', self.getKey()) if 'apiError' not in req: self.awardsUpda = tsnow() defaults = {"req": json.dumps(req), "timestamp": tsnow()} try: self.tmpreq_set.update_or_create(type="awards", defaults=defaults) except BaseException as e: self.tmpreq_set.filter(type="awards").delete() self.tmpreq_set.update_or_create(type="awards", defaults=defaults) userInfo = req error = False else: error = req medals = awardsTorn["medals"] honors = awardsTorn["honors"] remove = [k for k, v in honors.items() if v["type"] == 1] for k in remove: del honors[k] myMedals = userInfo.get("medals_awarded", []) myHonors = userInfo.get("honors_awarded", []) awards = dict() summaryByType = dict({}) for type in AWARDS_CAT: awardsTmp, awardsSummary = createAwards(awardsTorn, userInfo, type) summaryByType[type.title()] = awardsSummary["All awards"] awards.update(awardsTmp) # get pinned pinnedAwards = {k: dict({}) for k in json.loads(self.awardsPinn)} # delete completed pinned awared todel = [] for aid in pinnedAwards: if aid[0] == "m" and aid[2:].isdigit() and int( aid[2:]) in myMedals: todel.append(aid) if aid[0] == "h" and aid[2:].isdigit() and int( aid[2:]) in myHonors: todel.append(aid) for aid in todel: del pinnedAwards[aid] self.awardsPinn = json.dumps([k for k in pinnedAwards]) i = len(pinnedAwards) for type, awardsTmp in awards.items(): for id in pinnedAwards: if id in awardsTmp: pinnedAwards[id] = awardsTmp[id] i -= 1 if not i: break summaryByType["AllAwards"] = { "nAwarded": len(myHonors) + len(myMedals), "nAwards": len(honors) + len(medals) } summaryByType["AllHonors"] = { "nAwarded": len(myHonors), "nAwards": len(honors) } summaryByType["AllMedals"] = { "nAwarded": len(myMedals), "nAwards": len(medals) } rScorePerso = 0.0 for k, v in awardsTorn["honors"].items(): if v.get("achieve", 0) == 1: rScorePerso += v.get("rScore", 0) for k, v in awardsTorn["medals"].items(): if v.get("achieve", 0) == 1: rScorePerso += v.get("rScore", 0) awardsPlayer = { "userInfo": userInfo, "awards": awards, "pinnedAwards": pinnedAwards, "summaryByType": dict({ k: v for k, v in sorted(summaryByType.items(), key=lambda x: x[1]['nAwarded'], reverse=True) }) } if self.tId > 0 and not error: self.awardsScor = int(rScorePerso * 10000) self.awardsNumb = len(myMedals) + len(myHonors) self.save() return awardsPlayer, awardsTorn, error
def list(request, type): try: if request.session.get('player'): print('[view.awards.list] get player id from session') tId = request.session["player"].get("tId") player = Player.objects.filter(tId=tId).first() player.lastActionTS = int(timezone.now().timestamp()) player.save() awardsJson = json.loads(player.awardsJson) print('[view.awards.list] award type: {}'.format(type)) tornAwards = Call.objects.first().load() userInfo = awardsJson.get('userInfo') summaryByType = awardsJson.get('summaryByType') if type in AWARDS_CAT: awards, awardsSummary = createAwards(tornAwards, userInfo, type) graph = [] graph2 = [] for type, honors in awards.items(): for k, h in honors.items(): # if h.get("rarity", "Unknown Rarity") not in ["Unknown Rarity"]: if h.get("circulation", 0) > 0: if h.get("awardType") in ["Honor"]: graph.append([ h.get("name", "?"), h.get("circulation", 0), int(h.get("achieve", 0)), h.get("img", ""), h.get("rScore", 0), h.get("unreach", 0) ]) elif h.get("awardType") in ["Medal"]: graph2.append([ h.get("name", "?"), h.get("circulation", 0), int(h.get("achieve", 0)), h.get("img", ""), h.get("rScore", 0), h.get("unreach", 0) ]) graph = sorted(graph, key=lambda x: -x[1]) graph2 = sorted(graph2, key=lambda x: -x[1]) context = { "player": player, "view": { "awards": True }, "awardscat": True, "awards": awards, "awardsSummary": awardsSummary, "summaryByType": summaryByType, "graph": graph, "graph2": graph2 } page = 'awards/list.html' if request.method == 'POST' else "awards.html" return render(request, page, context) elif type == "all": awards = awardsJson.get('awards') graph = [] updatePlayerAwards(player, tornAwards, userInfo) for k, h in sorted(tornAwards.get("honors").items(), key=lambda x: x[1]["circulation"], reverse=True): if h.get("circulation", 0) > 0: graph.append([ h.get("name", "?"), h.get("circulation", 0), int(h.get("achieve", 0)), h.get("img", ""), h.get("rScore", 0), h.get("unreach", 0) ]) graph2 = [] for k, h in sorted(tornAwards.get("medals").items(), key=lambda x: x[1]["circulation"], reverse=True): if h.get("circulation", 0) > 0: graph2.append([ h.get("name", "?"), h.get("circulation", 0), int(h.get("achieve", 0)), h.get("img", ""), h.get("rScore", 0), h.get("unreach", 0) ]) context = { "player": player, "view": { "awards": True }, "awardscat": True, "awards": awards, "summaryByType": summaryByType, "graph": graph, "graph2": graph2 } page = 'awards/content-reload.html' if request.method == 'POST' else "awards.html" return render(request, page, context) elif type == "hof": hof = [] hofGraph = [] for p in Player.objects.exclude(awardsInfo="N/A").all(): try: hof.append({ "player": p, "rscore": float(p.awardsInfo), # "nAwarded": json.loads(p.awardsJson)["summaryByType"]["AllHonors"]["nAwarded"], # "nAwards": json.loads(p.awardsJson)["summaryByType"]["AllHonors"]["nAwards"], "nAwarded": json.loads(p.awardsJson)["summaryByType"] ["AllAwards"]["nAwarded"], "nAwards": json.loads(p.awardsJson)["summaryByType"] ["AllAwards"]["nAwards"], }) hofGraph.append(float(p.awardsInfo)) except BaseException: print('[view.awards.list] error getting info on {}'. format(p)) awards = awardsJson.get('awards') graph = [] updatePlayerAwards(player, tornAwards, userInfo) for k, h in sorted(tornAwards.get("honors").items(), key=lambda x: x[1]["circulation"], reverse=True): if h.get("circulation", 0) > 0: graph.append([ h.get("name", "?"), h.get("circulation", 0), int(h.get("achieve", 0)), h.get("img", ""), h.get("rScore", 0), h.get("unreach", 0) ]) graph2 = [] for k, h in sorted(tornAwards.get("medals").items(), key=lambda x: x[1]["circulation"], reverse=True): if h.get("circulation", 0) > 0: graph2.append([ h.get("name", "?"), h.get("circulation", 0), int(h.get("achieve", 0)), h.get("img", ""), h.get("rScore", 0), h.get("unreach", 0) ]) bins = numpy.logspace(-2, 2, num=101) bins[0] = 0 histo, _ = numpy.histogram(hofGraph, bins=bins) cBins = [ 0.5 * float(a + b) for a, b in zip(bins[:-1], bins[1:]) ] hofGraph = [[ x, y, xm, xp, 0 ] for x, y, xm, xp in zip(cBins, histo, bins[:-1], bins[1:])] hofGraph[0][4] = hofGraph[0][1] for i in range(len(hofGraph) - 1): hofGraph[i + 1][4] = hofGraph[i + 1][1] + hofGraph[i][4] context = { "player": player, "view": { "hof": True }, "awardscat": True, "awards": awards, "summaryByType": summaryByType, "graph": graph, "graph2": graph2, "hof": sorted(hof, key=lambda x: -x["rscore"]), "hofGraph": hofGraph } page = 'awards/content-reload.html' if request.method == 'POST' else "awards.html" return render(request, page, context) else: return returnError(type=403, msg="You might want to log in.") except Exception: return returnError()
def list(request, type): try: if request.session.get('player'): print('[view.awards.list] get player id from session') tId = request.session["player"].get("tId") else: print('[view.awards.index] anon session') tId = -1 player = Player.objects.filter(tId=tId).first() player.lastActionTS = int(timezone.now().timestamp()) player.save() awardsJson = json.loads(player.awardsJson) print('[view.awards.list] award type: {}'.format(type)) tornAwards = AwardsData.objects.first().loadAPICall() userInfo = awardsJson.get('userInfo') summaryByType = awardsJson.get('summaryByType') if type in AWARDS_CAT: awards, awardsSummary = createAwards(tornAwards, userInfo, type) graph = [] graph2 = [] for type, honors in awards.items(): for k, h in honors.items(): # if h.get("rarity", "Unknown Rarity") not in ["Unknown Rarity"]: if h.get("circulation", 0) > 0: if h.get("awardType") in ["Honor"]: graph.append([ h.get("name", "?"), h.get("circulation", 0), int(h.get("achieve", 0)), h.get("img", ""), h.get("rScore", 0), h.get("unreach", 0) ]) elif h.get("awardType") in ["Medal"]: graph2.append([ h.get("name", "?"), h.get("circulation", 0), int(h.get("achieve", 0)), h.get("img", ""), h.get("rScore", 0), h.get("unreach", 0) ]) graph = sorted(graph, key=lambda x: -x[1]) graph2 = sorted(graph2, key=lambda x: -x[1]) context = { "player": player, "view": { "awards": True }, "awardscat": True, "awards": awards, "awardsSummary": awardsSummary, "summaryByType": summaryByType, "graph": graph, "graph2": graph2 } page = 'awards/list.html' if request.method == 'POST' else "awards.html" return render(request, page, context) elif type == "all": awards = awardsJson.get('awards') graph = [] updatePlayerAwards(player, tornAwards, userInfo) for k, h in sorted(tornAwards.get("honors").items(), key=lambda x: x[1]["circulation"], reverse=True): if h.get("circulation", 0) > 0: graph.append([ h.get("name", "?"), h.get("circulation", 0), int(h.get("achieve", 0)), h.get("img", ""), h.get("rScore", 0), h.get("unreach", 0) ]) graph2 = [] for k, h in sorted(tornAwards.get("medals").items(), key=lambda x: x[1]["circulation"], reverse=True): if h.get("circulation", 0) > 0: graph2.append([ h.get("name", "?"), h.get("circulation", 0), int(h.get("achieve", 0)), h.get("img", ""), h.get("rScore", 0), h.get("unreach", 0) ]) context = { "player": player, "view": { "awards": True }, "awardscat": True, "awards": awards, "summaryByType": summaryByType, "graph": graph, "graph2": graph2 } page = 'awards/content-reload.html' if request.method == 'POST' else "awards.html" return render(request, page, context) elif type == "hof": hof = [] playerInHOF = False for p in Player.objects.filter( awardsRank__lt=(HOF_SIZE + 1)).order_by('awardsRank'): try: playerInHOF = True if tId == p.tId else playerInHOF hof.append({ "player": p, "rscore": float(p.awardsScor / 10000.0), # "nAwarded": json.loads(p.awardsJson)["summaryByType"]["AllHonors"]["nAwarded"], # "nAwards": json.loads(p.awardsJson)["summaryByType"]["AllHonors"]["nAwards"], "nAwarded": json.loads(p.awardsJson)["summaryByType"]["AllAwards"] ["nAwarded"], "nAwards": json.loads(p.awardsJson)["summaryByType"]["AllAwards"] ["nAwards"], }) except BaseException: print('[view.awards.list] error getting info on {}'.format( p)) if not playerInHOF and player.tId > 0: hof.append({ "player": player, "rscore": float(player.awardsScor / 10000.0), # "nAwarded": json.loads(p.awardsJson)["summaryByType"]["AllHonors"]["nAwarded"], # "nAwards": json.loads(p.awardsJson)["summaryByType"]["AllHonors"]["nAwards"], "nAwarded": json.loads(player.awardsJson)["summaryByType"]["AllAwards"] ["nAwarded"], "nAwards": json.loads(player.awardsJson)["summaryByType"]["AllAwards"] ["nAwards"], }) awards = awardsJson.get('awards') graph = [] updatePlayerAwards(player, tornAwards, userInfo) for k, h in sorted(tornAwards.get("honors").items(), key=lambda x: x[1]["circulation"], reverse=True): if h.get("circulation", 0) > 0: graph.append([ h.get("name", "?"), h.get("circulation", 0), int(h.get("achieve", 0)), h.get("img", ""), h.get("rScore", 0), h.get("unreach", 0) ]) graph2 = [] for k, h in sorted(tornAwards.get("medals").items(), key=lambda x: x[1]["circulation"], reverse=True): if h.get("circulation", 0) > 0: graph2.append([ h.get("name", "?"), h.get("circulation", 0), int(h.get("achieve", 0)), h.get("img", ""), h.get("rScore", 0), h.get("unreach", 0) ]) context = { "player": player, "view": { "hof": True }, "awardscat": True, "awards": awards, "summaryByType": summaryByType, "graph": graph, "graph2": graph2, "hof": sorted(hof, key=lambda x: -x["rscore"]), "hofGraph": json.loads(AwardsData.objects.first().hofHistogram) } page = 'awards/content-reload.html' if request.method == 'POST' else "awards.html" return render(request, page, context) # else: # return returnError(type=403, msg="You might want to log in.") except Exception: return returnError()