def players(request, name): """ controls the player show """ while "undefined" in name: name = name[:-10] url = '/player_statistics/casual/nickname/' + name data = api_call.get_json(url) if data is not None: statsdict = data s = player.player_math(statsdict) ### Get Match history ### api.heroesofnewerth.com/match_history/ranked/accountid/123456/?token=yourtoken url = '/match_history/casual/nickname/' + name data = api_call.get_json(url) history = [] if data is not None: history = match.recent_matches(data, 9) ### Get Match History Data ### history_detail = player.match_history_data(history, s['id']) ### deliver to view ### t = loader.get_template('player.html') c = Context({'nick': name, 'stats': s, 'mdata': history_detail}) return HttpResponse(t.render(c)) else: t = loader.get_template('error.html') c = Context({'id': name}) return HttpResponse(t.render(c))
def match_history_data(history, account_id): """ this will take a player history and decide which matches need to be downloaded and pass them to a multimatch api call this will auto call the function to parse a single players match history """ url = '/multi_match/all/matchids/' plus = False count = 0 needed = [] for m in history: if not match.checkfile(m[0]): if plus: url = url + '+' + str(m[0]) else: url = url + str(m[0]) plus = True count += 1 needed.append(m) if count > 0: data = api_call.get_json(url) if data is not None: match.multimatch(data, needed) return get_player_from_matches(history, account_id) else: return get_player_from_matches(history, account_id) else: return get_player_from_matches(history, account_id)
def player_view(request, name, mode, url, p): exists = False if p.exists(): p = p.values()[0] exists = True tdelta = datetime.now() - datetime.strptime(str(p['updated']), "%Y-%m-%d %H:%M:%S") if tdelta.seconds + (tdelta.days * 86400) < 900: return render_to_response('player.html', {'stats': p, 'mode': mode, 'view': "player"}) else: if mode is "rnk": update_player_count() data = get_json(url) if data is not None: # if account id is 0 send fancy error page. if int(data['account_id']) is 0: return render_to_response('player_error.html', {'name': name}) p = player_math(data, mode) player_save(p, mode) return render_to_response('player.html', {'stats': p, 'mode': mode, 'view': "player"}) else: # server down, show old results if exists: return render_to_response('player.html', {'stats': p, 'mode': mode, 'view': "player", 'fallback': True}) else: return error(request, "S2 server down or name is incorrect. Try another name or gently refreshing the page.")
def update_player(pid, mode): if mode == 'rnk': url = "/player_statistics/ranked/accountid/" + str(pid) elif mode == 'cs': url = '/player_statistics/casual/accountid/' + str(pid) elif mode == 'acc': url = '/player_statistics/public/accountid/' + str(pid) data = api_call.get_json(url) p = player_math(data, mode) player_save(p, mode)
def player_hero_stats(request, name, hero, mode, modename): search = PlayerHeroStats.objects.filter(nickname=name, hero_id=hero, mode=mode) if search.exists(): tdelta = datetime.now() - datetime.strptime(str(search.values('updated')[0]['updated']), "%Y-%m-%d %H:%M:%S") if tdelta.seconds + (tdelta.days * 86400) < 1000: data = json.loads(search.values('data')[0]['data']) else: url = "/hero_statistics/" + modename + "/nickname/" + name + "/heroid/" + hero data = api_call.get_json(url) search.delete() PlayerHeroStats(nickname=name, hero_id=hero, data=json.dumps(data), mode=mode).save() else: url = "/hero_statistics/" + modename + "/nickname/" + name + "/heroid/" + hero data = api_call.get_json(url) PlayerHeroStats(nickname=name, hero_id=hero, data=json.dumps(data), mode=mode).save() if data is not None: if mode is "acc": mode = "" else: mode = mode + '_' hero = {} hero['id'] = data[0]['hero_id'] hero['wins'] = data[0][mode+ 'ph_wins'] hero['losses'] = data[0][mode+ 'ph_losses'] hero['percent'] = int(divided(data[0][mode+ 'ph_wins'], data[0][mode+ 'ph_used']) * 100) hero['kills'] = round(divided(data[0][mode+ 'ph_herokills'], data[0][mode+ 'ph_used']), 1) hero['deaths'] = round(divided(data[0][mode+ 'ph_deaths'], data[0][mode+ 'ph_used']), 1) hero['kdr'] = round(divided(data[0][mode+ 'ph_herokills'], data[0][mode+ 'ph_deaths']), 1) hero['consume'] = round(divided(data[0][mode+ 'ph_consumables'], data[0][mode+ 'ph_used']), 1) hero['gpm'] = round(divided(data[0][mode+ 'ph_gold'], divided(data[0][mode+ 'ph_secs'], 60)), 1) hero['apm'] = round(divided(data[0][mode+ 'ph_actions'], divided(data[0][mode+ 'ph_secs'], 60)), 1) hero['exp'] = round(divided(data[0][mode+ 'ph_exp'], divided(data[0][mode+ 'ph_secs'], 60)), 1) hero['cs'] = round(divided(data[0][mode+ 'ph_teamcreepkills'], data[0][mode+ 'ph_used']), 1) hero['cd'] = round(divided(data[0][mode+ 'ph_denies'], data[0][mode+ 'ph_used']), 1) hero['wards'] = round(divided(data[0][mode+ 'ph_wards'], data[0][mode+ 'ph_used']), 1) return render_to_response('player_hero_stats.html', {'hero': hero}) else: return HttpResponse('Error! Returned no stats with this hero.')
def match(match_id): """ initial hit for match view makes some decisions based on if the match is parsed already """ if checkfile(match_id): return prepare_match(load_match(match_id), match_id) else: url = '/multi_match/all/matchids/' + str(match_id) data = api_call.get_json(url) h = [[str(match_id), '1/1/1']] if data is not None: multimatch(data, h) return match(match_id) else: return None
def match_view(request, match_id): """ /match/####/ url leads here """ match = Matches.objects.filter(match_id=match_id) if match.exists(): team1, team2 = [], [] # get match and setup data for view match = match.values()[0] # get players and setup for view players = PlayerMatches.objects.filter(match_id=match_id).order_by('position').values() for player in players: player['cli_name'] = HeroData.objects.get(hero_id=player['hero']).cli_name player['items'] = json.loads(player['items']) if player['kdr'] == 999: player['kdr'] = "Inf" if player['team'] == 1: team1.append(player) else: team2.append(player) if len(team1) > 0: t1exist = True else: t1exist = False if len(team2) > 0: t2exist = True else: t2exist = False thread.start_new_thread(update_check, (player['player_id'], match['mode'])) return render_to_response('match.html', {'match_id': match_id, 'match': match, 'players': players, 'team1': team1, 'team2': team2, 't1exist': t1exist, 't2exist': t2exist}) else: # grab solo match for f***s sake url = '/multi_match/all/matchids/' + str(match_id) data = api_call.get_json(url) h = [str(match_id)] if data is not None: mode = "rnk" if int(data[0][0]['cas']) == 1: mode = "cs" if int(data[0][0]['nl']) == 0: mode = "acc" multimatch(data, h, mode) return match_view(request, match_id) else: return error(request, "S2 Servers down or match id is incorrect. Try another match or gently refreshing the page.")
def verify_matches(data, mode): """ this checks for matches to exist in the database. If they do not exist they are then downloaded. """ findexisting = Matches.objects.filter(match_id__in=data).values('match_id') existing = set([int(match['match_id']) for match in findexisting]) missing = [x for x in data if x not in existing] # if any are missing FIND THEM if len(missing) != 0: strmatches = '' for match in missing: strmatches += str(match) if match != missing[-1]: strmatches += '+' url = '/multi_match/all/matchids/' + strmatches raw = get_json(url) if raw != None: multimatch(raw, missing, mode)
def banner_view(request, name): location = directory + str(name) + ".png" # check file exists if path.isfile(location): # check file age now = time() fileCreation = path.getctime(location) day_ago = now - 60*60*24 if fileCreation < day_ago: remove(location) return banner_view(request, name) else: # older than day remove response = HttpResponse(mimetype="image/png") img = Image.open(location) img.save(response, 'png') return response else: p = PlayerStats.objects.filter(nickname=name) url = '/player_statistics/ranked/nickname/' + name if p.exists(): tdelta = datetime.now() - datetime.strptime(str(p.values()[0]['updated']), "%Y-%m-%d %H:%M:%S") if tdelta.seconds + (tdelta.days * 86400) < 1000: s = p.values()[0] response = HttpResponse(mimetype="image/png") img = banner(s) img.save(response, 'png') img.save(directory + str(name) + ".png") return response data = get_json(url) if data is not None: statsdict = data s = player_math(statsdict, "rnk") player_save(s, "rnk") response = HttpResponse(mimetype="image/png") img = banner(s) img.save(response, 'png') img.save(directory + str(name) + ".png") return response else: response = HttpResponse() response.status_code = 404 return response
def match_view(request, match_id): """ /match/####/ url leads here """ match = Matches.objects.filter(match_id=match_id)[:1] if match.exists(): match = match[0] team1, team2 = [], [] # get players and setup for view PMObj = pmoselect(match.mode) players = PMObj.objects.filter(match_id=match_id).order_by('position') for player in players: player.items = loads(player.items) if player.team == 1: team1.append(player) else: team2.append(player) if len(team1) > 0: t1exist = True else: t1exist = False if len(team2) > 0: t2exist = True else: t2exist = False start_new_thread(update_check, (player.player_id, match.mode)) return render_to_response('match.html', {'match_id': match_id, 'match': match, 'players': players, 'team1': team1, 'team2': team2, 't1exist': t1exist, 't2exist': t2exist}) else: # grab solo match for f***s sake url = '/multi_match/all/matchids/' + str(match_id) data = get_json(url) h = [str(match_id)] if data is not None: mode = "rnk" if int(data[0][0]['cas']) == 1: mode = "cs" if int(data[0][0]['nl']) == 0: mode = "acc" multimatch(data, h, mode) return match_view(request, match_id) else: return error(request, "S2 Servers down or match id is incorrect. Try another match or gently refreshing the page.")
def update_history(url, account_id, mode, exists): """ Updates a player's history and saves it to db. [] is saved if no result """ raw = get_json(url) # if no recent matches just save an empty array try: raw = raw[0]['history'] except: # try to fallback if api down if exists: data = loads(PlayerHistory.objects.filter(player_id=account_id, mode=mode).values()[0]['history']) PlayerHistory(player_id=account_id, history=dumps(data), mode=mode).save() return data else: PlayerHistory(player_id=account_id, history=dumps([]), mode=mode).save() return [] data = [] for match in raw.split(','): if len(match) > 20: # this fixes an error on broken player histories data.append(int(match.split('|')[0])) PlayerHistory(player_id=account_id, history=dumps(data[::-1]), mode=mode).save() return data[::-1]