| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace App\Babel\Extension\noj; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use App\Models\OJModel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use App\Models\JudgerModel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use App\Models\ProblemModel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use App\Models\CompilerModel; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 9 |  |  | class TestRunner | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |     public $ocode="noj"; | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |     public $oid=null; | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |     public $pid=null; | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     public $coid=null; | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     public $solution=null; | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     public $verdict=null; | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     public $verdictDict=[ | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |         -2 => "Compile Error", | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |         -1 => "Wrong Answer", | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |         0 => "Accepted", | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |         1 => "Time Limit Exceed", | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |         2 => "Real Time Limit Exceed", | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |         3 => "Memory Limit Exceed", | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |         4 => "Runtime Error", | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |         5 => "System Error", | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |         6 => "Pending", | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |         7 => "Judging", | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         8 => "Partially Accepted" | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     public function __construct($config) | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         $this->oid=OJModel::oid($this->ocode); | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         $this->pid=$config['pid']; | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         $this->coid=$config['coid']; | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         $this->solution=$config['solution']; | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     public function run() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $judgerModel=new JudgerModel(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $compilerModel=new CompilerModel(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $problemModel=new ProblemModel(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $language=$compilerModel->detail($this->coid)['lcode']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $bestServer=$judgerModel->server($this->oid); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         if (is_null($bestServer)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             $this->verdict=[ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 "verdict"=>"Compile Error", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 "compile_info"=>"No Available Judger.", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                 "data"=>[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $probBasic=$problemModel->basic($this->pid); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $submitURL="http://".$bestServer["host"].":".$bestServer["port"]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $submit_data=[ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             "solution" => $this->solution, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             "language" => $language, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             "max_cpu_time" => $probBasic["time_limit"] * ($language=="java" ? 3 : 1), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             "max_memory" => $probBasic["memory_limit"] * 1024, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             "test_case_id" => $probBasic["pcode"], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             "token" => $bestServer["token"], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             "spj_version" => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             "spj_config" => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             "spj_compile_config" => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             "spj_src" => null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         if ($probBasic["spj"] && $probBasic["spj_version"]) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             $submit_data["spj_version"]=$probBasic["spj_version"]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             $submit_data["spj_config"]=$probBasic["spj_lang"]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             $submit_data["spj_compile_config"]=[ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 "src_name" => "spj-{spj_version}.c", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 "exe_name" => "spj-{spj_version}", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 "max_cpu_time" => 3000, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 "max_real_time" => 5000, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 "max_memory" => 1073741824, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 "compile_command" => "/usr/bin/gcc -DONLINE_JUDGE -O2 -w -fmax-errors=3 -std=c99 {src_path} -lm -o {exe_path}" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             ]; // fixed at C99, future linked with spj_lang | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             $submit_data["spj_src"]=$probBasic["spj_src"]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $judgeClient=new JudgeClient($submit_data["token"], $submitURL); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $temp=$judgeClient->judge($submit_data["solution"], $submit_data["language"], $submit_data["test_case_id"], [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             'output' => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             'max_cpu_time'=>$submit_data['max_cpu_time'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             'max_memory'=>$submit_data['max_memory'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             'spj_version'=>$submit_data['spj_version'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             'spj_config'=>$submit_data['spj_config'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             'spj_compile_config'=>$submit_data['spj_compile_config'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             'spj_src'=>$submit_data['spj_src'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         if (!is_null($temp["err"])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             if (strpos($temp["data"], 'Compiler runtime error, info: ')!==false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                 $tempRes=json_decode(explode('Compiler runtime error, info: ', $temp["data"])[1], true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                 $this->verdict['verdict']=$this->verdictDict[$tempRes["result"]]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 $this->verdict['compile_info']=null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 $this->verdict['verdict']=$this->verdictDict["-2"]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 $this->verdict['compile_info']=$temp["data"]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             $this->verdict['data']=[]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             return $this->verdict; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $this->verdict['verdict']="Accepted"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         foreach ($temp["data"] as $record) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             if ($record["result"]) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                 // well... WA or anyway | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                 $this->verdict['verdict']=$this->verdictDict[$record["result"]]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         $this->verdict['data']=$temp["data"]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $this->verdict['compile_info']=null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         foreach ($this->verdict['data'] as &$record) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             $record["result"]=$this->verdictDict[$record["result"]]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 118 |  |  |         return $this->verdict; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 119 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 120 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 121 |  |  |  |