summaryrefslogtreecommitdiff
path: root/persistence.py
blob: 8003a41dd2bd3b8e42917d373cec5f7e722ddd12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import sqlite3, typing
from Types import *

class UserRepository:

    def __init__(self):
        self.__initialize()
    
    def __initialize(self):
        cur = self.__getConnection().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 __getConnection(self):
        return sqlite3.connect('db.sqlite')
    
    def getUser(self, chat_id):
        connection = self.__getConnection()
        cur = connection.cursor()
        query = "select * from user where chat_id = {chat_id}".format(chat_id = chat_id)
        cur.execute(query)
        return cur.fetchone()

    def getAllUsers(self):
        connection = self.__getConnection()
        cur = connection.cursor()
        query = "select * from user"
        cur.execute(query)
        return cur.fetchall()
    
    def removeChat(self, chat_id):
        connection = self.__getConnection()
        cur = connection.cursor()
        query = "DELETE FROM user where chat_id = {chat_id}".format(
            chat_id = chat_id)
        cur.execute(query)
        connection.commit()

    
    def putUser(self, chat_id, alias):
        if not self.getUser(chat_id):
            connection = self.__getConnection()
            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 __getConnection(self):
        return sqlite3.connect('db.sqlite')
    
    def __initialize(self):
        cur = self.__getConnection().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 putStats(self, user: User):
        stats: UserStats = await user.fetch_stats()

        connection = self.__getConnection()
        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 getStats(self) -> typing.List[UserStats]:
        connection = self.__getConnection()
        cur = connection.cursor()
        query = "SELECT * FROM stats"
        cur.execute(query)
        result = cur.fetchall()
        return [self.__mapFromDb(stats) for stats in result]
    
    def __mapFromDb(self, record):
        userStats = UserStats()
        userStats.user_id = str(record[0])
        userStats.user_display_name = str(record[1])
        userStats.level = int(record[2])
        userStats.matches_played = int(record[3])
        userStats.kills = int(record[4])
        userStats.wins = int(record[5])
        return userStats

class PresenceRepository:

    def __init__(self):
        self.__initialize()
    
    def __getConnection(self):
        return sqlite3.connect('db.sqlite')
    
    def __initialize(self):
        cur = self.__getConnection().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 getLastUserPresence(self, display_name):
        connection = self.__getConnection()
        cur = connection.cursor()
        query = "select last_online from user_presence where display_name = '{display_name}'s".format(display_name = display_name)
        cur.execute(query)
        user_presence = cur.fetchone()

        if user_presence:
            return user_presence[0]
        else:
            return 0
    
    def setLastUserPresence(self, display_name: str, last_online: int):
        connection = self.__getConnection()
        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()