class DominionReader(): BREAK_STRING = '--------' INIT_STRING = 'Game Setup' OVER_STRING = 'Game Over' # Starting the game, playing the game, ending STATUS_INIT = 0 STATUS_PLAY = 1 STATUS_OVER = 2 def __init__(self, filename): self.filename = filename self.game = DominionGame() self.status = '' def dump(self): for turn in self.game.rounds: turn.dump() def analyze(self): self.process() def openFile(self): # TODO: multiple files, keep track of them return open(self.filename) def process(self): f = self.openFile() for line in f: self.processLine(line.strip()) return self def processLine(self, line): if line == '': return if self.BREAK_STRING in line: # Change context and player if necessary self.handleBreak(line) return if self.isPlayTurn(): player, label, cards = self.parseAction(line) self.game.addAction(player, label, cards) elif self.isInitTurn(): self.handleInitTurn(line) def handleBreak(self, line): if self.INIT_STRING in line: self.game.setStatus(self.STATUS_INIT) elif (self.OVER_STRING in line): self.game.setStatus(self.STATUS_OVER) else: self.game.setStatus(self.STATUS_PLAY) number = self.parseTurnNumber(line) if self.game.getRoundNumber() < number: self.game.newRound(number) def handleInitTurn(self, line): line_split = line.split(':') name = line_split[0]; if name == 'Supply cards': supply = stripList(line_split[1].split(',')) self.game.setSupply(supply) return elif name == 'Rating system': # Do something else return # Custom parsing for these init lines player = line.split('-')[0].strip() label = stripList(line.split('-'))[1] if 'starting cards' in label: cards = stripList(label.split(':')[1].split(',')) self.game.initPlayer(player, cards) # add to player deck here elif 'draws' in label: # Remove commas, split by spaces draws = stripList(label.replace(',', '').split(' ')) label = draws[0] self.game.addAction(player, label, draws[1:]) def isPlayTurn(self): return self.game.getStatus() == self.STATUS_PLAY def isInitTurn(self): return self.game.getStatus() == self.STATUS_INIT def parseAction(self, line): line = line.split('-') player = line[0].strip() move = line[1].strip().replace(',', '').split(' ') label = move[0] cards = move[1:] return player, label, cards def parsePlayer(self, line): return line.replace('-', '').strip().split(':')[0] def parseTurnNumber(self, line): return line.replace('-', '').split(':')[1].strip().split()[1]
def __init__(self, filename): self.filename = filename self.game = DominionGame() self.status = ''