示例#1
0
文件: action.py 项目: ryuvsken/irelia
def actions():
    state_map = request.args.get('state_map')
    side = request.args.get('side')
    if not state_map or side not in ('b', 'r'):
        return json.dumps({'error': True, 'msg': 'invalid params', 'data': {'state_map': state_map, 'side': side}})
    result = KoreanChess.get_actions(json.loads(state_map), side)

    return json.dumps(result)
示例#2
0
def action():
    state_map = request.args.get('state_map')

    side = request.args.get('side')
    if not state_map or side not in ('b', 'r'):
        return json.dumps({
            'error': True,
            'msg': 'invalid params',
            'data': {
                'state_map': state_map,
                'side': side
            }
        })

    state_map = json.loads(state_map)
    filter_state_map(state_map)
    if side == 'b':
        reverse_state_map = KoreanChess.reverse_state_map(state_map)
        db_name = './q_blue.db'
    else:
        reverse_state_map = state_map
        db_name = './q_red.db'

    state_key = KoreanChess.convert_state_key(reverse_state_map)

    conn = sqlite3.connect(db_name)

    c = conn.cursor()

    c.execute("SELECT quality_json FROM t_quality WHERE state_key='" +
              state_key + "'")

    result = c.fetchone()
    reversed_state_map = KoreanChess.reverse_state_map(state_map)
    actions = KoreanChess.get_actions(reversed_state_map, side)
    if result:
        if result[0] == '0':
            action = KoreanChess.similar_action(actions, state_key, side, c)
        else:
            q_values = json.loads(result[0])
            max_action = int(
                max(q_values.iteritems(), key=operator.itemgetter(1))[0])
            if len(actions) <= max_action:
                action = KoreanChess.similar_action(actions, state_key, side,
                                                    c)
            else:
                action = actions[max_action]
    else:
        action = KoreanChess.similar_action(actions, state_key, side, c)

    c.close()
    return json.dumps(action)