This repository has been archived on 2025-01-27. You can view files and clone it, but cannot push or open issues or pull requests.
wot_stats_server/wotstats/views/technic.py

165 lines
5.1 KiB
Python
Raw Normal View History

import requests
from flask import (
g, Blueprint, render_template, abort, current_app, redirect,
redirect, request, url_for, session, flash, jsonify
)
from jinja2 import TemplateNotFound
from wotstats.openid import oid
from wotstats.log import log
from wotstats.database import db
from wotstats.models import User
2017-12-11 05:14:18 +03:00
from wotstats.lib import parse_wargaming_openid_url, get_player_personal_data, statistics as texts_statistics
from .technic_list import t
pages_technic = Blueprint(
'technic', __name__, url_prefix='/technic', template_folder='templates')
def __get_technic():
log.debug(session)
user_id = parse_wargaming_openid_url(session['openid'])[0]
2017-12-11 05:14:18 +03:00
g.wg_user_id = user_id
url = "https://api.worldoftanks.ru/wot/account/tanks/"
payload = {
"application_id": current_app.config['WG_APPLICATION_ID'],
"account_id": user_id
}
__ = requests.get(url, params=payload).json()
2017-12-16 16:40:33 +03:00
xx = __.get('data', {}).get(user_id)
return xx
2017-12-11 05:14:18 +03:00
def __get_technic_user_tank(tank_id):
# ?application_id=502910c1c785c3c7ca2e83c9e89bde02&account_id=69552613&tank_id=5121&r_realm=ru&run=1
# ?application_id=502910c1c785c3c7ca2e83c9e89bde02&account_id=69552613
user_id = parse_wargaming_openid_url(session['openid'])[0]
g.wg_user_id = user_id
url = "https://api.worldoftanks.ru/wot/tanks/stats/"
payload = {
"application_id": current_app.config['WG_APPLICATION_ID'],
"account_id": user_id,
"tank_id": tank_id,
}
__ = requests.get(url, params=payload).json()
return __.get('data', {}).get(user_id)
@pages_technic.route('/')
def index():
"""Summary statistics"""
if not g.user:
return redirect(url_for('pages_home.index'))
2017-12-16 16:40:33 +03:00
list_levels = []
print(request.args.getlist('level[]'))
if len(request.args.getlist('level[]')) > 0:
list_levels = []
for i in request.args.getlist('level[]'):
list_levels.append(int(i))
# TODO: total accounts
#
account_technic = list()
2017-12-16 16:40:33 +03:00
for x in __get_technic():
2017-12-16 16:40:33 +03:00
if len(list_levels) == 0:
x['details'] = t.get(str(x['tank_id']))
account_technic.append(x)
else:
if t.get(str(x['tank_id'])).get('level') in list_levels:
x['details'] = t.get(str(x['tank_id']))
account_technic.append(x)
order_keys = {
2018-01-13 13:13:28 +03:00
'level': lambda data: data['details'].get('level', 0),
2017-12-16 16:40:33 +03:00
'nation': lambda data: data['details']['nation'],
'type': lambda data: data['details']['type'],
'name': lambda data: data['details']['name_i18n'],
'battles': lambda data: data['statistics']['battles'],
'wins': lambda data: data['statistics']['wins'],
}
if request.args.get('order', 'level') in order_keys:
order = order_keys[request.args.get('order', 'level')]
else:
order = order_keys['level']
reverse = False if request.args.get('sort') == 'asc' else True
technic = sorted(account_technic, key=order, reverse=reverse)
return render_template(
'pages/account/technic.html',
2017-12-16 16:40:33 +03:00
account_technic=technic,
args={
'level': list_levels,
'sort': reverse,
'order': request.args.get('order', 'level'),
}
)
2017-10-29 17:31:48 +03:00
@pages_technic.route('/list.json')
def list_json():
if not g.user:
return redirect(url_for('pages_home.index'))
list_levels = []
2017-12-16 16:40:33 +03:00
print(request.args.getlist('level'))
if len(request.args.getlist('level')) > 0:
2017-10-29 17:31:48 +03:00
list_levels = []
2017-12-16 16:40:33 +03:00
for i in request.args.getlist('levelss'):
2017-10-29 17:31:48 +03:00
list_levels.append(int(i))
# TODO: total accounts
#
account_technic = list()
for x in __get_technic():
if len(list_levels) == 0:
x['details'] = t.get(str(x['tank_id']))
account_technic.append(x)
else:
if t.get(str(x['tank_id'])).get('level') in list_levels:
x['details'] = t.get(str(x['tank_id']))
account_technic.append(x)
2017-12-16 16:40:33 +03:00
order_keys = {
'level': lambda data: data['details']['level'],
'nation': lambda data: data['details']['nation'],
'type': lambda data: data['details']['type'],
'name': lambda data: data['details']['name_i18n'],
'battles': lambda data: data['statistics']['battles'],
'wins': lambda data: data['statistics']['wins'],
}
if request.args.get('order', 'level') in order_keys:
order = order_keys[request.args.get('order', 'level')]
else:
order = order_keys['level']
reverse = False if request.args.get('sort') == 'asc' else True
technic = sorted(account_technic, key=order, reverse=reverse)
return jsonify(technic=technic, levels=list_levels)
2017-12-11 05:14:18 +03:00
@pages_technic.route('/<int:user_id>/<int:tank_id>/')
def user_tech_stats(user_id, tank_id):
"""Summary statistics"""
if not g.user:
return redirect(url_for('pages_home.index'))
print(g.user)
# TODO: total accounts
#
tank_stats = __get_technic_user_tank(tank_id)
return render_template(
'pages/account/technic_user_tank.html',
tank_stats=tank_stats[0],
texts_statistics=texts_statistics,
)