hxp
2025-06-04 f4a514d5ac952110da846636ecbb9de951eaf3d2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
 
/**
 * 查询世界 或 地区 榜单
 */
 
include_once "../../../Common/CommFunc.php";
include_once "../../../Common/Logging.php";
include_once '../../../Common/MongoDB7.php';
include_once "errorCode.php";
 
\Logging\CreateLogging("queryWorldRank.php");
 
$openid = $_GET['openid']; // 可选参数
$gamename = $_GET['gamename'];
$areaType = (int) $_GET['areaType']; // 1-世界
$scoreType = (int) $_GET['scoreType']; // 0-层数 1-关卡
$page = (int) $_GET['page']; // 分页查询,0代表自己周围
if ($gamename == "") {
    echo json_encode(ErrorCode::retErr(ErrorCode::$ParamErr, "no gamename"));
    return;
}
if ($areaType != 1) {
    echo json_encode(ErrorCode::retErr(ErrorCode::$ParamErr, "areaType must be 1"));
    return;
}
if ($scoreType != 0 and $scoreType != 1) {
    echo json_encode(ErrorCode::retErr(ErrorCode::$ParamErr, "areaType must be 0 or 1"));
    return;
}
 
$gameconfig = parse_ini_file("config/" . $gamename . ".ini", true);
$dbOpt = new \MongoDB7\MongoDb("Player", $gameconfig["db"]);
 
$rankData = ['areaType' => $areaType, 'scoreType' => $scoreType, 'page' => $page];
$queryTop = 20; // 每页查询数量
 
// 层数榜
if ($scoreType === 0) {
    if ($page > 0) {
        $where = ['weekScore' => ['$gt' => 0]]; // 大于0的数据
        $options = [
            'projection' => ['_id' => 0, 'weekScore' => 1, 'openid' => 1, 'avatarUrl' => 1, 'nickName' => 1],
            'sort' => ['weekScore' => -1], // 倒序
            'limit' => $queryTop,
            'skip' => ($page - 1) * $queryTop,
        ];
        $dbRet = $dbOpt->query($where, $options);
        $rankData["listData"] = $dbRet;
    }
 
    // 查自己
    else if ($openid) {
        $where = ['weekScore' => ['$gt' => 0]]; // 大于0的数据
        $options = [
            'projection' => ['_id' => 0, 'weekScore' => 1, 'openid' => 1, 'avatarUrl' => 1, 'nickName' => 1],
            'sort' => ['weekScore' => -1], // 倒序
        ];
        $dbRet = $dbOpt->query($where, $options);
        if ($dbRet) {
            $rankData["listData"] = getSelfArroudData($openid, $dbRet);
        }
    }
}
 
// 关卡榜
else if ($scoreType === 1) {
    if ($page > 0) {
        $where = ['missionNum' => ['$gt' => 0]]; // 大于0的数据
        $options = [
            'projection' => ['_id' => 0, 'missionNum' => 1, 'openid' => 1, 'avatarUrl' => 1, 'nickName' => 1],
            'sort' => ['missionNum' => -1], // 倒序
            'limit' => $queryTop,
            'skip' => ($page - 1) * $queryTop,
        ];
        $dbRet = $dbOpt->query($where, $options);
        $rankData["listData"] = $dbRet;
    }
 
    // 查自己
    else if ($openid) {
        $where = ['missionNum' => ['$gt' => 0]]; // 大于0的数据
        $options = [
            'projection' => ['_id' => 0, 'missionNum' => 1, 'openid' => 1, 'avatarUrl' => 1, 'nickName' => 1],
            'sort' => ['missionNum' => -1], // 倒序
        ];
        $dbRet = $dbOpt->query($where, $options);
        if ($dbRet) {
            $rankData["listData"] = getSelfArroudData($openid, $dbRet);
        }
    }
}
 
echo json_encode($rankData);
 
// 获取玩家排行上下x名玩家数据,这里暂用遍历的,实际不能用这样处理
function getSelfArroudData($openid, $dbRet, $getCount = 2)
{
    $playerRank = 0;
    foreach ($dbRet as $i => $d) {
        $dbRet[$i]['rank'] = $i + 1; // 赋值排名
        if ($d['openid'] == $openid) {
            $playerRank = $dbRet[$i]['rank'];
        }
        if ($playerRank > 0 and $d['rank'] >= ($playerRank + $getCount)) {
            break;
        }
    }
    if ($playerRank <= 0) {
        return [];
    }
    $start = max(0, $playerRank - $getCount - 1);
    return array_slice($dbRet, $start, $getCount * 2 + 1);
}