Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,30 @@
import asyncio

from aiogram import Bot, Dispatcher
from aiogram.fsm.storage.redis import RedisStorage, Redis

from config.config import Config, load_config
from handlers import user_handlers
from data.sql_database import LobbyDatabase
from data.sql_database import LobbyDatabase, UsersDatabase
from services.set_menu import set_main_menu


logger = logging.getLogger(__name__)
lobby_database = LobbyDatabase('test3')
lobby_database = LobbyDatabase('test6')
users_database = UsersDatabase('test5')
config: Config = load_config()
bot = Bot(token=config.tg_bot.token,
parse_mode='HTML')
redis = Redis(host='127.0.0.1:6379')
storage = RedisStorage(redis=redis)


async def main():
lobby_database.create_table()
for i in range(1, 5):
lobby_database.reset_lobby(i)
lobby_database.default_lobby(i)
lobby_database.reset_lobby(-1)
lobby_database.default_lobby(-1)
await lobby_database.create_table()
await users_database.create_table()
lobbies_id = lobby_database.get_all_lobby_stat()
for i in lobbies_id:
await lobby_database.delete_lobby(lobby_id=i[0])
logging.basicConfig(level=logging.INFO,
format='%(filename)s:%(lineno)d #%(levelname)-8s '
'[%(asctime)s] - %(name)s - %(message)s')
Expand All @@ -37,4 +42,4 @@ async def main():


if __name__ == '__main__':
asyncio.run(main())
asyncio.run(main())
182 changes: 152 additions & 30 deletions data/sql_database.py
Original file line number Diff line number Diff line change
@@ -1,63 +1,185 @@
import sqlite3
import psycopg2

conn = sqlite3.connect('test.py')
conn = psycopg2.connect(host='localhost', dbname='postgres', user='postgres', password='1234', port=5432)


class LobbyDatabase:
def __init__(self, name):
self.name = name

def create_table(self):
async def create_table(self):
cur = conn.cursor()
cur.execute(f"""CREATE TABLE IF NOT EXISTS {self.name}
(id INT PRIMARY KEY,
people TEXT);""")
(lobby_id BIGSERIAL PRIMARY KEY,
users TEXT);
""")
conn.commit()
cur.close()
print('[INFO] TABLE CREATED SUCCESSFULLY')

def default_lobby(self, lobby_id: int):
async def enter_lobby(self, lobby_id: int, user_chat_id: str):
cur = conn.cursor()
cur.execute(f"""INSERT OR IGNORE INTO {self.name} (id, people)
VALUES ({lobby_id}, '');
""")
cur.execute(f"""SELECT users FROM {self.name}
WHERE lobby_id={lobby_id};
""")
users = cur.fetchone()[0].split('~~~')
users.append(f"""{user_chat_id}""")
cur.execute(f"""UPDATE {self.name} SET users='{'~~~'.join(users)}' WHERE lobby_id={lobby_id};""")
conn.commit()
cur.close()

async def exit_lobby(self, lobby_id: int, user_chat_id: str):
cur = conn.cursor()
cur.execute(f"""SELECT users FROM {self.name}
WHERE lobby_id={lobby_id};
""")
users = cur.fetchone()[0].split('~~~')
users.remove(f"""{user_chat_id}""")
cur.execute(f"""UPDATE {self.name} SET users='{'~~~'.join(users)}' WHERE lobby_id={lobby_id};""")
conn.commit()
cur.close()

def get_lobby_stat(self, lobby_id: int):
cur = conn.cursor()
cur.execute(f"""SELECT users FROM {self.name}
WHERE lobby_id={lobby_id};""")
s = cur.fetchone()[0].split('~~~')
conn.commit()
cur.close()
return s

def create_new_lobby(self, user_chat_id: str):
cur = conn.cursor()
cur.execute(f"""INSERT INTO {self.name} (users) VALUES ('{user_chat_id}')""")
cur.execute(f"""SELECT lobby_id FROM {self.name}
WHERE users='{user_chat_id}';""")
lobby_id = cur.fetchone()[0]
conn.commit()
cur.close()
return lobby_id

def get_all_lobby_stat(self):
cur = conn.cursor()
cur.execute(f"""SELECT lobby_id, users FROM {self.name}""")
all_lobby_stat = cur.fetchall()
return all_lobby_stat

async def delete_lobby(self, lobby_id: int):
cur = conn.cursor()
cur.execute(f"""DELETE FROM {self.name} WHERE lobby_id={lobby_id};""")
conn.commit()
cur.close()

def enter_lobby(self, lobby_id: int, user_chat_id: str):

class UsersDatabase:
def __init__(self, name):
self.name = name

async def create_table(self):
cur = conn.cursor()
cur.execute(f"""SELECT people FROM {self.name}
WHERE id={lobby_id};
cur.execute(f"""CREATE TABLE IF NOT EXISTS {self.name}
(chat_id BIGINT PRIMARY KEY,
user_name TEXT,
lobbies_page_message_id BIGINT,
game_page_message_id BIGINT,
balance INT,
games_amount INT,
wines_amount INT,
loses_amount INT,
last_free_reward_date_timestamp INT);""")
conn.commit()
cur.close()
print('[INFO] TABLE CREATED SUCCESSFULLY')

async def insert_new_user(self, chat_id: int, user_name: str):
cur = conn.cursor()
cur.execute(f"""INSERT INTO {self.name}
(chat_id, user_name, lobbies_page_message_id, games_amount, wines_amount, loses_amount, balance,
last_free_reward_date_timestamp)
VALUES ({chat_id}, '{user_name}', NULL, 0, 0, 0, 1000, 0)
ON CONFLICT (chat_id) DO NOTHING;
""")
s = cur.fetchone()[0].split()
s.append(user_chat_id)
cur.execute(f"""UPDATE {self.name} SET people='{' '.join(s)}' WHERE id={lobby_id};""")
conn.commit()
cur.close()

def exit_lobby(self, lobby_id: int, user_chat_id: str):
def get_statistic_of_users_without_lobby(self):
cur = conn.cursor()
cur.execute(f"""SELECT people FROM {self.name}
WHERE id={lobby_id};
""")
s = cur.fetchone()[0].split()
s.remove(user_chat_id)
cur.execute(f"""UPDATE {self.name} SET people='{' '.join(s)}' WHERE id={lobby_id};""")
cur.execute(f"""SELECT chat_id, lobbies_page_message_id FROM {self.name}
WHERE lobbies_page_message_id IS NOT NULL;""")
stat = cur.fetchall()
conn.commit()
cur.close()
return stat

def reset_lobby(self, lobby_id: int):
def get_user_name(self, chat_id: int):
cur = conn.cursor()
cur.execute(f"""UPDATE {self.name} SET people='' WHERE id={lobby_id};""")
cur.execute(f"""SELECT user_name FROM {self.name} WHERE chat_id={chat_id}""")
user_name = cur.fetchone()
conn.commit()
cur.close()
return user_name[0]

def get_lobby_stat(self, lobby_id: int):
async def update_lobbies_page_message_id(self, chat_id: int, lobbies_page_message_id: int):
cur = conn.cursor()
cur.execute(f"""SELECT people FROM {self.name}
WHERE id={lobby_id};
""")
s = cur.fetchone()[0].split()
cur.execute(f"""UPDATE {self.name} SET lobbies_page_message_id={lobbies_page_message_id}
WHERE chat_id={chat_id};""")
conn.commit()
cur.close()

async def delete_lobbies_page_message_id(self, chat_id: int):
cur = conn.cursor()
cur.execute(f"""UPDATE {self.name} SET lobbies_page_message_id=NULL WHERE chat_id={chat_id};""")
conn.commit()
cur.close()

def get_user_statistic(self, chat_id):
cur = conn.cursor()
cur.execute(f"""SELECT user_name, balance, games_amount, wines_amount, loses_amount
FROM {self.name} WHERE chat_id={chat_id}""")
stat = cur.fetchall()
conn.commit()
cur.close()
return stat[0]

def get_user_balance(self, chat_id):
cur = conn.cursor()
cur.execute(f"""SELECT balance FROM {self.name} WHERE chat_id={chat_id}""")
balance = cur.fetchone()
conn.commit()
cur.close()
return balance[0]

def update_user_balance(self, chat_id: int, balance: int):
cur = conn.cursor()
cur.execute(f"""UPDATE {self.name} SET balance={balance} WHERE chat_id={chat_id};""")
conn.commit()
cur.close()

async def update_game_page_message_id(self, chat_id, message_id):
cur = conn.cursor()
cur.execute(f"""UPDATE {self.name} SET game_page_message_id={message_id} WHERE chat_id={chat_id};""")
conn.commit()
cur.close()

def get_user_game_page_message_id(self, chat_id: int):
cur = conn.cursor()
cur.execute(f"""SELECT game_page_message_id FROM {self.name} WHERE chat_id={chat_id}""")
page_message_id = cur.fetchone()
conn.commit()
cur.close()
return page_message_id[0]

def get_last_free_reward_date_timestamp(self, chat_id: int):
cur = conn.cursor()
cur.execute(f"""SELECT last_free_reward_date_timestamp FROM {self.name} WHERE chat_id={chat_id}""")
last_free_reward_date_timestamp = cur.fetchone()
conn.commit()
cur.close()
return last_free_reward_date_timestamp[0]

def update_last_free_reward_date_timestamp(self, chat_id: int, last_free_reward_date_timestamp: int):
cur = conn.cursor()
cur.execute(f"""UPDATE {self.name} SET last_free_reward_date_timestamp={last_free_reward_date_timestamp}
WHERE chat_id={chat_id}""")
conn.commit()
cur.close()
return s
Loading