|
|
|
@ -1,5 +1,7 @@
|
|
|
|
|
from fastapi import FastAPI, HTTPException
|
|
|
|
|
from pydantic import BaseModel
|
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
from statistics import median
|
|
|
|
|
|
|
|
|
|
# Fix 3.3 + 0.15 = 3.4499999999999997
|
|
|
|
|
from decimal import Decimal as d
|
|
|
|
@ -19,6 +21,23 @@ def token_check(token):
|
|
|
|
|
else:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
def stat_run(cdm_change):
|
|
|
|
|
stat = read('stat.json')
|
|
|
|
|
date = datetime.today().strftime('%Y-%m-%d')
|
|
|
|
|
if date in stat:
|
|
|
|
|
stat[date]['gbal'] += cdm_change
|
|
|
|
|
write(stat, 'stat.json')
|
|
|
|
|
else:
|
|
|
|
|
db = read()
|
|
|
|
|
bal = 0
|
|
|
|
|
for id in db['id']:
|
|
|
|
|
bal += db['id'][id]['bal']
|
|
|
|
|
stat[date] = {'gbal': bal, 'time2cdm': 0}
|
|
|
|
|
write(stat, 'stat.json')
|
|
|
|
|
|
|
|
|
|
def format_sign(number):
|
|
|
|
|
return f"+{number}" if number > 0 else f"{number}"
|
|
|
|
|
|
|
|
|
|
class User_in_db(BaseModel):
|
|
|
|
|
token: str
|
|
|
|
|
id: str = None
|
|
|
|
@ -74,7 +93,7 @@ def user_add(it: User_add):
|
|
|
|
|
print(id)
|
|
|
|
|
if token_check(token):
|
|
|
|
|
db = read()
|
|
|
|
|
db['id'][id] = {'tg': tg, 'ds': ds, 'mine': mine, 'nick': nick, 'passwd': passwd, 'bal': 0.0}
|
|
|
|
|
db['id'][id] = {'tg': tg, 'ds': ds, 'mine': mine, 'nick': nick, 'passwd': passwd, 'bal': 0.0, 'time': 0}
|
|
|
|
|
db['nick'][nick] = id
|
|
|
|
|
if tg:
|
|
|
|
|
db['tg'][tg] = id
|
|
|
|
@ -120,6 +139,7 @@ def coins_add(it: Coins_add):
|
|
|
|
|
db = read()
|
|
|
|
|
db['id'][id]['bal'] = fix_add(db['id'][id]['bal'], amount)
|
|
|
|
|
write(db)
|
|
|
|
|
stat_run(amount)
|
|
|
|
|
return 'OK'
|
|
|
|
|
else:
|
|
|
|
|
return 'Error'
|
|
|
|
@ -135,6 +155,7 @@ def coins_del(it: Coins_del):
|
|
|
|
|
db = read()
|
|
|
|
|
db['id'][id]['bal'] = fix_sub(db['id'][id]['bal'], amount)
|
|
|
|
|
write(db)
|
|
|
|
|
stat_run(amount*-1)
|
|
|
|
|
return 'OK'
|
|
|
|
|
else:
|
|
|
|
|
return 'Error'
|
|
|
|
@ -257,7 +278,7 @@ class Update_passwd(BaseModel):
|
|
|
|
|
id: str
|
|
|
|
|
passwd: str
|
|
|
|
|
@app.post('/api/update_passwd/')
|
|
|
|
|
def update_tg(it: Update_passwd):
|
|
|
|
|
def update_passwd(it: Update_passwd):
|
|
|
|
|
token, id, passwd = it.token, it.id, it.passwd
|
|
|
|
|
if token_check(token):
|
|
|
|
|
db = read()
|
|
|
|
@ -267,6 +288,37 @@ def update_tg(it: Update_passwd):
|
|
|
|
|
else:
|
|
|
|
|
return 'Error'
|
|
|
|
|
|
|
|
|
|
class Update_time(BaseModel):
|
|
|
|
|
token: str
|
|
|
|
|
id: str
|
|
|
|
|
time: str
|
|
|
|
|
@app.post('/api/update_time/')
|
|
|
|
|
def update_time(it: Update_time):
|
|
|
|
|
token, id, time = it.token, it.id, int(it.time)
|
|
|
|
|
if token_check(token):
|
|
|
|
|
db = read()
|
|
|
|
|
db['id'][id]['time'] = time
|
|
|
|
|
write(db)
|
|
|
|
|
return 'OK'
|
|
|
|
|
else:
|
|
|
|
|
return 'Error'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Add_time(BaseModel):
|
|
|
|
|
token: str
|
|
|
|
|
id: str
|
|
|
|
|
time: str
|
|
|
|
|
@app.post('/api/add_time/')
|
|
|
|
|
def add_time(it: Add_time):
|
|
|
|
|
token, id, time = it.token, it.id, int(it.time)
|
|
|
|
|
if token_check(token):
|
|
|
|
|
db = read()
|
|
|
|
|
db['id'][id]['time'] += time
|
|
|
|
|
write(db)
|
|
|
|
|
return 'OK'
|
|
|
|
|
else:
|
|
|
|
|
return 'Error'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Check_bal(BaseModel):
|
|
|
|
|
token: str
|
|
|
|
@ -340,6 +392,66 @@ def get_passwd(it: Get_passwd):
|
|
|
|
|
else:
|
|
|
|
|
return 'Error'
|
|
|
|
|
|
|
|
|
|
class Get_time(BaseModel):
|
|
|
|
|
token: str
|
|
|
|
|
id: str
|
|
|
|
|
@app.post('/api/get_time/')
|
|
|
|
|
def get_time(it: Get_time):
|
|
|
|
|
token, id = it.token, it.id
|
|
|
|
|
if token_check(token):
|
|
|
|
|
db = read()
|
|
|
|
|
return db['id'][id]['time']
|
|
|
|
|
else:
|
|
|
|
|
return 'Error'
|
|
|
|
|
|
|
|
|
|
class Get_stat(BaseModel):
|
|
|
|
|
token: str
|
|
|
|
|
@app.post('/api/get_stat/')
|
|
|
|
|
def get_stat(it: Get_stat):
|
|
|
|
|
token = it.token
|
|
|
|
|
if token_check(token):
|
|
|
|
|
# Общий баланс, среднее значение, медиана
|
|
|
|
|
# Низшее значение, высшее значение
|
|
|
|
|
# Изменение баланса и time2cdm
|
|
|
|
|
# Прирост за счёт алмазов, за счёт time2cdm
|
|
|
|
|
stat_run(0)
|
|
|
|
|
stats = {'gbal': 0, 'average': 0, 'median': 0, 'time2cdm': 0,
|
|
|
|
|
'gbal_delta': '0', 'time2cdm_delta': '',
|
|
|
|
|
'min': 0, 'max': 0,
|
|
|
|
|
'up_diamond': 0, 'up_time2cdm': 0}
|
|
|
|
|
|
|
|
|
|
stat = read('stat.json')
|
|
|
|
|
date = datetime.today().strftime('%Y-%m-%d')
|
|
|
|
|
yesterday = (datetime.today() - timedelta(days=1)).strftime('%Y-%m-%d')
|
|
|
|
|
if yesterday in stat:
|
|
|
|
|
gbal_y = stat[yesterday]['gbal']
|
|
|
|
|
time2cdm_y = stat[yesterday]['time2cdm']
|
|
|
|
|
else:
|
|
|
|
|
gbal_y, time2cdm_y = 0, 0
|
|
|
|
|
gbal = stat[date]['gbal']
|
|
|
|
|
time2cdm = stat[date]['time2cdm']
|
|
|
|
|
|
|
|
|
|
# Получаем все балансы
|
|
|
|
|
db = read()
|
|
|
|
|
bals = []
|
|
|
|
|
for id in db['id']:
|
|
|
|
|
bals.append(db['id'][id]['bal'])
|
|
|
|
|
|
|
|
|
|
# Заполняем данные
|
|
|
|
|
stats['gbal'] = round(gbal, 3)
|
|
|
|
|
stats['average'] = round(sum(bals)/len(bals), 3)
|
|
|
|
|
stats['median'] = round(median(bals), 3)
|
|
|
|
|
stats['time2cdm'] = round(time2cdm, 3)
|
|
|
|
|
stats['gbal_delta'] = format_sign(round(gbal-gbal_y, 3))
|
|
|
|
|
stats['time2cdm_delta'] = format_sign(round(time2cdm-time2cdm_y, 3))
|
|
|
|
|
stats['min'] = round(min([x for x in bals if x != 0]), 3)
|
|
|
|
|
stats['max'] = round(max(bals), 3)
|
|
|
|
|
stats['up_diamond'] = round(gbal - (gbal_y + time2cdm + time2cdm_y), 3)
|
|
|
|
|
|
|
|
|
|
return stats
|
|
|
|
|
else:
|
|
|
|
|
return 'Error'
|
|
|
|
|
|
|
|
|
|
# TODO: unauth from account
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|