diff --git a/static/src/js/app/oj/problem/problem.js b/static/src/js/app/oj/problem/problem.js index fd544cf7..6826e7b2 100644 --- a/static/src/js/app/oj/problem/problem.js +++ b/static/src/js/app/oj/problem/problem.js @@ -1,13 +1,19 @@ -require(["jquery", "code_mirror", "csrf"], function ($, code_mirror, csrfHeader) { +require(["jquery", "code_mirror", "csrf", "bs_alert"], function ($, code_mirror, csrfHeader, bs_alert) { var code_editor = code_mirror($("#code-editor")[0], "text/x-csrc"); - var language = "1"; + var language = $("input[name='language'][checked]").val(); + var submission_id; - $("#language-selector").change(function () { - language = $("#language-selector").val(); - var language_types = {c: "text/x-csrc", cpp: "text/x-c++src", java: "text/x-java"}; + $("input[name='language']").change(function () { + language = this.value; + var language_types = {"1": "text/x-csrc", "2": "text/x-c++src", "3": "text/x-java"}; code_editor.setOption("mode", language_types[language]); }); + $("#show-more-btn").click(function () { + $(".hide").attr("class", "problem-section"); + $("#show-more-btn").hide(); + }); + function show_loading() { $("#submit-code-button").attr("disabled", "disabled"); $("#loading-gif").show(); @@ -18,24 +24,108 @@ require(["jquery", "code_mirror", "csrf"], function ($, code_mirror, csrfHeader) $("#loading-gif").hide(); } + + function get_result_html(result) { + console.log(result); + // 0 结果正确 1 运行错误 2 超时 3 超内存 4 编译错误 + // 5 格式错误 6 结果错误 7 系统错误 8 等待判题 + var results = { + 0: {"alert_class": "success", message: "Accepted"}, + 1: {"alert_class": "danger", message: "Runtime Error"}, + 2: {"alert_class": "warning", message: "Time Limit Exceeded"}, + 3: {"alert_class": "warning", message: "Memory Limit Exceeded"}, + 4: {"alert_class": "danger", message: "Compile Error"}, + 5: {"alert_class": "warning", message: "Format Error"}, + 6: {"alert_class": "danger", message: "Wrong Answer"}, + 7: {"alert_class": "danger", message: "System Error"}, + 8: {"alert_class": "info", message: "Waiting"} + }; + + var html = ''; + console.log(html); + return html; + } + + function get_result() { + $.ajax({ + url: "/api/submission/?submission_id=" + submission_id, + method: "get", + dataType: "json", + success: function (data) { + if (!data.code) { + // 8是还没有完成判题 + if (data.data.result == 8) { + // 1秒之后重新去获取 + setTimeout(get_result, 1000); + } + else { + hide_loading(); + $("#result").html(get_result_html(data.data.result)); + } + } + else { + bs_alert(data.data); + hide_loading(); + } + } + }) + } + $("#submit-code-button").click(function () { + var problem_id = window.location.pathname.split("/")[2]; + var code = code_editor.getValue(); + show_loading(); + + if(!code){ + bs_alert("请填写代码!"); + hide_loading(); + return false; + } + + $("#result").html(""); + $.ajax({ beforeSend: csrfHeader, url: "/api/submission/", method: "post", - data: JSON.stringify({problem_id: 2, language: language, code: code_editor.getValue()}), - contentType: "application/json" + data: JSON.stringify({ + problem_id: window.location.pathname.split("/")[2], + language: language, + code: code_editor.getValue() + }), + contentType: "application/json", + success: function (data) { + if (!data.code) { + submission_id = data.data.submission_id; + // 获取到id 之后2秒去查询一下判题结果 + setTimeout(get_result, 2000); + } + else { + bs_alert(data.data); + hide_loading(); + } + } }); - setTimeout( - function () { - $("#a").animate({opacity: '1'}) - }, 3); + }); - $("#show-more-btn").click(function(){ - $(".hide").attr("class", "problem-section"); - $("#show-more-btn").hide(); + $.ajax({ + url : "/api/user/", + method: "get", + dataType: "json", + success: function(data){ + if(data.code){ + $("#submit-code-button").attr("disabled", "disabled"); + $("#result").html(''); + } + } }) - }); diff --git a/submission/views.py b/submission/views.py index 7d2ef237..83cfb886 100644 --- a/submission/views.py +++ b/submission/views.py @@ -22,7 +22,7 @@ class SubmissionnAPIView(APIView): connection = pymongo.MongoClient(host=mongodb_setting["HOST"], port=mongodb_setting["PORT"]) return connection["oj"]["oj_submission"] - # @login_required + @login_required def post(self, request): """ 提交代码 @@ -48,12 +48,12 @@ class SubmissionnAPIView(APIView): else: return serializer_invalid_response(serializer) - # @login_required + @login_required def get(self, request): submission_id = request.GET.get("submission_id", None) if not submission_id: return error_response(u"参数错误") - submission = self._create_mondodb_connection().find_one({"_id": ObjectId(submission_id), "user_id": result.user.id}) + submission = self._create_mondodb_connection().find_one({"_id": ObjectId(submission_id), "user_id": request.user.id}) if submission: return success_response({"result": submission["result"]}) else: diff --git a/template/oj/problem/problem.html b/template/oj/problem/problem.html index b3f1623e..f3a30211 100644 --- a/template/oj/problem/problem.html +++ b/template/oj/problem/problem.html @@ -9,7 +9,8 @@

{{ problem.title }}

-

发布时间: {{ problem.create_time }} CPU: {{ problem.time_limit }}ms 内存: {{ problem.memory_limit }}M

+

发布时间: {{ problem.create_time }} CPU: {{ problem.time_limit }}ms + 内存: {{ problem.memory_limit }}M

@@ -28,35 +29,35 @@

第一行包括两个数n,k

{% for item in samples %} -
- +
+
 {{ item.input }}
-
-
+
+
- +
 {{ item.output }}
-
+
{% endfor %}
{% if problem.hind %} -
- +
+ -

{{ problem.hint|safe }}

-
- {% endif %} +

{{ problem.hint|safe }}

+
+ {% endif %}

{% for tag in problem.tags.all %} - {{ tag.name }} + {{ tag.name }} {% endfor %}

@@ -66,13 +67,13 @@
@@ -89,24 +90,10 @@ -
-
- - - - + +
+
{% endblock %}