def call_gnugo(sgf, seq, move): color = seq % 2 == 1 and 'b' or 'w' sgf_file = tempfile.NamedTemporaryFile( mode='wb', dir='/tmp', prefix='congo_sgf_', suffix='.sgf', ) sgf_file.write(sgf) sgf_file.flush() cmd = """ printf 'play %s %s\\ncaptures b\\ncaptures w\\nprintsgf' | %s --mode=gtp -l %s """ % ( color, Pretty.pos(move), settings.GNUGO_PATH, sgf_file.name, ) gnugo = os.popen(cmd) gnugo_result = gnugo.read() sgf_file.close() return gnugo_result
def GET(self, seq, pos): count = Vote.count(web.ctx.game.id, seq, pos) comments = Comment.details(web.ctx.game.id, seq, pos) votes = Vote.details(web.ctx.game.id, seq, pos) all_comments = {} for c in comments: all_comments[(c.rating, c.name)] = (c.notes, 'comment') for v in votes: all_comments[(v.rating, v.name)] = (v.notes, 'vote') sorted_comments = sorted( all_comments.iteritems(), key=lambda ((rating, name), _): -rating, ) return json.dumps({ 'display_pos': Pretty.pos(pos), 'count': count, 'votes': [ { 'name': name, 'rating': Pretty.rating(rating), 'notes': notes, 'type': note_type, } for (rating, name), (notes, note_type) in sorted_comments ], })
def GET(self, seq, pos): count = Vote.count(web.ctx.game.id, seq, pos) comments = Comment.details(web.ctx.game.id, seq, pos) votes = Vote.details(web.ctx.game.id, seq, pos) all_comments = {} for c in comments: all_comments[(c.rating, c.name)] = (c.notes, 'comment') for v in votes: all_comments[(v.rating, v.name)] = (v.notes, 'vote') sorted_comments = sorted( all_comments.iteritems(), key=lambda ((rating, name), _): -rating, ) return json.dumps({ 'display_pos': Pretty.pos(pos), 'count': count, 'votes': [ { 'name': name, 'rating': Pretty.rating(rating), 'notes': notes, 'type': note_type, } for (rating, name), (notes, note_type) in sorted_comments ], })
def call_gnugo(sgf, seq, move): color = seq % 2 == 1 and 'b' or 'w' sgf_file = tempfile.NamedTemporaryFile( mode='wb', dir='/tmp', prefix='congo_sgf_', suffix='.sgf', ) sgf_file.write(sgf) sgf_file.flush() cmd = """ printf 'play %s %s\\ncaptures b\\ncaptures w\\nprintsgf' | %s --mode=gtp -l %s """ % ( color, Pretty.pos(move), settings.GNUGO_PATH, sgf_file.name, ) gnugo = os.popen(cmd) gnugo_result = gnugo.read() sgf_file.close() return gnugo_result
def next_move(): game = Game.current() last_state = GameState.get(game_id=game.id, seq=game.current_seq - 1) if last_state is None: GameState.insert( game_id=game.id, seq=0, black_captures=0, white_captures=0, illegal=json.dumps([]), board=json.dumps([[0] * 19] * 19), sgf=DEFAULT_SGF, ) return True top_moves = Vote.summary(game.id, game.current_seq) if not top_moves: return False top_move = top_moves[0].move result = call_gnugo( last_state.sgf, game.current_seq, top_move, ) next_state = parse_gnugo(result) next_state['black_captures'] += last_state['black_captures'] next_state['white_captures'] += last_state['white_captures'] GameState.insert(game_id=game.id, seq=game.current_seq, move=top_move, **next_state) Game.insert_or_update( keys=('id', ), id=game.id, current_seq=game.current_seq + 1, ) message = 'Move %d, %s plays %s.' % ( game.current_seq, game.current_seq % 2 and "Black" or "White", Pretty.pos(top_move), ) for room_id in (1, 2): ChatMessage.insert( room_id=room_id, user_id=0, message=message, ) signal_message(room_id, 'send')
def next_move(): game = Game.current() last_state = GameState.get( game_id=game.id, seq=game.current_seq - 1 ) if last_state is None: GameState.insert( game_id=game.id, seq=0, black_captures=0, white_captures=0, illegal=json.dumps([]), board=json.dumps([[0] * 19] * 19), sgf=DEFAULT_SGF, ) return True top_moves = Vote.summary(game.id, game.current_seq) if not top_moves: return False top_move = top_moves[0].move result = call_gnugo( last_state.sgf, game.current_seq, top_move, ) next_state = parse_gnugo(result) next_state['black_captures'] += last_state['black_captures'] next_state['white_captures'] += last_state['white_captures'] GameState.insert( game_id=game.id, seq=game.current_seq, move=top_move, **next_state ) Game.insert_or_update( keys=('id',), id=game.id, current_seq=game.current_seq + 1, ) message = 'Move %d, %s plays %s.' % ( game.current_seq, game.current_seq % 2 and "Black" or "White", Pretty.pos(top_move), ) for room_id in (1, 2): ChatMessage.insert( room_id=room_id, user_id=0, message=message, ) signal_message(room_id, 'send')