示例#1
0
文件: teams.py 项目: samr85/KPH
 def createTeam(self, name, password, fullName=None):
     """ Make a new team """
     with self.lock:
         name = html.escape(name)
         if name in self.teamList:
             raise ErrorMessage("A team of that name already exists")
         if name.lower() in ["all", "admin"]:
             raise ErrorMessage("Team name %s is not allowed" % (name))
         print("Creating new team: %s" % (name))
         newTeam = Team(name, password, fullName)
         self.teamList[name] = newTeam
         return newTeam
示例#2
0
文件: answers.py 项目: samr85/KPH
    def requestHint(self):
        if self.correct():
            raise ErrorMessage(
                "Attempting to request a hint for an already correct question")
        if self.hintCount < len(self.question.hints):
            self.hintCount += 1
            self.team.notifyTeam("Hint for puzzle %s unlocked" %
                                 (self.question.name))
            self.update()

        else:
            raise ErrorMessage("All hints already requested")
示例#3
0
def markAnswer(_server, messageList, _time):
    """ Message from the admins specifying if submitted answer is correct or not """
    # messageList = teamName questionName correct/incorrect [mark]
    if len(messageList) == 3:
        CTX.answerQueue.markAnswer(messageList[0], messageList[1], messageList[2].lower() == "correct", 0)
    elif len(messageList) == 4:
        try:
            score = int(messageList[3])
        except ValueError:
            raise ErrorMessage("Invalid number for score: %s"%(messageList[3]))
        CTX.answerQueue.markAnswer(messageList[0], messageList[1], messageList[2].lower() == "correct", score)
    else:
        raise ErrorMessage("Incorrect number of parameters to markAnswer (got %d)!"%(len(messageList)))
示例#4
0
文件: teams.py 项目: samr85/KPH
 def getTeam(self, name, password=None):
     """ Get the team structure from the name.  Check the team's password if one is given """
     with self.lock:
         # Might have to html escape the name, if this was called manually
         if html.escape(name) in self.teamList:
             name = html.escape(name)
         if name in self.teamList:
             team = self.teamList[name]
             if password != None and password != team.password:
                 raise ErrorMessage("Invalid password")
             return team
         else:
             raise ErrorMessage("Team %s doesn't exist!" % (name))
示例#5
0
文件: teams.py 项目: samr85/KPH
 def submitAnswer(self, questionId, answerString, time):
     if questionId not in self.questionAnswers:
         raise ErrorMessage("Team does not have access to question: %s" %
                            (questionId))
     answerItem = self.questionAnswers[questionId]
     answerString = html.unescape(answerString)
     answerItem.submitAnswer(re.sub(r'\W+', '', answerString), time)
示例#6
0
文件: answers.py 项目: samr85/KPH
 def queueAnswer(self, newAnswer):
     """ Team is wanting a question to be marked """
     with self.lock:
         for answer in self.answerList:
             if answer.team == newAnswer.team:
                 raise ErrorMessage(
                     "Cannot submit another answer until your previous one has been marked (answer: %s pending for question: %s)"
                     % (answer.answer, answer.question.name))
         self.answerList.append(newAnswer)
示例#7
0
文件: answers.py 项目: samr85/KPH
 def submitAnswer(self, answerString, time):
     if not self.enabled:
         raise ErrorMessage("Cannot submit answer to disabled question")
     msg = self.question.submissionCheck(self, answerString, time)
     if msg:
         raise ErrorMessage(msg)
     if self.status == INCORRECT:
         self.answer = answerString
         CTX.answerQueue.queueAnswer(self)
         self.status = SUBMITTED
         if time:
             self.answeredTime = time
         else:
             self.answeredTime = datetime.datetime.now()
         self.question.submitAnswer(self)
         self.update()
     else:
         raise ErrorMessage(
             "Can't submit an answer to an already answered question!")
示例#8
0
    def checkAndRun(self, server, messageList, time):
        if self.messageListLen >= 0:
            if len(messageList) != self.messageListLen:
                raise ErrorMessage("Invalid parameters for %s: %d required" %
                                   (self.name, self.messageListLen))
        if self.teamRequired and not server.team:
            raise ErrorMessage(
                "Cannot do %s if you're not logged in as a team" % (self.name))
        if self.adminRequired and not server.admin:
            raise ErrorMessage("Cannot do %s if you're not admin" %
                               (self.name))

        if self.logMessage:
            self.log(server, messageList, time)

        if self.teamRequired and server.team:
            with server.team.lock:
                self.function(server, messageList, time)
        else:
            self.function(server, messageList, time)
示例#9
0
文件: answers.py 项目: samr85/KPH
 def markAnswer(self, teamName, questionId, mark, value=0):
     """ Admin has replied to the mark request """
     found = False
     with self.lock:
         for answer in self.answerList:
             if answer.team.name == teamName and answer.question.id == questionId:
                 found = answer
                 self.answerList.remove(answer)
                 break
     if found:
         # Can't lock team while we have the answerSubQueue lock
         with found.team.lock:
             found.mark(mark, value)
     else:
         raise ErrorMessage("Answer not found to mark!")
示例#10
0
def sendDummyMessage(message, messageList, team=None, admin=False, time=None):
    if message in COMMANDS:

        class DummyServer:
            def __init__(self, team, admin):
                self.team = team
                self.admin = admin

            def write_message(self, _message, _binary=False):
                pass

            def sendRefresh(self):
                pass

        server = DummyServer(team, admin)
        if not time:
            time = datetime.datetime.now()
        handleSplitMessage(server, message, messageList, time)
    else:
        raise ErrorMessage("Invalid command: %s" % (message))
示例#11
0
def adjustAnswer(_server, messageList, _time):
    """ Modify the answer referring to a team's submission """
    adjustType = messageList[0]
    questionId = messageList[1]
    teamName = messageList[2]
    newValue = messageList[3]
    team = CTX.teams.getTeam(teamName)
    try:
        answer = team.questionAnswers[questionId]
    except KeyError:
        raise ErrorMessage("Invalid questionId: %s"%(questionId))
    if adjustType == "hintLevel":
        try:
            newValue = int(newValue)
        except ValueError:
            raise ErrorMessage("Invalid hint level: %s"%(newValue))
        if newValue > len(answer.question.hints):
            raise ErrorMessage("Hint level too high: %d/%d"%(newValue, len(answer.question.hints)))
        answer.hintCount = newValue
        answer.update()
    elif adjustType == 'score':
        try:
            newValue = int(newValue)
        except ValueError:
            raise ErrorMessage("Invalid score: %s"%(newValue))
        answer.score = newValue
        answer.update()
    elif adjustType == 'mark':
        if newValue == "true":
            mark = True
        elif newValue == "false":
            mark = False
        else:
            raise ErrorMessage("Invalid true/false: %s"%(newValue))
        answer.mark(mark, 0)
    else:
        raise ErrorMessage("Unknown command: %s"%(adjustType))
示例#12
0
文件: sections.py 项目: samr85/KPH
 def requestUpdateList(self, requestor):
     if self.name not in requestor.sectionAdditionalInformation:
         raise ErrorMessage("You've not requested what team to view")
     return self.requestUpdateListSegment(
         requestor, requestor.sectionAdditionalInformation[self.name])
示例#13
0
文件: teams.py 项目: samr85/KPH
 def renderQuestion(self, questionId, admin=False):
     if questionId in self.questionAnswers:
         return self.questionAnswers[questionId].renderQuestion(admin)
     raise ErrorMessage("Invalid question: %s" % (questionId))
示例#14
0
文件: teams.py 项目: samr85/KPH
 def requestHint(self, questionId):
     if questionId not in self.questionAnswers:
         raise ErrorMessage("Team does not have access to question: %s" %
                            (questionId))
     self.questionAnswers[questionId].requestHint()
示例#15
0
def handleSplitMessage(server, command, mList, time):
    if command in COMMANDS:
        COMMANDS[command].checkAndRun(server, mList, time)
    else:
        raise ErrorMessage("Unknown command: %s" % command)