Informatik10/quiz/neuesquiz_controller.py

140 lines
3.7 KiB
Python
Raw Normal View History

2022-01-16 20:52:19 +00:00
from random import choice
from neuesquiz_data import *
DEBUG = True
class Controller():
""" Controller of Application
Schnittstelle zwischen Daten und Anzeige
Steuerung des Programms
"""
def __init__(self, app):
self.app = app
self.data = Quizdata(self)
self.sample_size = 4
self.current_selection = dict() # {num: "name of president, …}
self.chosen_element = dict() # {num: "name of president"} single element
self.count_all = 0
self.count_correct = 0
self.test()
self.make_selection()
def test(self):
print("===TEST===")
self.data.test_get_random_sample()
print("===TEST===")
def reset_counter(self):
self.count_all = 0
self.count_correct = 0
def raise_counter(self, correct: bool):
self.count_all = self.count_all + 1
if correct:
self.count_correct = self.count_correct + 1
else:
pass
return 0
def get_count_all(self):
return self.count_all
def get_count_correct(self):
return self.count_correct
def make_selection(self):
# Select sample from the data
sample_size = self.sample_size
self.current_selection = self.data.get_random_sample_dict(sample_size)
# Select element of the sample to be asked for
self.chosen_element = self.data.get_single_from_sample_dict(self.current_selection)
# +++ DEBUG
if DEBUG:
print("Dict for selection: {}".format(self.current_selection))
print("Correct element: {}".format(self.chosen_element))
# --- DEBUG
def get_key_of_chosen(self):
key = list(self.chosen_element.keys())[0]
return key
# TODO: What happens if answer is selcted?
def select_answer(self, event=None):
pass
def check_user_selection(self):
# Set app to examination mode
self.app.change_state_examine()
# Get selection of user
user_selection = int(self.app.selected.get())
# Get key of chosen element
correct_answer = int(self.get_key_of_chosen())
#+++DEBUG
if DEBUG:
print("correct answer would be: {}".format(correct_answer))
print("user selected element: {}".format(user_selection))
#---DEBUG
if user_selection is correct_answer:
self.raise_counter(correct=True)
# +++ DEBUG
if DEBUG:
print("correct")
# --- DEBUG
self.app.set_response_correct()
self.app.update_counter()
else:
self.raise_counter(correct=False)
# +++ DEBUG
if DEBUG:
print("incorrect")
# --- DEBUG
self.app.set_response_incorrect()
self.app.update_counter()
self.start_timer_next()
return 0
def start_timer_next(self):
# automatisches Stellen der nächsten Frage
seconds_to_next = 5
self.timer = self.app.after(seconds_to_next*1000,self.next_question)
#+++DEBUG
if DEBUG:
print("timer of {} seconds to next question started".format(seconds_to_next))
#---DEBUG
return self.timer
def stop_timer_next(self):
self.app.after_cancel(self.timer)
#+++DEBUG
if DEBUG:
print("timer stopped")
#---DEBUG
return 0
def next_question(self):
self.stop_timer_next()
self.make_selection()
self.app.update_answers()
self.app.change_state_user_input()
# TODO: implement win
def win(self):
print("Gewonnen!")
pass
# TODO: reset of all elements
def reset(self):
pass