From 616a2ccd173cf0b212baede390cb5e0b48fed6ac Mon Sep 17 00:00:00 2001 From: randomuser Date: Tue, 20 Jul 2021 23:50:37 -0500 Subject: [PATCH] add database system for ducks --- db.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 db.py diff --git a/db.py b/db.py new file mode 100644 index 0000000..aa6b0f8 --- /dev/null +++ b/db.py @@ -0,0 +1,55 @@ +class DuckDB: + def __init__(self, location=None): + self.location = location + self.db = [] + if location != None: + fd = open(location, "r") + self.parse(fd) + fd.close() + def parse(self, fd): + lines = [i.rstrip() for i in fd.readlines()] + for i in lines: + self.db.append(DuckEvent(i)) + def output(self, fd): + for i in self.db: + fd.write(i.stringify() + "\n") + +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) + 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] + def stringify(self): + return "{}{} {} {} {}".format( + self.status, + self.nick, + self.time, + self.offset, + self.channel + ) + +class DuckStats: + def __init__(self, db): + self.db = db + def countstatus(self, nick, status): + cnt = 0 + for i in self.db.db: + if i.status == status and i.nick == nick: + cnt += 1 + return cnt + def cought(self, nick): + return self.countstatus(nick, "B") + def missed(self, nick): + return self.countstatus(nick, "M") + def ratio(self, nick): + return (self.cought(nick) / self.missed(nick)) * 100