PVPProc.php 34 KB

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