1
0
mirror of http://aero2k.de/t/repos/urlbot-native.git synced 2017-09-06 15:25:38 +02:00

fiddle with config syntax, add disabled new plugin for quiz

This commit is contained in:
Thorsten
2016-01-28 19:15:48 +01:00
parent 113b9d94b1
commit 6b20f89581
2 changed files with 91 additions and 52 deletions

View File

@@ -13,6 +13,8 @@ import json
import logging
import os
import sys
from contextlib import contextmanager
from fasteners import interprocess_locked
from configobj import ConfigObj
from validate import Validator
@@ -99,3 +101,11 @@ def runtimeconf_deepget(key, default=None):
if value is None:
break
return value
@contextmanager
def plugin_config(name):
cfg = runtimeconf_deepget('plugins.{}'.format(name), {})
yield cfg
runtime_config_store['plugins'][name] = cfg
runtimeconf_persist()

View File

@@ -851,61 +851,90 @@ def isdown(argv, **args):
@pluginfunction('poll', 'create a poll', ptypes_COMMAND)
def poll(argv, **args):
pollcfg = config.runtimeconf_deepget('plugins.poll')
current_poll = pollcfg.get('subject') if pollcfg else None
with config.plugin_config('poll') as pollcfg:
current_poll = pollcfg.get('subject')
if not argv:
# return poll info
if not current_poll:
return {'msg': 'no poll running.'}
if not argv:
# return poll info
if not current_poll:
return {'msg': 'no poll running.'}
else:
return {'msg': 'current poll: {}'.format(current_poll)}
elif len(argv) == 1:
if argv[0] == 'stop':
if not current_poll:
return {'msg': 'no poll to stop.'}
pollcfg.clear()
return {'msg': 'stopped the poll "{}"'.format(current_poll)}
elif argv[0] == 'show_raw':
if not current_poll:
return {'msg': 'no poll to show.'}
return {'msg': 'current poll (raw): {}'.format(str(pollcfg))}
elif argv[0] == 'show':
if not current_poll:
return {'msg': 'no poll to show.'}
lines = ['current poll: "{}"'.format(current_poll)]
for option, voters in pollcfg.items():
if option == 'subject':
continue
lines.append('{0: <4} {1}'.format(len(voters), option))
return {'msg': lines}
if current_poll and argv[0] in pollcfg:
user = args['reply_user']
for option, voters in pollcfg.items():
if user in voters:
pollcfg[option].remove(user)
pollcfg[argv[0]] = list(set(pollcfg[argv[0]] + [user]))
return {'msg': 'voted.'}
else:
return {'msg': 'current poll: {}'.format(current_poll)}
elif len(argv) == 1:
if argv[0] == 'stop':
if not current_poll:
return {'msg': 'no poll to stop.'}
pollcfg.clear()
return {'msg': 'stopped the poll "{}"'.format(current_poll)}
elif argv[0] == 'show_raw':
if not current_poll:
return {'msg': 'no poll to show.'}
return {'msg': 'current poll (raw): {}'.format(str(pollcfg))}
elif argv[0] == 'show':
if not current_poll:
return {'msg': 'no poll to show.'}
lines = ['current poll: "{}"'.format(current_poll)]
for option, voters in pollcfg.items():
if option == 'subject':
continue
lines.append('{0: <4} {1}'.format(len(voters), option))
return {'msg': lines}
if current_poll and argv[0] in pollcfg:
user = args['reply_user']
for option, voters in pollcfg.items():
if user in voters:
pollcfg[option].remove(user)
pollcfg[argv[0]] = list(set(pollcfg[argv[0]] + [user]))
return {'msg': 'voted.'}
else:
subject = argv[0]
choices = argv[1:]
if len(choices) == 1:
return {'msg': 'creating a poll with a single option is "alternativlos"'}
else:
if current_poll:
return {'msg': 'a poll is already running ({})'.format(current_poll)}
if pollcfg is None:
pollcfg = dict()
# create an item for each option
pollcfg['subject'] = subject
pollcfg.update({k: [] for k in choices})
return {'msg': 'created the poll.'}
config.runtime_config_store['plugins']['poll'] = pollcfg
config.runtimeconf_persist()
subject = argv[0]
choices = argv[1:]
if len(choices) == 1:
return {'msg': 'creating a poll with a single option is "alternativlos"'}
else:
if current_poll:
return {'msg': 'a poll is already running ({})'.format(current_poll)}
# create an item for each option
pollcfg['subject'] = subject
pollcfg.update({k: [] for k in choices})
return {'msg': 'created the poll.'}
@pluginfunction('vote', 'alias for poll', ptypes_COMMAND)
@pluginfunction('vote', 'alias for poll', ptypes_COMMAND, enabled=False)
def vote(argv, **args):
return poll(argv, **args)
@pluginfunction('quiz', 'play quiz', ptypes_COMMAND, enabled=False)
def quiz(argv, **args):
usage = """quiz mode usage: "quiz start", "quiz stop", "quiz answer", "quiz skip";
if the quiz mode is active, sentences are parsed and compared against the answer.
"""
questions = [
'die antwort auf alle fragen?',
'keks',
'Welcher Stern ist der Erde am nächsten?',
'Sonne'
]
if not argv:
return usage
with config.plugin_config('quiz') as quizcfg:
if quizcfg is None:
quizcfg = dict()
if argv[0] == 'start':
# select a random question
used_ids = quizcfg['used_ids']
q_index = -1
while q_index == -1:
rand = random.choice(range(0, len(questions)-2, 2))
if rand not in used_ids:
q_index = rand
quizcfg['active_id'] = q_index
quizcfg['used_ids'] = used_ids + []
return {'msg': ['Q: {}'.format(questions[q_index]),
'A: {}'.format(questions[q_index+1]), ]}