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
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")
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)))
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))
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)
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)
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!")
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)
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!")
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))
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))
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])
def renderQuestion(self, questionId, admin=False): if questionId in self.questionAnswers: return self.questionAnswers[questionId].renderQuestion(admin) raise ErrorMessage("Invalid question: %s" % (questionId))
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()
def handleSplitMessage(server, command, mList, time): if command in COMMANDS: COMMANDS[command].checkAndRun(server, mList, time) else: raise ErrorMessage("Unknown command: %s" % command)