PVPProc.php 32 KB

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