from quizfactory import conf
from random import shuffle


class Game(object):

    quiz = None
    questions = None

    _pointer = 0

    DIRECTIONS = {"prev": -1, "next": 1}

    def __init__(self, key):
        self.quiz = conf.quizzes[key]

        self.questions = self.quiz.questions[:]
        shuffle(self.questions)

    @property
    def now_question(self):
        return self.questions[self._pointer]

    def change_pointer(self, pointer):
        if pointer in range(len(self.questions)):
            self._pointer = pointer
            return True
        else:
            return False

    def move(self, direction):
        try:
            pointer = self._pointer + self.DIRECTIONS[direction]
        except KeyError:
            return False
        else:
            return change_pointer(pointer)

    @property
    def is_last(self):
        return self._pointer < len(self.questions)

    @property
    def is_first(self):
        return self._pointer >= 0

    @property
    def pointer(self):
        return self._pointer
    

@@ -31,29 +31,1235 @@
 rom 
-random import shuffle
+hashlib import sha1%0Afrom random import shuffle, random%0A%0A%0Aclass GameQuestion(object):%0A    answers = None%0A    question = None%0A    choice = None%0A%0A    def __init__(self, q):%0A        self.question = q%0A        self.answers = %7B%0A            sha1(str(random()) + a.text).hexdigest()%5B:8%5D: a%0A            for a in q.answers%0A        %7D%0A%0A        t = self.question.answers_type%0A%0A        if t == %22radio%22:%0A            self.choice = %22%22  # nothing selected%0A        elif t == %22checkbox%22:%0A            self.choice = %5B%5D  # list of selected answers%0A        elif t == %22text%22:%0A            self.choice = %22%22  # text%0A%0A    def get_errors(self):%0A        %22%22%22Return list of errors (only key)%22%22%22%0A        t = self.question.answers_type%0A        choice = self.choice%0A        items = self.answers.items%0A%0A        if t == %22radio%22:%0A            good_answers = %5Bk for k, v in items() if v.is_correct%5D%0A            return %5Bself.choice%5D if choice in good_answers else %5B%5D%0A        elif t == %22checkbox%22:%0A            good_answers = %7Bk for k, v in items() if v.is_correct%7D%0A            return list(set(choice) & good_answers)%0A        elif t == %22text%22:%0A            return %5Bk for k, v in items() if v.is_correct and v.text == choice%5D%0A%0A        return %5B%5D  # if is OK - send empty list
 %0A%0A%0Ac
@@ -1310,24 +1310,62 @@
 tions = None
+%0A    answers = None%0A    finish = False
 %0A%0A    _point
@@ -1506,16 +1506,42 @@
 stions =
+ %5BGameQuestion(q) for q in
  self.qu
@@ -1552,18 +1552,16 @@
 uestions
-%5B:
 %5D%0A      
@@ -1577,32 +1577,96 @@
 (self.questions)
+%0A        self.answers = %5B%7B%7D for _ in range(len(self.questions))%5D
 %0A%0A    @property%0A
@@ -1677,11 +1677,11 @@
 def 
-now
+get
 _que
@@ -2120,16 +2120,21 @@
  return 
+self.
 change_p
@@ -2382,9 +2382,4 @@
 ter%0A
-    %0A

add GameQuestion model