diff options
Diffstat (limited to 'persistence')
| -rw-r--r-- | persistence/__init__.py | 128 | 
1 files changed, 128 insertions, 0 deletions
diff --git a/persistence/__init__.py b/persistence/__init__.py new file mode 100644 index 0000000..9d9220e --- /dev/null +++ b/persistence/__init__.py @@ -0,0 +1,128 @@ +import sqlite3, typing +from app_types import * + +class UserRepository: + +    def __init__(self): +        self.__initialize() +     +    def __initialize(self): +        cur = self.__get_connection().cursor() +        cur.execute("CREATE TABLE IF NOT EXISTS user(chat_id INT, alias TEXT)") +        cur.execute("CREATE UNIQUE INDEX IF NOT EXISTS chat_id_idx ON user(chat_id)") +     +    def __get_connection(self): +        return sqlite3.connect('db.sqlite') +     +    def get_user(self, chat_id): +        connection = self.__get_connection() +        cur = connection.cursor() +        query = "select * from user where chat_id = {chat_id}".format(chat_id = chat_id) +        cur.execute(query) +        return cur.fetchone() + +    def get_all_users(self): +        connection = self.__get_connection() +        cur = connection.cursor() +        query = "select * from user" +        cur.execute(query) +        return cur.fetchall() +     +    def remove_chat(self, chat_id): +        connection = self.__get_connection() +        cur = connection.cursor() +        query = "DELETE FROM user where chat_id = {chat_id}".format( +            chat_id = chat_id) +        cur.execute(query) +        connection.commit() + +     +    def put_user(self, chat_id, alias): +        if not self.get_user(chat_id): +            connection = self.__get_connection() +            cur = connection.cursor() +            query = "INSERT INTO user(chat_id, alias) VALUES({chat_id}, '{text}')".format( +                chat_id = chat_id, +                text = alias) +            cur.execute(query) +            connection.commit() + +class StatsRepository: + +    def __init__(self): +        self.__initialize() +     +    def __get_connection(self): +        return sqlite3.connect('db.sqlite') +     +    def __initialize(self): +        cur = self.__get_connection().cursor() +        cur.execute("CREATE TABLE IF NOT EXISTS stats(user_id TEXT, display_name TEXT, level INT, matches_played INT, kills INT, wins INT)") +        cur.execute("CREATE UNIQUE INDEX IF NOT EXISTS user_id_idx ON stats(user_id)") +     +    async def put_stats(self, user: User): +        stats: UserStats = await user.fetch_stats() + +        connection = self.__get_connection() +        cur = connection.cursor() +        query = "INSERT OR REPLACE INTO stats(user_id, display_name, level, matches_played, kills, wins) VALUES('{user_id}', '{display_name}', {level}, {matches_played}, {kills}, {wins})".format( +            user_id = user.id, +            display_name = user.display_name, +            level = stats.level, +            matches_played = stats.matches_played, +            kills = stats.kills, +            wins = stats.wins) +        cur.execute(query) +        connection.commit() +     +    def get_stats(self) -> typing.List[UserStats]: +        connection = self.__get_connection() +        cur = connection.cursor() +        query = "SELECT * FROM stats" +        cur.execute(query) +        result = cur.fetchall() +        return [self.__map_from_db(stats) for stats in result] +     +    def __map_from_db(self, record): +        user_stats = UserStats() +        user_stats.user_id = str(record[0]) +        user_stats.user_display_name = str(record[1]) +        user_stats.level = int(record[2]) +        user_stats.matches_played = int(record[3]) +        user_stats.kills = int(record[4]) +        user_stats.wins = int(record[5]) +        return user_stats + +class PresenceRepository: + +    def __init__(self): +        self.__initialize() +     +    def __get_connection(self): +        return sqlite3.connect('db.sqlite') +     +    def __initialize(self): +        cur = self.__get_connection().cursor() +        cur.execute("CREATE TABLE IF NOT EXISTS user_presence(display_name TEXT, last_online INT)") +        cur.execute("CREATE UNIQUE INDEX IF NOT EXISTS display_name_idx ON user_presence(display_name)") + +    def get_last_user_presence(self, display_name): +        connection = self.__get_connection() +        cur = connection.cursor() +        query = "select last_online from user_presence where display_name = '{display_name}'".format(display_name = display_name) +        cur.execute(query) +        user_presence = cur.fetchone() + +        if user_presence: +            return user_presence[0] +        else: +            return 0 +     +    def set_last_user_presence(self, display_name: str, last_online: int): +        connection = self.__get_connection() +        cur = connection.cursor() +        query = "INSERT OR REPLACE INTO user_presence(display_name, last_online) VALUES('{display_name}', {last_online})".format( +            display_name = display_name, +            last_online = last_online) +        cur.execute(query) +        connection.commit()
\ No newline at end of file  | 
