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 GET(self): get_data = web.input(last_id=0) last_id = int(get_data.last_id) room_id = web.ctx.game.player_color messages = [] delete_id = None if last_id == 0: messages = ChatMessage.load(room_id, last_id) else: action, kwargs = wait_for_message(room_id) #sleep up to 300 ms to prevent the thundering herd time.sleep(random.random() * .3) if action == 'send': messages = ChatMessage.load(room_id, last_id) elif action == 'delete': delete_id = kwargs['id'] deletable = is_admin() ChatRoom.set_online(room_id) online_users = [{ 'name': u[1], 'rating': Pretty.rating(u[2]), } for u in ChatRoom.get_online(room_id)] return json.dumps({ 'messages': list( reversed([{ 'id': x.id, 'name': x.name, 'rating': Pretty.rating(x.rating), 'message': x.message, 'deletable': deletable, } for x in messages])), 'online_users': online_users, 'refresh': False, 'delete_id': delete_id, })
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 GET(self): get_data = web.input(last_id=0) last_id = int(get_data.last_id) room_id = web.ctx.game.player_color messages = [] delete_id = None if last_id == 0: messages = ChatMessage.load(room_id, last_id) else: action, kwargs = wait_for_message(room_id) #sleep up to 300 ms to prevent the thundering herd time.sleep(random.random() * .3) if action == 'send': messages = ChatMessage.load(room_id, last_id) elif action == 'delete': delete_id = kwargs['id'] deletable = is_admin() ChatRoom.set_online(room_id) online_users = [{ 'name': u[1], 'rating': Pretty.rating(u[2]), } for u in ChatRoom.get_online(room_id)] return json.dumps({ 'messages': list(reversed([ { 'id': x.id, 'name': x.name, 'rating': Pretty.rating(x.rating), 'message': x.message, 'deletable': deletable, } for x in messages ])), 'online_users': online_users, 'refresh': False, 'delete_id': delete_id, })
def GET(self): game_id = web.ctx.game.id; players = Player.game_stats(game_id) num_players = len(players) black = [] white = [] for player in players: ([], black, white)[player.color].append('%s (%s)' % ( player.name, Pretty.rating(player.rating), )) num_votes = Vote.game_stats(game_id) return json.dumps({ 'num_players': num_players, 'num_votes': num_votes, 'black_players': '<br>'.join(black), 'white_players': '<br>'.join(white), })
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 GET(self): game = Game.current() web.header( 'Content-Disposition', 'attachment; filename="ConGo-game-%s.sgf"' % (game.id,), ) web.header('Content-Type', 'application/x-go-sgf') data = [ '(;GM[1]FF[4]CA[UTF-8]AP[ConGo:0.1]ST[2]', 'RU[Japanese]SZ[19]KM[6.50]', 'GN[ConGo Game %s]PW[White Team]PB[Black Team]' % (game.id,), 'CP[2015 Jay Chan]RO[%s]' % (game.id), ] end_seq = game.current_seq + 1 for seq in range(1, end_seq): vote_counts = Vote.summary(game.id, seq) show_current_move = seq < end_seq - 1 if seq > 1: data.append(';%s[%s]' % ( (seq - 1) % 2 == 1 and 'B' or 'W', prev_chosen_move, )) if show_current_move: data.append('LB') vote_data = [] for i, vote in enumerate(list(vote_counts)[:7]): label = chr(i + 65) if i == 0: chosen_move = vote.move if vote.move != 'tt': if show_current_move: data.append('[%s:%s]' % (vote.move, label)) vote_data.append('%s: %s votes\n' % (label, vote.cnt)) else: vote_data.append('Pass: %s votes\n' % (vote.cnt,)) if seq == 1: data.append('C[con-go.net\n\n') else: data.append('C[') votes = Vote.details(game.id, seq - 1, prev_chosen_move) for vote in list(votes)[:5]: data.append('%s (%s)\\: ' % (vote.name, Pretty.rating(vote.rating))) notes = re.sub('\n', ' ', vote.notes) notes = re.sub('\[', '(', notes) notes = re.sub('\]', ')', notes) notes = re.sub(r'\\', '\\\\', notes) notes = re.sub(r':', '\\:', notes) data.append(notes + '\n\n') data.append('\n') if show_current_move: data.extend(vote_data) data.append(']') prev_chosen_move = chosen_move data.append(')') return ''.join(data)