Commit e2397ae4 authored by Kento HASEGAWA's avatar Kento HASEGAWA

Add 3D viewer to show the solution

parent 5e4c621e
......@@ -60,7 +60,7 @@ def load_questions():
"board_size": board_size,
"line_num": line_num,
"last_req": None,
"answers": [],
"answers": OrderedDict(),
}
update_answer_list(_name)
......@@ -87,7 +87,7 @@ def update_answer_list(qname):
global app_args
global questions
questions[qname]['answers'] = []
questions[qname]['answers'] = OrderedDict()
_folder_name = os.path.splitext(qname)[0]
_answers_path = "{}/{}".format(app_args['out'], _folder_name)
......@@ -97,9 +97,10 @@ def update_answer_list(qname):
answer_log_file.reverse()
for v2 in answer_log_file:
json_name = os.path.basename(v2)
with open(v2, "r") as fp:
answer_log = json.load(fp)
questions[qname]['answers'].append(answer_log)
questions[qname]['answers'][json_name] = answer_log
@app.before_request
def before_request():
......@@ -282,6 +283,109 @@ def get_status():
return json.dumps(res)
@app.route("/board-data", methods=["POST"])
def get_board_data():
global questions
qname = request.form['qname']
json_name = request.form['jname']
lines = questions[qname]['answers'][json_name]['answer'].split("\n")
# board_size = [int(v) for v in lines[1].rstrip().split()[1].split("X")]
board_size = [int(v) for v in questions[qname]['board_size'].split("X")]
raw_data = [[[0 for x in range(board_size[0])] for y in range(board_size[1])] for z in range(board_size[2])]
data = []
max_line_num = 0
for l in lines[1:]:
if l.rstrip() == "":
continue
elif l.startswith("LAYER"):
z = int(l.rstrip().split()[1])-1
y = 0
continue
else:
ldat = l.rstrip().split(",")
for x, v in enumerate(ldat):
raw_data[z][y][x] = int(v)
max_line_num = max(max_line_num, int(v))
y += 1
terminals = [[] for v in range(max_line_num+1)]
for z in range(board_size[2]):
for y in range(board_size[1]):
for x in range(board_size[0]):
line_num = raw_data[z][y][x]
if line_num == 0:
continue
adj_cnt = 0
if((0 <= x-1 < board_size[0]) and (raw_data[z][y][x-1] == line_num)):
adj_cnt += 1
if((0 <= x+1 < board_size[0]) and (raw_data[z][y][x+1] == line_num)):
adj_cnt += 1
if((0 <= y-1 < board_size[1]) and (raw_data[z][y-1][x] == line_num)):
adj_cnt += 1
if((0 <= y+1 < board_size[1]) and (raw_data[z][y+1][x] == line_num)):
adj_cnt += 1
if((0 <= z-1 < board_size[2]) and (raw_data[z-1][y][x] == line_num)):
adj_cnt += 1
if((0 <= z+1 < board_size[2]) and (raw_data[z+1][y][x] == line_num)):
adj_cnt += 1
if adj_cnt == 1:
terminals[line_num].append((x, y, z))
lines = {}
for i in range(1, max_line_num+1):
if len(terminals[i]) != 2:
continue
start = terminals[i][0]
goal = terminals[i][1]
lines[i] = []
lines[i].append(start)
lines[i].append(start)
now = start
_next = (0, 0, 0)
while now != goal:
x, y, z = now
adj_cnt = 0
if((0 <= x-1 < board_size[0]) and (raw_data[z][y][x-1] == i) and (lines[i][-2] != (x-1, y, z))):
_next = (x-1, y, z)
elif((0 <= x+1 < board_size[0]) and (raw_data[z][y][x+1] == i) and (lines[i][-2] != (x+1, y, z))):
_next = (x+1, y, z)
elif((0 <= y-1 < board_size[1]) and (raw_data[z][y-1][x] == i) and (lines[i][-2] != (x, y-1, z))):
_next = (x, y-1, z)
elif((0 <= y+1 < board_size[1]) and (raw_data[z][y+1][x] == i) and (lines[i][-2] != (x, y+1, z))):
_next = (x, y+1, z)
elif((0 <= z-1 < board_size[2]) and (raw_data[z-1][y][x] == i) and (lines[i][-2] != (x, y, z-1))):
_next = (x, y, z-1)
elif((0 <= z+1 < board_size[2]) and (raw_data[z+1][y][x] == i) and (lines[i][-2] != (x, y, z+1))):
_next = (x, y, z+1)
else:
print("Error: Not found an adjacent node")
_next = goal
lines[i].append(_next)
now = _next
res = {
'line': lines,
'board': board_size,
}
return json.dumps(res)
@app.route("/get_clients")
def get_clients():
global clients
......@@ -294,6 +398,11 @@ def get_clients():
return json.dumps(res)
@app.route("/board-viewer", methods=["GET"])
def show_board_viewer():
return render_template("board-viewer.html")
@app.route("/get_question_table")
def question_table():
......
This diff is collapsed.
......@@ -166,6 +166,13 @@ $(function(){
$("#client-control-pane").find(".stop-button").eq(0).click(function(){
pm.sendStop();
});
$(".answer-detail-row td").click(function(){
var json_name = $(this).parent("tr").data("json");
var qname = $(this).parent("tr").data("qname");
var viewer_url = "/board-viewer#" + qname + "," + json_name
window.open(viewer_url, null);
})
});
}
......
This diff is collapsed.
This diff is collapsed.
......@@ -39,8 +39,8 @@
<th>Client</th>
<th>Score</th>
</tr>
{% for v in qdata.answers %}
<tr>
{% for k, v in qdata.answers.items() %}
<tr class="answer-detail-row" data-json="{{k}}" data-qname="{{qname}}">
<td>{{v.timestamp}}</td>
<td>{{v.solver}}</td>
<td>{{v.nlcheck}}</td>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment