added statistics

This commit is contained in:
hopelessjames 2023-06-19 17:09:04 +10:00
parent 2357ebe6bd
commit 61075491ec
3 changed files with 62 additions and 35 deletions

View File

@ -1,16 +1,21 @@
from aiogram import types, Dispatcher from aiogram import types, Dispatcher
from sqlalchemy.orm.session import close_all_sessions from sqlalchemy.orm.session import close_all_sessions
from create_bot import bot, dp from create_bot import bot, dp
from sqdb import user, session from sqdb import user, session, DickRequest
import random import random
import traceback
from random import randint from random import randint
import datetime import datetime
from dicktxt import ForReadDict from dicktxt import ForReadDict
size_change = None
dick_plus = None
dick_minus = None
@dp.message_handler(commands=["dick"]) @dp.message_handler(commands=["dick"])
async def up_dick(message: types.Message): async def up_dick(message: types.Message): # рандомайзер
# рандомайзер
numb = randint(-6, 10) numb = randint(-6, 10)
global size_change, dick_plus, dick_minus global size_change, dick_plus, dick_minus
dick_plus = ('<b>вырос</b>', '<b>подрос</b>', '<b>привстал</b>') dick_plus = ('<b>вырос</b>', '<b>подрос</b>', '<b>привстал</b>')
@ -18,25 +23,27 @@ async def up_dick(message: types.Message):
if numb > 0: if numb > 0:
size_change = random.choice(dick_plus) + '🚀' size_change = random.choice(dick_plus) + '🚀'
# if numb = 0 size_change = random.choice(const) TODO # if numb = 0 size_change = random.choice(const) TODO
else: size_change = random.choice(dick_minus) + '' else:
size_change = random.choice(dick_minus) + ''
# работа с таблицей # работа с таблицей
try: try:
dick_request = DickRequest(size_change=numb, date_changed=datetime.datetime.now())
b = session.query(user).filter(user.user_id == message.from_user.id).first() b = session.query(user).filter(user.user_id == message.from_user.id).first()
if b: if b:
last_time = b.datetimes last_time = b.date_updated
now_time = datetime.datetime.now() now_time = datetime.datetime.now()
diff = now_time.day - last_time.day diff = now_time.day - last_time.day
if abs(diff) <= 0: if abs(diff) <= 0:
try: try:
current_dick = b.dick_size current_dick = b.dick_size
await bot.send_message(message.chat.id, f'@{message.from_user.username},'
f'🚫 вы уже крутили пипису, '
await bot.send_message(message.chat.id, f'@{message.from_user.username},🚫 вы уже крутили пипису, ее размер <b>{current_dick}</b> см! ') f'ее размер <b>{current_dick}</b> см! ')
except: except Exception as e:
print('ошибка в if diff <=0') print('ошибка в if diff <=0 ' + "\n".join(traceback.format_tb(e.__traceback__)))
session.rollback() session.rollback()
finally: finally:
session.close_all() session.close_all()
@ -44,41 +51,51 @@ async def up_dick(message: types.Message):
else: else:
try: try:
a = session.query(user).filter(user.user_id == message.from_user.id).first() # запрос на поиск в таблице usr = session.query(user)
updetedDick = a.dick_size = (a.dick_size + numb) a = usr.filter(user.user_id == message.from_user.id).first() # запрос на поиск в таблице
a.datetimes = datetime.datetime.now() updated_dick = a.dick_size = (a.dick_size + numb)
dick_request.user = a
a.date_updated = datetime.datetime.now()
session.add(dick_request)
session.flush() session.flush()
session.commit() # обновляет запись session.commit() # обновляет запись
randomword = ForReadDict.RandomDick() randomword = ForReadDict.RandomDick()
await bot.send_message(message.chat.id, f'@{message.from_user.username}, ваш <em>{randomword}</em> {size_change}\ await bot.send_message(message.chat.id, f'@{message.from_user.username}, '
на <b>{abs(numb)}</b> см!\n\ f'ваш <em>{randomword}</em> {size_change}'
Теперь он равен <b>{updetedDick}</b> см! ') f'на <b>{abs(numb)}</b> см!\n'
except: f'Теперь он равен <b>{updated_dick}</b> см! ')
except Exception as e:
session.rollback() session.rollback()
print('ошибка в обновлении пиписы') print('ошибка в обновлении пиписы, трейсбек-бек-бек: ' +
"\n".join(traceback.format_tb(e.__traceback__)))
finally: finally:
session.close_all() session.close_all()
print('пиписа обновлена') print('пиписа обновлена')
else: else:
session.close() session.close()
try: try:
int_table = user(user_id = message.from_user.id, user_fullname = message.from_user.full_name, dick_size = numb, datetimes = datetime.datetime.now(),\ int_table = user(user_id=message.from_user.id,
user_fullname=message.from_user.full_name,
dick_size=numb,
date_updated=datetime.datetime.now(),
date_created=datetime.datetime.now(),
chat_id=message.chat.id) chat_id=message.chat.id)
dick_request.user = int_table
session.add(int_table) session.add(int_table)
session.add(dick_request)
session.flush() session.flush()
session.commit() session.commit()
session.close() session.close()
await bot.send_message(message.chat.id, f'@{message.from_user.username}, '
await bot.send_message(message.chat.id, f'@{message.from_user.username}, ваш писюн {size_change}\n\ f'ваш писюн {size_change}\n'
на <b>{abs(numb)}</b> см!\n\ f'на <b>{abs(numb)}</b> см!\n'
Теперь он равен <b>{numb}</b> см!') f'Теперь он равен <b>{numb}</b> см!')
except: except Exception as e:
print('ошибка в добавлении нового пользователя') print('ошибка в добавлении нового пользователя, трейсбек: ' +
'\n'.join(traceback.format_tb(e.__traceback__)))
session.rollback() session.rollback()
finally: finally:
session.close_all() session.close_all()
@ -95,6 +112,7 @@ async def up_dick(message: types.Message):
async def send_topchat(message: types.Message): async def send_topchat(message: types.Message):
try: try:
changechat_id = session.query(user).filter(user.user_id == message.from_user.id).first() changechat_id = session.query(user).filter(user.user_id == message.from_user.id).first()
delimiter = ': '
chats = message.chat.id chats = message.chat.id
if changechat_id.chat_id != chats: if changechat_id.chat_id != chats:
changechat_id.chat_id = chats changechat_id.chat_id = chats
@ -103,7 +121,6 @@ async def send_topchat(message: types.Message):
else: else:
session.close() session.close()
top = session.query(user.user_fullname, user.dick_size).order_by(user.dick_size.desc()).filter(user.chat_id == chats).limit(10).all() top = session.query(user.user_fullname, user.dick_size).order_by(user.dick_size.desc()).filter(user.chat_id == chats).limit(10).all()
delimiter = ': '
# проверка на длину списка, если меньше limit, то: # проверка на длину списка, если меньше limit, то:

14
sqdb.py
View File

@ -1,7 +1,7 @@
from sqlalchemy import create_engine, MetaData, Table, Integer, String, BIGINT, \ from sqlalchemy import create_engine, MetaData, Table, Integer, String, BIGINT, \
Column, DateTime, ForeignKey, Numeric Column, DateTime, ForeignKey, Numeric
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import sessionmaker, relationship
from sqlalchemy import select, update from sqlalchemy import select, update
engine = create_engine('postgresql+psycopg2://postgres:postgres@postgres.vdk2ch.ru:5432/jack_bot', echo=True) engine = create_engine('postgresql+psycopg2://postgres:postgres@postgres.vdk2ch.ru:5432/jack_bot', echo=True)
@ -19,12 +19,22 @@ class user(Base):
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True)
user_id = Column(Integer) user_id = Column(Integer)
date_created = (Column(DateTime))
user_fullname = Column(String) user_fullname = Column(String)
dick_size = (Column(Integer)) dick_size = (Column(Integer))
datetimes = (Column(DateTime)) date_updated = (Column(DateTime))
chat_id = Column(BIGINT) chat_id = Column(BIGINT)
dick_requests = relationship("DickRequest", back_populates="user")
class DickRequest(Base):
__tablename__ = 'dick_requests'
id = Column(Integer, primary_key=True)
size_change = Column(Integer)
user_id = Column(Integer, ForeignKey("users.user_id"))
user = relationship("User", back_populates="dick_requests")
date_changed = (Column(DateTime))