HEX
Server: Apache/2.4.59 (Debian)
System: Linux keymana 4.19.0-21-cloud-amd64 #1 SMP Debian 4.19.249-2 (2022-06-30) x86_64
User: lijunjie (1003)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/uileague/wp-content/themes/uileague/helper/matches.php
<?php
class MatchesHelper
{
    public static $pb_table = "orm_matches";

    public static function getList($args)
    {
        global $wpdb;
        $keyword = htmlspecialchars($args['keyword']);
        $keyword = empty($keyword) ? '' : trim($keyword);
        $page = intval($args['cpage']) > 1 ? intval($args['cpage']) : 1;
        $all = intval($args['pageLimit']) == -1;
        $pageLimit = intval($args['pageLimit']) > 1 ? intval($args['pageLimit']) : 10;
        $filterLeagueId = trim($args['league_id']);
        $filterTeamId = trim($args['team_id']);
        $filterStatus = trim($args['status']);
        $filterStatusTag = trim($args['status_tag']);
        $order_by = "dt desc";
        $filterSql = "";
        if ($filterLeagueId != "" && is_numeric($filterLeagueId)) {
            $filterSql .= $wpdb->prepare(" AND `league_id` = %d ", $filterLeagueId);
        }
        if ($filterTeamId != "" && is_numeric($filterTeamId)) {
            $filterSql .= $wpdb->prepare(" AND (`team_1` = %d  or `team_2` = %d) ", $filterTeamId, $filterTeamId);
        }
        if ($filterStatus != "" && is_numeric($filterStatus)) {
            $filterSql .= $wpdb->prepare(" AND `status` = %d ", $filterStatus);
        }
        if ($filterStatusTag == "pending") {
            $filterSql .= $wpdb->prepare(" AND `status` = %d ", 0);
            $order_by = "dt asc";
        } else if ($filterStatusTag == "start") {
            $filterSql .= $wpdb->prepare(" AND (`status` = %d or `status` = %d or `status` = %d) ", 1, 2, 3);
            $order_by = "dt asc";
        } else if ($filterStatusTag == "end") {
            $filterSql .= $wpdb->prepare(" AND `status` = %d ", 4);
        }
        $table = self::$pb_table;
        $total = intval($wpdb->get_var("SELECT count(id) FROM `{$table}` WHERE `deleted` is null AND (`team_1` Like '%" . $keyword . "%' or `team_2` Like '%" . $keyword . "%' ) " . $filterSql));
        if ($all) {
            $results = $wpdb->get_results("SELECT * FROM `{$table}` WHERE `deleted` is null AND (`team_1` Like '%" . $keyword . "%' or `team_2` Like '%" . $keyword . "%') " . $filterSql . " order by " . $order_by);
        } else {
            $results = $wpdb->get_results("SELECT * FROM `{$table}` WHERE `deleted` is null AND (`team_1` Like '%" . $keyword . "%' or `team_2` Like '%" . $keyword . "%') " . $filterSql . $wpdb->prepare(" order by {$order_by} LIMIT %d,%d", ($page - 1) * $pageLimit, $pageLimit));
        }
        $dataList = array_map(function ($item) {
            return MatchesHelper::map_item($item);
        }, $results);
        $result = new stdClass();
        $result->rc = ApiStatus::SUCCESS[0];
        $result->msg = ApiStatus::SUCCESS[1];
        $result->currentPage = $page;
        $result->total = $total;
        $result->data = $dataList;
        return $result;
    }

    public static function getLatestList($args)
    {
        global $wpdb;
        $team_id = $args['team_id'];

        $pageLimit = intval($args['pageLimit']);
        $table = self::$pb_table;
        $total = $pageLimit > 0 ? $pageLimit : 1; // 取最接近當前日期的條數
        $sql = "";
        if (!empty($team_id)) {
            $sql_before = $wpdb->prepare("SELECT *,ABS(NOW()-dt) as difftime FROM `{$table}` WHERE `deleted` is null AND (`team_1` = %d  or `team_2` = %d) AND dt<NOW() order by difftime asc Limit %d", $team_id, $team_id, $total);
            $sql_after = $wpdb->prepare("SELECT *,ABS(NOW()-dt) as difftime FROM `{$table}` WHERE `deleted` is null AND (`team_1` = %d  or `team_2` = %d) AND dt>NOW() order by difftime asc Limit %d", $team_id, $team_id, $total);
            $sql = "select * from (select * from ({$sql_before}) a union select * from ({$sql_after}) b ) t order by difftime asc limit {$total}";
            $results = $wpdb->get_results($sql);
            $dataList = array_map(function ($item) {
                return MatchesHelper::map_item($item);
            }, $results);
        } else {
            $sql_before = $wpdb->prepare("SELECT *,ABS(NOW()-dt) as difftime FROM `{$table}` WHERE `deleted` is null AND dt<NOW() order by difftime asc Limit %d",  $total);
            $sql_after = $wpdb->prepare("SELECT *,ABS(NOW()-dt) as difftime FROM `{$table}` WHERE `deleted` is null AND dt>NOW() order by difftime asc Limit %d",  $total);
            $sql = "select * from (select * from ({$sql_before}) a union select * from ({$sql_after}) b ) t order by difftime asc limit {$total}";
            $results = $wpdb->get_results($sql);
            $dataList = array_map(function ($item) {
                return MatchesHelper::map_item($item);
            }, $results);
        }

        $result = new stdClass();
        $result->rc = ApiStatus::SUCCESS[0];
        $result->msg = ApiStatus::SUCCESS[1];
        $result->currentPage = 1;
        $result->total = $total;
        $result->data = $dataList;
        return $result;
    }

    public static function get_by_id($id, $map_item = true)
    {
        if (empty($id)) return null;
        global $wpdb;
        $row = $wpdb->get_row($wpdb->prepare("select * from `" . self::$pb_table . "` where `deleted` is null AND id = %d", $id));
        return $map_item ? self::map_item($row) : $row;
    }

    public static function map_item($row)
    {
        if (empty($row)) return null;
        $row->league_obj = LeagueHelper::get_by_id($row->league_id);
        $row->team_1_obj = TeamHelper::get_by_id($row->team_1);
        $row->team_2_obj = TeamHelper::get_by_id($row->team_2);
        // 这个结果需要比赛结果才会更新
        $row->team_1_pts = TeamPts2Helper::get_by_team($row->team_1, $row->id, false); // $map_item 一定要为false,否则死循环
        $row->team_2_pts = TeamPts2Helper::get_by_team($row->team_2, $row->id, false);
        // 弃权的队伍
        $row->abstain_team = null;
        if ($row->team_1_pts != null && $row->team_2_pts != null) {
            if ($row->team_1_pts->abstain > 0) {
                $row->abstain_team = $row->team_1_obj;
            } else if ($row->team_2_pts->abstain > 0) {
                $row->abstain_team = $row->team_2_obj;
            }
        }
        // 这个结果是比赛的实时比分
        $row->team_1_total = PlayersPtsHelper::get_pts_by_team($row->id, $row->team_1);
        $row->team_2_total = PlayersPtsHelper::get_pts_by_team($row->id, $row->team_2);
        // 球衣颜色(不设置则使用默认球衣)
        $row->team_1_jersey_color = MatchesPtsHelper::get_value($row->id, $row->team_1, MatchesPtsHelper::KEY_JERSEY_COLOR, $row->team_1_obj->jersey_color);
        $row->team_2_jersey_color = MatchesPtsHelper::get_value($row->id, $row->team_2, MatchesPtsHelper::KEY_JERSEY_COLOR, $row->team_2_obj->jersey_color);
        $row->team_1_jersey_color_deep = MatchesHelper::is_deep_color($row->team_1_jersey_color) ? "1" : "0";
        $row->team_2_jersey_color_deep = MatchesHelper::is_deep_color($row->team_2_jersey_color) ? "1" : "0";
        $row->location_obj = LocationHelper::get_by_id($row->location_id);
        $row->link = home_url('match-detail?id=' . $row->id);
        return $row;
    }

    public static function is_deep_color($hex)
    {
        $rgb = MatchesHelper::hex2rgb($hex);
        if (count($rgb) == 3) {
            $r = $rgb[0];
            $g = $rgb[1];
            $b = $rgb[2];
            $y = 0.299 * $r + 0.587 * $g + 0.114 * $b;
            return $y < 150;
        }
        return false;
    }

    public static function hex2rgb($hex)
    {
        if (empty($hex)) return [];
        $hex = trim($hex);
        $hex = str_replace("#", "", $hex);
        $arr = str_split($hex);
        $arrLen = count($arr);
        if ($arrLen == 6) {
            $rs = $arr[0] . $arr[1];
            $gs = $arr[2] . $arr[3];
            $bs = $arr[4] . $arr[5];
            $r = hexdec($rs);
            $g = hexdec($gs);
            $b = hexdec($bs);
            if ($r > 255 || $g > 255 || $b > 255) {
                return [];
            }
            return [$r, $g, $b];
        } else if ($arrLen == 3) {
            $rs = $arr[0] . $arr[0];
            $gs = $arr[1] . $arr[1];
            $bs = $arr[2] . $arr[2];
            $r = hexdec($rs);
            $g = hexdec($gs);
            $b = hexdec($bs);
            if ($r > 255 || $g > 255 || $b > 255) {
                return [];
            }
            return [$r, $g, $b];
        } else {
            return [];
        }
    }

    // 上半場
    public static function start($match_id, $uid = 0, $wpuid = 0)
    {
        return MatchesHelper::update_status(array(
            'id' => $match_id,
            'status' => 1,
        ), $uid, $wpuid);
    }

    // 中場休息
    public static function half_time($match_id, $uid = 0, $wpuid = 0)
    {
        return MatchesHelper::update_status(array(
            'id' => $match_id,
            'status' => 2,
        ), $uid, $wpuid);
    }

    // 下半場
    public static function last_half($match_id, $uid = 0, $wpuid = 0)
    {
        return MatchesHelper::update_status(array(
            'id' => $match_id,
            'status' => 3,
        ), $uid, $wpuid);
    }

    // 結束(正常)
    public static function finished($match_id, $uid = 0, $wpuid = 0)
    {
        return MatchesHelper::match_finished($match_id, 0, $uid, $wpuid);
    }

    // 結束(弃权)
    public static function abstain($match_id, $team_id = 0, $uid = 0, $wpuid = 0)
    {
        return MatchesHelper::match_finished($match_id, $team_id, $uid, $wpuid);
    }

    /**
     * 結束(是否弃权)
     * 
     * @param $team_id 如果不为0,表示该球队弃权,否则表示正常结束比赛
     */
    public static function match_finished($match_id, $team_id = 0, $uid = 0, $wpuid = 0)
    {
        $result = MatchesHelper::update_status(array(
            'id' => $match_id,
            'status' => 4,
        ), $uid, $wpuid);
        if ($result->rc == ApiStatus::SUCCESS[0]) { // 比赛成功结束,计算比赛结果
            return TeamPts2Helper::match_finished($match_id, $team_id, $uid, $wpuid);
        }
        return $result;
    }

    public static function update_status($args, $uid = 0, $wpuid = 0)
    {
        global $wpdb;
        $id = intval($args['id']);
        $status = intval($args['status']);

        $result = new stdClass();

        if (empty($id)) {
            $result->rc = ApiStatus::ERR_PARAMS[0];
            $result->msg = ApiStatus::ERR_PARAMS[1];
        } else {
            global $wpdb;
            $row = MatchesHelper::get_by_id($id);
            if ($row == null) {
                $result->rc = ApiStatus::ERR_MATCHES_NONE[0];
                $result->msg = ApiStatus::ERR_MATCHES_NONE[1];
            } else {
                $new_data = array(
                    'status' => $status,
                    'updated' => current_time('mysql'),
                );
                $res = $wpdb->update(
                    self::$pb_table,
                    $new_data,
                    array('id' => $id),
                    array('%d', '%s'),
                    array('%d')
                );
                if ($res === false) {
                    $result->rc = ApiStatus::ERR_MATCHES_UPDATE[0];
                    $result->msg = ApiStatus::ERR_MATCHES_UPDATE[1];
                } else {
                    Logger::log("更新了比賽狀態", self::$pb_table, $id, $row, $new_data, $uid, $wpuid);
                    uileague_sock_push(array(
                        'type' => WebSocketHelper::TYPE_UPDATE_MATCH,
                        'data_id' => $id,
                    ));
                    $result->rc = ApiStatus::SUCCESS[0];
                    $result->msg = ApiStatus::SUCCESS[1];
                }
            }
        }
        return $result;
    }

    public static function add($args, $uid = 0, $wpuid = 0)
    {
        global $wpdb;
        $league_id = intval($args['league_id']);
        $team_1 = intval($args['team_1']);
        $team_2 = intval($args['team_2']);
        $location_id = intval($args['location_id']);
        $dt = trim($args['dt']);
        $status = intval($args['status']);
        $result = new stdClass();

        if (empty($league_id) || empty($team_1) || empty($team_2)) {
            $result->rc = ApiStatus::ERR_PARAMS[0];
            $result->msg = ApiStatus::ERR_PARAMS[1];
        } else if ($team_1 == $team_2) {
            $result->rc = ApiStatus::ERR_MATCHES_TEAM_DUPLICATION[0];
            $result->msg = ApiStatus::ERR_MATCHES_TEAM_DUPLICATION[1];
        } else {
            $new_data = array(
                'league_id' => $league_id,
                'team_1' => $team_1,
                'team_2' => $team_2,
                'location_id' => $location_id,
                'dt' => $dt,
                'status' => $status,
                'created' => current_time('mysql'),
                'updated' => current_time('mysql'),
            );
            $res = $wpdb->insert(
                self::$pb_table,
                $new_data,
                array('%d', '%d', '%d', '%d', '%s', '%d', '%s', '%s')
            );
            if ($res === false) {
                $result->rc = ApiStatus::ERR_MATCHES_ADD[0];
                $result->msg = ApiStatus::ERR_MATCHES_ADD[1];
            } else {
                $insert_id = $wpdb->insert_id;
                Logger::log("添加了比賽", self::$pb_table, $insert_id, null, $new_data, $uid, $wpuid);
                // 初始化比賽結果
                TeamPts2Helper::init($team_1, $insert_id, $uid, $wpuid);
                TeamPts2Helper::init($team_2, $insert_id, $uid, $wpuid);
                uileague_sock_push(array(
                    'type' => WebSocketHelper::TYPE_GET_MATCHES,
                ));
                $result->rc = ApiStatus::SUCCESS[0];
                $result->msg = ApiStatus::SUCCESS[1];
                $result->data = $insert_id;
            }
        }
        return $result;
    }

    public static function update($args, $uid = 0, $wpuid = 0)
    {
        global $wpdb;
        $id = intval($args['id']);
        $team_1 = intval($args['team_1']);
        $team_2 = intval($args['team_2']);
        $location_id = intval($args['location_id']);
        $dt = trim($args['dt']);
        $status = intval($args['status']);

        $result = new stdClass();

        if (empty($id) || empty($team_1) || empty($team_2)) {
            $result->rc = ApiStatus::ERR_PARAMS[0];
            $result->msg = ApiStatus::ERR_PARAMS[1];
        } else if ($team_1 == $team_2) {
            $result->rc = ApiStatus::ERR_MATCHES_TEAM_DUPLICATION[0];
            $result->msg = ApiStatus::ERR_MATCHES_TEAM_DUPLICATION[1];
        } else {
            global $wpdb;
            $row = MatchesHelper::get_by_id($id);
            if ($row == null) {
                $result->rc = ApiStatus::ERR_MATCHES_NONE[0];
                $result->msg = ApiStatus::ERR_MATCHES_NONE[1];
            } else {
                $new_data = array(
                    'team_1' => $team_1,
                    'team_2' => $team_2,
                    'location_id' => $location_id,
                    'dt' => $dt,
                    'status' => $status,
                    'updated' => current_time('mysql'),
                );
                $res = $wpdb->update(
                    self::$pb_table,
                    $new_data,
                    array('id' => $id),
                    array('%d', '%d', '%d', '%s', '%d', '%s'),
                    array('%d')
                );
                if ($res === false) {
                    $result->rc = ApiStatus::ERR_MATCHES_UPDATE[0];
                    $result->msg = ApiStatus::ERR_MATCHES_UPDATE[1];
                } else {
                    Logger::log("更新了比賽", self::$pb_table, $id, $row, $new_data, $uid, $wpuid);
                    // 初始化比賽結果
                    TeamPts2Helper::init($team_1, $id, $uid, $wpuid);
                    TeamPts2Helper::init($team_2, $id, $uid, $wpuid);
                    uileague_sock_push(array(
                        'type' => WebSocketHelper::TYPE_UPDATE_MATCH,
                        'data_id' => $id,
                    ));
                    $result->rc = ApiStatus::SUCCESS[0];
                    $result->msg = ApiStatus::SUCCESS[1];
                }
            }
        }
        return $result;
    }

    public static function delete($args, $uid = 0, $wpuid = 0)
    {
        $id = intval($args['id']);
        $row = MatchesHelper::get_by_id($id);

        $result = new stdClass();

        if ($row == null) {
            $result->rc = ApiStatus::ERR_MATCHES_NONE[0];
            $result->msg = ApiStatus::ERR_MATCHES_NONE[1];
        } else {
            global $wpdb;
            // $res = $wpdb->delete(self::$pb_table, array('id' => $id), array('%d'));
            $res = $wpdb->update(
                self::$pb_table,
                array('deleted' => current_time('mysql')),
                array('id' => $id),
                array('%s'),
                array('%d')
            );
            if ($res === false) {
                $result->rc = ApiStatus::ERR_MATCHES_DELETE[0];
                $result->msg = ApiStatus::ERR_MATCHES_DELETE[1];
            } else {
                Logger::log("刪除了比賽", self::$pb_table, $id, $row, null, $uid, $wpuid);
                // 刪除比賽結果
                TeamPts2Helper::delete_by_team($row->team_1, $id, $uid, $wpuid);
                TeamPts2Helper::delete_by_team($row->team_2, $id, $uid, $wpuid);
                uileague_sock_push(array(
                    'type' => WebSocketHelper::TYPE_GET_MATCHES,
                ));
                $result->rc = ApiStatus::SUCCESS[0];
                $result->msg = ApiStatus::SUCCESS[1];
            }
        }
        return $result;
    }
}