You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

同等级玩家匹配同排、无匹配标注功能开发问题咨询

解决方案

核心实现逻辑

  • 先将查询到的所有玩家按等级归类分组,同等级玩家存入同一个数组
  • 对每个等级下的玩家数组做随机打乱,符合原有随机匹配的需求
  • 遍历分组两两配对生成表格行,若该等级玩家总数为奇数,剩余单个玩家的另一半位置标注no match found

代码修改

Model 代码(无需改动,保留原有随机排序逻辑即可)

function playerss(){
    $this->db->order_by('rand()');
    $query = $this->db->get('entry');
    return $query->result();
}

Controller 代码(替换原有逻辑,核心匹配规则在此实现)

function players(){
    $query = $this->model->playerss();
    // 按等级分组玩家
    $groupedPlayers = [];
    foreach ($query as $player) {
        $groupedPlayers[$player->level][] = $player;
    }

    $tableHtml = '';
    foreach ($groupedPlayers as $level => $players) {
        // 同等级玩家打乱顺序,实现随机匹配
        shuffle($players);
        $playerCount = count($players);
        // 两两配对生成行
        for ($i = 0; $i < $playerCount; $i += 2) {
            $tableHtml .= '<tr>';
            // 红方玩家信息
            $tableHtml .= '<td>'.$players[$i]->player.'</td>';
            $tableHtml .= '<td>'.$players[$i]->level.'</td>';
            // 判断是否有同等级匹配玩家
            if (isset($players[$i + 1])) {
                // 蓝方玩家信息
                $tableHtml .= '<td>'.$players[$i + 1]->player.'</td>';
                $tableHtml .= '<td>'.$players[$i + 1]->level.'</td>';
            } else {
                // 无匹配时跨列显示提示
                $tableHtml .= '<td colspan="2" class="text-center">no match found</td>';
            }
            $tableHtml .= '</tr>';
        }
    }
    echo json_encode($tableHtml);
}

其余代码说明

原有HTML、JS逻辑无需修改,直接沿用即可,修改后即可实现同等级玩家匹配、无同等级玩家显示提示的需求。

内容的提问来源于stack exchange,提问作者Ulquiorra Schiffer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 04:45:03