PVPProc.php 35 KB

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