def save_game_setup(): global event_store form_data = request.form try: n_rounds = int(form_data.get("num_rounds")) app.logger.info("Saved n_rounds: %d" % n_rounds) # By using multiple form input fields with the same name we can use # getlist() to load a list of names and birth years. This allows # processing a form with an arbitrary number of player. names_and_birthyears = zip(form_data.getlist('player_name'), form_data.getlist('player_birthyear')) app.logger.debug(names_and_birthyears) # TODO(dlundquist): handle bad player data players = [Player(n, y) for n, y in names_and_birthyears if n] app.logger.info("Added players: %s" % [str(p) for p in players]) convo = Conversation.new_conversation(event_store, players, n_rounds) # Save conversation to session for k, v in convo.to_session_cookies().iteritems(): session[k] = v return redirect("/play", code=302) except ValueError as e: app.logger.error(e, exc_info=True) return render_template("setup.html", notice=("There was a problem with your input: " "%s" % e))
def play_game(): global events_store # Load conversation try: convo = Conversation.load_from_session(event_store, session) except ValueError as e: app.logger.error(e, exc_info=True) return render_template("setup.html", notice=("There was a problem loading your game " "state: %s" % e)) if convo.game_is_active(): player, event, question = convo.get_next_question() description = event.format_for_player(player) app.logger.info("Asking a question.") app.logger.info("Player %s, event %s, question %s" % (player.name, description, question)) # Save conversation to session -- to advance to next question for k, v in convo.to_session_cookies().iteritems(): session[k] = v return render_template("play.html", player_name=player.name, event=description, question=question, next_button_text="Next", next_button_target="/play") else: return end_game()
def end_game(): # Clear out game state from session for k in Conversation.session_cookie_keys(): session.pop(k) return render_template("feedback.html", event='Game over! Thanks for playing :)', next_button_text="Play again?", next_button_target="/setup")
def main(): print "Welcome! Lets get a little information before we start the conversation." test = input('Is this a test run? 1 = yes, 0 = no ') if test: players = [Player("Judy", 1982), Player("Rachel", 1986)] n_rounds = 3 else: players, n_rounds = setup_game() game = Conversation(n_rounds=n_rounds, players=players, min_age_to_play=7, events_file="data/firstHistoricClimateEvents.xlsx", min_q_age=10) e_indices, events = load_events(game, players, n_rounds) questions = load_questions(game, e_indices) n_events = len(events) if (n_events != len(questions)): sys.exit('ERROR: should have same number of questions as events.') delay_in_seconds = 1 for event_i in range(n_events): os.system('clear') print events[event_i] time.sleep(delay_in_seconds) print '' print questions[event_i] time.sleep(delay_in_seconds) raw_input( "\nPress enter when you are ready to move on to the next prompt.") print "\nThat's all! Thanks for playing."
class TestConversationWithSetup: ''' Test Conversation class constructor. ''' def setUp(self): es = EventStore.load_from_excel("tests/gdrive_frozen_20172806.xlsx") p = [Player("First player", 1980), Player("Second player", 1998)] self.c = Conversation(es, p, 3) def tearDown(self): pass # def test_initial_n_players(self): # assert self.c.n_players == 2 # def test_n_players_after_removal(self): # self.c.remove_player("First player", 1980) # assert self.c.n_players == 1 # def test_n_players_after_add(self): # p = Player("Third player", 1990) # self.c.add_player(p) # assert self.c.n_players == 3 # def test_valid_players(self): # current_year = datetime.now().year # for p in self.c.players: # assert len(p.name) > 0 # assert 1910 < p.birth_year < (current_year - self.c.min_q_age) # def test_reject_duplicate_player(self): # p = Player("First player", 1980) # assert self.c.add_player(p) is False # assert self.c.n_players == 2 def test_game_is_active(self): assert self.c.game_is_active()
def setUp(self): es = EventStore.load_from_excel("tests/gdrive_frozen_20172806.xlsx") p = [Player("First player", 1980), Player("Second player", 1998)] self.c = Conversation(es, p, 3)
class TestConversationWithoutSetup(object): ''' Conversation does not get regenerated for every test. ''' es = EventStore.load_from_excel("tests/gdrive_frozen_20172806.xlsx") p = [Player("First player", 1980), Player("Second player", 1998)] c = Conversation(es, p, 3)