update
This commit is contained in:
parent
b6f8d378c8
commit
d4e996b33b
1 changed files with 65 additions and 42 deletions
|
@ -26,25 +26,30 @@ def get_stats_tokenized_users():
|
||||||
print 111
|
print 111
|
||||||
|
|
||||||
|
|
||||||
@celery.task(name='wotstats.tasks.hello')
|
@celery.task(name='wotstats.tasks.crontab_rush_preparation')
|
||||||
def hello():
|
def crontab_rush_preparation():
|
||||||
|
""""""
|
||||||
if Rush.query.filter_by(status='preparation').count() == 0:
|
if Rush.query.filter_by(status='preparation').count() == 0:
|
||||||
now = datetime.now().replace(minute=0, second=0)
|
now = datetime.now().replace(minute=0, second=0)
|
||||||
|
|
||||||
new_rush = Rush()
|
new_rush = Rush()
|
||||||
|
new_rush.at_start = now + timedelta(hours=1)
|
||||||
|
new_rush.at_finish = now + timedelta(hours=24)
|
||||||
try:
|
try:
|
||||||
new_rush.at_start = now + timedelta(hours=1)
|
|
||||||
new_rush.at_finish = now + timedelta(hours=24)
|
|
||||||
db.session.add(new_rush)
|
db.session.add(new_rush)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print e
|
print e
|
||||||
db.session.rollback()
|
db.session.rollback()
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
@celery.task(name='wotstats.tasks.crontab_rush_start')
|
||||||
|
def crontab_rush_start():
|
||||||
to_start = Rush.query.filter(
|
to_start = Rush.query.filter(
|
||||||
Rush.status == 'preparation',
|
Rush.status == 'preparation', Rush.at_start <= datetime.now())
|
||||||
Rush.at_start <= datetime.now()
|
|
||||||
)
|
|
||||||
if to_start.count() > 0:
|
if to_start.count() > 0:
|
||||||
p = to_start.first()
|
p = to_start.first()
|
||||||
# Count
|
# Count
|
||||||
|
@ -54,6 +59,7 @@ def hello():
|
||||||
if rush_accounts.count() == 0:
|
if rush_accounts.count() == 0:
|
||||||
p.status = 'canceled'
|
p.status = 'canceled'
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
return True
|
||||||
else:
|
else:
|
||||||
for ra in rush_accounts.all():
|
for ra in rush_accounts.all():
|
||||||
# Get current stats by ra.account_id
|
# Get current stats by ra.account_id
|
||||||
|
@ -82,47 +88,62 @@ def hello():
|
||||||
pass
|
pass
|
||||||
p.status = 'started'
|
p.status = 'started'
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
@celery.task(name='wotstats.tasks.crontab_rush_finish')
|
||||||
|
def crontab_rush_finish():
|
||||||
to_finish = Rush.query.filter(
|
to_finish = Rush.query.filter(
|
||||||
Rush.status == 'started',
|
Rush.status == 'started', Rush.at_finish <= datetime.now())
|
||||||
Rush.at_finish <= datetime.now()
|
if to_finish.count() == 0:
|
||||||
)
|
return False
|
||||||
if to_finish.count() > 0:
|
|
||||||
p = to_finish.first()
|
|
||||||
# Harvert stats for all rush members
|
|
||||||
for ra in RushAccounts.query.filter(RushAccounts.rush_id == p.id):
|
|
||||||
# TODO: Get WOT accoount data
|
|
||||||
# TODO: Get Stats from WOT server
|
|
||||||
# TODO: Save Stats
|
|
||||||
print ra.account_id
|
|
||||||
# Get current stats by ra.account_id
|
|
||||||
# Save battle stats to database
|
|
||||||
#
|
|
||||||
app_id = current_app.config['WG_APPLICATION_ID']
|
|
||||||
url = 'https://api.worldoftanks.ru/wot/account/info/'
|
|
||||||
__ = requests.get('{}?application_id={}&account_id={}'.format(url, app_id, ra.account_id)).json()
|
|
||||||
|
|
||||||
account_statistics = __.get('data', {}).get("{}".format(ra.account_id)).get('statistics', {})
|
finish_stats = dict()
|
||||||
account_data = __['data']["{}".format(ra.account_id)]['statistics']['all']
|
|
||||||
|
|
||||||
data = {
|
p = to_finish.first()
|
||||||
'battles': account_data['battles'],
|
# Harvert stats for all rush members
|
||||||
'wins': account_data['wins'],
|
for ra in RushAccounts.query.filter(RushAccounts.rush_id == p.id):
|
||||||
'losses': account_data['losses'],
|
# TODO: Get WOT accoount data
|
||||||
'draws': account_data['draws'],
|
# TODO: Get Stats from WOT server
|
||||||
}
|
# TODO: Save Stats
|
||||||
|
account_id = ra.account_id
|
||||||
|
# Get current stats by ra.account_id
|
||||||
|
# Save battle stats to database
|
||||||
|
#
|
||||||
|
app_id = current_app.config['WG_APPLICATION_ID']
|
||||||
|
url = 'https://api.worldoftanks.ru/wot/account/info/'
|
||||||
|
__ = requests.get('{}?application_id={}&account_id={}'.format(url, app_id, account_id)).json()
|
||||||
|
|
||||||
xx = RushAccounts.query.filter(
|
account_statistics = __.get('data', {}).get("{}".format(ra.account_id)).get('statistics', {})
|
||||||
RushAccounts.rush_id == p.id,
|
account_data = __['data']["{}".format(account_id)]['statistics']['all']
|
||||||
RushAccounts.account_id == ra.account_id
|
|
||||||
).first()
|
data = {
|
||||||
xx.finish_data = data
|
'battles': account_data['battles'],
|
||||||
db.session.commit()
|
'wins': account_data['wins'],
|
||||||
pass
|
'losses': account_data['losses'],
|
||||||
# TODO: ...
|
'draws': account_data['draws'],
|
||||||
p.status = 'finished'
|
}
|
||||||
|
|
||||||
|
xx = RushAccounts.query.filter(
|
||||||
|
RushAccounts.rush_id == p.id,
|
||||||
|
RushAccounts.account_id == account_id
|
||||||
|
).first()
|
||||||
|
xx.finish_data = data
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
|
finish_stats[account_id] = {
|
||||||
|
'wins': xx.finish_data['wins'] - account_data['wins'],
|
||||||
|
'battles': xx.finish_data['battles'] - account_data['battles']
|
||||||
|
}
|
||||||
|
|
||||||
|
result = sorted(finish_stats.items(), key=lambda t: t[1]['wins'], reverse=True)
|
||||||
|
pass
|
||||||
|
# TODO: ...
|
||||||
|
p.status = 'finished'
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
# time.sleep(3)
|
# time.sleep(3)
|
||||||
# current_app.log.error('wdwqd')
|
# current_app.log.error('wdwqd')
|
||||||
# with current_app.test_request_context() as request:
|
# with current_app.test_request_context() as request:
|
||||||
|
@ -143,7 +164,9 @@ def mail_welcome():
|
||||||
@celery.on_after_configure.connect
|
@celery.on_after_configure.connect
|
||||||
def setup_periodic_tasks(sender, **kwargs):
|
def setup_periodic_tasks(sender, **kwargs):
|
||||||
# Calls test('hello') every 10 seconds.
|
# Calls test('hello') every 10 seconds.
|
||||||
sender.add_periodic_task(10.0, hello.s(), name='add every 10')
|
sender.add_periodic_task(10.0, crontab_rush_preparation.s(), name='prepare every 10')
|
||||||
|
sender.add_periodic_task(10.0, crontab_rush_start.s(), name='start every 10')
|
||||||
|
sender.add_periodic_task(10.0, crontab_rush_finish.s(), name='finish every 10')
|
||||||
|
|
||||||
# Calls test('world') every 30 seconds
|
# Calls test('world') every 30 seconds
|
||||||
# sender.add_periodic_task(30.0, test.s('world'), expires=10)
|
# sender.add_periodic_task(30.0, test.s('world'), expires=10)
|
||||||
|
|
Reference in a new issue