def test_score_count_empty_score_values(self): row = {'some': 'value'} leader_board_entry = LeaderboardEntry(row) bonus = ScoreService.count_bonus(leader_board_entry) final_score = ScoreService.count_final_score(leader_board_entry, bonus) self.assertEqual(bonus, 0) self.assertEqual(final_score, 0)
def leader_board(self): """returns sorted by score descending list of user scores :return: Loaded LeaderboardEntry list """ return sorted( [LeaderboardEntry(row) for row in self.__loaded_leader_board], key=lambda x: x.score, reverse=True)
def test_score_count_happy_path(self): row = {LeaderboardEntry.LEVEL: 1, LeaderboardEntry.POWER_UPS: 1, LeaderboardEntry.HITS: 18, LeaderboardEntry.SCORE: 200} leader_board_entry = LeaderboardEntry(row) bonus = ScoreService.count_bonus(leader_board_entry) final_score = ScoreService.count_final_score(leader_board_entry, bonus) self.assertEqual(bonus, 9) self.assertEqual(final_score, 209)
def test_score_count_invalid_score_values(self): row = {LeaderboardEntry.LEVEL: -20, LeaderboardEntry.POWER_UPS: 8, LeaderboardEntry.HITS: 5, LeaderboardEntry.SCORE: -50} leader_board_entry = LeaderboardEntry(row) bonus = ScoreService.count_bonus(leader_board_entry) final_score = ScoreService.count_final_score(leader_board_entry, bonus) self.assertEqual(bonus, 0) self.assertEqual(final_score, 0)
def setUp(self): self.base_test_case_data = os.path.join(os.path.dirname(__file__), 'test_data') self.test_file = os.path.join(self.base_test_case_data, 'empty.csv') self.expected_file = os.path.join(self.base_test_case_data, 'expected_file.csv') self.examplary_data_record = LeaderboardEntry({ LeaderboardEntry.DATE: '2017-02-06 02:13:36.082461', LeaderboardEntry.PLAYER_NAME: 'anonymous player ', LeaderboardEntry.LEVEL: '5', LeaderboardEntry.POWER_UPS: '6', LeaderboardEntry.HITS: '10', LeaderboardEntry.SCORE: '20' })
def test_invalid_data_exception(self): row = None with self.assertRaises(TypeError): leader_board_entry = LeaderboardEntry(row) ScoreService.count_bonus(leader_board_entry)
def run(self, clock, screen, args=None): entry = LeaderboardEntry(args) bonus = ScoreService.count_bonus(entry) entry.score = ScoreService.count_final_score(entry, bonus) self.__leaderBoardService.load_leader_board() text_input = TextInput(font_size=25, antialias=True, text_color=GameColors.WHITE) running = True next_screen = GameScreens.LEADER_BOARD_SCREEN user_entered_nick = '' while running: clock.tick(GameSettings.FPS) events = pygame.event.get() for event in pygame.event.get(): if event.type == pygame.QUIT: next_screen = GameScreens.QUIT_GAME running = False if event.type == pygame.KEYUP: if event.key == pygame.K_SPACE: running = False user_accepted_text = text_input.update(events) screen.fill(GameColors.BLACK) screen.blit( self.__resourceContext.imgResources.background, self.__resourceContext.imgResources.background.get_rect()) self.draw_text( screen, self.__localizationContext.game_over_screen.title_label, 50, GameSettings.WIDTH // 2, GameSettings.HEIGHT // 2 - 300) self.draw_text( screen, self.__localizationContext.game_over_screen.score_label + str(entry.score), 50, GameSettings.WIDTH // 2, GameSettings.HEIGHT // 2 - 200) self.draw_text( screen, self.__localizationContext.game_over_screen.bonus_label + str(bonus), 18, GameSettings.WIDTH // 2, GameSettings.HEIGHT // 2 - 150) self.draw_text( screen, self.__localizationContext.game_over_screen.level_label + str(entry.level), 25, GameSettings.WIDTH // 2, GameSettings.HEIGHT // 2 - 100) self.draw_text( screen, self.__localizationContext.game_over_screen.hits_label + str(entry.hits), 25, GameSettings.WIDTH // 2, GameSettings.HEIGHT // 2 - 50) self.draw_text( screen, self.__localizationContext.game_over_screen.power_ups_label + str(entry.power_ups), 25, GameSettings.WIDTH // 2, GameSettings.HEIGHT // 2) self.draw_text( screen, self.__localizationContext.game_over_screen.name_enter_label, 25, GameSettings.WIDTH // 2, GameSettings.HEIGHT // 2 + 100) screen.blit( text_input.get_surface(), (GameSettings.WIDTH // 2 - 50, GameSettings.HEIGHT // 2 + 150)) if len(user_entered_nick) > 0: self.draw_text( screen, self.__localizationContext.game_over_screen.continue_label, 25, GameSettings.WIDTH // 2, GameSettings.HEIGHT // 2 + 200) pygame.display.flip() user_entered_nick = text_input.get_text() if user_accepted_text and len(user_entered_nick) > 0: running = False entry.player_name = user_entered_nick self.__leaderBoardService.add_entry(entry) self.__leaderBoardService.persist_leader_board() return {BaseScreen.SCREEN_NEXT: next_screen, "entry": entry}