inital commit 1

This commit is contained in:
mxdyeah committed 2026-07-08 19:25:24 +08:00
commit 5c7b9edf97
42 files changed
+2371

No files matched your search

+170
View File
@@ -0,0 +1,170 @@
<?php
class JudgeClient
{
private $ch = null;
private $serverBaseUrl = '';
private $token;
private static $languageConfigs = [];
public function __construct($token, $serverBaseUrl)
{
$this->serverBaseUrl = rtrim($serverBaseUrl, '/');
$this->token = hash('sha256', $token);
}
public function ping()
{
return $this->post($this->serverBaseUrl . '/ping');
}
/**
* 调用判题 api
* @param string $src 提交的源代码
* @param string $language 使用的编程语言
* @param string $testCaseId test_case_id
* @param array $config 额外配置
* @return array
* @throws Exception
*/
public function judge($src, $language, $testCaseId, $config = [])
{
$languageConfig = static::getLanguageConfigByLanguage($language);
if (is_null($languageConfig)) {
throw new Exception("don't support \"$language\" language!");
}
$default = [
'language_config' => $languageConfig,
'src' => $src,
'test_case_id' => $testCaseId,
'max_cpu_time' => $languageConfig['compile']['max_cpu_time'],
'max_memory' => $languageConfig['compile']['max_memory'],
'spj_version' => null,
'spj_config' => null,
'spj_compile_config' => null,
'spj_src' => null,
'output' => false
];
return $this->post($this->serverBaseUrl . '/judge', array_merge($default, $config));
}
public function compileSpj($src, $spjVersion, $spjCompileConfig)
{
$data = [
'src' => $src,
'spj_version' => $spjVersion,
'spj_compile_config' => $spjCompileConfig,
];
return $this->post($this->serverBaseUrl . '/compile_spj', $data);
}
public static function loadLanguageConfigs()
{
if (empty(static::$languageConfigs))
static::$languageConfigs = require('languages.php');
}
public static function getLanguageConfigByLanguage($language)
{
return static::getLanguageConfigByKey($language . '_lang_config');
}
public static function getLanguageConfigByKey($key)
{
return isset(static::$languageConfigs[$key]) ? static::$languageConfigs[$key] : null;
}
private function needCreateCurl()
{
return is_null($this->ch);
}
/**
* 获取 curl 资源
* @return null|resource
*/
private function getCurl()
{
if ($this->needCreateCurl()) {
$this->ch = curl_init();
$defaults = [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
// set HTTP request header
CURLOPT_HTTPHEADER => [
'Content-type: application/json',
'X-Judge-Server-Token: ' . $this->token
],
];
curl_setopt_array($this->ch, $defaults);
}
return $this->ch;
}
/**
* 发送 GET 请求
* @param string $url 请求的url
* @param array $data 请求参数
* @return array
*/
private function get($url, $data = [])
{
return $this->request('GET', $url, $data);
}
/**
* 发送 POST 请求
* @param string $url 请求的url
* @param array $data 请求参数
* @return array
*/
private function post($url, $data = [])
{
return $this->request('POST', $url, $data);
}
/**
* 发送 http 请求
* @param string $method http method
* @param string $url 请求的url
* @param array $data 请求参数
* @return array
*/
private function request($method, $url, $data = [])
{
$ch = $this->getCurl();
$method = strtoupper($method);
if (in_array($method, ['GET', 'HEAD', 'DELETE', 'POST', 'PUT', 'PATCH']))
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, empty($data) ? '{}' : json_encode($data));
if (!$result = curl_exec($this->ch)) {
trigger_error(curl_error($this->ch));
}
return json_decode($result, true);
}
/**
* 关闭 curl 资源
*/
public function close()
{
if (is_resource($this->ch)) {
curl_close($this->ch);
}
$this->ch = null;
}
public function __destruct()
{
$this->close();
}
}
JudgeClient::loadLanguageConfigs();
+94
View File
@@ -0,0 +1,94 @@
<?php
require('JudgeClient.php');
$token = 'YOUR_TOKEN_HERE';
$cSrc = <<<'CODE'
#include <stdio.h>
int main(){
int a, b;
scanf("%d%d", &a, &b);
printf("%d\n", a+b);
return 0;
}
CODE;
$cSpjSrc = <<<'CODE'
#include <stdio.h>
int main(){
return 1;
}
CODE;
$cppSrc = <<<'CODE'
#include <iostream>
using namespace std;
int main()
{
int a,b;
cin >> a >> b;
cout << a+b << endl;
return 0;
}
CODE;
$javaSrc = <<<'CODE'
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
int a=in.nextInt();
int b=in.nextInt();
System.out.println(a + b);
}
}
CODE;
$py2src = <<<'CODE'
s = raw_input()
s1 = s.split(" ")
print int(s1[0]) + int(s1[1])
CODE;
$py3src = <<<'CODE'
s = input()
s1 = s.split(" ")
print(int(s1[0]) + int(s1[1]))
CODE;
$judgeClient = new JudgeClient($token, 'http://127.0.0.1:12358');
echo "ping:\n";
print_r($judgeClient->ping());
echo "\n\ncompile_spj:\n";
print_r($judgeClient->compileSpj($cSpjSrc, '2', JudgeClient::getLanguageConfigByKey('c_lang_spj_compile')));
echo "\n\nc_judge:\n";
print_r($judgeClient->judge($cSrc, 'c', 'normal', [
'output' => true
]));
echo "\n\nc_spj_judge:\n";
print_r($judgeClient->judge($cSrc, 'c', 'spj', [
'spj_version' => '3',
'spj_config' => JudgeClient::getLanguageConfigByKey('c_lang_spj_config'),
'spj_compile_config' => JudgeClient::getLanguageConfigByKey('c_lang_spj_compile'),
'spj_src' => $cSpjSrc,
]));
echo "\n\ncpp_judge:\n";
print_r($judgeClient->judge($cppSrc, 'cpp', 'normal'));
echo "\n\njava_judge:\n";
print_r($judgeClient->judge($javaSrc, 'java', 'normal'));
echo "\n\npy2_judge:\n";
print_r($judgeClient->judge($py2src, 'py2', 'normal'));
echo "\n\npy3_judge:\n";
print_r($judgeClient->judge($py3src, 'py3', 'normal'));
+97
View File
@@ -0,0 +1,97 @@
<?php
$default_env = ["LANG=en_US.UTF-8", "LANGUAGE=en_US:en", "LC_ALL=en_US.UTF-8"];
return [
'c_lang_config' => [
'compile' => [
'src_name' => 'main.c',
'exe_name' => 'main',
'max_cpu_time' => 3000,
'max_real_time' => 5000,
'max_memory' => 128 * 1024 * 1024,
'compile_command' => '/usr/bin/gcc -DONLINE_JUDGE -O2 -w -fmax-errors=3 -std=c99 -static {src_path} -lm -o {exe_path}',
],
'run' => [
'command' => '{exe_path}',
'seccomp_rule' => 'c_cpp',
'env' => $default_env
]
],
'c_lang_spj_compile' => [
'src_name' => 'spj-{spj_version}.c',
'exe_name' => 'spj-{spj_version}',
'max_cpu_time' => 3000,
'max_real_time' => 5000,
'max_memory' => 1024 * 1024 * 1024,
'compile_command' => '/usr/bin/gcc -DONLINE_JUDGE -O2 -w -fmax-errors=3 -std=c99 {src_path} -lm -o {exe_path}'
],
'c_lang_spj_config' => [
'exe_name' => 'spj-{spj_version}',
'command' => '{exe_path} {in_file_path} {user_out_file_path}',
'seccomp_rule' => 'c_cpp',
'env' => $default_env
],
'cpp_lang_config' => [
'name' => 'cpp',
'compile' => [
'src_name' => 'main.cpp',
'exe_name' => 'main',
'max_cpu_time' => 3000,
'max_real_time' => 5000,
'max_memory' => 128 * 1024 * 1024,
'compile_command' => '/usr/bin/g++ -DONLINE_JUDGE -O2 -w -fmax-errors=3 -std=c++11 {src_path} -lm -o {exe_path}',
],
'run' => [
'command' => '{exe_path}',
'seccomp_rule' => 'c_cpp',
]
],
'java_lang_config' => [
'name' => 'java',
'compile' => [
'src_name' => 'Main.java',
'exe_name' => 'Main',
'max_cpu_time' => 3000,
'max_real_time' => 5000,
'max_memory' => -1,
'compile_command' => '/usr/bin/javac {src_path} -d {exe_dir} -encoding UTF8'
],
'run' => [
'command' => '/usr/bin/java -cp {exe_dir} -Xss1M -XX:MaxPermSize=16M -XX:PermSize=8M -Xms16M -Xmx{max_memory}k -Djava.security.manager -Djava.security.policy==/etc/java_policy -Djava.awt.headless=true Main',
'seccomp_rule' => null,
'env' => $default_env,
'memory_limit_check_only' => 1
]
],
'py2_lang_config' => [
'compile' => [
'src_name' => 'solution.py',
'exe_name' => 'solution.pyc',
'max_cpu_time' => 3000,
'max_real_time' => 5000,
'max_memory' => 128 * 1024 * 1024,
'compile_command' => '/usr/bin/python -m py_compile {src_path}',
],
'run' => [
'command' => '/usr/bin/python {exe_path}',
'seccomp_rule' => null,
'env' => $default_env
]
],
'py3_lang_config' => [
'compile' => [
'src_name' => 'solution.py',
'exe_name' => '__pycache__/solution.cpython-35.pyc',
'max_cpu_time' => 3000,
'max_real_time' => 5000,
'max_memory' => 128 * 1024 * 1024,
'compile_command' => '/usr/bin/python3 -m py_compile {src_path}',
],
'run' => [
'command' => '/usr/bin/python3 {exe_path}',
'seccomp_rule' => 'general',
'env' => array_merge(['MALLOC_ARENA_MAX=1'], $default_env)
]
]
];