universalducks/db.py

87 lines
2.1 KiB
Python
Raw Permalink Normal View History

2021-07-21 04:50:37 +00:00
class DuckDB:
def __init__(self, location=None):
self.location = location
self.db = []
2021-07-21 04:58:37 +00:00
if location != None: self.read(location)
def add(self, state, nick, abstime, reltime, channel):
self.db.append(DuckEvent(
"{}{} {} {} {}".format(
state,
nick,
str(abstime),
str(reltime),
channel,
)
2021-07-21 05:13:01 +00:00
))
2021-07-21 04:51:54 +00:00
2021-07-21 04:50:37 +00:00
def parse(self, fd):
lines = [i.rstrip() for i in fd.readlines()]
for i in lines:
self.db.append(DuckEvent(i))
2021-07-21 04:51:54 +00:00
2021-07-21 04:50:37 +00:00
def output(self, fd):
for i in self.db:
fd.write(i.stringify() + "\n")
def read(self, location):
fd = open(location, "r")
self.parse(fd)
fd.close()
def write(self, location):
fd = open(location, "w")
self.output(fd)
2021-07-21 05:04:17 +00:00
fd.close()
2021-07-21 04:50:37 +00:00
class DuckEvent:
def __init__(self, line=None):
self.status = ""
self.nick = ""
self.time = None
self.offset = None
self.channel = None
if not line == None: self.internalize(line)
2021-07-21 04:51:54 +00:00
2021-07-21 04:50:37 +00:00
def internalize(self, line):
self.status = line[0].upper()
spl = line.split(' ')
self.nick = spl[0][1:]
self.time = float(spl[1])
self.offset = float(spl[2])
self.channel = spl[3]
2021-07-21 04:51:54 +00:00
2021-07-21 04:50:37 +00:00
def stringify(self):
return "{}{} {} {} {}".format(
self.status,
self.nick,
self.time,
self.offset,
self.channel
)
class DuckStats:
def __init__(self, db):
self.db = db
2021-07-21 04:51:54 +00:00
2021-07-21 04:50:37 +00:00
def countstatus(self, nick, status):
cnt = 0
for i in self.db.db:
2021-07-21 04:51:54 +00:00
if i.status == status and i.nick == nick: cnt += 1
2021-07-21 04:50:37 +00:00
return cnt
2021-07-21 04:51:54 +00:00
2021-07-21 04:50:37 +00:00
def cought(self, nick):
return self.countstatus(nick, "B")
2021-07-21 04:51:54 +00:00
2021-07-21 04:50:37 +00:00
def missed(self, nick):
return self.countstatus(nick, "M")
2021-07-21 04:51:54 +00:00
2021-07-21 04:50:37 +00:00
def ratio(self, nick):
return self.cought(nick) / self.missed(nick)
2021-07-21 05:17:24 +00:00
def channels(self, nick):
channels = set()
for i in self.db.db:
if i.nick == nick: channels.add(i.channel)
2021-07-21 05:17:24 +00:00
return len(channels)