PVPProc.php 35 KB

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