From 71bfb67807445217b7789867189c34286bc27e0e Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 4 Dec 2024 12:07:45 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B1=D0=B0=D0=B3=D0=B0=20=D1=81?= =?UTF-8?q?=D1=82=D0=B0=D1=82=D0=B8=D1=81=D1=82=D0=B8=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/api.py b/api.py index fb30646..04715cd 100644 --- a/api.py +++ b/api.py @@ -5,6 +5,7 @@ from datetime import datetime, timedelta from statistics import median from uuid import uuid4 from random import randint +from time import sleep # Fix 3.3 + 0.15 = 3.4499999999999997 from decimal import Decimal as d @@ -24,12 +25,20 @@ def token_check(token): else: return False +############## STATS ################## + +STAT_RUN = False def stat_run(cdm_change): # Общий баланс, среднее значение, медиана # Низшее значение, высшее значение # Изменение баланса и time2cdm # Прирост за счёт алмазов, за счёт time2cdm + # Защита от конфликтов + global STAT_RUN + while STAT_RUN: + sleep(1) + STAT_RUN = True # Получаем все балансы db = read() bals = [] @@ -72,6 +81,9 @@ def stat_run(cdm_change): stat[date] = stats write(stat, 'stat.json') + STAT_RUN = False + +####################### class User_in_db(BaseModel): token: str @@ -333,7 +345,6 @@ class Add_time(BaseModel): def add_time(it: Add_time): token, id, time = it.token, it.id, int(it.time) if token_check(token): - stat_run(0) course = read('conf.json')['time2cdm'] amount = time*course # Пополнение баланса @@ -353,6 +364,8 @@ def add_time(it: Add_time): stat = read('stat.json') stat[date]['time2cdm'] += amount write(stat, 'stat.json') + + stat_run(0) return 'OK' else: return 'Error' @@ -452,10 +465,12 @@ def get_time(it: Get_time2cdm): class Get_stat(BaseModel): token: str - date: Optional[str] = datetime.today().strftime('%Y-%m-%d') + date: Optional[str] = None @app.post('/api/get_stat/') def get_stat(it: Get_stat): token, date = it.token, it.date + if not date: + date = datetime.today().strftime('%Y-%m-%d') if token_check(token): stat_run(0) db = read('stat.json')