-
Notifications
You must be signed in to change notification settings - Fork 22
/
annotate_game.py
138 lines (118 loc) · 4.64 KB
/
annotate_game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
""" Nicely render a game with deck composition viewer and some graphs of
the game as it progresses."""
import itertools
import simplejson as json
import game
import goals
import parse_game
import sofia_predict
def _pretty_format_html(v):
return '<br>' + pprint.pformat(v).replace(
'\n', '<br>').replace(' ', ' ')
win_predictor = sofia_predict.SofiaWinPredictor('data/logreg-peg.model')
def make_graph(label, div_name):
return """
<tr>
<td width=50px>%s</td>
<td><div id="%s" style="width:1000px;height:250px;"></div></td>
</tr>
""" % (label, div_name)
def get_goals(game):
glist = goals.check_goals(game)
if len(glist)==0:
goal_contents = None
else:
goal_contents = '<table cellpadding="7">'
for goal in glist:
goal_name = goal['goal_name']
player = goal['player']
reason = goal['reason']
reason = str.lower(reason[0:1]) + reason[1:]
goal_contents += '<tr><td><img src="%s" alt="%s"><td><strong>%s</strong><br>%s %s</tr>' % (goals.GetGoalImageFilename(goal_name), goal_name, goal_name, player, reason)
goal_contents += '</table>'
return "<tr><td>goals</td><td>%s</td></tr>"%goal_contents
def annotate_game(contents, game_id, debug=False):
""" Decorate game contents with some JS that makes a score keeper
and provides anchors per turn."""
contents = contents.replace('—', '---')
parsed_game = parse_game.parse_game(contents, dubious_check = False)
states = []
game_val = game.Game(parsed_game)
win_prob_enabled = len(game_val.get_player_decks()) == 2
if win_prob_enabled:
predictions = win_predictor.predict_all_turns(game_val)
for idx, game_state in enumerate(game_val.game_state_iterator()):
encoded = game_state.encode_game_state()
encoded['win_prob'] = predictions[idx] if win_prob_enabled else 0
states.append(encoded)
parsed_game['game_states'] = states
ret = u''
start_body = contents.find('<body>') + len('<body>')
ret += contents[:start_body]
ret += """
<div id="game-display"></div>
<script src="static/flot/jquery.js"></script>
<script src="static/game_viewer.js"></script>
<script src="static/flot/jquery.js"></script>
<script src="static/flot/jquery.flot.js"></script>
<script type="text/javascript">
var game = %s;
var card_list = %s;
$(document).ready(DecorateGame);
</script>
""" % (json.dumps(parsed_game, indent=2),
open('static/card_list.js', 'r').read())
contents = contents[start_body:]
if debug > 2:
ret += _pretty_format_html(parsed_game)
if debug > 1:
for turn in game_val.get_turns():
ret += '%d %d %d %s %s<br>' % (
turn.get_turn_no(),
turn.get_player().TurnOrder(), turn.get_poss_no(),
turn.turn_dict.get('poss', False),
turn.get_player().name())
import cStringIO as StringIO
output_buf = StringIO.StringIO()
if not parse_game.check_game_sanity(game_val, output_buf):
ret += 'Parsing error, data mismatch, '
ret += '''<a href="game?game_id=%s&debug=1">be a hero, find the
bug</a> and tell [email protected]<br>''' % game_id
ret += output_buf.getvalue().replace('\n', '<br>\n')
cur_turn_ind = 0
split_turn_chunks = parse_game.split_turns(contents)
ret += split_turn_chunks[0]
split_turn_chunks.pop(0)
for idx, (turn_chunk, game_state) in enumerate(
itertools.izip(split_turn_chunks, game_val.game_state_iterator())):
split_chunk = turn_chunk.split('\n')
turn_id = game_state.turn_label()
show_turn_id = game_state.turn_label(for_anchor=True)
ret += '<div id="%s"></div>' % turn_id
ret += '<a name="%s"></a><a href="#%s">%s</a>' % (
show_turn_id, show_turn_id, split_chunk[0])
if debug:
ret += '<br>' + repr(game_val.get_turns()[cur_turn_ind]).replace(
'\n', '<br>')
cur_turn_ind += 1
if idx != len(split_turn_chunks) - 1:
ret += turn_chunk[turn_chunk.find('\n'):]
else:
before_end = turn_chunk.find('</html')
ret += turn_chunk[turn_chunk.find('\n'): before_end]
win_prob_graph = ''
if win_prob_enabled:
win_prob_graph = make_graph('win prob', 'win-prob-graph')
ret += """
<div id="end-game"></div>
<table>
%s
%s
%s
%s
</table>
""" % (make_graph('score', 'score-graph'), make_graph('money', 'money-graph'),
win_prob_graph, get_goals(game_val))
ret += '</div> <br>\n' * 10
ret += '</html>'
return ret