def test_parse_line_with_brace_symbol_in_name(self): line = "23:56 | 11 | 三般東喰赤- | 烏龍茶(黒)(+43.0) misery20(-7.0) NoName(-36.0)" results = parse_log_line(line) self.assertEqual(results["players"][0]["name"], "烏龍茶(黒)") self.assertEqual(results["players"][1]["name"], "misery20") self.assertEqual(results["players"][2]["name"], "NoName")
def test_parse_line_with_brace_symbol_in_name(self): line = u'23:56 | 11 | 三般東喰赤- | 烏龍茶(黒)(+43.0) misery20(-7.0) NoName(-36.0)' results = parse_log_line(line) self.assertEqual(results['players'][0]['name'], u'烏龍茶(黒)') self.assertEqual(results['players'][1]['name'], u'misery20') self.assertEqual(results['players'][2]['name'], u'NoName')
def test_parse_line(self): line = "22:34 | 23 | 四上南喰赤- | 天草(+48.0) 宮永|咲(+2.0) Curtis(-19.0) 橋から(-31.0)" results = parse_log_line(line) self.assertEqual(results["players"][0]["name"], "天草") self.assertEqual(results["players"][0]["place"], 1) self.assertEqual(results["players"][1]["name"], "宮永|咲") self.assertEqual(results["players"][1]["place"], 2) self.assertEqual(results["players"][2]["name"], "Curtis") self.assertEqual(results["players"][2]["place"], 3) self.assertEqual(results["players"][3]["name"], "橋から") self.assertEqual(results["players"][3]["place"], 4) self.assertEqual(results["game_rules"], "四上南喰赤-")
def test_parse_line(self): line = u'22:34 | 23 | 四上南喰赤- | 天草(+48.0) 宮永|咲(+2.0) Curtis(-19.0) 橋から(-31.0)' results = parse_log_line(line) self.assertEqual(results['players'][0]['name'], u'天草') self.assertEqual(results['players'][0]['place'], 1) self.assertEqual(results['players'][1]['name'], u'宮永|咲') self.assertEqual(results['players'][1]['place'], 2) self.assertEqual(results['players'][2]['name'], u'Curtis') self.assertEqual(results['players'][2]['place'], 3) self.assertEqual(results['players'][3]['name'], u'橋から') self.assertEqual(results['players'][3]['place'], 4) self.assertEqual(results['game_rules'], u'四上南喰赤-')
def handle(self, *args, **options): print('{0}: Start'.format(get_date_string())) tenhou_objects = TenhouNickname.objects.all() watching_nicknames = [] cached_objects = {} for obj in tenhou_objects: watching_nicknames.append(obj.tenhou_username) cached_objects[obj.tenhou_username] = obj results = [] temp_folder = os.path.join('/tmp', 'tenhou') if not os.path.exists(temp_folder): os.mkdir(temp_folder) archive_names = self.download_archives_with_games( temp_folder, settings.TENHOU_LATEST_GAMES_URL) lines = self.load_game_records(temp_folder, archive_names) for line in lines: date = line[0] result = parse_log_line(line[1]) for player in result['players']: # player from watched list # was found in latest games if player['name'] in watching_nicknames: # skip sanma games for now if result['game_rules'][0] == u'三': continue game_date = '{} {} +0900'.format(date.strftime('%Y-%d-%m'), result['game_time']) game_date = datetime.strptime(game_date, '%Y-%d-%m %H:%M %z') results.append({ 'name': player['name'], 'place': player['place'], 'game_rules': result['game_rules'], 'game_length': result['game_length'], 'game_date': game_date }) added_accounts = {} with transaction.atomic(): for result in results: tenhou_object = cached_objects[result['name']] TenhouGameLog.objects.get_or_create( tenhou_object=tenhou_object, place=result['place'], game_date=result['game_date'], game_rules=result['game_rules'], game_length=result['game_length'], lobby=lobbies_dict[result['game_rules'][1]]) added_accounts[tenhou_object.id] = tenhou_object for tenhou_object in added_accounts.values(): recalculate_tenhou_statistics(tenhou_object) print('{0}: End'.format(get_date_string()))
def handle(self, *args, **options): print("{0}: Start".format(get_date_string())) tenhou_objects = TenhouNickname.objects.all() watching_nicknames = [] cached_objects = {} for obj in tenhou_objects: watching_nicknames.append(obj.tenhou_username) cached_objects[obj.tenhou_username] = obj results = [] temp_folder = os.path.join("/tmp", "tenhou") if not os.path.exists(temp_folder): os.mkdir(temp_folder) archive_names = self.download_archives_with_games( temp_folder, settings.TENHOU_LATEST_GAMES_URL) lines = self.load_game_records(temp_folder, archive_names) for line in lines: date = line[0] result = parse_log_line(line[1]) for player in result["players"]: # player from watched list # was found in latest games if player["name"] in watching_nicknames: # skip sanma games for now if result["game_rules"][0] == "三": continue game_date = "{} {} +0900".format(date.strftime("%Y-%d-%m"), result["game_time"]) game_date = datetime.strptime(game_date, "%Y-%d-%m %H:%M %z") results.append({ "name": player["name"], "place": player["place"], "game_rules": result["game_rules"], "game_length": result["game_length"], "game_date": game_date, }) added_accounts = {} with transaction.atomic(): for result in results: tenhou_object = cached_objects[result["name"]] TenhouGameLog.objects.get_or_create( game_players=TenhouGameLog.FOUR_PLAYERS, tenhou_object=tenhou_object, place=result["place"], game_date=result["game_date"], game_rules=result["game_rules"], game_length=result["game_length"], lobby=lobbies_dict[result["game_rules"][1]], ) added_accounts[tenhou_object.id] = tenhou_object for tenhou_object in added_accounts.values(): recalculate_tenhou_statistics_for_four_players(tenhou_object) print("{0}: End".format(get_date_string()))