1
10 传入参数:
11 code :源代码
12 problemid: ProblemOjAnswer中的problemId
13 language:"C,C++,Java "
14 callback回掉函数获取socket返回参数 {result:result,ResultMsg:ResultMsg,ResultStatus:msgBody.ResultMsg,Score:ojscore};
15 disabledFunction :禁用函数声明 a,b,c
16
17 返回参数:
18 socketinfo {result:result,ResultMsg:ResultMsg,ResultStatus:msgBody.ResultStatus,Score:ojscore};
19
20 notice:
21 ResultStatus:0=无测试用例的情况,1=测试通过,2=测试失败,3=编译失败,原因不明,4=编译错误,有错误原因,5=线程处理超时
22 ResultMsg:
23 当Status=1或2的时候,返回测试用例的每组测试结果的一个数组,每个数组对象包括Index:测试用例序号
24 Output:程序输出结果(进行了urlencode编码)Result:Pass或者Failure
25 Reason:未通过时的原因,Pass时为空
26 Status=3、4、5时,直接返回错误原因字符串(进行了urlencode编码)
27
28 */
29
30
31 function compile(code,problemId,language,callback,disabledFunction) {
32 debugger;
33 $.ajax({
34 url: "ojProblem/getAnswerId",
35 type: "post",
36 dataType: "json",
37 data: {
38 problemId: problemId
39 },
40 success: function (data) {
41 var item = data.rows;
42 var testCase = "[";
43 for (var i = 0; i < item.length; i++) {
44 var ss = "{\"Input\":\"" + item[i].input + "\",\"Output\":\"" + item[i].output + "\"},"
45 testCase = testCase + ss;
46 }
47 testCase = testCase + "],"
48 var socketaddr = "ws://192.168.0.199:9001";
49 var socket = new WebSocket(socketaddr);
50 try {
51 socket.onopen = function (msg) {
52 //判断函数题还是编程题
53 if(disabledFunction == undefined)
54 var msg = "{\"Language\":\"" + language + "\",\"Code\":\"" + code + "\",\"TestCase\":" + testCase + "\"IsFilePath\":1}";
55 else
56 var msg = "{\"Language\":\"" + language + "\",\"Code\":\"" + code + "\",\"TestCase\":" + testCase + "\"IsFilePath\":1,\"disabledFunction\":\"" + disabledFunction + "\"}";
57 //发送msg
58 socket.send(msg);
59 };
60 socket.onerror = function (msg) {
61 console.log('Client error!', msg);
62 }
63 socket.onmessage = function (msg) {
64 //console.log('Client OK!', msg);
65 var msgBody = window.JSON.parse(msg.data);
66 var ResultMsg = "";
67 var ojscore = 0;
68 var result = true;
69 $(document.body).append("<div class='modal fade' id='myModal' tabindex='-1' role='dialog' aria-labelledby='myModalLabel'><div class='modal-dialog' role='document'><div class='modal-content'><div class='modal-header'><button type='button' class='close' data-dismiss='modal' aria-label='Close'><span aria-hidden='true'>×</span></button><span class='secondtitle' >运行结果</span></div><div class='modal-body'><table class='table table-default'></table><div class='final'></div></div></div></div></div>");
70 switch (msgBody.ResultStatus) {
71 case "1":
72 //alert("全部通过");
73 ResultMsg = msgBody.ResultMsg;
74 var text = "";
75 for (var i = 0; i < ResultMsg.length; i++) {
76 text += " <tr> <td>" + (i + 1) + "</td> <td>Pass</td> <td></td> <td>" + item[i].score + "</td> </tr>";
77 ojscore += item[i].score;
78 }
79 $(".table-default").html(" <tr> <th>用例</th> <th>结果</th><th>原因</th> <th>分值</th> </tr>");
80 $(".table-default").append(text);
81 $(".final").html('<span >最终得分:</span><span class="finalscore">' + ojscore + '</span><br /><span class="finalresult">最终结果:<div class="pass">通过</div></span>');
82 JSON.stringify(ResultMsg);
83 ResultMsg = JSON.stringify(ResultMsg);
84 $('#myModal').modal('show');
85 break;
86 case "2":
87 result = false;
88 //alert("有用例未通过!");
89 ResultMsg = msgBody.ResultMsg;
90 var text = "";
91 for (var i = 0; i < ResultMsg.length; i++) {
92 if (ResultMsg[i].Result == "Pass") {
93 text += " <tr> <td>" + (i + 1) + "</td> <td>Pass</td> <td></td> <td>" + item[i].score + "</td> </tr>";
94 ojscore += item[i].score;
95 }
96 else {
97 text += " <tr> <td>" + (i + 1) + "</td> <td>" + ResultMsg[i].Reason + "</td><td>" + decodeURIComponent(ResultMsg[i].Output) + "</td> <td>" + item[i].score + "</td> </tr>";
98 }
99 }
100 $(".table-default").html(" <tr> <th>用例</th> <th>结果</th><th>原因</th> <th>分值</th> </tr>");
101 $(".table-default").append(text);
102 $(".final").html('<span >最终得分:</span><span class="finalscore">' + ojscore + '</span><br /><span class="finalresult">最终结果:<div class="notpass">未通过</div></span>');
103 /* $(".not-allow").text(" 您的代码编译未通过,原因可能为:"+ResultMsg);
104 $(".not-allow").css('display','block');*/
105 JSON.stringify(ResultMsg);
106 ResultMsg = JSON.stringify(ResultMsg);
107 $('#myModal').modal('show');
108 break;
109 default:
110 result = false;
111 //console.log(msgBody.ResultStatus);
112 //console.log(ResultMsg.length);
113 ResultMsg = decodeURIComponent(msgBody.ResultMsg);
114 //alert(ResultMsg);
115 $(".not-allow").text(" 您的代码编译未通过,原因可能为:" + ResultMsg);
116 $(".not-allow").css('display', 'block');
117 /* $(".finalscore").html(0);
118 $(".finalresult").html(" 最终结果:<div class='notpass'>未通过</div>");*/
119 }
120 var socketinfo={result:result,ResultMsg:ResultMsg,ResultStatus:msgBody.ResultStatus,Score:ojscore};
121 callback(socketinfo);
122 // return msgBody.ResultStatus;
123 }
124
125 } catch (ex) {
126 console.log(ex);
127 return "error";
128 }
129 },
130 error: function (err) {
131 alert("error");
132 }
133 });
134 //拼装 testcase
135
136 }