-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdb.py
96 lines (79 loc) · 3.46 KB
/
db.py
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
import discord
from discord.ext import commands
import psycopg2
import config
import datetime as dt
class DB(commands.Cog):
def __init__(self, bot):
self.bot = bot
def dbConnect(self):
conn = psycopg2.connect(dbname=config.DB_NAME, user=config.DB_USER, password=config.DB_PASS, host=config.DB_ADDR)
conn.set_session(autocommit=True)
return(conn)
async def addWorldRank(self, ranks, insertIdx):
conn = self.dbConnect()
with conn.cursor() as cur:
for rank in ranks:
r, g, p = rank
r = int(r)
p = int(p.replace(",", ""))
#print("inserting {0}".format(str(rank)))
cur.execute("SELECT * FROM worldrankings WHERE guild=%s AND race=%s AND date=%s;", (g, insertIdx, dt.date.today()))
res = cur.fetchone()
print(res)
if(res == None):
cur.execute("INSERT INTO worldrankings (guild, points, rank, race) VALUES (%s, %s, %s, %s);", (g, p, r, insertIdx))
else:
cur.execute("UPDATE worldrankings SET points=%s, rank=%s WHERE id=%s;", (p, r, res[0]))
conn.close()
async def getWeeklyWorldRanks(self):
conn = self.dbConnect()
today = dt.date.today()
monday = today + dt.timedelta(days=-today.weekday())
res = []
with conn.cursor() as cur:
cur.execute("SELECT * FROM worldrankings WHERE date >= %s", (monday, ))
res = cur.fetchall()
conn.close()
return(res)
async def getLatestWorldRanks(self):
conn = self.dbConnect()
today = dt.date.today()
monday = today + dt.timedelta(days=-today.weekday())
res = []
with conn.cursor() as cur:
cur.execute("SELECT * FROM (SELECT DISTINCT ON (guild) * FROM worldrankings WHERE date >= %s and race=(SELECT MAX (race) FROM worldrankings WHERE date=%s) ORDER BY guild, race DESC) t ORDER BY points DESC", (monday, today,))
res = cur.fetchall()
conn.close()
return(res)
async def deleteLastRace(self):
conn = self.dbConnect()
today = dt.date.today()
with conn.cursor() as cur:
cur.execute("DELETE FROM worldrankings WHERE race=(SELECT MAX (race) FROM worldrankings WHERE date=%s)", (today, ))
conn.close()
async def getLatestDifferential(self):
latest = await self.getLatestWorldRanks()
guilds = tuple(entry[2] for entry in latest)
print(latest)
race = latest[0][5]
conn = self.dbConnect()
today = dt.date.today()
monday = today + dt.timedelta(days=-today.weekday())
past = {}
with conn.cursor() as cur:
cur.execute("SELECT * FROM (SELECT DISTINCT ON (guild) * FROM worldrankings WHERE date >= %s AND race < %s AND guild IN %s ORDER BY guild, race DESC) t ORDER BY points DESC", (monday, race, guilds,))
past = {x[2] : x for x in cur.fetchall()}
print(past)
ret = []
for entry in latest:
name = entry[2]
if name in past:
##guildname, points, ptdiff, rank, rankdiff
ret.append([entry[2], entry[3], entry[3] - past[name][3], entry[4], entry[4] - past[name][4]])
else:
ret.append([entry[2], entry[3], None, entry[4], None])
conn.close()
return(ret)
def setup(bot):
bot.add_cog(DB(bot))