Esempio n. 1
0
 def get_players_data(self, connection, app_id, clan_id, members):
     players_data = []
     for member in members:
         account_id = member['account_id']
         account_name = member['account_name']
         try:
             player_data = self.get_player_data(connection, app_id, clan_id, account_id, account_name)
             if self.requires_recalculation(player_data):
                 # get accounts stat
                 account_stats = self._retrieve_account_stats(account_id, app_id)
                 player_data = AccountStatsToPlayerDataMapper.map(account_stats, account_id, account_name)
                 personal_stats = self._retrieve_personal_stats(account_id, app_id)
                 player_data = AccountStatsToPlayerDataMapper.amend_player_data(player_data, personal_stats, account_id)
                 # create snapshot
                 self.create_snapshot(connection, player_data)
                 # calculate 30 day
                 from_date = AppTime.get_now() - datetime.timedelta(days=30)
                 self._calculate_stats_at_date(connection, player_data, from_date)
                 # update player_stat
                 PlayerRepository.update_player_stats(connection, player_data)
             players_data.append(player_data)
         except Exception as e:
             print('Failed on player: {account_name} - error: {error}'
                   .format(account_name=member['account_name'], error=str(e)))
     players_data.sort(key=lambda x: x['account_name'].lower())
     return players_data
Esempio n. 2
0
 def test_create_player_stat(self):
     TestTime.set_app_time(datetime.datetime(2015, 8, 1))
     DatabaseUtils.recreate_database(DatabaseUtils.get_db_connection())
     conn = DatabaseUtils.get_db_connection()
     player_data = {
         'clan_id': 1,
         'account_id': 777,
         'account_name': 'test player',
         'total_resources_earned': 1200,
         'stronghold_skirmish_battles': 100,
         'stronghold_defense_battles': 10,
         'seven_day_resources_earned': 320,
         'thirty_day_resources_earned': 320,
         'last_update': AppTime.get_now(),
         'thirty_day_defense_battles': 0,
         'thirty_day_skirmish_battles': 0,
         'clan_battles': 10,
         'thirty_day_clan_battles': 5,
         'all_battles': 100,
         'thirty_day_all_battles': 50
     }
     controller = ClanController()
     controller.create_player_stat(conn, player_data)
     row = PlayerRepository.get_player(conn, player_data['clan_id'],
                                       player_data['account_id'])
     player_stat_dict = dict(zip(row.keys(), row))
     expected = {
         'clan_id': 1,
         'account_id': 777,
         'account_name': u'test player',
         'total_resources_earned': 1200,
         'stronghold_skirmish_battles': 100,
         'thirty_day_skirmish_battles': 0,
         'stronghold_defense_battles': 10,
         'thirty_day_defense_battles': 0,
         'seven_day_resources_earned': 320,
         'thirty_day_resources_earned': 320,
         'clan_battles': 10,
         'thirty_day_clan_battles': 5,
         'all_battles': 100,
         'thirty_day_all_battles': 50,
         'last_update': str(AppTime.get_now())
     }
     self.assertEqual(expected, player_stat_dict)
     from_date = AppTime.get_now()
     to_date = from_date - datetime.timedelta(days=30)
     rows = PlayerRepository.get_player_stats(conn, player_data['clan_id'],
                                              player_data['account_id'],
                                              from_date, to_date)
     self.assertEqual(1, len(rows))
     player_stat_snapshot_dict = dict(zip(rows[0].keys(), rows[0]))
     expected = {
         'stronghold_defense_battles': 10,
         'account_id': 777,
         'stronghold_skirmish_battles': 100,
         'clan_id': 1,
         'total_resources_earned': 1200,
         'created_date': str(AppTime.get_now())
     }
     self.assertEqual(expected, player_stat_snapshot_dict)
Esempio n. 3
0
 def get_players_data(self, connection, app_id, clan_id, members):
     players_data = []
     for member in members:
         account_id = member['account_id']
         account_name = member['account_name']
         try:
             player_data = self.get_player_data(connection, app_id, clan_id,
                                                account_id, account_name)
             if self.requires_recalculation(player_data):
                 # get accounts stat
                 account_stats = self._retrieve_account_stats(
                     account_id, app_id)
                 player_data = AccountStatsToPlayerDataMapper.map(
                     account_stats, account_id, account_name)
                 personal_stats = self._retrieve_personal_stats(
                     account_id, app_id)
                 player_data = AccountStatsToPlayerDataMapper.amend_player_data(
                     player_data, personal_stats, account_id)
                 # create snapshot
                 self.create_snapshot(connection, player_data)
                 # calculate 30 day
                 from_date = AppTime.get_now() - datetime.timedelta(days=30)
                 self._calculate_stats_at_date(connection, player_data,
                                               from_date)
                 # update player_stat
                 PlayerRepository.update_player_stats(
                     connection, player_data)
             players_data.append(player_data)
         except Exception as e:
             print(
                 'Failed on player: {account_name} - error: {error}'.format(
                     account_name=member['account_name'], error=str(e)))
     players_data.sort(key=lambda x: x['account_name'].lower())
     return players_data
Esempio n. 4
0
 def create_snapshot(self, connection, player_data):
     row = [player_data['clan_id'],
            player_data['account_id'],
            player_data['total_resources_earned'],
            player_data['stronghold_defense_battles'],
            player_data['stronghold_skirmish_battles'],
            player_data['clan_battles'],
            player_data['all_battles'],
            player_data['last_update']]
     PlayerRepository.create_player_stat_snapshot(connection, row)
Esempio n. 5
0
 def create_snapshot(self, connection, player_data):
     row = [
         player_data['clan_id'], player_data['account_id'],
         player_data['total_resources_earned'],
         player_data['stronghold_defense_battles'],
         player_data['stronghold_skirmish_battles'],
         player_data['clan_battles'], player_data['all_battles'],
         player_data['last_update']
     ]
     PlayerRepository.create_player_stat_snapshot(connection, row)
 def test_create_player_stat(self):
     TestTime.set_app_time(datetime.datetime(2015, 8, 1))
     DatabaseUtils.recreate_database(DatabaseUtils.get_db_connection())
     conn = DatabaseUtils.get_db_connection()
     player_data = {'clan_id': 1,
                    'account_id': 777,
                    'account_name': 'test player',
                    'total_resources_earned': 1200,
                    'stronghold_skirmish_battles': 100,
                    'stronghold_defense_battles': 10,
                    'seven_day_resources_earned': 320,
                    'thirty_day_resources_earned': 320,
                    'last_update': AppTime.get_now(),
                    'thirty_day_defense_battles': 0,
                    'thirty_day_skirmish_battles': 0,
                    'clan_battles': 10,
                    'thirty_day_clan_battles': 5,
                    'all_battles': 100,
                    'thirty_day_all_battles': 50}
     controller = ClanController()
     controller.create_player_stat(conn, player_data)
     row = PlayerRepository.get_player(conn, player_data['clan_id'], player_data['account_id'])
     player_stat_dict = dict(zip(row.keys(), row))
     expected = {'clan_id': 1,
                 'account_id': 777,
                 'account_name': u'test player',
                 'total_resources_earned': 1200,
                 'stronghold_skirmish_battles': 100,
                 'thirty_day_skirmish_battles': 0,
                 'stronghold_defense_battles': 10,
                 'thirty_day_defense_battles': 0,
                 'seven_day_resources_earned': 320,
                 'thirty_day_resources_earned': 320,
                 'clan_battles': 10,
                 'thirty_day_clan_battles': 5,
                 'all_battles': 100,
                 'thirty_day_all_battles': 50,
                 'last_update': str(AppTime.get_now())}
     self.assertEqual(expected, player_stat_dict)
     from_date = AppTime.get_now()
     to_date = from_date - datetime.timedelta(days=30)
     rows = PlayerRepository.get_player_stats(conn, player_data['clan_id'], player_data['account_id'], from_date,
                                              to_date)
     self.assertEqual(1, len(rows))
     player_stat_snapshot_dict = dict(zip(rows[0].keys(), rows[0]))
     expected = {'stronghold_defense_battles': 10,
                 'account_id': 777,
                 'stronghold_skirmish_battles': 100,
                 'clan_id': 1,
                 'total_resources_earned': 1200,
                 'created_date': str(AppTime.get_now())}
     self.assertEqual(expected, player_stat_snapshot_dict)
Esempio n. 7
0
 def _calculate_stats_at_date(self, connection, player_data, from_date):
     resources,  defenses, skirmishes, clan, all_battles = PlayerRepository.get_stats_at_date(connection, player_data['clan_id'],
                                                                                              player_data['account_id'],
                                                                                              from_date)
     player_data['thirty_day_resources_earned'] = player_data['total_resources_earned'] - resources
     player_data['thirty_day_defense_battles'] = player_data['stronghold_defense_battles'] - defenses
     player_data['thirty_day_skirmish_battles'] = player_data['stronghold_skirmish_battles'] - skirmishes
     player_data['thirty_day_clan_battles'] = player_data['clan_battles'] - clan
     player_data['thirty_day_all_battles'] = player_data['all_battles'] - all_battles
Esempio n. 8
0
 def get_player_data(self, connection, app_id, clan_id, account_id, account_name):
     record = PlayerRepository.get_player(connection, clan_id, account_id)
     if record is None:
         account_stats = self._retrieve_account_stats(account_id, app_id)
         player_data = AccountStatsToPlayerDataMapper.map(account_stats, account_id, account_name)
         personal_stats = self._retrieve_personal_stats(account_id, app_id)
         player_data = AccountStatsToPlayerDataMapper.amend_player_data(player_data, personal_stats, account_id)
         self.create_player_stat(connection, player_data)
     else:
         player_data = RecordToPlayerDataMapper.map(record)
     return player_data
Esempio n. 9
0
 def get_player_data(self, connection, app_id, clan_id, account_id,
                     account_name):
     record = PlayerRepository.get_player(connection, clan_id, account_id)
     if record is None:
         account_stats = self._retrieve_account_stats(account_id, app_id)
         player_data = AccountStatsToPlayerDataMapper.map(
             account_stats, account_id, account_name)
         personal_stats = self._retrieve_personal_stats(account_id, app_id)
         player_data = AccountStatsToPlayerDataMapper.amend_player_data(
             player_data, personal_stats, account_id)
         self.create_player_stat(connection, player_data)
     else:
         player_data = RecordToPlayerDataMapper.map(record)
     return player_data
Esempio n. 10
0
 def _calculate_stats_at_date(self, connection, player_data, from_date):
     resources, defenses, skirmishes, clan, all_battles = PlayerRepository.get_stats_at_date(
         connection, player_data['clan_id'], player_data['account_id'],
         from_date)
     player_data['thirty_day_resources_earned'] = player_data[
         'total_resources_earned'] - resources
     player_data['thirty_day_defense_battles'] = player_data[
         'stronghold_defense_battles'] - defenses
     player_data['thirty_day_skirmish_battles'] = player_data[
         'stronghold_skirmish_battles'] - skirmishes
     player_data[
         'thirty_day_clan_battles'] = player_data['clan_battles'] - clan
     player_data['thirty_day_all_battles'] = player_data[
         'all_battles'] - all_battles
Esempio n. 11
0
 def test_get_player_data_that_is_not_in_database(self):
     TestTime.set_app_time(datetime.datetime(2015, 8, 1))
     conn = DatabaseUtils.get_db_connection()
     DatabaseUtils.recreate_database(conn)
     conn = DatabaseUtils.get_db_connection()
     controller = ClanController()
     controller._retrieve_account_stats = lambda account_id, app_id: AccountStatsFixtures.standard_accounts_record(
     )
     controller._retrieve_personal_stats = lambda account_id, app_id: AccountStatsFixtures.personal_data_response(
     )
     player_data = controller.get_player_data(conn, 'app_id', 500050913,
                                              512841364, 'test_player')
     expected = {
         "clan_id": 500050913,
         "account_id": 512841364,
         "account_name": 'test_player',
         "total_resources_earned": 5002,
         "stronghold_defense_battles": 9,
         "thirty_day_defense_battles": 0,
         "stronghold_skirmish_battles": 731,
         "thirty_day_skirmish_battles": 0,
         "seven_day_resources_earned": 0,
         "thirty_day_resources_earned": 0,
         'clan_battles': 46,
         'thirty_day_clan_battles': 0,
         'all_battles': 25346,
         'thirty_day_all_battles': 0,
         "last_update": AppTime.get_now()
     }
     self.maxDiff = None
     self.assertEqual(expected, player_data)
     from_date = AppTime.get_now()
     to_date = from_date - datetime.timedelta(days=30)
     rows = PlayerRepository.get_player_stats(conn, player_data['clan_id'],
                                              player_data['account_id'],
                                              from_date, to_date)
     self.assertEqual(1, len(rows))
     player_stat_snapshot_dict = dict(zip(rows[0].keys(), rows[0]))
     expected = {
         'stronghold_defense_battles': 9,
         'account_id': 512841364,
         'stronghold_skirmish_battles': 731,
         'clan_id': 500050913,
         'total_resources_earned': 5002,
         'created_date': str(AppTime.get_now())
     }
     self.assertEqual(expected, player_stat_snapshot_dict)
Esempio n. 12
0
 def test_get_player_data_that_is_not_in_database(self):
     TestTime.set_app_time(datetime.datetime(2015, 8, 1))
     conn = DatabaseUtils.get_db_connection()
     DatabaseUtils.recreate_database(conn)
     conn = DatabaseUtils.get_db_connection()
     controller = ClanController()
     controller._retrieve_account_stats = lambda account_id, app_id: AccountStatsFixtures.standard_accounts_record()
     controller._retrieve_personal_stats = lambda account_id, app_id: AccountStatsFixtures.personal_data_response()
     player_data = controller.get_player_data(conn, 'app_id', 500050913, 512841364, 'test_player')
     expected = {
         "clan_id": 500050913,
         "account_id": 512841364,
         "account_name": 'test_player',
         "total_resources_earned": 5002,
         "stronghold_defense_battles": 9,
         "thirty_day_defense_battles": 0,
         "stronghold_skirmish_battles": 731,
         "thirty_day_skirmish_battles": 0,
         "seven_day_resources_earned": 0,
         "thirty_day_resources_earned": 0,
         'clan_battles': 46,
         'thirty_day_clan_battles': 0,
         'all_battles': 25346,
         'thirty_day_all_battles': 0,
         "last_update": AppTime.get_now()
     }
     self.maxDiff = None
     self.assertEqual(expected, player_data)
     from_date = AppTime.get_now()
     to_date = from_date - datetime.timedelta(days=30)
     rows = PlayerRepository.get_player_stats(conn, player_data['clan_id'], player_data['account_id'], from_date,
                                              to_date)
     self.assertEqual(1, len(rows))
     player_stat_snapshot_dict = dict(zip(rows[0].keys(), rows[0]))
     expected = {'stronghold_defense_battles': 9,
                 'account_id': 512841364,
                 'stronghold_skirmish_battles': 731,
                 'clan_id': 500050913,
                 'total_resources_earned': 5002,
                 'created_date': str(AppTime.get_now())}
     self.assertEqual(expected, player_stat_snapshot_dict)