#!/bin/env python # # Thu Jul 13 18:28:42 CEST 2006 "Gunnar Bluth" # "Achim Mueller" # # This script converts party gammon *.gam into *.mat files. Usage: # "python convert_gam_mat.py *.gam" # import sys class player: def __init__(self, name, rating=None): self.name=name self.rating=rating def __str__(self): return self.name def __cmp__(self, other): if self.name == "%s" % other: return 0 else: return 1 class move: def __init__(self, player, dice, cube, checkers, time): self.player = player self.dice = dice self.cube = cube self.checkers = checkers self.time = time def dump_mat(self): if self.cube: return " %s" % self.cube return " %s: %s" % (self.dice, " ".join(self.checkers )) class game(dict): def __init__(self, match, nr, player1, player2, score1, score2, winner, endscore1, endscore2, start=None, end=None): self.match, self.nr = match, nr self.players = [ player1, player2 ] self.winner = winner self.score1, self.score2 = int(score1), int(score2) self.endscore1, self.endscore2 = int(endscore1), int(endscore2) self.value = [ self.endscore1 - self.score1, self.endscore2 - self.score2 ] self.start, self.end = start, end self.moves = [] def addmove(self, p_nr, dice, cube, checkers, time=None): self.moves.append(move(self.players[int(p_nr)-1], dice, cube, checkers, time)) def dump_mat(self, lastmatch=0): lines = [" %s : %s %s : %s" % (self.players[0], self.score1, self.players[1], self.score2), ] offset = 0 line = 1 if self.players[0] != self.moves[0].player: # print "Player to start is %s (not %s)" % ( self.moves[0].player, self.players[0]) offset = 1 line = 2 lines.append(" 1) %-26s %s" % ("", self.moves[0].dump_mat())) for i in range(offset,len(self.moves),2): try: lines.append("%2s) %-26s %s" % ((line), self.moves[i].dump_mat(), self.moves[i+1].dump_mat())) except: lines.append("%2s) %-26s" % ((line), self.moves[i].dump_mat())) line += 1 winnerindex = self.players.index(self.winner) lines.append("%sWins %s point%s%s" % (" " * 44 * winnerindex, self.value[winnerindex], "s" * (self.value[winnerindex] > 1), " and the match" * lastmatch ) ) return "\n".join(lines) class match(list): def __init__(self, gameid=None, type=None, length=0, player1=None, player2=None, boardid=None ): self.gameid, self.boardid = gameid, boardid self.type, self.length = type, int(length) self.player1, self.player2 = player1, player2 self.games = [] self.filebasename = "" def addgame(self, nr, score1, score2, winner, endscore1, endscore2): assert(None not in [self.player1, self.player2]) i = game(self, nr, self.player1, self.player2, score1, score2,winner, endscore1, endscore2 ) self.games.append(i) return i def dump_mat(self): assert(self.filebasename) try: open("%smat" % self.filebasename, 'r') #return "Already there!" except IOError: pass FILE = open("%smat" % self.filebasename, 'w') FILE.write("%d Point Match\n\n" % self.length) for i in range(0,len(self.games)): FILE.write(" Game %i\n" % (i+1)) FILE.write(self.games[i].dump_mat(i==len(self.games)-1)) FILE.write("\n\n") FILE.close() return "OK" class mat_parser: pass class gam_parser: def __init__(self): pass def parse(self,filename): data = { 'GAME_0_SCORE1':0, 'GAME_0_SCORE2': 0} FILE=open(filename, 'r') for i in FILE.readlines(): try: key, value = i.strip().split("=",1) if not data.has_key(key): data[key] = value else: raise(Exception("Double key %s in this match!" % key)) except Exception, e: print e print i raise FILE.close() players = {data['CREATOR']: player(data['CREATOR'], data['CREATOR_RATING']), data['OPPONENT']: player(data['OPPONENT'], data['OPPONENT_RATING']) } m = match(data['GAMEID'], data['GAMETYPE'], data['MATCHLENGTH'], players[data['CREATOR']], players[data['OPPONENT']], data['BOARDID']) m.filebasename = filename.strip("gam") gamenr = 1 while data.has_key('GAME_%i_START' % gamenr): g = m.addgame(data['GAMEID_%i' % gamenr], data['GAME_%s_SCORE1' % (gamenr-1)], data['GAME_%s_SCORE2' % (gamenr-1)], players[data["GAME_%i_WIN" % gamenr]], data['GAME_%s_SCORE1' % (gamenr)], data['GAME_%s_SCORE2' % (gamenr)] ) for move in data['GAME_%i_GAMEPLAY' % gamenr].split('#'): if not move: continue #print "Adding move: %s" % move # 1 156 167 65 24/18 18/13 # 2 Double => 2 # 1 Drop # 1 Accept move = "25".join(move.split("bar")) move = "0".join(move.split("off")) try: #parts = move.replace("*","").split(" ") parts = move.split(" ") if parts[1] in [ "Double" ]: g.addmove(parts[0], None, "%s %s %s" % (parts[1], parts[2], parts[3]), None) elif parts[1] in [ "Drop", "Take" ]: g.addmove(parts[0], None, parts[1], None) else: g.addmove(parts[0], parts[3], None, parts[4:]) except IndexError: try: g.addmove(parts[0], parts[3], None, []) except Exception, e: print "Skipped move: '%s' because of %s" % (move, e) gamenr += 1 return m types = { 'mat': { 'type': '.mat Format', 'parser': mat_parser() }, 'gam': { 'type': 'Partygammon', 'parser': gam_parser() } } def main(): players = [None, None] matches = [] for matchfile in sys.argv[1:]: print "Analysing %s" % matchfile for type in types.keys(): if matchfile.endswith(type): print "%s is in %s format, parsing..." % (matchfile, types[type]['type']) try: matches.append(types[type]['parser'].parse(matchfile)) except Exception, e: print "Oops, parser raised:" print "%s" % e raise for i in matches: print i.dump_mat() if __name__ == '__main__': main()