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

quiz frickel

This commit is contained in:
Thorsten
2016-01-31 21:18:09 +01:00
parent 400a77b1d5
commit e845e0c626
2 changed files with 63 additions and 51 deletions

View File

@@ -911,18 +911,12 @@ def vote(argv, **args):
@pluginfunction('quiz', 'play quiz', ptypes_COMMAND) @pluginfunction('quiz', 'play quiz', ptypes_COMMAND)
def quiz_control(argv, **args): def quiz_control(argv, **args):
usage = """quiz mode usage: "quiz start [secs interval]", "quiz stop", "quiz answer", "quiz skip", usage = """quiz mode usage: "quiz start [secs interval:default 30]", "quiz stop", "quiz rules;
"quiz rules; Not yet implemented: "quiz answer", "quiz skip".
if the quiz mode is active, sentences are parsed and compared against the answer. If the quiz mode is active, all messages 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: if not argv:
return usage return {'msg': usage}
rules = """ rules = """
The answers will be matched by characters/words. Answers will be The answers will be matched by characters/words. Answers will be
@@ -938,10 +932,12 @@ The answers will be matched by characters/words. Answers will be
quizcfg = dict() quizcfg = dict()
if argv[0] == 'start': if argv[0] == 'start':
interval = argv[1] if len(argv) > 1 else 60 quizcfg['stop_bit'] = False
return quiz.start_random_question(quizcfg, interval) interval = int(argv[1]) if len(argv) > 1 else 30
quizcfg['interval'] = interval
return quiz.start_random_question()
elif argv[0] == 'stop': elif argv[0] == 'stop':
return quiz.stop(quizcfg) return quiz.end(quizcfg)
elif argv[0] == 'answer': elif argv[0] == 'answer':
return quiz.answer(quizcfg) return quiz.answer(quizcfg)
elif argv[0] == 'skip': elif argv[0] == 'skip':

View File

@@ -6,6 +6,7 @@ from functools import lru_cache
import re import re
import time import time
from config import plugin_config
@lru_cache(10) @lru_cache(10)
@@ -40,41 +41,48 @@ def get_current_question(quizcfg):
return int(quizcfg['active_id']) return int(quizcfg['active_id'])
def end_question(quizcfg): def end_question():
lines = ['Question time over!'] with plugin_config('quiz') as quizcfg:
lines = ['Question time over!']
score = float(quizcfg.get('current_max_score', 0)) score = float(quizcfg.get('current_max_score', 0))
winner = quizcfg.get('current_max_user', 'nobody') winner = quizcfg.get('current_max_user', 'nobody')
print(winner, score) print(winner, score)
win_msg = '{} scores with {:.2f}%'.format(winner, score) win_msg = '{} scores with {:.2f}%'.format(winner, score)
lose_msg = 'nobody scores.' lose_msg = 'nobody scores.'
if score > 50.0: if score > 50.0:
lines.append(win_msg) lines.append(win_msg)
else: else:
lines.append(lose_msg) lines.append(lose_msg)
quizcfg['current_max_user'] = 'nobody' quizcfg["locked"] = False
quizcfg['current_max_score'] = 0 quizcfg['current_max_user'] = 'nobody'
quizcfg['current_max_score'] = 0
quizcfg['active_id'] = None quizcfg['active_id'] = None
start_next = None action = {
if not quizcfg.get('stop_bit', False): 'msg': lines
start_next = {
'time': time.time() + 10,
'command': (start_random_question, ([quizcfg],))
} }
return { if quizcfg.get('stop_bit', False):
'msg': lines, quizcfg['stop_bit'] = False
'event': start_next lines.append('stopping the quiz now.')
} else:
action['event'] = {
'time': time.time() + 10,
'command': (start_random_question, ([],))
}
lines.append('continuing.')
return action
def stop(quizcfg): def end(quizcfg):
# TODO: cleanup the switches
quizcfg['stop_bit'] = True quizcfg['stop_bit'] = True
return end_question(quizcfg) quizcfg['locked'] = False
def rate(quizcfg, response, user): def rate(quizcfg, response, user):
@@ -87,9 +95,9 @@ def rate(quizcfg, response, user):
""" """
questions = get_questions() questions = get_questions()
current_quiz_question = get_current_question(quizcfg) current_quiz_question = get_current_question(quizcfg)
answer = questions[current_quiz_question+1].lower() the_answer = questions[current_quiz_question+1].lower()
anwer_words = set(re.findall('[a-zA-ZäöüÄÖÜß]+', answer)) anwer_words = set(re.findall('[a-zA-ZäöüÄÖÜß0-9]+', the_answer))
words = set(response.lower().split()) words = set(response.lower().split())
# stripping all fill words seems like a tedious task... # stripping all fill words seems like a tedious task...
@@ -115,32 +123,40 @@ def rate(quizcfg, response, user):
# } # }
def start_random_question(quizcfg, interval): def start_random_question():
stop(quizcfg) with plugin_config('quiz') as quizcfg:
qa = get_random_question(quizcfg) if quizcfg.get("locked", False):
return {'msg': 'already running!'}
return { else:
'msg': ['Q: {}'.format(qa[0])], quizcfg["locked"] = True
'event': { qa = get_random_question(quizcfg)
'command': (end_question, ([quizcfg],)),
'time': time.time() + interval return {
'msg': ['Q: {}'.format(qa[0])],
'event': {
'command': (end_question, ([],)),
'time': time.time() + int(quizcfg['interval'])
}
} }
}
# TODO: fix those
def skip(quizcfg): def skip(quizcfg):
""" skip the current question, omitting the """ skip the current question, omitting the
answer and removing it from the used ones """ answer and removing it from the used ones """
raise NotImplementedError()
quizcfg['used_ids'].remove(get_current_question(quizcfg)) quizcfg['used_ids'].remove(get_current_question(quizcfg))
return end_question(quizcfg) return end_question()
def answer(quizcfg): def answer(quizcfg):
raise NotImplementedError()
the_answer = get_questions()[get_current_question(quizcfg)+1] the_answer = get_questions()[get_current_question(quizcfg)+1]
return { return {
'msg': 'Answer to the question: {}'.format(the_answer), 'msg': 'Answer to the question: {}'.format(the_answer),
'event': { 'event': {
'command': (end_question, ([quizcfg], )), 'command': (end_question, ([], )),
'time': time.time() + 3 'time': time.time() + 3
} }
} }