Exemplo n.º 1
0
    def submitSolution(cls, instruction_uid=None, calendar=None):
        """Submits a solution to this do-puzzle, returns information
    about whether the solution is correct."""
        # get the instruction (and the next instruction if it extists)
        instruction = Instruction.getBy("uid", instruction_uid)
        next_instruction = Instruction.getBy("previous_uid", instruction_uid)
        if next_instruction:
            assert instruction.created_by == next_instruction.created_by

        # if the instruction was already completed, then we're done
        if instruction.state == "done":
            return {"submission": "success", "next_puzzle": cls.dequeueInstruction()}

        # make sure the calendar doesn't exist
        assert Calendar.getBy("uid", calendar.uid) == None

        # store the calendar
        calendar.put()

        # indicate that the next instruction should be processed
        def pass_batton(instruction, next_instruction, solution):
            instruction.soln_cal_uid = solution.uid
            instruction.state = "done"
            instruction.put()
            if not next_instruction:
                return
            next_instruction.calendar_uid = solution.uid
            assert instruction.previousChainAllDone()  # <- debug
            next_instruction.state = "processing"
            next_instruction.put()

        db.run_in_transaction(pass_batton, instruction, next_instruction, calendar)

        # update the player who typed this instruction
        logging.error("notifying: %s" % instruction.created_by)
        player_to_notify = Player.getByUser(instruction.created_by)
        player_channel = player_to_notify.getChannel()
        player_channel.postSolution(
            solved_instruction=instruction, solved_calendar=calendar, next_instruction=next_instruction
        )

        logging.error("TODO: update the player with a channel")
        #

        # return a new puzzle
        return {"submission": "success", "next_puzzle": cls.dequeueInstruction()}
Exemplo n.º 2
0
    def dequeueInstruction(cls):
        """Returns an instruction and calendar which is being processed."""
        # get the earliest-created instruction to process
        instructions = Instruction.all()
        instructions.filter("state =", "processing")
        instructions.order("created_on")  # earliest first
        instruction = instructions.get()

        # if none could be found, then recycle an old one
        if instruction == None:
            instructions = Instruction.all()
            instructions.filter("state =", "done")
            index = random.randint(0, instructions.count() - 1)
            instruction = instructions[index]

        # get the associated calendar
        calendar = Calendar.all().filter("uid =", instruction.calendar_uid).get()
        assert calendar, "Instruction does not have associated calendar: %s" % instruction.calendar_uid

        # return both
        return [instruction, calendar]