def set_answer(self, storage: StorageAPI, index, value): if not CheckWitness(self.owner): print("must be owner to set answer") return False clue_str = concat('answer', index) storage.putitem(clue_str, value) return b'Set Answer'
def set_clue(self, storage: StorageAPI, index, value): if not CheckWitness(self.owner): print("must be owner to set clue") return False clue_str = concat('clue', index) storage.putitem(clue_str, value) return True
def set_progress(self, storage: StorageAPI, index, addr): if CheckWitness(self.owner): progress_key = concat('progress_', addr) current_val = storage.getitem(progress_key) # we don't want people to go backwards if they answer an old question if index > current_val: storage.putitem(progress_key, index) return True return False
def submit_answer(self, storage: StorageAPI, index, answer, address): if address == 0: return b'Please attach at least .02 gas to submit an answer' can_submit_answer = self.get_progress(storage, address, index) if not can_submit_answer: return b'You must answer all previous questions before answering this one' hash_answer = hash256(answer) answer_key = concat('answer', index) real_answer = storage.getitem(answer_key) # Notify(hash_answer) if hash_answer == real_answer: next = index + 1 if next == 5: total_winners = self.check_winners(storage, address) if total_winners == 1: message = b'You have won 1st prize in the scavenger hunt! Please contact [email protected] to claim your prize.' elif total_winners == 2: message = b'You have won 2nd prize in the scavenger hunt! Please contact [email protected] to claim your prize.' elif total_winners == 3: message = b'You have won 3rd place in the scavenger hunt! Please contact [email protected] to claim your prize' elif total_winners == 10000: message = b'You are the runner of this contract. You cant win.' elif total_winners == 0: message = b'You already won.' else: message = b'You have finished the scavenger hunt! Unfortunately others have beaten you. Thanks for trying' else: ok = self.set_progress(storage, next, address) message = concat( b'Answer Correct! You may now move on to question ', next) return message return b'Incorrect Answer'
def Main(operation, args): trigger = GetTrigger() if trigger == Verification(): if CheckWitness(owner): return True return False storage = StorageAPI() questions = Questions() questions.owner = owner if operation == 'get_clue': addr = get_addr(clue_gas_req) cluenum = args[0] return questions.get_clue(storage, cluenum, addr) elif operation == 'set_clue': cluenum = args[0] clueval = args[1] return questions.set_clue(storage, cluenum, clueval) elif operation == 'set_answer': cluenum = args[0] answer = args[1] return questions.set_answer(storage, cluenum, answer) elif operation == 'submit_answer': addr = get_addr(answer_gas_req) if len(args) < 2: return b'Not enough arguments' cluenum = args[0] answer = args[1] return questions.submit_answer(storage, cluenum, answer, addr) elif operation == 'total_questions': return total_q elif operation == 'progress': addr = get_addr(progress_gas_req) key = concat('progress_', addr) return storage.getitem(key) elif operation == 'total_winners': return storage.getitem('total_winners') elif operation == 'first_place': return storage.getitem('first_place') elif operation == 'second_place': return storage.getitem('second_place') elif operation == 'third_place': return storage.getitem('third_place') return b'Method not found'
def get_progress(self, storage: StorageAPI, address, required): key = concat('progress_', address) current = storage.getitem(key) if required > current: return False return True
def get_clue(self, storage: StorageAPI, index, address): if address == 0: return b'Please attach at least .01 gas to get this clue' can_get_clue = self.get_progress(storage, address, index) if not can_get_clue: return b'You must answer all previous questions before getting this one' clue_str = concat('clue', index) return storage.getitem(clue_str)
def check_winners(self, storage: StorageAPI, address): if address == self.owner: return 10000 place_key = concat('place', address) current_place = storage.getitem(place_key) # if they aleady won, we don't want to let them do it again if current_place > 0: return 0 total_winners = storage.getitem('total_winners') place = total_winners + 1 storage.putitem('total_winners', place) storage.putitem(place_key, place) # persist winners if place == 1: storage.putitem('first_place', address) elif place == 2: storage.putitem('second_place', address) elif place == 3: storage.putitem('third_place', address) return place