mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-11-30 14:59:34 +01:00
Multiple choices for polls. Still broke.. needs tests.. etc etc. Work in progress :)
This commit is contained in:
parent
0c4a60cda5
commit
15639d8a1d
288
plugins/Poll.py
288
plugins/Poll.py
@ -56,56 +56,53 @@ def configure(onStart, afterConnect, advanced):
|
|||||||
from questions import expect, anything, something, yn
|
from questions import expect, anything, something, yn
|
||||||
onStart.append('load Poll')
|
onStart.append('load Poll')
|
||||||
|
|
||||||
dbFilename = os.path.join(conf.dataDir, 'Poll.db')
|
class Poll(callbacks.Privmsg, plugins.ChannelDBHandler):
|
||||||
|
def __init__(self):
|
||||||
|
callbacks.Privmsg.__init__(self)
|
||||||
|
plugins.ChannelDBHandler.__init__(self)
|
||||||
|
|
||||||
def makeDb(dbfilename):
|
def makeDb(self, filename):
|
||||||
if os.path.exists(dbfilename):
|
if os.path.exists(filename):
|
||||||
db = sqlite.connect(dbfilename)
|
db = sqlite.connect(filename)
|
||||||
else:
|
else:
|
||||||
db = sqlite.connect(dbfilename)
|
db = sqlite.connect(filename)
|
||||||
cursor = db.cursor()
|
cursor = db.cursor()
|
||||||
try:
|
|
||||||
cursor.execute("""SELECT * FROM polls LIMIT 1""")
|
|
||||||
except sqlite.DatabaseError:
|
|
||||||
cursor.execute("""CREATE TABLE polls (
|
cursor.execute("""CREATE TABLE polls (
|
||||||
id INTEGER PRIMARY KEY,
|
id INTEGER PRIMARY KEY,
|
||||||
question TEXT,
|
question TEXT,
|
||||||
started_by INTEGER,
|
started_by INTEGER,
|
||||||
yes INTEGER,
|
|
||||||
no INTEGER,
|
|
||||||
expires TIMESTAMP)""")
|
expires TIMESTAMP)""")
|
||||||
try:
|
cursor.execute("""CREATE TABLE options (
|
||||||
cursor.execute("""SELECT * FROM votes LIMIT 1""")
|
poll_id INTEGER,
|
||||||
except sqlite.DatabaseError:
|
option_id INTEGER,
|
||||||
|
option TEXT,
|
||||||
|
votes INTEGER,
|
||||||
|
UNIQUE (poll_id, option_id) ON CONFLICT IGNORE)""")
|
||||||
cursor.execute("""CREATE TABLE votes (
|
cursor.execute("""CREATE TABLE votes (
|
||||||
user_id INTEGER,
|
user_id INTEGER,
|
||||||
poll_id INTEGER,
|
poll_id INTEGER,
|
||||||
vote BOOLEAN)""")
|
option_id INTERER)""")
|
||||||
db.commit()
|
db.commit()
|
||||||
return db
|
return db
|
||||||
|
|
||||||
class Poll(callbacks.Privmsg):
|
|
||||||
def __init__(self):
|
|
||||||
callbacks.Privmsg.__init__(self)
|
|
||||||
self.db = makeDb(dbFilename)
|
|
||||||
|
|
||||||
def new(self, irc, msg, args):
|
def new(self, irc, msg, args):
|
||||||
"""[<lifespan in seconds>] <question>
|
"""[<channel>] [<lifespan in seconds>] <question>
|
||||||
|
|
||||||
Creates a new poll with the given question and optional lifespan.
|
Creates a new poll with the given question and optional lifespan.
|
||||||
Without a lifespan the poll will never expire and accept voting
|
Without a lifespan the poll will never expire and accept voting
|
||||||
until it is closed or removed.
|
until it is closed.
|
||||||
"""
|
"""
|
||||||
|
channel = privmsgs.getChannel(msg, args)
|
||||||
(lifespan, question) = privmsgs.getArgs(args, optional=1)
|
(lifespan, question) = privmsgs.getArgs(args, optional=1)
|
||||||
try:
|
try:
|
||||||
lifespan = int(lifespan)
|
lifespan = float(lifespan)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
if question:
|
if question:
|
||||||
question = '%s %s' % (lifespan, question)
|
question = '%s %s' % (lifespan, question)
|
||||||
else:
|
else:
|
||||||
question = lifespan
|
question = lifespan
|
||||||
lifespan = 0
|
lifespan = 0.0
|
||||||
if lifespan:
|
if lifespan != 0.0:
|
||||||
lifespan += time.time()
|
lifespan += time.time()
|
||||||
if not question:
|
if not question:
|
||||||
raise callbacks.ArgumentError
|
raise callbacks.ArgumentError
|
||||||
@ -115,29 +112,31 @@ class Poll(callbacks.Privmsg):
|
|||||||
irc.error(msg, conf.replyNotRegistered)
|
irc.error(msg, conf.replyNotRegistered)
|
||||||
return
|
return
|
||||||
|
|
||||||
cursor = self.db.cursor()
|
db = self.getDb(channel)
|
||||||
|
cursor = db.cursor()
|
||||||
cursor.execute("""INSERT INTO polls VALUES
|
cursor.execute("""INSERT INTO polls VALUES
|
||||||
(NULL, %s, %s, 0, 0, %s)""", question,
|
(NULL, %s, %s, %s)""", question,
|
||||||
userId, lifespan)
|
userId, lifespan)
|
||||||
self.db.commit()
|
db.commit()
|
||||||
cursor.execute("""SELECT id FROM polls WHERE question=%s""", question)
|
cursor.execute("""SELECT id FROM polls WHERE question=%s""", question)
|
||||||
id = cursor.fetchone()[0]
|
id = cursor.fetchone()[0]
|
||||||
irc.reply(msg, '%s (poll #%s)' % (conf.replySuccess, id))
|
irc.reply(msg, '%s (poll #%s)' % (conf.replySuccess, id))
|
||||||
|
|
||||||
def open(self, irc, msg, args):
|
def open(self, irc, msg, args):
|
||||||
"""[<lifespan in seconds>] <id>
|
"""[<channel>] [<lifespan in seconds>] <id>
|
||||||
|
|
||||||
Reopens a closed poll with the given <id> and optional lifespan.
|
Reopens a closed poll with the given <id> and optional lifespan.
|
||||||
Without a lifespan the poll will never expire and accept voting
|
Without a lifespan the poll will never expire and accept voting
|
||||||
until it is closed or removed.
|
until it is closed.
|
||||||
"""
|
"""
|
||||||
|
channel = privmsgs.getChannel(msg, args)
|
||||||
(lifespan, id) = privmsgs.getArgs(args, optional=1)
|
(lifespan, id) = privmsgs.getArgs(args, optional=1)
|
||||||
if not id:
|
if not id:
|
||||||
id = lifespan
|
id = lifespan
|
||||||
lifespan = 0
|
lifespan = 0
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
lifespan = int(lifespan)
|
lifespan = float(lifespan)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
irc.error(msg, 'The <lifespan> argument must be an integer.')
|
irc.error(msg, 'The <lifespan> argument must be an integer.')
|
||||||
return
|
return
|
||||||
@ -146,109 +145,182 @@ class Poll(callbacks.Privmsg):
|
|||||||
except ValueError:
|
except ValueError:
|
||||||
irc.error(msg, 'The <id> argument must be an integer.')
|
irc.error(msg, 'The <id> argument must be an integer.')
|
||||||
return
|
return
|
||||||
if lifespan:
|
if lifespan != 0.0:
|
||||||
lifespan += time.time()
|
lifespan += time.time()
|
||||||
|
|
||||||
cursor = self.db.cursor()
|
db = self.getDb(channel)
|
||||||
|
cursor = db.cursor()
|
||||||
cursor.execute("""UPDATE polls SET expires=%s WHERE id=%s""",
|
cursor.execute("""UPDATE polls SET expires=%s WHERE id=%s""",
|
||||||
lifespan, id)
|
lifespan, id)
|
||||||
self.db.commit()
|
db.commit()
|
||||||
irc.reply(msg, conf.replySuccess)
|
irc.reply(msg, conf.replySuccess)
|
||||||
|
|
||||||
def close(self, irc, msg, args):
|
def close(self, irc, msg, args):
|
||||||
"""<id>
|
"""[<channel>] <id>
|
||||||
|
|
||||||
Closes the poll with the given <id>; further votes will not be allowed.
|
Closes the poll with the given <id>; further votes will not be allowed.
|
||||||
"""
|
"""
|
||||||
|
channel = privmsgs.getChannel(msg, args)
|
||||||
id = privmsgs.getArgs(args)
|
id = privmsgs.getArgs(args)
|
||||||
try:
|
try:
|
||||||
id = int(id)
|
id = int(id)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
irc.error(msg, 'The <id> argument must be an integer.')
|
irc.error(msg, 'The <id> argument must be an integer.')
|
||||||
return
|
return
|
||||||
|
|
||||||
cursor = self.db.cursor()
|
db = self.getDb(channel)
|
||||||
|
cursor = db.cursor()
|
||||||
cursor.execute("""UPDATE polls SET expires=%s WHERE id=%s""",
|
cursor.execute("""UPDATE polls SET expires=%s WHERE id=%s""",
|
||||||
int(time.time()), id)
|
int(time.time()), id)
|
||||||
self.db.commit()
|
db.commit()
|
||||||
irc.reply(msg, conf.replySuccess)
|
irc.reply(msg, conf.replySuccess)
|
||||||
|
|
||||||
def vote(self, irc, msg, args):
|
def add(self, irc, msg, args):
|
||||||
"""<id> <Yes,No>
|
"""[<channel>] <id> <option>
|
||||||
|
|
||||||
Vote yes or no on an active poll with the given id. This command can
|
Add an option to poll <id>.
|
||||||
also be used to override the previous vote.
|
|
||||||
"""
|
"""
|
||||||
(id, vote) = privmsgs.getArgs(args, required=2)
|
channel = privmsgs.getChannel(msg, args)
|
||||||
|
(id, option) = privmsgs.getArgs(args, required=2)
|
||||||
try:
|
try:
|
||||||
id = int(id)
|
id = int(id)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
irc.error(msg, 'The <id> argument must be an integer.')
|
irc.error(msg, 'The <id> argument must be an integer.')
|
||||||
return
|
return
|
||||||
if vote.capitalize() == 'Yes':
|
|
||||||
vote = 1
|
|
||||||
elif vote.capitalize() == 'No':
|
|
||||||
vote = 0
|
|
||||||
else:
|
|
||||||
raise callbacks.ArgumentError
|
|
||||||
try:
|
try:
|
||||||
userId = ircdb.users.getUserId(msg.prefix)
|
userId = ircdb.users.getUserId(msg.prefix)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
irc.error(msg, conf.replyNotRegistered)
|
irc.error(msg, conf.replyNotRegistered)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
db = self.getDb(channel)
|
||||||
|
cursor = db.cursor()
|
||||||
|
cursor.execute("""SELECT started_by FROM polls WHERE id=%s""" % id)
|
||||||
|
if cursor.rowcount == 0:
|
||||||
|
irc.error(msg, 'There is no such poll.')
|
||||||
|
return
|
||||||
|
elif userId != cursor.fetchone()[0]:
|
||||||
|
irc.error(msg, 'That poll isn\'t yours.')
|
||||||
|
return
|
||||||
|
|
||||||
|
cursor.execute("""INSERT INTO options VALUES
|
||||||
|
(%s, NULL, %%s, 0)""" % id, option)
|
||||||
|
db.commit()
|
||||||
|
cursor.execute("""SELECT option_id FROM options
|
||||||
|
WHERE poll_id=%s
|
||||||
|
AND votes=0
|
||||||
|
AND option=%%s""" % id, option)
|
||||||
|
id = cursor.fetchone()[0]
|
||||||
|
irc.reply(msg, '%s (option #%s)' % (conf.replySuccess, id))
|
||||||
|
|
||||||
|
def remove(self, irc, msg, args):
|
||||||
|
"""[<channel>] <poll id> <option id>
|
||||||
|
|
||||||
cursor = self.db.cursor()
|
Remove option <option id> from poll <poll id>.
|
||||||
cursor.execute("""SELECT yes, no, expires
|
"""
|
||||||
|
channel = privmsgs.getChannel(msg, args)
|
||||||
|
(pollId, optionId) = privmsgs.getArgs(args, required=2)
|
||||||
|
try:
|
||||||
|
pollId = int(pollId)
|
||||||
|
optionId = int(optionId)
|
||||||
|
except ValueError:
|
||||||
|
irc.error(msg, 'The <poll id> and <option id> '
|
||||||
|
'arguments must be integers.')
|
||||||
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
|
userId = ircdb.users.getUserId(msg.prefix)
|
||||||
|
except KeyError:
|
||||||
|
irc.error(msg, conf.replyNotRegistered)
|
||||||
|
return
|
||||||
|
|
||||||
|
db = self.getDb(channel)
|
||||||
|
cursor = db.cursor()
|
||||||
|
cursor.execute("""SELECT started_by FROM polls WHERE id=%s""" % pollId)
|
||||||
|
if cursor.rowcount == 0:
|
||||||
|
irc.error(msg, 'There is no such poll.')
|
||||||
|
return
|
||||||
|
elif userId != cursor.fetchone()[0]:
|
||||||
|
irc.error(msg, 'That poll isn\'t yours.')
|
||||||
|
return
|
||||||
|
|
||||||
|
cursor.execute("""DELETE FROM options
|
||||||
|
WHERE poll_id=%s
|
||||||
|
AND option_id=%s""" % (pollId, optionId))
|
||||||
|
irc.reply(msg, conf.replySuccess)
|
||||||
|
|
||||||
|
def vote(self, irc, msg, args):
|
||||||
|
"""[<channel>] <poll id> <option id>
|
||||||
|
|
||||||
|
Vote <option id> on an active poll with the given <poll id>.
|
||||||
|
This command can also be used to override the previous vote.
|
||||||
|
"""
|
||||||
|
channel = privmsgs.getChannel(msg, args)
|
||||||
|
(id, option) = privmsgs.getArgs(args, required=2)
|
||||||
|
try:
|
||||||
|
id = int(id)
|
||||||
|
option = int(option)
|
||||||
|
except ValueError:
|
||||||
|
irc.error(msg, 'The <poll id> and <option id> '
|
||||||
|
'arguments must be an integers.')
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
userId = ircdb.users.getUserId(msg.prefix)
|
||||||
|
except KeyError:
|
||||||
|
irc.error(msg, conf.replyNotRegistered)
|
||||||
|
return
|
||||||
|
|
||||||
|
db = self.getDb(channel)
|
||||||
|
cursor = db.cursor()
|
||||||
|
cursor.execute("""SELECT expires
|
||||||
FROM polls WHERE id=%s""", id)
|
FROM polls WHERE id=%s""", id)
|
||||||
if cursor.rowcount == 0:
|
if cursor.rowcount == 0:
|
||||||
irc.error(msg, 'There is no such poll.')
|
irc.error(msg, 'There is no such poll.')
|
||||||
return
|
return
|
||||||
(yVotes, nVotes, expires) = cursor.fetchone()
|
expires = float(cursor.fetchone()[0])
|
||||||
expires = float(expires)
|
if expires != 0.0 and time.time() >= expires:
|
||||||
if expires != 0 and time.time() >= expires:
|
|
||||||
irc.error(msg, 'That poll is closed.')
|
irc.error(msg, 'That poll is closed.')
|
||||||
return
|
return
|
||||||
|
|
||||||
|
cursor.execute("""SELECT option_id FROM options
|
||||||
|
WHERE poll_id=%s
|
||||||
|
AND option_id=%s""" % (id, option))
|
||||||
|
if cursor.rowcount == 0:
|
||||||
|
irc.error(msg, 'There is no such option.')
|
||||||
|
return
|
||||||
|
|
||||||
cursor.execute("""SELECT vote FROM votes WHERE user_id=%s
|
cursor.execute("""SELECT vote FROM votes WHERE user_id=%s
|
||||||
AND poll_id=%s""", userId, id)
|
AND poll_id=%s""", userId, id)
|
||||||
if cursor.rowcount == 0:
|
if cursor.rowcount == 0:
|
||||||
cursor.execute("""INSERT INTO votes VALUES (%s, %s, %s)""",
|
cursor.execute("""INSERT INTO votes VALUES (%s, %s, %s)""",
|
||||||
userId, id, vote)
|
userId, id, option)
|
||||||
if vote:
|
db.commit()
|
||||||
yVotes += 1
|
irc.reply(msg, 'You voted option #%s on poll #%s.' % (option, id))
|
||||||
sql = """UPDATE polls SET yes=%s WHERE id=%%s""" % yVotes
|
|
||||||
else:
|
|
||||||
nVotes += 1
|
|
||||||
sql = """UPDATE polls SET no=%s WHERE id=%%s""" % nVotes
|
|
||||||
cursor.execute(sql, id)
|
|
||||||
self.db.commit()
|
|
||||||
irc.reply(msg, 'You voted %s on poll #%s.'\
|
|
||||||
% (ircutils.bold(args[1].capitalize()), id))
|
|
||||||
else:
|
else:
|
||||||
oldVote = cursor.fetchone()[0]
|
oldVote = int(cursor.fetchone()[0])
|
||||||
if vote == int(oldVote):
|
if option == oldVote:
|
||||||
irc.error(msg, 'You already voted %s on that poll.'\
|
irc.error(msg, 'You already voted option #%s '
|
||||||
% ircutils.bold(args[1].capitalize()))
|
'on that poll.' % option)
|
||||||
return
|
return
|
||||||
elif vote:
|
cursor.execute("""UPDATE options SET votes=votes-1
|
||||||
yVotes += 1
|
WHERE poll_id=%s AND option_id=%s""" \
|
||||||
nVotes -= 1
|
% (id, oldVote))
|
||||||
else:
|
cursor.execute("""UPDATE options SET votes=votes+1
|
||||||
nVotes += 1
|
WHERE poll_id=%s AND option_id=%s""" \
|
||||||
yVotes -= 1
|
% (id, option))
|
||||||
cursor.execute("""UPDATE polls SET yes=%s, no=%s WHERE id=%s""",
|
cursor.execute("""UPDATE votes SET option_id=%s WHERE user_id=%s
|
||||||
yVotes, nVotes, id)
|
AND poll_id=%s""", option, userId, id)
|
||||||
cursor.execute("""UPDATE votes SET vote=%s WHERE user_id=%s
|
db.commit()
|
||||||
AND poll_id=%s""", vote, userId, id)
|
irc.reply(msg, 'Your vote on poll #%s has been updated to option '
|
||||||
self.db.commit()
|
'#%s.' % (id, option))
|
||||||
irc.reply(msg, 'Your vote on poll #%s has been updated to %s.'\
|
|
||||||
% (id, ircutils.bold(args[1].capitalize())))
|
|
||||||
|
|
||||||
def results(self, irc, msg, args):
|
def results(self, irc, msg, args):
|
||||||
"""<id>
|
"""[<channel>] <id>
|
||||||
|
|
||||||
Shows the (current) results for the poll with the given id.
|
Shows the (current) results for the poll with the given id.
|
||||||
"""
|
"""
|
||||||
|
channel = privmsgs.getChannel(msg, args)
|
||||||
id = privmsgs.getArgs(args)
|
id = privmsgs.getArgs(args)
|
||||||
try:
|
try:
|
||||||
id = int(id)
|
id = int(id)
|
||||||
@ -256,35 +328,49 @@ class Poll(callbacks.Privmsg):
|
|||||||
irc.error(msg, 'The <id> argument must be an integer.')
|
irc.error(msg, 'The <id> argument must be an integer.')
|
||||||
return
|
return
|
||||||
|
|
||||||
cursor = self.db.cursor()
|
db = self.getDb(channel)
|
||||||
|
cursor = db.cursor()
|
||||||
cursor.execute("""SELECT * FROM polls WHERE id=%s""", id)
|
cursor.execute("""SELECT * FROM polls WHERE id=%s""", id)
|
||||||
if cursor.rowcount == 0:
|
if cursor.rowcount == 0:
|
||||||
irc.error(msg, 'There is no such poll.')
|
irc.error(msg, 'There is no such poll.')
|
||||||
return
|
return
|
||||||
(id, question, startedBy, yVotes, nVotes, expires) = cursor.fetchone()
|
(id, question, startedBy, expires) = cursor.fetchone()
|
||||||
tVotes = yVotes + nVotes
|
|
||||||
try:
|
try:
|
||||||
startedBy = ircdb.users.getUser(msg.prefix).name
|
startedBy = ircdb.users.getUser(msg.prefix).name
|
||||||
except KeyError:
|
except KeyError:
|
||||||
startedBy = 'an unknown user'
|
startedBy = 'an unknown user'
|
||||||
return
|
return
|
||||||
reply = 'Results for poll #%s: "%s" by %s' % (id, question, startedBy)
|
reply = 'Results for poll #%s: "%s" by %s' % (id, question, startedBy)
|
||||||
if tVotes == 0:
|
cursor.execute("""SELECT option_id, option, votes FROM options
|
||||||
reply = '%s - There have been no votes on this poll yet.' % reply
|
WHERE poll_id=%s ORDER BY option_id""" % id)
|
||||||
|
totalVotes = 0
|
||||||
|
results = []
|
||||||
|
if cursor.rowcount == 0:
|
||||||
|
reply = '%s - This poll has no options yet.' % reply
|
||||||
else:
|
else:
|
||||||
pc = lambda x: int(float(x) / float(tVotes) * 100.0)
|
L = cursor.fetchall()
|
||||||
reply = '%s - %s %s (%s%%), %s %s (%s%%), %s %s.'\
|
for (optionId, option, votes) in L:
|
||||||
% (reply, ircutils.bold('Yes:'), yVotes, pc(yVotes),
|
totalVotes += votes
|
||||||
ircutils.bold('No:'), nVotes, pc(nVotes),
|
if totalVotes == 0:
|
||||||
ircutils.bold('Total votes:'), tVotes)
|
reply = '%s - There have been no votes on this poll yet.' % reply
|
||||||
|
else:
|
||||||
|
for (optionId, option, votes) in L:
|
||||||
|
if votes == 0:
|
||||||
|
percent = 0
|
||||||
|
else:
|
||||||
|
percent = int(float(votes) / float(totalVotes) * 100.0)
|
||||||
|
results.append('%s. %s: %s (%s%%)'\
|
||||||
|
% (ircutils.bold(option_id), option,
|
||||||
|
ircutils.bold(votes), percent))
|
||||||
|
reply = '%s - %s' % (reply, utils.commaAndify(results))
|
||||||
expires = float(expires)
|
expires = float(expires)
|
||||||
if expires != 0:
|
if expires != 0.0:
|
||||||
if time.time() >= expires:
|
if time.time() >= expires:
|
||||||
reply = '%s Poll is closed.' % reply
|
reply = '%s - Poll is closed.' % reply
|
||||||
else:
|
else:
|
||||||
expires -= time.time()
|
expires -= time.time()
|
||||||
reply = '%s Poll expires in %s' % (reply,
|
reply = '%s - Poll expires in %s' % (reply,
|
||||||
utils.timeElapsed(expires))
|
utils.timeElapsed(int(expires)))
|
||||||
irc.reply(msg, reply)
|
irc.reply(msg, reply)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user