def MaxCardVersion(player_dict): return max(DeckInfo.Version(card) for card in player_dict['cards'])
def ParseGame(page_contents, card_id_to_name): page_contents = page_contents.replace('<br/>', '<br>') version_loc = page_contents.index(': Game #') version_with_context = page_contents[version_loc - 50:version_loc + 50] is_gs = 'Gathering Storm' in version_with_context version = int(is_gs) using_goals = False goals = set() if 'Available goals' in version_with_context: end_goal_loc = page_contents.find('Chosen Actions') snip_with_goals = page_contents[version_loc - 50:end_goal_loc] goals.update(ParseGoals(snip_with_goals)) using_goals = True ret = { 'player_list': [], 'game_id': ('http://genie.game-host.org/game.htm?gid=' + GAME_NUM_RE.search(version_with_context).group(1)) } if 'Status:' not in page_contents: print 'could not find status' raise CompleteButRejectedGame() page_contents = page_contents[:page_contents.find('Comments'):] status_list = page_contents.split('Status:')[1:] for status in status_list: player_result = {} status_lines = status.split('<br>') if len(status_lines) < 3: print 'confused, could not under status lines', status_lines game_end_line = status_lines[0] if 'Game End' not in game_end_line: if 'Frozen' in game_end_line: raise CompleteButRejectedGame() print "Don't think game", ret['game_no'], "is over" return None name_points = status_lines[1] player_result['name'] = name_points[:name_points.find("'")] player_result['points'] = int(POINTS_RE.search(name_points).group(1)) player_result['chips'] = int(CHIPS_RE.search(name_points).group(1)) if player_result['points'] <= 5: raise CompleteButRejectedGame() card_goal_list_line = status_lines[2] card_goal_list_imgs = card_goal_list_line.split('src=') cards = [] won_goals = [] goods = 0 for card_or_goal in card_goal_list_imgs: if 'border-width:5px' in card_or_goal: goods += 1 for card_or_goal in card_goal_list_imgs[1:]: img = card_or_goal.replace('/', ' ').replace('.', ' ').split()[1] if img[0] == 'g': won_goals.append(GOAL_NUM_TO_NAME[int(img[1:])]) else: card_data = card_id_to_name[img] card_name = card_data['name'] cards.append(card_name) version = max(version, DeckInfo.Version(card_name)) player_result['cards'] = cards if using_goals: goals.update(won_goals) player_result['goals'] = won_goals player_result['goods'] = goods ret['player_list'].append(player_result) if len(ret['player_list']) <= 1: print 'insufficient players' raise CompleteButRejectedGame() if using_goals: ret['goals'] = list(goals) hand_sizes = [int(x) for x in CARD_NUM_RE.findall(page_contents)] for player_result, hand_size in zip(ret['player_list'], hand_sizes): player_result['hand'] = hand_size ret['advanced'] = int('Action1:' in page_contents and 'Action2:' in page_contents) version = max(version, int(len(goals) > 1)) ret['expansion'] = version return ret