<?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);
|
}
|