示例#1
0
文件: contest.php 项目: CDFLS/CWOJ
function update_cont_scr($cont_id)
{
    require __DIR__ . '/../conf/database.php';
    $row = mysqli_fetch_row(mysqli_query($con, "select start_time,end_time,judge_way from contest where contest_id={$cont_id} limit 1"));
    $cont_start = $row[0];
    $cont_end = $row[1];
    $cont_judgeway = $row[2];
    //Obtain problems.
    $prob_arr = get_cont_probs($cont_id);
    //Update scores for each user.
    $res = mysqli_query($con, "select user_id,enroll_time,leave_time from contest_status where contest_id={$cont_id}");
    while ($row = mysqli_fetch_row($res)) {
        $user_id = $row[0];
        $tot_score = 0;
        $tot_time = 0;
        //Obtain time range.
        $range_start = $cont_start;
        $range_end = $cont_end;
        if ($cont_start < strtotime($row[1])) {
            $range_start = $row[1];
        }
        if (isset($row[2]) && $cont_end > strtotime($row[2])) {
            $range_end = $row[2];
        }
        //As for every problem.
        for ($i = 0; $i < sizeof($prob_arr); $i++) {
            $score = 0;
            $time = 0;
            $result = NULL;
            if ($cont_judgeway == 3) {
                //OI-Like: Only recognize the first submit.
                $s_row = mysqli_fetch_row(mysqli_query($con, "select score,result,in_date from solution where user_id='{$user_id}' and in_date>'" . $range_start . "' and in_date<'" . $range_end . "' and problem_id=" . $prob_arr[$i] . ' order by in_date limit 1'));
                //Process score.
                if (isset($s_row[0])) {
                    $score = $s_row[0];
                }
                $tot_score += $score;
                //Process result.
                if (isset($s_row[1])) {
                    $result = $s_row[1];
                }
                //Process time.
                if (isset($s_row[2])) {
                    $time = strtotime($s_row[2]) - strtotime($range_start);
                }
                $tot_time += $time;
            } else {
                //Others: Select the highest score among eligible submits.
                $s_row = mysqli_fetch_row(mysqli_query($con, "select MAX(score),COUNT(score),MIN(result),MAX(in_date) from solution where user_id='{$user_id}' and in_date>'" . $range_start . "' and in_date<'" . $range_end . "' and problem_id=" . $prob_arr[$i]));
                //Process score.
                if (isset($s_row[0])) {
                    if ($cont_judgeway == 2 && $s_row[0] == 100) {
                        //ACM: if score != 100 then score = 0.
                        $score = 100;
                    } else {
                        if ($cont_judgeway == 1 && $s_row[1] != 0) {
                            //CWOJ: Minus 5 points per non-AC submit.
                            $score = $s_row[0] - 5 * ($s_row[1] - 1);
                            if ($score < 0) {
                                $score = 0;
                            }
                        } else {
                            if ($cont_judgeway == 0) {
                                //Training: MAX(score).
                                $score = $s_row[0];
                            }
                        }
                    }
                }
                $tot_score += $score;
                //Process result.
                if (isset($s_row[2])) {
                    $result = $s_row[2];
                } else {
                    $result = 'NULL';
                }
                //Process time.
                if (isset($s_row[3])) {
                    if ($s_row[0] == 100) {
                        $time = strtotime($s_row[3]) - strtotime($range_start) + 1200 * ($s_row[1] - 1);
                    } else {
                        $time = 1200 * $s_row[1];
                    }
                }
                $tot_time += $time;
            }
            //Write into database.
            mysqli_query($con, "INSERT into contest_detail (user_id,contest_id,problem_id,result,score,time) VALUES ('{$user_id}',{$cont_id}," . $prob_arr[$i] . ",{$result},{$score},{$time}) ON DUPLICATE KEY UPDATE result={$result},score={$score},time={$time}");
            mysqli_query($con, "UPDATE contest_status SET tot_score={$tot_score}, tot_time={$tot_time} where user_id='{$user_id}' and contest_id={$cont_id} limit 1");
        }
    }
    //Update user rank.
    update_cont_rank($cont_id);
    //Update last_upd_time.
    mysqli_query($con, "update contest set last_upd_time=NOW() where contest_id={$cont_id}");
}
示例#2
0
     if (mysqli_num_rows(mysqli_query($con, "select 1 from contest_status where user_id='{$uid}' and contest_id={$cont_id} limit 1"))) {
         echo json_encode(array('success' => true));
         exit;
     }
     //Insert contest_status.
     if (!mysqli_query($con, "insert into contest_status (contest_id,user_id,tot_score,tot_time,rank,enroll_time,leave_time) VALUES ({$cont_id},'{$uid}',0,0,0,NOW(),NULL)")) {
         echo json_encode(array('success' => false, 'message' => _('Database operation failed...')));
         exit;
     }
     //Update enroll_user
     if (!mysqli_query($con, "update contest set enroll_user=enroll_user+1 where contest_id={$cont_id} limit 1")) {
         echo json_encode(array('success' => false, 'message' => _('Database operation failed...')));
         exit;
     }
     //Obtain problems
     $prob_arr = get_cont_probs($cont_id);
     //Insert contest_detail
     for ($i = 0; $i < sizeof($prob_arr); $i++) {
         if (!mysqli_query($con, "insert into contest_detail (user_id,contest_id,problem_id,result,score,time) VALUES ('{$uid}',{$cont_id},{$prob_arr[$i]},NULL,0,0)")) {
             echo json_encode(array('success' => false, 'message' => "insert into contest_detail (user_id,contest_id,problem_id,result,score,time) VALUES ('{$uid}',{$cont_id},{$prob_arr[$i]},NULL,0,0)"));
             exit;
         }
     }
     //Update contest rank.
     update_cont_rank($cont_id);
     echo json_encode(array('success' => true));
 } else {
     if ($op == 'leave') {
         $row = mysqli_fetch_row(mysqli_query($con, "select start_time, end_time from contest where contest_id={$cont_id} limit 1"));
         if (!$row) {
             echo json_encode(array('success' => false, 'message' => _('No such contest...')));