def GET(self, seq=None): now = time.localtime() hours_left = 23 - now.tm_hour mins_left = 59 - now.tm_min time_left = "%02d:%02d" % (hours_left, mins_left) current_seq = web.ctx.game.current_seq if seq is None: seq = current_seq else: seq = int(seq) if web.ctx.game.your_turn or seq != current_seq: vote_counts = Vote.summary(web.ctx.game.id, seq) top_votes = [ { 'pos': vote.move, 'count': int(vote.cnt), 'label': vote.move != 'tt' and chr(i + 65) or 'Pass', } for i, vote in enumerate(vote_counts) ] comment_counts = Comment.summary(web.ctx.game.id, seq) comments = [ { 'pos': comment.move, } for i, comment in enumerate(comment_counts) ] else: top_votes = [] comments = [] turn = seq % 2 == 1 and "Black's turn." or "White's turn." game_state = GameState.get( game_id=web.ctx.game.id, seq=seq - 1, ) system_message = SystemMessage.get() if system_message: system_message = system_message.message return json.dumps({ 'id': web.ctx.game.id, 'seq': seq, 'turn': turn, 'current_seq': current_seq, 'board_size': 19, 'board': json.loads(game_state.board), 'last_move': game_state.move, 'illegal': json.loads(game_state.illegal), 'black_captures': game_state.black_captures, 'white_captures': game_state.white_captures, 'votes': top_votes, 'comments': comments, 'time_left': time_left, 'system_message': system_message, })
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')
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)