Created
March 31, 2011 14:27
-
-
Save anrie/896436 to your computer and use it in GitHub Desktop.
post-processing sc2reader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| from sc2reader import Replay | |
| from helper import AttributeDict | |
| import calendar | |
| import re | |
| import datetime | |
| class ReplayParser(object): | |
| """ | |
| Example output for ReplayParser().data: | |
| {'length': '0:58', | |
| 'map': 'Delta Quadrant', | |
| 'replay_date': datetime.date(2010, 12, 22), | |
| 'teams': {1: [('LeFrog', 'Zerg')], 2: [('Quotidian', 'Terran')]}, | |
| 'type': '1v1', | |
| 'winner': 1} | |
| """ | |
| def __init__(self, replay_file): | |
| self.replay_file = replay_file | |
| self.replay = self.read_replay() | |
| self.process_replay() | |
| data = AttributeDict() | |
| def read_replay(self): | |
| """ | |
| Reads a starcraft replay an returns the data as a dict | |
| TODO: basic checks and validation. | |
| """ | |
| try: | |
| return Replay(self.replay_file) | |
| except Exception, e: | |
| raise ReplayParserException("Replay could not be processed: %s" % e) | |
| def process_replay(self): | |
| return [self.data.update({Processor(self.replay).name: Processor(self.replay).process()}) for Processor in ReplayProcessor.__subclasses__()] | |
| class ReplayProcessor(object): | |
| """ | |
| Base Class for different processors that adjust the values returned from sc2reader as needed. | |
| TODO: | |
| * implement as abstract base class | |
| * Raise 'not implemented' if process-method is missing etc. | |
| """ | |
| def __init__(self, replay=None): | |
| self.replay = replay | |
| class DateProcessor(ReplayProcessor): | |
| name = "replay_date" | |
| def process(self): | |
| try: | |
| date = self.replay.date.split(' ') | |
| month_dict = dict([(name[:3], index) for index, name in enumerate(calendar.month_name)]) | |
| r_month = int(month_dict[date[1]]) | |
| r_day = int(date[2]) | |
| r_year = int(date[4]) | |
| date = datetime.date(r_year, r_month, r_day) | |
| return date | |
| except Exception, e: | |
| raise ReplayProcessorException('Date could not be parsed: %s' % e) | |
| class LengthProcessor(ReplayProcessor): | |
| name = "length" | |
| def process(self): | |
| length = [str(n) for n in self.replay.length] | |
| return ':'.join(length) | |
| class MapProcessor(ReplayProcessor): | |
| name = "map" | |
| def process(self): | |
| return self.replay.map | |
| class TypeProcessor(ReplayProcessor): | |
| name = "type" | |
| def process(self): | |
| return self.replay.type | |
| class TeamsProcessor(ReplayProcessor): | |
| name = "teams" | |
| def process(self): | |
| teams = {} | |
| for team, players in self.replay.teams.iteritems(): | |
| teams[team] = [PlayerProcessor(player).process() for player in players] | |
| return teams | |
| class ResultProcessor(ReplayProcessor): | |
| name = "winner" | |
| def process(self): | |
| winner = 0 | |
| for team, players in self.replay.teams.iteritems(): | |
| # default to 0 if winner can't be determined | |
| if 'Won' in self.replay.results[team]: | |
| winner = int(team) | |
| return winner | |
| class PlayerProcessor(object): | |
| def __init__(self, player): | |
| self.player = player | |
| def process(self): | |
| """ | |
| Converts the original player string from sc2reader | |
| "Player 3 - Lucky (Terraner)" is rendered as | |
| a tuple (Lucky, Terraner) | |
| """ | |
| info = str(self.player).split('-') | |
| pinfo = re.search(r'(\w+)\s(\(\w+\))', info[1]) | |
| player = pinfo.groups()[0] | |
| race = pinfo.groups()[1].replace('(', '').replace(')', '') | |
| return (player, race) | |
| class ReplayParserException(Exception): | |
| pass | |
| class ReplayProcessorException(Exception): | |
| pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment