PVPProc.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. <?php
  2. namespace loyalsoft;
  3. /**
  4. * PVPProc pvp 战斗模块, 挑战玩家镜像
  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. * [6803] 挑战 - 查询对手信息 等级、头像、昵称、战队信息(言灵师,等级,星级,武器,技能,言灵)
  13. * @param req $req
  14. */
  15. public static function GetChallengeAdversaryInfo($req) {
  16. // 参数: 无
  17. $targetUID = $req->paras[0]; # 对手的UID
  18. $uinfo = UserProc::getUserInfo($req->mem, $req->zoneid, $targetUID); # 读取玩家信息
  19. $adversary = array(# # 拼装玩家信息
  20. 'uid' => $targetUID,
  21. 'name' => my_null_default($uinfo->game->name, ""),
  22. 'level' => my_null_default($uinfo->game->level, 1),
  23. 'headImg' => my_null_default($uinfo->game->img, ""),
  24. // 'skills' => null, # # skills暂时没有实例数据
  25. 'equipment' => my_null_default($uinfo->game->store->equipment, new \stdClass()), # 武器
  26. 'yanling' => my_null_default($uinfo->game->store->yanling, new \stdClass()), # 言灵
  27. 'heros' => my_null_default($uinfo->game->heros->collectHeros, new \stdClass()), # 英雄集合
  28. );
  29. $result = array(# # 拼装返回值
  30. "adversaryInfo" => $adversary
  31. );
  32. return Resp::ok($result);
  33. }
  34. const maxLogCount = 49;
  35. /**
  36. * [6804] 挑战 - 记录挑战结果
  37. * @param req $req
  38. */
  39. static function LogChallengeInfo($req) {
  40. // 参数: 对手uid,对手昵称,对手头像, 对战结果, 胜利者的留言(失败时无法留言"")
  41. list($targetUID, $name, $headImg, $win, $msg) = $req->paras;
  42. $key_mine = MemKey_User::OffensiveLog_zset($req->zoneid, $req->uid);
  43. $key_target = MemKey_User::DefensiveLog_zset($req->zoneid, $targetUID);
  44. $ts = now(); # 记录时间戳
  45. $req->mem->zadd($key_mine, array(# # 组装被挑战对手信息
  46. JsonUtil::encode(array(
  47. 'uid' => $targetUID,
  48. 'name' => $name,
  49. 'headImg' => $headImg,
  50. 'win' => $win,
  51. 'msg' => $msg,
  52. 'ts' => $ts
  53. )) => $ts));
  54. $req->mem->zremrangebyrank($key_mine, self::maxLogCount, -1); # 保留50条数据
  55. $req->mem->zadd($key_target, array(# # 组装挑战者信息
  56. JsonUtil::encode(array(
  57. 'uid' => $req->uid,
  58. 'name' => $req->userInfo->game->name,
  59. 'headImg' => $req->userInfo->game->img,
  60. 'win' => !$win,
  61. 'msg' => $msg,
  62. 'ts' => $ts
  63. )) => $ts));
  64. $req->mem->zremrangebyrank($key_target, self::maxLogCount, -1); # 保留50条数据
  65. // 暂无发放奖励流程
  66. // 更新每日任务
  67. // 返回
  68. return Resp::ok(); # 返回成功
  69. }
  70. /**
  71. * [6805] 挑战 - 拉取挑战记录
  72. * @param req $req
  73. */
  74. static function GetChagllengeLog($req) {
  75. // 参数:无
  76. $key_off = MemKey_User::OffensiveLog_zset($req->zoneid, $req->uid);
  77. $key_def = MemKey_User::DefensiveLog_zset($req->zoneid, $req->uid);
  78. // 拉取自己的挑战记录
  79. $offLog = $req->mem->zrange($key_off, 0, self::maxLogCount);
  80. $defLog = $req->mem->zrange($key_def, 0, self::maxLogCount);
  81. // Ps. 挑战记录分为2个榜, 且按照时间戳记录,晚于指定时间戳的判定为未读消息,挑战记录最多记录50条
  82. // if (!CommUtil::isPropertyExists($req->userInfo->game->privateState, "lastCheckDefLog")) {
  83. $req->userInfo->game->privateState->lastCheckDefLog = now(); # 记录时间戳
  84. // }
  85. UserProc::updateUserInfo(); # 回写数据
  86. // 记录拉取时间戳(在主界面有个未读消息条数显示, 需要靠最后拉取时间戳对比, 时间戳之后的消息是未读消息)
  87. // 回传数据记录
  88. return Resp::ok(array(
  89. 'offLog' => $offLog,
  90. 'defLog' => $defLog
  91. ));
  92. }
  93. //
  94. // <editor-fold defaultstate="collapsed" desc=" 旧版PVP 681x ">
  95. /**
  96. * 查找对手数量
  97. */
  98. const matchCount = 5;
  99. /**
  100. * [6810] pvp 拉取主界面信息
  101. * @param Req $req
  102. */
  103. static function pvpMainInfo($req) {
  104. $mem = gMem();
  105. $uid = $req->uid;
  106. $zoneid = $req->zoneid;
  107. $pvp = new UserPVPModel($req->userInfo->game->pvp); # 设计玩家pvp数据结构
  108. $key = MemKey_GameRun::Game_PVPScoreByZone_zset($zoneid); # 积分总榜
  109. $key_last = MemKey_GameRun::Game_PVPScoreByZone_zset_lastWeek($zoneid); # 上周积分榜
  110. $score = self::_getScore_by_uid($req, $uid); # 取玩家当前pvp积分
  111. $tsLastWeek = TimeUtil::tsWeek() - 1;
  112. $lastWeekScore = $mem->zscore($key_last, $uid);
  113. if (!in_array($tsLastWeek, $pvp->weeklyRewardRecords) # 尚未发放上周奖励
  114. && $lastWeekScore) { # 并且上周活跃
  115. $pvp->weeklyRewardRecords[] = $tsLastWeek; # 添加发奖记录
  116. $leagueid = self::GetLeagueByScore($lastWeekScore); # 上周积分所处阶段
  117. EmailProc::SendPvpLeagueReward($zoneid, $uid, $leagueid); # 阶段奖励
  118. $pvp->clearWeekData(); # 每周清理胜率数据
  119. $pvp->curMatches = self::getNewMatches($pvp, $mem, $uid, $zoneid); # 更新对手列表
  120. $req->userInfo->game->pvp = $pvp;
  121. UserProc::updateUserInfo();
  122. }
  123. self::sendLastRankReward($req); # 发放每周上榜人员奖励
  124. self::UpdateChanlianInfo($req); # 更新蝉联数据(并发奖)
  125. $pvp->score = $score; # 插入score数据
  126. $rank = $mem->zrevrank($key, $uid); # 提取总榜排行信息
  127. $pvp->rank = (is_null($rank) ? -1 : $rank + 1);
  128. return Resp::ok($pvp); # 返回pvp数据
  129. }
  130. /**
  131. *
  132. * @param Req $req
  133. * @param type $uid
  134. */
  135. static function _getScore_by_uid($req, $uid) {
  136. if ("" == $uid) {
  137. CLog::err('"uid" is Null!');
  138. return 10;
  139. }
  140. $mem = $req->mem;
  141. $zoneid = $req->zoneid;
  142. $key = MemKey_GameRun::Game_PVPScoreByZone_zset($zoneid); # 积分榜
  143. $score = $mem->zscore($key, $uid);
  144. if (!$score || $score <= 0) { # 分数异常
  145. $leagueScore = GameConfig::pvp_leaguescore_getItem(1); # 新手基础分
  146. $score = $leagueScore->minScore;
  147. $mem->zadd($key, array($uid => $score)); # 更新玩家积分
  148. $mem->zadd(MemKey_GameRun::Game_PVPScoreByZone_zset_curWeek($zoneid), array($uid => $score));
  149. }
  150. return $score;
  151. }
  152. /**
  153. *
  154. * @param Req $req
  155. * @param type $uid
  156. * @param type $addValue Description
  157. */
  158. private static function _addScore_by_uid($req, $uid, $addValue) {
  159. if ($addValue <= 0) { # 防御
  160. return;
  161. }
  162. $mem = $req->mem;
  163. $zoneid = $req->zoneid;
  164. $key = MemKey_GameRun::Game_PVPScoreByZone_zset($zoneid); # 积分榜
  165. $score = $mem->zscore($key, $uid);
  166. if (!$score || $score <= 0) { # 分数异常
  167. $leagueScore = GameConfig::pvp_leaguescore_getItem(1); # 新手基础分
  168. $score = $leagueScore->minScore;
  169. $mem->zadd($key, array($uid => $score)); # 重置玩家积分
  170. }
  171. $newscore = $mem->zincrby($key, $uid, $addValue); # 返回最新值
  172. $mem->zadd(MemKey_GameRun::Game_PVPScoreByZone_zset_curWeek($zoneid), array($uid => $newscore));
  173. return $newscore;
  174. }
  175. /**
  176. *
  177. * @param type $req
  178. * @param type $uid
  179. * @param type $subValue
  180. */
  181. private static function _subScore_by_uid($req, $uid, $subValue) {
  182. if ($subValue >= 0) { # 防御异常数值
  183. return;
  184. }
  185. $mem = $req->mem;
  186. $zoneid = $req->zoneid;
  187. $key = MemKey_GameRun::Game_PVPScoreByZone_zset($zoneid); # 积分榜
  188. $score = $mem->zscore($key, $uid);
  189. $leagueScore = GameConfig::pvp_leaguescore_getItem(1); # 新手基础分
  190. if (!$score || $score <= 0 #
  191. || ($score + $subValue) <= $leagueScore->minScore) { # 分数异常
  192. $score = $leagueScore->minScore; # 新手基础分
  193. $mem->zadd($key, array($uid => $score)); # 重置玩家积分
  194. } else {
  195. $score = $mem->zincrby($key, $uid, $subValue); # 扣除积分并返回最新值
  196. }
  197. $mem->zadd(MemKey_GameRun::Game_PVPScoreByZone_zset_curWeek($zoneid), array($uid => $score)); # 同步记录
  198. return $score;
  199. }
  200. /**
  201. * 发送上周上榜奖励
  202. * @param Req $req
  203. */
  204. public static function sendLastRankReward($req) {
  205. $mem = gMem();
  206. $zoneid = $req->zoneid;
  207. $tsweek = TimeUtil::tsWeek();
  208. $key = MemKey_GameRun::Game_PVPRankRewardRecord_set($zoneid);
  209. if (!$mem->sismember($key, $tsweek)) { # 上周榜单尚未处理, 每周只处理一次
  210. // 上周是空的情况.
  211. $lastKey = MemKey_GameRun::Game_PVPRankRewardRecord_byWeek_str($zoneid, $tsweek - 1);
  212. $mem->sadd($key, $tsweek); # 添加处理记录
  213. $key_total = MemKey_GameRun::Game_PVPScoreByZone_zset($zoneid); # 拉取上榜人员
  214. $uids = $mem->zrevrange($key_total, 0, 99);
  215. $rank = 0;
  216. foreach ($uids as $uid) {
  217. EmailProc::SendPvpRankReward($zoneid, $uid, ++$rank); # 发送奖励邮件
  218. }
  219. $mem->add($lastKey, $uids);
  220. }
  221. }
  222. /**
  223. * [6819] pvp - 拉取蝉联信息
  224. * @param Req $req
  225. */
  226. static function getChanlianINfo($req) {
  227. $zoneid = $req->zoneid;
  228. $key = MemKey_GameRun::Game_PVP_Chanlian_normal($zoneid);
  229. $pvpChanlian = new GamePvpChanlian(gMem()->get($key));
  230. $pvpHistory = gMem()->hgetall(MemKey_GameRun::Game_PVP_Chanlian_History_hash($zoneid));
  231. return Resp::ok(array(
  232. 'info' => $pvpChanlian,
  233. 'history' => array_values((array) $pvpHistory)
  234. ));
  235. }
  236. /**
  237. * [6811] pvp 挑战对手xx
  238. * @param Req $req
  239. */
  240. static function pvp_PK($req) {
  241. $uid = $req->uid;
  242. $target_uid = $req->paras[0]; # 对手id
  243. $result = $req->paras[1]; # 胜负结果 0负,1胜
  244. $pvp = $req->userInfo->game->pvp;
  245. if (!property_exists($pvp->curMatches, $target_uid)//
  246. or property_exists($pvp->curMatches->$target_uid, 'killed')) { # 对手已被ko
  247. return Resp::err(ErrCode::pvp_wrongMather);
  248. }
  249. $g = glc();
  250. if (now() - $pvp->tiliTs > $g->PVP_reCover_Tili_costSec * 5) { # 检查并扣除体力
  251. $pvp->tiliTs = now() - $g->PVP_reCover_Tili_costSec * 5;
  252. }
  253. if ($pvp->tiliExtra > 0) { # 优先扣除溢出值
  254. $pvp->tiliExtra--;
  255. } else {
  256. $pvp->tiliTs += $g->PVP_reCover_Tili_costSec;
  257. if ($pvp->tiliTs > now()) {
  258. return Resp::err(ErrCode::pvp_wrongtili);
  259. }
  260. }
  261. # 发放对应奖励并记录挑战记录
  262. $score = 0;
  263. if ($result) { # 1 胜, 奖金币、积分、pvp币
  264. UserGameModel::Add_Gold($req->userInfo->game, $g->PVP_PK_Win_Gold);
  265. # × 奖励积分的公式: 获胜基础奖励积分 + 积分差距修正系数 * 积分差
  266. # 挑战胜利, 积分改为固定值.
  267. $winnerGainScore = $g->PVP_PK_Win_Score; # 本局得分
  268. $score = self::_addScore_by_uid($req, $uid, intval($winnerGainScore)); # 增加积分
  269. $pvp->pvpCoins += $g->PVP_PK_Win_PvpCoin; # 发放金币
  270. self::_subScore_by_uid($req, $target_uid, -intval($g->PVP_PK_be_defeated_score)); # 给对手扣除积分
  271. $pvp->contWin++; # 连胜记录
  272. $pvp->winTimes++; # 获胜记录
  273. $all = true; # 全部战胜标记
  274. foreach ($pvp->curMatches as $pid => &$p) {
  275. if ($pid == $target_uid) { #
  276. $p->killed = 1; # 添加ko标志
  277. if (property_exists($p, 'fog')) {
  278. $score = self::_addScore_by_uid($req, $uid, intval($winnerGainScore)); # 神秘对手, 积分翻倍(再加一遍)
  279. }
  280. }
  281. if (!property_exists($p, 'killed')) {
  282. $all = false; # 尚未全部战胜
  283. }
  284. }
  285. if ($all) { # 全部战胜之后,刷新对手
  286. $pvp->curMatches = self::getNewMatches($pvp, $req->mem, $req->uid, $req->zoneid);
  287. $pvp->nextRefreshTs = now(glc()->PVP_refresh_Match_RecoverSeconds);
  288. }
  289. } else { # 0 挑战失败 只扣除积分
  290. # × 奖励积分的公式: 获胜基础奖励积分 + 积分差距修正系数 * 积分差
  291. # 挑战失败, 扣除固定积分
  292. $loserGainScore = $g->PVP_PK_Fail_Score;
  293. $score = self::_subScore_by_uid($req, $uid, -intval($loserGainScore)); # 扣除积分
  294. self::_addScore_by_uid($req, $target_uid, intval($g->PVP_PK_be_undefeated_score)); # 给对手发放积分
  295. $pvp->contWin = 0; # 连胜置零
  296. }
  297. if (property_exists($pvp->curMatches, $target_uid) && property_exists($pvp->curMatches->$target_uid, 'fog')) {
  298. unset($pvp->curMatches->$target_uid->fog);
  299. }
  300. $pvp->totalTimes++;
  301. $leagueid = self::GetLeagueByScore($score);
  302. if ($pvp->leagueId != $leagueid) {
  303. $pvp->leagueId = $leagueid;
  304. SystemProc::PVP_league($req->zoneid, $req->userInfo->game, $leagueid);
  305. }
  306. $pvp->actives++;
  307. $pvp->dailyPkCnt++;
  308. $req->userInfo->game->pvp = $pvp;
  309. //todo: debuging...
  310. UserProc::updateUserInfo(); # 回写数据
  311. $ret = array(
  312. 'contWin' => $pvp->contWin,
  313. 'leagueId' => $pvp->leagueId,
  314. 'newscore' => $score, # # 返回最新积分
  315. );
  316. return Resp::ok($ret);
  317. }
  318. /**
  319. * [6812] pvp 刷新对手
  320. * @param Req $req
  321. */
  322. static function pvp_Refresh($req) {
  323. $amt = $req->paras[0]; # 提取 params , 验证下花费
  324. $pvp = new UserPVPModel($req->userInfo->game->pvp);
  325. $ts = now();
  326. if ($amt <= 0) { # 免费情况
  327. if ($pvp->nextRefreshTs > $ts) { # 验证时间
  328. return Resp::err(ErrCode::pvp_refresh_time);
  329. }
  330. } else { # 扣除钻石刷新的情况
  331. $priceArr = explode(',', glc()->PVP_reFresh_Match_cost); # 价格
  332. if (count($priceArr) < $pvp->dailyRefreshMatchTimes) { # 超过最大刷新次数
  333. return Resp::err(ErrCode::pvp_refresh_max);
  334. }
  335. $costCash = $priceArr[$pvp->dailyRefreshMatchTimes++]; # 查找对应的定价, 并计次
  336. if ($costCash != $amt) { # 跟预期值不一致,
  337. return Resp::err(ErrCode::pvp_refresh_cost_ilegal);
  338. }
  339. if (!UserGameModel::Consume_Cash($req->userInfo->game, $costCash)) {# 扣除钻石失败
  340. return Resp::err(ErrCode::notenough_cash_msg);
  341. }
  342. }
  343. $pvp->curMatches = self::getNewMatches($pvp, $req->mem, $req->uid, $req->zoneid);
  344. $pvp->nextRefreshTs = now(glc()->PVP_refresh_Match_RecoverSeconds);
  345. $req->userInfo->game->pvp = $pvp;
  346. UserProc::updateUserInfo(); # 回写数据
  347. $ret = array(
  348. 'nextRefreshTs' => $pvp->nextRefreshTs, # # 下次免费刷新的时间戳
  349. 'dailyRefreshMatchTimes' => $pvp->dailyRefreshMatchTimes,
  350. 'curMatches' => $pvp->curMatches # # 当前对手清单
  351. );
  352. return Resp::ok($ret);
  353. }
  354. /**
  355. * [6813] 购买体力 v2.0 阶梯定价.
  356. * @param Req $req
  357. * @return type
  358. */
  359. static function pvp_buytili($req) {
  360. $amt = $req->paras[0]; # 验证下本次扣除的钻石数量
  361. $pvp = new UserPVPModel($req->userInfo->game->pvp);
  362. $g = glc();
  363. $curTili = intval((now() - $pvp->tiliTs) / $g->PVP_reCover_Tili_costSec);
  364. if ($curTili > 5) {
  365. $curTili = 5;
  366. }
  367. if ($pvp->tiliExtra > 0) { # 体力溢出值大于0的时候不要购买
  368. return Resp::err(ErrCode::pvp_tili_chargenum);
  369. }
  370. $priceArr = explode(',', $g->PVP_recover_tili_cost_cash);
  371. if (count($priceArr) < $pvp->dailyBuyTiliTimes) {
  372. return Resp::err(ErrCode::pvp_tili_soldout);
  373. }
  374. $costCash = $priceArr[$pvp->dailyBuyTiliTimes++]; # 查找对应的定价, 并计次
  375. if ($costCash != $amt) { # 跟预期值不一致,
  376. return Resp::err(ErrCode::pvp_tili_cost_ilegal);
  377. }
  378. if (!UserGameModel::Consume_Cash($req->userInfo->game, $costCash)) { # 扣除钻石失败
  379. return Resp::err(ErrCode::notenough_cash_msg);
  380. }
  381. if ($curTili > 0) { # 增加溢出值
  382. $pvp->tiliExtra = $curTili;
  383. }
  384. $pvp->tiliTs = now() - ( 5 * $g->PVP_reCover_Tili_costSec); # 补满
  385. $req->userInfo->game->pvp = $pvp;
  386. UserProc::updateUserInfo(); # 回写玩家数据
  387. $ret = array(
  388. 'tiliTs' => $pvp->tiliTs,
  389. 'tiliExtra' => $pvp->tiliExtra,
  390. 'costCash' => $costCash,
  391. 'dailyBuyTiliTimes' => $pvp->dailyBuyTiliTimes,
  392. 'userCash' => $req->userInfo->game->cash
  393. );
  394. return Resp::ok($ret); # 返回
  395. }
  396. /**
  397. * [6814] pvp 拉取榜单数据
  398. * @param Req $req
  399. */
  400. static function pvp_getRank($req) {
  401. $maxAmt = 10; # 一次最多取10个玩家信息
  402. $zoneid = $req->zoneid;
  403. $index = $req->paras[0]; # 起始(0)
  404. $n = $req->paras[1]; # 数量, (n<=max)
  405. if ($n < 1 || $n > $maxAmt) { # 防御非法情况
  406. $n = $maxAmt;
  407. }
  408. $arr = self::getRankPlayers($req->mem, $zoneid, $index - 1, ($index + $n) - 1);
  409. $rankId = $index;
  410. $result = ObjectInit();
  411. if (count($arr)) {
  412. foreach ($arr as $key => $value) {//$value 的数据类型是array
  413. $value["uid"] = $key;
  414. $result->$rankId = $value;
  415. $rankId += 1;
  416. }
  417. }
  418. $ret = array(
  419. 'arr' => $result
  420. );
  421. return Resp::ok($ret);
  422. }
  423. /**
  424. * [6815] pvp 领取活跃度奖励
  425. * @param Req $req
  426. */
  427. static function pvp_drawacitverewards($req) {
  428. $rewardId = $req->paras[0]; # 提取参数
  429. $reward = GameConfig::pvp_activityreward_getItem($rewardId);
  430. if (!$reward) {
  431. return Resp::err(ErrCode::err_const_no);
  432. }
  433. $pvp = new UserPVPModel($req->userInfo->game->pvp);
  434. if ($reward->activityNum > $pvp->actives) {
  435. return Resp::err(ErrCode::pvp_activenotenough);
  436. }
  437. $pvp->actives = 0; # 活跃记录直接清零
  438. $req->userInfo->game->pvp = $pvp; # 回写pvp数据
  439. $err = StoreProc::AddMultiItemInStore($req, $reward->reward1); # 发放奖励
  440. if ($err) {
  441. return Resp::err($err);
  442. }
  443. UserProc::updateUserInfo(); # 回写玩家数据
  444. $ret = array(# # 返回值
  445. 'actives' => $pvp->actives, // # 最新的 活跃度值
  446. 'store' => $req->userInfo->game->store, // # 获得奖励之后的玩家仓库信息
  447. 'hero' => $req->userInfo->game->heros, // # 获得奖励之后的玩家英雄信息
  448. 'reward' => $reward->reward1,
  449. );
  450. return Resp::ok($ret);
  451. }
  452. // ---------------- 辅助函数 -----------------------
  453. /**
  454. * 清理每日计数器
  455. * @param Req $req
  456. */
  457. public static function ClearDailyPkcnt($req) {
  458. $pvp = new UserPVPModel($req->userInfo->game->pvp);
  459. $pvp->dailyPkCnt = 0;
  460. $pvp->dailyBuyTiliTimes = 0;
  461. $pvp->dailyRefreshMatchTimes = 0;
  462. $pvp->dailyMatchRecord = array($req->uid); # 屏蔽自己
  463. $pvp->curMatches = self::getNewMatches($pvp, $req->mem, $req->uid, $req->zoneid); # added by: 李宁,2017年8月1日 11:45:41
  464. $pvp->nextRefreshTs = tsDay() * 86400 + glc()->PVP_refresh_Match_RecoverSeconds;
  465. $req->userInfo->game->pvp = $pvp;
  466. }
  467. /**
  468. * 更新蝉联信息(并发奖,如果有)
  469. * @param Req $req
  470. */
  471. private static function UpdateChanlianInfo($req) {
  472. $zoneid = $req->zoneid;
  473. $tsWeek = TimeUtil::tsWeek();
  474. $key = MemKey_GameRun::Game_PVP_Chanlian_normal($zoneid);
  475. // $key_lastWeek = MemKey_GameRun::Game_PVPScoreByZone_zset_lastWeek($zoneid);
  476. $pvpChanlian = new GamePvpChanlian(gMem()->get($key));
  477. if ($pvpChanlian->tsWeek == $tsWeek) {
  478. return; # 已处理
  479. }
  480. $pvpChanlian->tsWeek = $tsWeek;
  481. $arr_1 = gMem()->zrevrange(MemKey_GameRun::Game_PVPScoreByZone_zset($zoneid), 0, 0);
  482. $uid = $arr_1[0]; # 取上次第一
  483. if ($uid == $pvpChanlian->uid) { # 蝉联了
  484. $pvpChanlian->times++;
  485. if ($pvpChanlian->times == 3) { # 达到3次, 完成蝉联, 发放奖励
  486. EmailProc::SendPvpChanlianReward($zoneid, $uid); # 发放奖励
  487. $historyKey = MemKey_GameRun::Game_PVP_Chanlian_History_hash($zoneid);
  488. gMem()->hset($historyKey, $tsWeek, $pvpChanlian->userInfo); # 进入历届榜
  489. }
  490. if ($pvpChanlian->times > 3) { # 第四次蝉联的时候
  491. $pvpChanlian->times -= 3; # 循环蝉联计数信息
  492. }
  493. } else { # 替换掉上一届
  494. $arr = self::GetPlayerInfosForPVP(gMem(), $zoneid, array($uid => 100)); # 提取玩家信息
  495. $pvpChanlian->userInfo = $arr[$uid]; # 更新蝉联玩家信息
  496. $pvpChanlian->uid = $uid; # 玩家ID
  497. $pvpChanlian->times = 1; # 首次
  498. }
  499. gMem()->set($key, $pvpChanlian); # 回写数据
  500. }
  501. //
  502. // <editor-fold defaultstate="collapsed" desc=" 辅助 函数 ">
  503. /**
  504. * 依据积分获得所在阶段的id
  505. * @param type $curScore
  506. * @return int
  507. */
  508. private static function GetLeagueByScore($curScore) {
  509. foreach (GameConfig::pvp_leaguescore() as $id => $lg) {
  510. isEditor() and $lg = new sm_pvp_leaguescore;
  511. if ($lg->maxScore >= $curScore && $lg->minScore <= $curScore) {
  512. return $id;
  513. }
  514. }
  515. return 1; # 找不到对应所属阶段的时候,返回 默认值 1
  516. }
  517. /**
  518. * 获取对手匹配结果
  519. * @param UserPVPModel $pvp
  520. * @param CRedisutil $mem
  521. * @param type $uid
  522. * @param type $zoneid
  523. */
  524. private static function getNewMatches($pvp, $mem, $uid, $zoneid) {
  525. $arr = array();
  526. $key = MemKey_GameRun::Game_PVPScoreByZone_zset($zoneid);
  527. self::findmatcher($zoneid, $key, $uid, $pvp, $arr); # 积分榜查找
  528. if (count($arr) < self::matchCount) { # 如果没有凑足人数,战斗力候补来源,
  529. $keyfp = MemKey_GameRun::Game_FightPowerRank_zset($zoneid);
  530. self::findmatcher($zoneid, $keyfp, $uid, $pvp, $arr); # 从战斗力榜找
  531. }
  532. if (count($arr) < self::matchCount) { # 再不行, 准备机器人吧
  533. // todo: 这里引入gm对手数据,
  534. CLog::err('PVP刷对手数量不足, 赶快考虑加入机器人.', 'PVP');
  535. }
  536. $ret = self::GetPlayerInfosForPVP($mem, $zoneid, $arr);
  537. $keys = array_keys($ret);
  538. $k = $keys[0];
  539. $v = $ret[$k];
  540. $v['fog'] = true;
  541. $ret[$k] = $v;
  542. // var_dump($v);
  543. // echoLine(JsonUtil::encode($ret[$k]));
  544. return $ret;
  545. }
  546. /**
  547. * 查找匹配的对手
  548. * @param type $zoneid
  549. * @param type $key
  550. * @param type $uid
  551. * @param UserPVPModel $pvp
  552. * @param type $arr
  553. */
  554. private static function findmatcher($zoneid, $key, $uid, &$pvp, &$arr) {
  555. $mem = gMem();
  556. $rank = $mem->zrevrank($key, $uid); # 直接查排名
  557. $zlen = $mem->zlen($key); # 数据长度
  558. $i = 0; # 计数器
  559. $b = false; # 上下标志位
  560. while (count($arr) < self::matchCount && $i < 50) { # 找够数量为止,或者查找超过100个位置, 跳出查找
  561. $index = $rank + ($b ? - $i : $i);
  562. // todo: 这里引入连胜/连负修正系数.
  563. $b = !$b;
  564. if ($b) { # 变化查找方向
  565. $i++; #
  566. }
  567. if ($index < 0 || $index >= $zlen) { # 跳过不合理位置
  568. continue;
  569. }
  570. $back = $mem->zrevrange($key, $index, $index, true); # 取1个人
  571. if (count($back) <= 0) {
  572. continue;
  573. }
  574. $us = array_keys($back);
  575. $d_uid = $us[0];
  576. if ($d_uid == $uid) { # 避开自己
  577. continue;
  578. }
  579. $user = $mem->get(MemKey_User::Info($zoneid, $d_uid));
  580. if (null == $user) {
  581. continue;
  582. }
  583. $fp = HeroProc::CalcTeamFightPower($zoneid, $d_uid, $user);
  584. if ($fp > 0) {
  585. // $pvp->dailyMatchRecord
  586. if (!in_array($d_uid, $pvp->dailyMatchRecord)) { # 已经匹配过的对手不在出现
  587. $arr[$d_uid] = $back[$d_uid];
  588. $pvp->dailyMatchRecord[] = $d_uid;
  589. }
  590. }
  591. }
  592. }
  593. /**
  594. * 获取榜单玩家
  595. * @param Credisutil $mem
  596. * @param int $zoneid
  597. * @param int $start
  598. * @param int $stop
  599. * @return type
  600. */
  601. private static function getRankPlayers($mem, $zoneid, $start, $stop) {
  602. $key = MemKey_GameRun::Game_PVPScoreByZone_zset($zoneid);
  603. $retUidsWithScore = $mem->zrevrange($key, $start, $stop, true);
  604. return self::GetPlayerInfosForPVP($mem, $zoneid, $retUidsWithScore);
  605. }
  606. /**
  607. * 拉取玩家信息-pvp模块专用信息
  608. * @param CredisUtil $mem
  609. * @param type $zoneid
  610. * @param type $retUidsWithScore
  611. * @return type
  612. */
  613. private static function GetPlayerInfosForPVP($mem, $zoneid, $retUidsWithScore) {
  614. // var_dump($retUidsWithScore);
  615. $retUids = array_keys($retUidsWithScore);
  616. $keysOfUserInfo = array_map(function($u)use($zoneid) {
  617. return MemKey_User::Info($zoneid, $u);
  618. }, $retUids);
  619. $arrUserInfos = $mem->getMulti($keysOfUserInfo);
  620. $arr = ArrayInit();
  621. $i = 0;
  622. foreach ($arrUserInfos as $userGameInfo) {
  623. isEditor() && $userGameInfo = new UserGameModel;
  624. $uid = $retUids[$i++];
  625. // $sharedHeroId = $userGameInfo->heros->firendSupportHeroUID;
  626. $team = JsonUtil::decode('{"teamLeader": 0,"heros":{}}');
  627. $teamConfig = JsonUtil::decode($userGameInfo->heroTeamConfig);
  628. if ($teamConfig && $teamConfig->curUseTeamID > 0) {
  629. $heros = ObjectInit();
  630. $teamid = $teamConfig->curUseTeamID;
  631. $heros_uid = $teamConfig->teamDic->$teamid->heros;
  632. $m = 0;
  633. foreach ($heros_uid as $heroid) {
  634. $m++;
  635. $heros->$m = $heroid ? self::getHeroInfoForShare($userGameInfo, $heroid) : null;
  636. }
  637. $team = array(
  638. 'teamLeader' => $teamConfig->teamDic->$teamid->teamLeader,
  639. 'heros' => $heros
  640. );
  641. }
  642. $pvp = StlUtil::array2class(array('contWin' => '', 'totalTimes' => '', 'winTimes' => '', 'leagueId' => ''));
  643. CommUtil::loadObject($userGameInfo->pvp, $pvp); # 提取pvp数据
  644. $arr[$uid] = array(
  645. 'img' => $userGameInfo->img,
  646. 'imgBorderId' => $userGameInfo->imgBorderId,
  647. // 'sharedHero' => self::getHeroInfoForShare($userGameInfo, $sharedHeroId),
  648. 'lastLoginTs' => isset($userGameInfo->lastLogin) ? $userGameInfo->lastLogin : 0,
  649. 'nickname' => $userGameInfo->name,
  650. 'pvp' => $pvp,
  651. 'score' => $retUidsWithScore[$uid],
  652. 'level' => $userGameInfo->level,
  653. 'uid' => $uid,
  654. 'heroTeamConfig' => $team # 战队配置
  655. );
  656. }
  657. if (count($arr) <= 0) {
  658. $arr = null;
  659. }
  660. return $arr;
  661. }
  662. /**
  663. * 提取英雄信息以供其他人使用
  664. * @param type $userInfo
  665. * @param type $heroId
  666. * @return UserHeroModel
  667. */
  668. public static function getHeroInfoForShare($userInfo, $heroId) {
  669. if ($heroId) {
  670. $heroInfo = $userInfo->heros->collectHeros->$heroId;
  671. } else { // todo: 暂定如果没有设置共享英雄,默认取集合中第一个英雄, 征询策划是否修订规则
  672. $coll = $userInfo->heros->collectHeros;
  673. $keys = array_keys((array) $coll);
  674. if (count($keys) <= 0) {
  675. return null; // 暴力跳过, 防止出现更大的错误
  676. }
  677. $heroId = $keys[0];
  678. $heroInfo = $userInfo->heros->collectHeros->$heroId;
  679. }
  680. // 额外将英雄的装备数据替换为实例数据
  681. self::getArmor($userInfo, $heroInfo, 'weapon');
  682. self::getArmor($userInfo, $heroInfo, 'armor');
  683. self::getArmor($userInfo, $heroInfo, 'ring');
  684. return $heroInfo;
  685. }
  686. private static function getArmor($userInfo, $hero, $armor) {
  687. if (isset($hero->equip->$armor)) {
  688. if (property_exists($hero->equip->$armor, 'itemuid')) {
  689. $armorid = $hero->equip->$armor->itemuid;
  690. if ($armorid > 0 && isset($userInfo->store->equipment->$armorid)) {
  691. $hero->equip->$armor = $userInfo->store->equipment->$armorid;
  692. return; # 找到后直接跳出
  693. }
  694. }
  695. }
  696. $hero->equip->$armor = null;
  697. }
  698. // </editor-fold>
  699. //
  700. // </editor-fold>
  701. //
  702. }