def start(self, client_mono_time_sec): if self.state != State.NotYetStarted: return hp.result({"reason": "The game has already been started."}, ok=False) self.state = State.Started self.start_client_mono_time_sec = client_mono_time_sec self.start_server_mono_time_sec = time.monotonic() self.start_server_date_time = datetime.datetime.now() self.game_duration_sec = 30.0 self.generated_questions = gen_stock_data_for_player() return hp.result({"game_duration_sec": self.game_duration_sec, "question_list": self.generated_questions})
def choice(self, client_mono_time_sec, event_data): if self.state != State.Started: return hp.result({"reason": "The game has not yet been started."}, ok=False) if client_mono_time_sec - self.start_client_mono_time_sec >= self.game_duration_sec + self.latency_epsilon_sec: self.state = State.Finished return hp.result({"reason": "This game has expired"}, ok=False) question_index = int(event_data["question_index"]) if question_index + 1 == len(self.generated_questions): self.state = State.Finished answer_index = int(event_data["answer_index"]) choices = self.generated_questions[question_index] if choices[answer_index]["d_cents_pc"] > choices[(answer_index + 1) % 2]["d_cents_pc"]: score_reward = 100 else: score_reward = 0 self.score += score_reward return hp.result({"score_reward": score_reward})