Ejemplo n.º 1
0
def submit():
    """
    提交一个问题,请求中需要包含两个参数:q和a,分别表示question和answer
    :return:服务器想告诉用户的话
    """
    qa = json.loads(request.get_data(as_text=True))
    q = qa['q']
    a = qa['a']
    question = db.get_one("select id,answer from question where question=?",
                          (q, ))
    if not question:
        return "没有这个问题"
    a = lib.regularize_operation(a)
    ma = lib.from_xsb_string(q)
    if not lib.check_right(ma, a):
        return "答案根本就不对"
    if question.answer:
        if len(question.answer) < len(a):
            return f"回答正确,但是{len(a)}离最优解{len(question.answer)}还差点。"
        if len(question.answer) == len(a):
            return "恭喜你平了世界纪录"
        assert len(question.answer) > len(a)
    db.execute("update question set answer=? where id=?", (a, question.id))
    # 因为有新答案来了,所以需要清空缓存
    cache.clear_cache(db.get_all)
    return "恭喜你创造了奇迹"
Ejemplo n.º 2
0
def update_map():
    """
    更新数据库中的地图,在更新地图的同时,必须更新答案
    这个操作很危险,在执行之前必须做好数据库备份
    :return:
    """
    a: List[Question] = d.select_list(
        conn, "select id,question,answer from question")
    for q in tqdm(a, desc="update_map"):
        ma = lib.from_xsb_string(q.question)
        answer = lib.regularize_operation(q.answer) if q.answer else q.answer
        reg_ma = lib.regularize(ma)
        xsb_string = lib.to_xsb_string(reg_ma)
        if xsb_string != q.question or answer != q.answer:
            print("need format")
            if xsb_string != q.question and answer:
                """
                如果地图被规范化了,那么答案也必须规范化
                """
                answer = lib.try_op(ma, answer)
            conn.execute("update question set question=?,answer=? where id=?",
                         (
                             xsb_string,
                             answer,
                             q.id,
                         ))
    conn.commit()
Ejemplo n.º 3
0
def load_file(filepath: str):
    """
    加载一个文本文件,里面是若干个xsb格式的地图
    :param filepath: 加载的文本文件
    :return: 返回(文件描述,llint地图)二元组
    """
    maps = open(filepath).read().strip().split("\n\n")
    maps = [(f"{filepath}", lib.from_xsb_string(i)) for ind, i in enumerate(maps)]
    return maps
Ejemplo n.º 4
0
def update_hard():
    """
    使用hard评估函数更新难度
    :return:
    """
    a: List[Question] = d.select_list(conn, "select id,question from question")
    for q in tqdm(a, desc='update_hard'):
        ma = lib.from_xsb_string(q.question)
        hard = lib.calculate_hard(ma)
        conn.execute("update question set hard=? where id=?", (hard, q.id))
    conn.commit()
Ejemplo n.º 5
0
def validate_data():
    """
    调用validate函数检查数据库中的每一个问题
    :return:
    """
    a: List[Question] = d.select_list(
        conn, "select id,question,answer from question")
    for q in a:
        ma = lib.from_xsb_string(q.question)
        try:
            lib.validate(ma)
        except Exception as e:
            print('地图不合法', e)
            print(ma)
            print(q.question)
            assert False
        an = q.answer
        if an:
            assert lib.check_right(ma, an)
Ejemplo n.º 6
0
def add_question():
    # 向题库中添加问题
    s = request.args['map']
    question = db.get_by_question(s)
    if question:
        return "已经有相同的问题了"
    else:
        s = lib.regularize_xsb_string(s)
        ma = lib.from_xsb_string(s)
        # 校验一下问题
        lib.validate(ma)
        q = db.Question(id=snow.get_id(),
                        question=s,
                        answer=None,
                        hard=lib.calculate_hard(ma),
                        hash=None,
                        solve_times=0,
                        extra='')
        db.insert(q)
        cache.clear_cache(db.get_all)
        return "提交问题成功"
Ejemplo n.º 7
0
import os
from sokoban import lib, io

from fu import json

qa = json.load("../ans.json")
for k, v in qa.items():
    assert lib.check_right(lib.from_xsb_string(k), v)
a = [lib.from_xsb_string(i) for i in qa]
print('去重之前', len(a))
for ma in a:
    lib.validate(ma)
valid = lib.dedup(list(range(len(a))), a)
print('去重之后', len(valid))

ans_map = {}
for i in qa:
    k = lib.to_xsb_string(lib.regularize(lib.from_xsb_string(i)))
    v = lib.try_op(lib.from_xsb_string(k), qa[i])
    if not v:
        assert False
    if k in ans_map:
        if len(v) > len(ans_map[k]):
            continue
    ans_map[k] = v
for k, v in ans_map.items():
    assert lib.check_right(lib.from_xsb_string(k), v)
json.dump(ans_map, 'new_ans.json', indent=2)
print(len(ans_map))