PVPProc.php 30 KB

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