PVPProc.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. <?php
  2. namespace loyalsoft;
  3. /**
  4. * PVPProc 竞技场 战斗模块, 挑战玩家镜像
  5. * @version
  6. * 1.0.0 Created at 2017-6-26. by --gwang
  7. * @author gwang (mail@wanggangzero.cn)
  8. * @copyright ? 2017-6-26, SJZ LoyalSoft Corporation & gwang. All rights reserved.
  9. */
  10. class PVPProc {
  11. //
  12. // <editor-fold defaultstate="collapsed" desc=" 常量 ">
  13. /**
  14. * 挑战记录最大条数
  15. */
  16. const maxLogCount = 49;
  17. /**
  18. * 查找对手数量
  19. */
  20. const matchCount = 5;
  21. /**
  22. * 竞技场赛季起始时间戳
  23. */
  24. const pvpStartTs = 1588521600; # 2020年5月4日 0时0分0秒
  25. /**
  26. * 每个赛季持续时常
  27. */
  28. const pvpSeasonLengt = 86400 * 14; # 2周
  29. /**
  30. * 竞技场初始积分
  31. */
  32. const pvpBaseScore = 1000;
  33. /**
  34. * 竞技场 最大上榜人数
  35. */
  36. const pvpMaxRank = 500;
  37. // </editor-fold>
  38. /**
  39. * [6803] 挑战 - 查询对手信息 等级、头像、昵称、战队信息(言灵师,等级,星级,武器,技能,言灵)
  40. * @param req $req
  41. */
  42. public static function GetChallengeAdversaryInfo($req) {
  43. $targetUID = $req->paras[0]; # 参数: 对手的UID
  44. $uinfo = UserProc::getUserInfo($req->mem, $req->zoneid, $targetUID); # 读取玩家信息
  45. if (null == $uinfo) {
  46. Err(ErrCode::user_no_err);
  47. }
  48. $team = JsonUtil::decode($uinfo->game->heroTeamConfig);
  49. $heros = new \stdClass();
  50. $curTeamId = $team->curUseTeamID;
  51. $arr_nil = array();
  52. // var_dump($team->teamDic->$curTeamId);
  53. foreach ($team->teamDic->$curTeamId->heros as $i => $hid) {
  54. if ($hid > 0) {
  55. $n_hid = $hid - 10000;
  56. $heros->$n_hid = $uinfo->game->heros->collectHeros->$hid;
  57. }
  58. }
  59. $adversary = array(# # 拼装玩家信息
  60. 'uid' => $targetUID,
  61. 'name' => my_null_default($uinfo->game->baseInfo->name, ""),
  62. 'level' => my_null_default($uinfo->game->baseInfo->level, 1),
  63. 'headImg' => my_null_default($uinfo->game->baseInfo->img, ""),
  64. // 'skills' => null, # # skills暂时没有实例数据
  65. 'equipment' => array("equipments" => my_null_default($uinfo->game->store->equipment, new \stdClass())), # 武器
  66. 'yanling' => array("items" => my_null_default($uinfo->game->store->yanling, new \stdClass())), # 言灵
  67. 'heros' => my_null_default($heros, new \stdClass()), # # 英雄集合
  68. );
  69. $result = array(# # 拼装返回值
  70. "adversaryInfo" => $adversary
  71. );
  72. return Resp::ok($result);
  73. }
  74. /**
  75. * [6804] 挑战 - 记录挑战结果
  76. * @param req $req
  77. */
  78. static function LogChallengeInfo($req) {
  79. // 参数: 对手uid,对手昵称,对手头像, 对战结果, 胜利者的留言(失败时无法留言"")
  80. list($targetUID, $name, $headImg, $win, $msg) = $req->paras;
  81. $key_mine = MemKey_User::OffensiveLog_zset($req->zoneid, $req->uid);
  82. $key_target = MemKey_User::DefensiveLog_zset($req->zoneid, $targetUID);
  83. $ts = now(); # 记录时间戳
  84. $req->mem->zadd($key_mine, array(# # 组装被挑战对手信息
  85. JsonUtil::encode(array(
  86. 'uid' => my_null_default($targetUID, "-"),
  87. 'name' => my_null_default($name, ""),
  88. 'headImg' => my_null_default($headImg, ""),
  89. 'win' => my_null_default($win, false),
  90. 'msg' => my_null_default($msg, ""),
  91. 'ts' => $ts
  92. )) => $ts));
  93. $req->mem->zremrangebyrank($key_mine, self::maxLogCount, -1); # 保留50条数据
  94. $req->mem->zadd($key_target, array(# # 组装挑战者信息
  95. JsonUtil::encode(array(
  96. 'uid' => $req->uid,
  97. 'name' => $req->userInfo->game->baseInfo->name,
  98. 'headImg' => $req->userInfo->game->baseInfo->img,
  99. 'win' => !my_null_default($win, false),
  100. 'msg' => my_null_default($msg, ""),
  101. 'ts' => $ts
  102. )) => $ts));
  103. $req->mem->zremrangebyrank($key_target, self::maxLogCount, -1); # 保留50条数据
  104. // 暂无发放奖励流程
  105. // 更新每日任务
  106. TaskProc::OnRankChalenge();
  107. UserProc::updateUserInfo();
  108. // 返回
  109. return Resp::ok(); # 返回成功
  110. }
  111. /**
  112. * [6805] 挑战 - 拉取挑战记录
  113. * @param req $req
  114. */
  115. static function GetChagllengeLog($req) {
  116. // 参数:无
  117. $key_off = MemKey_User::OffensiveLog_zset($req->zoneid, $req->uid);
  118. $key_def = MemKey_User::DefensiveLog_zset($req->zoneid, $req->uid);
  119. // 拉取自己的挑战记录
  120. $offLog = $req->mem->zrange($key_off, 0, self::maxLogCount);
  121. $defLog = $req->mem->zrange($key_def, 0, self::maxLogCount);
  122. // Ps. 挑战记录分为2个榜, 且按照时间戳记录,晚于指定时间戳的判定为未读消息,挑战记录最多记录50条
  123. // if (!CommUtil::isPropertyExists($req->userInfo->game->privateState, "lastCheckDefLog")) {
  124. $req->userInfo->game->privateState->lastCheckDefLog_ts = now(); # 记录时间戳
  125. // }
  126. UserProc::updateUserInfo(); # 回写数据
  127. // 记录拉取时间戳(在主界面有个未读消息条数显示, 需要靠最后拉取时间戳对比, 时间戳之后的消息是未读消息)
  128. // 回传数据记录
  129. array_walk($offLog, function (&$i) { # 解码一下
  130. $i = JsonUtil::decode($i);
  131. });
  132. array_walk($defLog, function (&$i) { # 解码一下
  133. $i = JsonUtil::decode($i);
  134. });
  135. return Resp::ok(array(
  136. 'offLog' => $offLog,
  137. 'defLog' => $defLog
  138. ));
  139. }
  140. // <editor-fold defaultstate="collapsed" desc=" 竞技商店 ">
  141. /**
  142. * 王刚 16:23:39 (2020.5.9)
  143. 商店现在的模式定位:
  144. 商店显示所有物品, 刷新时是重置购买/售罄记录
  145. 刘海 16:24:23 (2020.5.9)
  146. 没错
  147. */
  148. /**
  149. * [6820] 竞技商店 主界面
  150. * @param req $req
  151. */
  152. public static function pvpShopMain($req) {
  153. $pvp = new UserPVPModel($req->userInfo->game->pvp); # PVP信息
  154. if ($pvp->shopRefreshTs < now()) { # 检查刷新时间<now => 刷新商品列表
  155. $pvp->shopRefreshTs = now() + glc()->PVP_shop_refresh_interval; # 更新刷新时间
  156. $pvp->curShopItems = GameConfig::pvp_shop(); # 重刷道具
  157. $req->userInfo->game->pvp = $pvp; # 回写
  158. UserProc::updateUserInfo();
  159. }
  160. return Resp::ok($pvp); // 返回
  161. }
  162. /**
  163. * [6821] 竞技 商店 购买道具
  164. * @param req $req
  165. */
  166. public static function pvpShopBuy($req) {
  167. $index = $req->paras[0]; # 参数:道具索引(typeId)
  168. $pvp = new UserPVPModel($req->userInfo->game->pvp); # PVP 数据
  169. my_Assert(CommUtil::isPropertyExists($pvp->curShopItems, $index), ErrCode::err_innerfault); # 没有找到改商品
  170. // isEditor() && $citem = new \sm_pvp_shop();
  171. $citem = $pvp->curShopItems->$index; # 查询物品数据
  172. my_Assert($citem->sold == 0, ErrCode::pvp_item_soldout); # 防御道具已售罄
  173. // var_dump($citem);
  174. my_Assert($citem->priceType == 5, ErrCode::pay_price_err); # 防御定价异常
  175. my_Assert($pvp->pvpCoins > $citem->price, ErrCode::pvp_coinnotenough); # pvp币不足
  176. $citem->sold += 1; # 设置已售罄/已购买标志
  177. $pvp->pvpCoins -= $citem->price; # 扣除竞技币
  178. StoreProc::AddMultiItemInStore($req, $citem->goods); // 发放道具
  179. $req->userInfo->game->pvp = $pvp; // 回写数据
  180. UserProc::updateUserInfo();
  181. return Resp::ok(array('pvp' => $pvp, 'store' => $req->userInfo->game->store)); # 返回
  182. }
  183. /**
  184. * [6822] 竞技 商店 刷新道具
  185. * @param req $req
  186. */
  187. public static function pvpShopRefresh($req) {
  188. // 扣除刷新消耗
  189. $pvp = new UserPVPModel($req->userInfo->game->pvp);
  190. $costCash = glc()->PVP_shop_refresh_cash;
  191. my_Assert(UserGameModel::Consume_Cash($req->userInfo->game, $costCash), ErrCode::notenough_cash_msg);
  192. // $pvp->shopRefreshTs = now() + glc()->PVP_shop_refresh_interval; # 更新刷新时间
  193. $pvp->curShopItems = GameConfig::pvp_shop(); # 重刷道具
  194. $req->userInfo->game->pvp = $pvp; # 回写
  195. UserProc::updateUserInfo(); # 回写玩家数据
  196. return Resp::ok($pvp); # 返回
  197. }
  198. // // </editor-fold>
  199. //
  200. // <editor-fold defaultstate="collapsed" desc=" 竞技场 681x">
  201. //
  202. /**
  203. * 辅助方法:取当前赛季的编号(从赛季起始开始算起)
  204. * @return int
  205. */
  206. public static function GetCurSeasonID() {
  207. $n = ceil((now() - self::pvpStartTs ) / self::pvpSeasonLengt); # 进一取整
  208. return $n;
  209. }
  210. /**
  211. * 辅助方法:取指定赛季的结束时间戳
  212. * @param int $seasonID
  213. * @return int
  214. */
  215. public static function GetSeasonEndTs($seasonID) {
  216. $ts = self::pvpStartTs + $seasonID * self::pvpSeasonLengt; # 从起始点开始 + n个赛季时常
  217. return $ts;
  218. }
  219. /**
  220. * [6810] 竞技场 拉取主界面信息
  221. * @param Req $req
  222. */
  223. static function pvpMainInfo($req) {
  224. $uid = $req->uid; # 快速访问UID
  225. $zoneid = $req->zoneid; # 快速访问zoneid
  226. $pvp = new UserPVPModel($req->userInfo->game->pvp); # 设计玩家pvp数据结构
  227. $pvp->refreshDailyData(); # 刷新免费挑战次数
  228. $seasonId = self::GetCurSeasonID(); # 当前赛季ID
  229. $key = MemKey_GameRun::Game_PVPScoreByZoneSeason_zset($zoneid, $seasonId); # 积分总榜
  230. $score = self::_getScore_by_uid($uid, $key); # 玩家积分
  231. $rank = self::_getRank_by_uid($uid, $key); # 玩家排名
  232. $fPower = HeroProc::CalcUserFightPower($zoneid, $uid, $req->userInfo->game); # 玩家总战力?还是当前防守队伍的战斗力?
  233. $numNewLog = 0; // todo: 真正查询是否有新战报
  234. $matches = self::getNewMatches($pvp, $uid, $zoneid); # 获得新的匹配对手
  235. $pvp->sendRewardEmail($zoneid, $uid, $seasonId); # 发奖励邮件
  236. // if ($pvp->haventReward_season >= 0 && $pvp->haventReward_season < $seasonId) { # 尚未发放上赛季奖励
  237. // $haventKey = MemKey_GameRun::Game_PVPScoreByZoneSeason_zset($zoneid, $pvp->haventReward_season);
  238. // // todo:发放上赛季奖励邮件
  239. // $rank = self::_getRank_by_uid($uid, $haventKey); # 查询上赛季排名
  240. // if ($rank <= self::pvpMaxRank) { # 防御未上榜
  241. // foreach (GameConfig::pvp_rankreward() as $cfg) {
  242. // isEditor() and $cfg = new \sm_pvp_rankreward();
  243. // if ($rank >= $cfg->minRank && $rank <= $cfg->maxRank) { # 找到对应的名次段
  244. // EmailProc::SendPvpRankReward_Season($zoneid, $uid, $rank); # 发放奖励邮件(竞技币)
  245. // }
  246. // }
  247. // UserProc::updateUserInfo();
  248. // }
  249. // }
  250. // $pvp->haventReward_season = $seasonId; # 更新待发奖赛季
  251. // if ($pvp->haventReward_tsDay == tsDay() - 1) { # 尚未发放昨天奖励
  252. // $haventKey_day = MemKey_GameRun::Game_PVPScoreByZone_zset_Day($zoneid, $pvp->haventReward_tsDay);
  253. // if (!gMem()->exists($haventKey_day) && $pvp->haventReward_tsDay == tsDay() - 1) { # 昨天的积分记录不存在
  254. // gMem()->zcopy($key, $haventKey_day); # 复制一份当前积分作为昨天的截止积分榜
  255. // } else {
  256. // // 不是昨天登录的, 且没有对应的数据 就不再复制当前数据了.直接未上榜处理
  257. // }
  258. // $rank = self::_getRank_by_uid($uid, $haventKey_day); # 查询上一天排名
  259. // if ($rank <= self::pvpMaxRank) { # 防御未上榜
  260. // foreach (GameConfig::pvp_rankreward() as $cfg) {
  261. // isEditor() and $cfg = new \sm_pvp_rankreward();
  262. // if ($rank >= $cfg->minRank && $rank <= $cfg->maxRank) { # 找到对应的名次段
  263. // EmailProc::SendPvpRankReward_Lastday($zoneid, $uid, $rank); # 发放奖励邮件(竞技币)
  264. // }
  265. // }
  266. // UserProc::updateUserInfo();
  267. // }
  268. // }
  269. // $pvp->haventReward_tsDay = tsDay(); # 更新待发放奖励日期
  270. $req->userInfo->game->pvp = $pvp;
  271. UserProc::updateUserInfo();
  272. // 组装 返回值结构
  273. $ret = array(
  274. 'score' => $score, # # 自己的积分
  275. 'rank' => $rank, # # 自己的排名
  276. 'pvpCoins' => $pvp->pvpCoins, # # 自己的竞技币
  277. 'fPower' => $fPower, # # 自己的总战力
  278. 'fightTicket' => $pvp->fightTicket, # # 自己的挑战票
  279. 'defTeam' => $pvp->defTeam, # # 自己的防守队伍
  280. 'bHasNewFightLog' => $numNewLog, # # 是否有战报刷新
  281. 'matches' => my_null_default($matches, array()), # # 对手列表
  282. );
  283. return Resp::ok($ret); # 返回
  284. }
  285. /**
  286. * [6811] 竞技场 刷新对手
  287. * @param Req $req
  288. */
  289. static function pvp_Refresh($req) {
  290. // 刷新无花费, 间隔时间3秒(客户端控制得了)
  291. $pvp = new UserPVPModel($req->userInfo->game->pvp);
  292. $ts = now();
  293. my_Assert($pvp->nextRefreshTs < $ts, ErrCode::pvp_refresh_time); # 验证时间间隔
  294. $pvp->curMatches = self::getNewMatches($pvp, $req->uid, $req->zoneid);
  295. $pvp->nextRefreshTs = now(3);
  296. $req->userInfo->game->pvp = $pvp;
  297. UserProc::updateUserInfo(); # 回写数据
  298. $ret = array(
  299. 'curMatches' => my_null_default($pvp->curMatches, array()), # # 当前对手清单
  300. );
  301. return Resp::ok($ret);
  302. }
  303. /**
  304. * [6812] 竞技场 挑战对手xx
  305. * @param Req $req
  306. */
  307. static function pvp_PK($req) {
  308. $uid = $req->uid;
  309. $zoneid = $req->zoneid;
  310. $baseInfo = $req->userInfo->game->baseInfo;
  311. list($target_uid, $result, $target_name, $target_HeadImg) = $req->paras; # 对手id,胜负结果 0负,1胜
  312. $pvp = $req->userInfo->game->pvp;
  313. if ($pvp->freeFightTickets > 0) { # 有免费挑战票,先扣除免费的
  314. $pvp->freeFightTickets -= 1;
  315. } else {
  316. my_Assert($pvp->fightTicket > 0, ErrCode::pvp_no_tickets); # 防御: 挑战票不足
  317. $pvp->fightTicket -= 1; # 扣除挑战票
  318. }
  319. $season = self::GetCurSeasonID(); # 当前赛季
  320. $key = MemKey_GameRun::Game_PVPScoreByZoneSeason_zset($zoneid, $season); # redis key
  321. $RA = self::_getScore_by_uid($uid, $key); # A的积分
  322. $RB = self::_getScore_by_uid($target_uid, $key); # B的积分
  323. $EA = 1 / (1 + pow(10, ($RA - $RB) / 400)); # A的胜率期望值
  324. $EB = 1 / (1 + pow(10, ($RB - $RA) / 400)); # B的胜率期望值
  325. $K = 32; # 浮动系数, 暂定为32
  326. $SA = $result ? 1 : 0; # 我的胜负结果
  327. $SB = $result ? 0 : 1; # 对手的胜负结果
  328. $R_A = intval($RA + $K * ($SA - $EA )); # 我的最终积分
  329. $R_B = intval($RB + $K * ($SB - $EB)); # 对手的最终积分
  330. #
  331. $myOldRank = self::_getRank_by_uid($uid, $key); # 记录下战前排名
  332. self::_addScore_by_uid($zoneid, $uid, $R_A - $RA); # 更新A的积分
  333. $B_Change = $result ? $R_B - $RB : 0; # 计算对方的积分变化:对方输了的情况下扣分,我输了的情况下,不加分
  334. if ($B_Change != 0) { #
  335. self::_addScore_by_uid($zoneid, $target_uid, $B_Change); # 更新B的积分
  336. }
  337. $myNewRank = self::_getRank_by_uid($uid, $key); # 查询战后排名
  338. if ($result) {
  339. $pvp->totalWin += 1;
  340. TaskProc::OnPvPWinN($pvp->totalWin);
  341. TaskProc::OnPvPScoreN($R_A);
  342. }
  343. $req->userInfo->game->pvp = $pvp;
  344. TaskProc::OnPvp(); # 每日PVP挑战即可
  345. UserProc::updateUserInfo(); # 回写数据
  346. // 写挑战记录
  347. $key_mine = MemKey_User::PVP_OffensiveLog_zset($zoneid, $uid); # 我的主动挑战记录
  348. self::_Log_PVP_PK_Info($key_mine, $target_uid, $target_name, $target_HeadImg, $result, $R_A - $RA); # 自己的挑战记录
  349. $key_target = MemKey_User::PVP_DefensiveLog_zset($zoneid, $target_uid); # 对手的被挑战记录
  350. self::_Log_PVP_PK_Info($key_target, $uid, $baseInfo->name, $baseInfo->img, !$result, $B_Change); # 对手的被挑战记录
  351. $ret = array(# # 组装返回值
  352. 'freeFightTickets' => $pvp->freeFightTickets, # # 自己剩余免费票
  353. 'fightTicket' => $pvp->fightTicket, # # 自己剩余挑战票
  354. 'RA' => $RA, # # 自己挑战之前积分
  355. 'RB' => $RB, # # 对手挑战之前积分
  356. 'R_A' => $R_A, # # 自己最新积分
  357. 'R_B' => $R_B, # # 对手最新积分
  358. 'rank_diff' => $myNewRank - $myOldRank, # # 自己的排名变化
  359. 'rank_new' => $myNewRank, # # 最新排名
  360. );
  361. return Resp::ok($ret); # 返回
  362. }
  363. /**
  364. * [6813] 竞技场 设置防守队伍
  365. * @param req $req
  366. */
  367. public static function pvp_setTeam($req) {
  368. $heros = $req->paras[0]; # para: 新阵容
  369. $pvp = new UserPVPModel($req->userInfo->game->pvp);
  370. my_Assert(is_array($heros), ErrCode::paras_err); # 参数检查
  371. $pvp->defTeam = $heros; # 更新阵容
  372. $req->userInfo->game->pvp = $pvp;
  373. UserProc::updateUserInfo(); # 回存数据
  374. return Resp::ok($pvp); # 返回
  375. }
  376. /**
  377. * [6814] 购买挑战票
  378. * @param Req $req
  379. * @return type
  380. */
  381. static function pvp_buyticket($req) {
  382. $amt = $req->paras[0]; # 购买数量
  383. # 检查并扣除消耗
  384. # 增加挑战票
  385. # 回存数据
  386. # 返回
  387. my_Assert($amt > 0, ErrCode::paras_err); # 数量>0
  388. $pvp = new UserPVPModel($req->userInfo->game->pvp);
  389. $g = glc();
  390. $costCash = $g->PVP_pk_ticket_price * $amt; # 计算消耗钻石
  391. my_Assert($costCash > 0, ErrCode::pvp_ticket_cost_ilegal); # 定价数据异常
  392. if (!UserGameModel::Consume_Cash($req->userInfo->game->baseInfo, $costCash)) { # 扣除钻石失败
  393. return Resp::err(ErrCode::notenough_cash_msg);
  394. }
  395. $pvp->fightTicket += $amt; # 发放挑战票
  396. $req->userInfo->game->pvp = $pvp;
  397. UserProc::updateUserInfo(); # 回写玩家数据
  398. $ret = array(
  399. 'fightTicket' => $pvp->fightTicket,
  400. 'costCash' => $costCash,
  401. 'userCash' => $req->userInfo->game->baseInfo->cash
  402. );
  403. return Resp::ok($ret); # 返回
  404. }
  405. /**
  406. * [6815] 竞技场 拉取榜单数据
  407. * @param Req $req
  408. */
  409. static function pvp_getRank($req) {
  410. $maxAmt = 10; # 一次最多取10个玩家信息
  411. $zoneid = $req->zoneid;
  412. $index = $req->paras[0]; # 起始(0)
  413. $n = $req->paras[1]; # 数量, (n<=max)
  414. #
  415. if ($n < 1 || $n > $maxAmt) { # 防御非法情况
  416. $n = $maxAmt;
  417. }
  418. $arr = self::getRankPlayers($req->mem, $zoneid, $index - 1, ($index + $n) - 1);
  419. //
  420. $rankId = $index;
  421. $result = ObjectInit();
  422. if (count($arr)) {
  423. foreach ($arr as $key => $value) {// $value 的数据类型是array
  424. $value["uid"] = $key;
  425. $result->$rankId = $value;
  426. $rankId += 1;
  427. }
  428. }
  429. $key_rank = MemKey_GameRun::Game_PVPScoreByZoneSeason_zset($zoneid, self::GetCurSeasonID());
  430. $myRank = self::_getRank_by_uid($req->uid, $key_rank);
  431. $myScore = self::_getScore_by_uid($uid, $key_rank);
  432. $ret = array(
  433. 'dic' => $result,
  434. 'myRank' => $myRank,
  435. 'myScore' => $myScore
  436. );
  437. return Resp::ok($ret);
  438. }
  439. /**
  440. * [6816] 竞技场 查看战报
  441. * @param Req $req
  442. */
  443. static function pvp_getFightLogs($req) {
  444. // 提取主动挑战+被挑战记录
  445. // 更新下刷新时间
  446. // 返回
  447. // 参数:无
  448. $key_off = MemKey_User::PVP_OffensiveLog_zset($req->zoneid, $req->uid);
  449. $key_def = MemKey_User::PVP_DefensiveLog_zset($req->zoneid, $req->uid);
  450. // 拉取自己的挑战记录
  451. $offLog = $req->mem->zrange($key_off, 0, self::maxLogCount); # 主动挑战数据
  452. $defLog = $req->mem->zrange($key_def, 0, self::maxLogCount); # 防守数据
  453. // Ps. 挑战记录分为2个榜, 且按照时间戳记录,晚于指定时间戳的判定为未读消息,挑战记录最多记录50条
  454. $pvp = new UserPVPModel($req->userInfo->game->pvp); # 玩家竞技场数据
  455. $pvp->lastCheckDefLog_ts = now(); # 记录时间戳
  456. UserProc::updateUserInfo(); # 回写数据
  457. array_walk($offLog, function (&$i) { # 解码一下
  458. $i = JsonUtil::decode($i);
  459. });
  460. array_walk($defLog, function (&$i) { # 解码一下
  461. $i = JsonUtil::decode($i);
  462. });
  463. return Resp::ok(array(
  464. 'offLog' => $offLog,
  465. 'defLog' => $defLog
  466. ));
  467. }
  468. // ---------------- 辅助函数 -----------------------
  469. // <editor-fold defaultstate="collapsed" desc=" 辅助 函数 ">
  470. /**
  471. * 竞技场 - 记录挑战结果
  472. */
  473. private static function _Log_PVP_PK_Info($key, $oppUID, $name, $headImg, $win, $score) {
  474. $ts = now(); # 记录时间戳
  475. gMem()->zadd($key, array(# # 组装被挑战对手信息
  476. JsonUtil::encode(array(
  477. 'uid' => my_null_default($oppUID, "-"), # # 对手uid
  478. 'name' => my_null_default($name, ""), # # 对手昵称
  479. 'headImg' => my_null_default($headImg, ""), # # 对手头像
  480. 'win' => my_null_default($win, false), # # 胜负结果
  481. 'msg' => "竞技场中没有留言羞辱对手的设定.", # # 胜利者留言
  482. 'scoreInfo' => my_null_default($score, 0), # # 积分变动
  483. 'ts' => $ts, # # 时间戳
  484. )) => $ts));
  485. gMem()->zremrangebyrank($key, self::maxLogCount, -1); # 保留50条数据
  486. }
  487. /**
  488. * 修改积分
  489. * @param int $zoneid
  490. * @param string $uid
  491. * @param int $addValue 可以是负值
  492. */
  493. private static function _addScore_by_uid($zoneid, $uid, $addValue) {
  494. $mem = gMem();
  495. $seasonId = self::GetCurSeasonID();
  496. $key = MemKey_GameRun::Game_PVPScoreByZoneSeason_zset($zoneid, $seasonId); # 积分榜
  497. $score = $mem->zscore($key, $uid);
  498. if (is_null($score) || $score <= 0) { # 分数异常, 理论上不会出现负分
  499. $score = self::pvpBaseScore; # 新手基础分
  500. $mem->zadd($key, array($uid => $score)); # 重置玩家积分
  501. }
  502. $newscore = $mem->zincrby($key, $uid, $addValue); # 返回最新值
  503. // var_dump($newscore);
  504. return $newscore;
  505. }
  506. /**
  507. * 竞技场 查询玩家的积分
  508. * @param type $uid
  509. * @return int
  510. */
  511. static function _getScore_by_uid($uid, $key) {
  512. if ("" == $uid) {
  513. CLog::err('"uid" is Null!');
  514. return 10; # 记录错误并返回一个极低分
  515. }
  516. $mem = gMem();
  517. // var_dump($key);
  518. $score = $mem->zscore($key, $uid);
  519. if (is_null($score) || $score <= 0) { # 分数异常
  520. $score = self::pvpBaseScore; # 新手基础分
  521. $mem->zadd($key, array($uid => $score)); # 更新玩家积分
  522. }
  523. return $score;
  524. }
  525. /**
  526. * 竞技场 查询玩家的排名
  527. * @param string $uid
  528. * @param string $key
  529. * @return int
  530. */
  531. static function _getRank_by_uid($uid, $key) {
  532. $rank = self::pvpMaxRank + 1; # 未上榜
  533. if ("" == $uid) {
  534. CLog::err('"uid" is Null!');
  535. return $rank; # 记录错误并返回未上榜
  536. }
  537. $mem = gMem();
  538. $r = $mem->zrevrank($key, $uid);
  539. if (!is_null($r)) {
  540. $rank = $r + 1; # 有名次,更新(zset从0开始排序)
  541. }
  542. return $rank; # 返回
  543. }
  544. /**
  545. * 获取对手匹配结果
  546. * @param UserPVPModel $pvp
  547. * @param CRedisutil $mem
  548. * @param type $uid
  549. * @param type $zoneid
  550. */
  551. private static function getNewMatches($pvp, $uid, $zoneid) {
  552. $seasonID = self::GetCurSeasonID();
  553. $key = MemKey_GameRun::Game_PVPScoreByZoneSeason_zset($zoneid, $seasonID); # redis key
  554. $arr = self::findmatcher($key, $uid); # 积分榜查找
  555. if (count($arr) < self::matchCount) { # 再不行, 准备机器人吧
  556. CLog::err('PVP刷对手数量不足, 赶快考虑加入机器人.', 'PVP'); // todo: 这里引入gm对手数据,
  557. }
  558. $ret = self::GetPlayerInfosForPVP(gMem(), $zoneid, $arr);
  559. return $ret;
  560. }
  561. /**
  562. * 查找匹配的对手
  563. * @param type $key
  564. * @param type $uid
  565. */
  566. private static function findmatcher($key, $uid) {
  567. // 根据匹配规格获得5个对手即可(1. 排除自己, 2. 最好不要出现已经刷到过的对手)
  568. // 低于玩家当前积分 -- 1个 -5%~-20%之间
  569. // 与玩家当前积分基本持平 2个, ±5%之间
  570. // 高于玩家当前积分 -- 1个 +5%~+40%之间
  571. $score = self::_getScore_by_uid($uid, $key); # 查积分
  572. // 计算 比自己弱的 上线下线
  573. $bH = ceil($score * (1 - 0.05)); # 低于当前玩家积分5%
  574. $bL = ceil($score * (1 - 0.2)); # 低于当前玩家积分20%
  575. $aL = ceil($score * (1 + 0.05)); # 高于当前玩家积分5%
  576. $aH = ceil($score * (1 + 0.2)); # 高于当前玩家积分20%
  577. $bPlayerUIDs = gMem()->zrevrangebyscore($key, $bH, $bL, true, true, 0, 10); # 取低于玩家积分的uids1个
  578. $mPlayerUIDs = gMem()->zrevrangebyscore($key, $aL, $bH, true, true, 0, 13); # 取玩家相当的uid2个(以防包含玩家自己)
  579. $aPlayerUIDs = gMem()->zrevrangebyscore($key, $aH, $aL, true, true, 0, 10); # 取高于玩家的uids1个
  580. if (array_key_exists($uid, $mPlayerUIDs)) { # 如果积分相近的那一组包含自己
  581. unset($mPlayerUIDs[$uid]); # 剔除掉
  582. } else { # 或者不包含自己
  583. array_pop($mPlayerUIDs); # 那多一个人,踢掉一个
  584. }
  585. $bPlayerUIDs = self::array_random_assoc($bPlayerUIDs, 1);
  586. $mPlayerUIDs = self::array_random_assoc($mPlayerUIDs, 2);
  587. $aPlayerUIDs = self::array_random_assoc($aPlayerUIDs, 1);
  588. $uidWithScore = array_merge($bPlayerUIDs, $mPlayerUIDs, $aPlayerUIDs); # 合并为玩家列表
  589. return $uidWithScore; # 返回uid集合
  590. }
  591. /**
  592. * 随机关联数组
  593. * @param type $arr
  594. * @param type $num
  595. * @return type
  596. */
  597. function array_random_assoc($arr, $num = 1) {
  598. $keys = array_keys($arr);
  599. shuffle($keys);
  600. if ($num > count($arr)) {
  601. $num = count($keys);
  602. }
  603. $r = array();
  604. for ($i = 0; $i < $num; $i++) {
  605. $r[$keys[$i]] = $arr[$keys[$i]];
  606. }
  607. return $r;
  608. }
  609. /**
  610. * 获取榜单玩家
  611. * @param Credisutil $mem
  612. * @param int $zoneid
  613. * @param int $start
  614. * @param int $stop
  615. * @return type
  616. */
  617. private static function getRankPlayers($mem, $zoneid, $start, $stop) {
  618. $key = MemKey_GameRun::Game_PVPScoreByZoneSeason_zset($zoneid, self::GetCurSeasonID());
  619. $retUidsWithScore = $mem->zrevrange($key, $start, $stop, true);
  620. return self::GetPlayerInfosForPVP($mem, $zoneid, $retUidsWithScore);
  621. }
  622. // private static function get
  623. /**
  624. * 拉取玩家信息-pvp模块专用信息
  625. * @param CredisUtil $mem
  626. * @param type $zoneid
  627. * @param type $retUidsWithScore
  628. * @return type
  629. */
  630. private static function GetPlayerInfosForPVP($mem, $zoneid, $retUidsWithScore) {
  631. $arr = ArrayInit();
  632. foreach ($retUidsWithScore as $uid => $score) {
  633. // isEditor() && $userGameInfo = new loyalsoft\UserInfoMo;
  634. $userGameInfo = UserProc::getUserInfo($mem, $zoneid, $uid); # 玩家数据
  635. // var_dump($userGameInfo->game->pvp);
  636. $teamConfig = $userGameInfo->game->pvp->defTeam; # 防守阵容
  637. // var_dump($teamConfig);
  638. $heros = new \stdClass(); # 英雄集合
  639. foreach ($teamConfig as $hid) {
  640. if ($hid > 0) {
  641. $n_hid = $hid - 10000;
  642. $heros->$n_hid = $userGameInfo->game->heros->collectHeros->$hid;
  643. }
  644. }
  645. $fpower = HeroProc::CalcUserFightPower($zoneid, $uid, $userGameInfo->game); # 计算总战力
  646. $adversary = array(# # 拼装玩家信息
  647. 'uid' => $uid,
  648. 'name' => my_null_default($userGameInfo->game->baseInfo->name, ""),
  649. 'level' => my_null_default($userGameInfo->game->baseInfo->level, 1),
  650. 'headImg' => my_null_default($userGameInfo->game->baseInfo->img, ""),
  651. // 'skills' => null, # # skills暂时没有实例数据
  652. 'equipment' => array("equipments" => my_null_default($userGameInfo->game->store->equipment, new \stdClass())), # 武器
  653. 'yanling' => array("items" => my_null_default($userGameInfo->game->store->yanling, new \stdClass())), # 言灵
  654. 'heros' => my_null_default($heros, new \stdClass()), # # 英雄集合
  655. 'score' => $score, # # 积分
  656. 'fpower' => $fpower, # # 总战力
  657. );
  658. // $arr[$uid] = $adversary;
  659. $arr[] = $adversary;
  660. }
  661. if (count($arr) <= 0) {
  662. $arr = null;
  663. }
  664. return $arr;
  665. }
  666. // </editor-fold>
  667. //
  668. // </editor-fold>
  669. //
  670. }