165 lines
5.1 KiB
Python
165 lines
5.1 KiB
Python
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
|
|
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]
|
|
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()
|
|
xx = __.get('data', {}).get(user_id)
|
|
return xx
|
|
|
|
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'))
|
|
|
|
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()
|
|
|
|
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)
|
|
|
|
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 render_template(
|
|
'pages/account/technic.html',
|
|
account_technic=technic,
|
|
args={
|
|
'level': list_levels,
|
|
'sort': reverse,
|
|
'order': request.args.get('order', 'level'),
|
|
}
|
|
)
|
|
|
|
|
|
@pages_technic.route('/list.json')
|
|
def list_json():
|
|
if not g.user:
|
|
return redirect(url_for('pages_home.index'))
|
|
|
|
list_levels = []
|
|
print(request.args.getlist('level'))
|
|
if len(request.args.getlist('level')) > 0:
|
|
list_levels = []
|
|
for i in request.args.getlist('levelss'):
|
|
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)
|
|
|
|
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)
|
|
#technic = account_technic
|
|
return jsonify(technic=technic, levels=list_levels)
|
|
|
|
|
|
@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,
|
|
)
|