PVPProc.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. <?php
  2. namespace loyalsoft;
  3. /**
  4. * PVPProc 竞技场 战斗模块, 挑战玩家镜像
  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. // <editor-fold defaultstate="collapsed" desc=" 常量 ">
  13. /**
  14. * 挑战记录最大条数
  15. */
  16. const maxLogCount = 49;
  17. /**
  18. * 查找对手数量
  19. */
  20. const matchCount = 5;
  21. /**
  22. * 竞技场赛季起始时间戳
  23. */
  24. const pvpStartTs = 1588521600; # 2020年5月4日 0时0分0秒
  25. /**
  26. * 每个赛季持续时常
  27. */
  28. const pvpSeasonLengt = 86400 * 14; # 2周
  29. /**
  30. * 竞技场初始积分
  31. */
  32. const pvpBaseScore = 1000;
  33. /**
  34. * 竞技场 最大上榜人数
  35. */
  36. const pvpMaxRank = 500;
  37. // </editor-fold>
  38. /**
  39. * [6803] 挑战 - 查询对手信息 等级、头像、昵称、战队信息(言灵师,等级,星级,武器,技能,言灵)
  40. * @param req $req
  41. */
  42. public static function GetChallengeAdversaryInfo($req) {
  43. $targetUID = $req->paras[0]; # 参数: 对手的UID
  44. $uinfo = UserProc::getUserInfo($req->mem, $req->zoneid, $targetUID); # 读取玩家信息
  45. if (null == $uinfo) {
  46. Err(ErrCode::user_no_err);
  47. }
  48. $team = JsonUtil::decode($uinfo->game->heroTeamConfig);
  49. $heros = new \stdClass();
  50. $curTeamId = $team->curUseTeamID;
  51. $arr_nil = array();
  52. var_dump($team->teamDic->$curTeamId);
  53. foreach ($team->teamDic->$curTeamId->heros as $i => $hid) {
  54. if ($hid > 0) {
  55. $n_hid = $hid - 10000;
  56. $heros->$n_hid = $uinfo->game->heros->collectHeros->$hid;
  57. }
  58. }
  59. $adversary = array(# # 拼装玩家信息
  60. 'uid' => $targetUID,
  61. 'name' => my_null_default($uinfo->game->name, ""),
  62. 'level' => my_null_default($uinfo->game->level, 1),
  63. 'headImg' => my_null_default($uinfo->game->img, ""),
  64. // 'skills' => null, # # skills暂时没有实例数据
  65. 'equipment' => array("equipments" => my_null_default($uinfo->game->store->equipment, new \stdClass())), # 武器
  66. 'yanling' => array("items" => my_null_default($uinfo->game->store->yanling, new \stdClass())), # 言灵
  67. 'heros' => my_null_default($heros, new \stdClass()), # # 英雄集合
  68. );
  69. $result = array(# # 拼装返回值
  70. "adversaryInfo" => $adversary
  71. );
  72. return Resp::ok($result);
  73. }
  74. /**
  75. * [6804] 挑战 - 记录挑战结果
  76. * @param req $req
  77. */
  78. static function LogChallengeInfo($req) {
  79. // 参数: 对手uid,对手昵称,对手头像, 对战结果, 胜利者的留言(失败时无法留言"")
  80. list($targetUID, $name, $headImg, $win, $msg) = $req->paras;
  81. $key_mine = MemKey_User::OffensiveLog_zset($req->zoneid, $req->uid);
  82. $key_target = MemKey_User::DefensiveLog_zset($req->zoneid, $targetUID);
  83. $ts = now(); # 记录时间戳
  84. $req->mem->zadd($key_mine, array(# # 组装被挑战对手信息
  85. JsonUtil::encode(array(
  86. 'uid' => my_null_default($targetUID, "-"),
  87. 'name' => my_null_default($name, ""),
  88. 'headImg' => my_null_default($headImg, ""),
  89. 'win' => my_null_default($win, false),
  90. 'msg' => my_null_default($msg, ""),
  91. 'ts' => $ts
  92. )) => $ts));
  93. $req->mem->zremrangebyrank($key_mine, self::maxLogCount, -1); # 保留50条数据
  94. $req->mem->zadd($key_target, array(# # 组装挑战者信息
  95. JsonUtil::encode(array(
  96. 'uid' => $req->uid,
  97. 'name' => $req->userInfo->game->name,
  98. 'headImg' => $req->userInfo->game->img,
  99. 'win' => !my_null_default($win, false),
  100. 'msg' => my_null_default($msg, ""),
  101. 'ts' => $ts
  102. )) => $ts));
  103. $req->mem->zremrangebyrank($key_target, self::maxLogCount, -1); # 保留50条数据
  104. // 暂无发放奖励流程
  105. // 更新每日任务
  106. // 返回
  107. return Resp::ok(); # 返回成功
  108. }
  109. /**
  110. * [6805] 挑战 - 拉取挑战记录
  111. * @param req $req
  112. */
  113. static function GetChagllengeLog($req) {
  114. // 参数:无
  115. $key_off = MemKey_User::OffensiveLog_zset($req->zoneid, $req->uid);
  116. $key_def = MemKey_User::DefensiveLog_zset($req->zoneid, $req->uid);
  117. // 拉取自己的挑战记录
  118. $offLog = $req->mem->zrange($key_off, 0, self::maxLogCount);
  119. $defLog = $req->mem->zrange($key_def, 0, self::maxLogCount);
  120. // Ps. 挑战记录分为2个榜, 且按照时间戳记录,晚于指定时间戳的判定为未读消息,挑战记录最多记录50条
  121. // if (!CommUtil::isPropertyExists($req->userInfo->game->privateState, "lastCheckDefLog")) {
  122. $req->userInfo->game->privateState->lastCheckDefLog_ts = now(); # 记录时间戳
  123. // }
  124. UserProc::updateUserInfo(); # 回写数据
  125. // 记录拉取时间戳(在主界面有个未读消息条数显示, 需要靠最后拉取时间戳对比, 时间戳之后的消息是未读消息)
  126. // 回传数据记录
  127. array_walk($offLog, function (&$i) {
  128. $i = JsonUtil::decode($i);
  129. });
  130. array_walk($defLog, function (&$i) {
  131. $i = JsonUtil::decode($i);
  132. });
  133. return Resp::ok(array(
  134. 'offLog' => $offLog,
  135. 'defLog' => $defLog
  136. ));
  137. }
  138. // <editor-fold defaultstate="collapsed" desc=" 竞技场 681x">
  139. //
  140. /**
  141. * 取当前赛季的编号(从赛季起始开始算起)
  142. * @return int
  143. */
  144. public static function GetCurSeasonID() {
  145. $n = ceil((now() - self::pvpStartTs ) / self::pvpSeasonLengt); # 进一取整
  146. return $n;
  147. }
  148. /**
  149. * 取指定赛季的结束时间戳
  150. * @param int $seasonID
  151. * @return int
  152. */
  153. public static function GetSeasonEndTs($seasonID) {
  154. $ts = self::pvpStartTs + $seasonID * self::pvpSeasonLengt; # 从起始点开始 + n个赛季时常
  155. return $ts;
  156. }
  157. /**
  158. * [6810] 竞技场 拉取主界面信息
  159. * @param Req $req
  160. */
  161. static function pvpMainInfo($req) {
  162. $uid = $req->uid; # 快速访问UID
  163. $zoneid = $req->zoneid; # 快速访问zoneid
  164. $pvp = new UserPVPModel($req->userInfo->game->pvp); # 设计玩家pvp数据结构
  165. $seasonId = self::GetCurSeasonID(); # 当前赛季ID
  166. $key = MemKey_GameRun::Game_PVPScoreByZoneSeason_zset($zoneid, $seasonId); # 积分总榜
  167. // 玩家数据
  168. // 积分 + 排名
  169. // 总战力
  170. // 战报小红点
  171. // 对手列表
  172. // =======
  173. // 检查昨日奖励是否发放
  174. // 检查赛季奖励是否发放
  175. // 返回
  176. $score = self::_getScore_by_uid($uid, $key); # 玩家积分
  177. $rank = self::_getRank_by_uid($uid, $key); # 玩家排名
  178. $fPower = HeroProc::CalcUserFightPower($zoneid, $uid, $req->userInfo->game); # 玩家总战力?还是当前防守队伍的战斗力?
  179. $bHasNewLog = true; // todo: 真正查询是否有新战报
  180. $matches = self::getNewMatches($pvp, gMem(), $uid, $zoneid); # 获得新的匹配对手
  181. if ($pvp->haventReward_season > 0 && $pvp->haventReward_season < $seasonId) { # 尚未发放上赛季奖励
  182. $haventKey = MemKey_GameRun::Game_PVPScoreByZoneSeason_zset($zoneid, $pvp->haventReward_season);
  183. // todo:发放上周奖励邮件
  184. }
  185. if ($pvp->haventReward_tsDay > 0 && $pvp->haventReward_tsDay < tsDay()) { # 尚未发放昨天奖励
  186. $haventKey_day = MemKey_GameRun::Game_PVPScoreByZone_zset_Day($zoneid, $day);
  187. if (gMem()->exists($haventKey_day)) { # 昨天的积分记录不存在
  188. gMem()->zcopy($key, $haventKey_day); # 复制一份当前积分作为昨天的截止积分榜
  189. }
  190. // todo:发放昨天奖励
  191. }
  192. // 组装 返回值结构
  193. $ret = array(
  194. 'score' => $score, # # 自己的积分
  195. 'rank' => $rank, # # 自己的排名
  196. 'pvpCoins' => $pvp->pvpCoins, # # 自己的竞技币
  197. 'fPower' => $fPower, # # 自己的总战力
  198. 'fightTicket' => $pvp->fightTicket, # # 自己的挑战票
  199. 'defTeam' => $pvp->defTeam, # # 自己的防守队伍
  200. 'bHasNewFightLog' => $bHasNewLog, # # 是否有战报刷新
  201. 'matches' => $matches, # # 对手列表
  202. );
  203. return Resp::ok($ret); # 返回
  204. }
  205. // </editor-fold>
  206. //
  207. // <editor-fold defaultstate="collapsed" desc=" 竞技商店 ">
  208. /**
  209. * [6820] 竞技商店 主界面
  210. * @param type $req
  211. */
  212. public static function pvpShopMain($req) {
  213. // 商品列表
  214. // 检查刷新时间<now => 刷新商品列表
  215. // 返回
  216. }
  217. /**
  218. * [6821] 竞技 商店 购买道具
  219. * @param type $req
  220. */
  221. public static function pvpShopBuy($req) {
  222. // 查询物品配置数据
  223. // 扣除竞技币
  224. // 发放道具
  225. // 回写数据
  226. // 返回
  227. }
  228. /**
  229. * [6822] 竞技 商店 刷新道具
  230. * @param type $req
  231. */
  232. public static function pvpShopRefresh($req) {
  233. // 扣除刷新消耗
  234. // 更新刷新时间
  235. // 重刷道具
  236. // 回写数据
  237. // 返回
  238. }
  239. /**
  240. * 【移动支付】获取神秘商城物品
  241. * 刷新规则: 根据玩家拥有的英雄、装备、道具等数据进行刷新。(因英雄、道具、装备数量不足以支撑该刷新规则,目前先按照随机刷新做,几率平等)
  242. * @param Req $req
  243. */
  244. public static function m_pay_getDynamic($req) {
  245. $user = $req->userInfo->game;
  246. $userSecretshop = new userSecretshopModel($user->userSecretshop);
  247. // 参数提取
  248. $refreshType = $req->paras[0]; # 刷新类型(参数)0,不刷,1,免费刷,2,钻石刷
  249. switch ($refreshType) {
  250. case 1: # 免费刷
  251. if (now() < $userSecretshop->lastRefreshTs + glc()->secretshop_refresh_interval) { // 检查是否达到免费刷新时间了, 执行自动更新
  252. return Resp::err(ErrCode::pay_secretshopt_freeRefresh_Time);
  253. }
  254. break;
  255. case 2: # 钻石刷
  256. if (glc()->secretshop_refresh_maxtimes <= $userSecretshop->refreshedTimes) { // 检查刷新次数, 已达上限, 返回错误信息
  257. return Resp::err(ErrCode::pay_refresh_times);
  258. } # 可以继续刷新,
  259. $cishu = $userSecretshop->refreshedTimes + 1; # 下次
  260. $amt = GameConfig::secretshop_refresh_getItem($cishu)->price;
  261. if (!UserGameModel::Consume_Cash($user, $amt)) { # 扣除本次所需费用, 余额不足, 返回错误信息
  262. return Resp::err(ErrCode::notenough_cash_msg);
  263. }
  264. $userSecretshop->refreshedTimes++; # 增加当天付费刷新计数
  265. break;
  266. case 0: # 不刷
  267. default : # 默认不刷
  268. // do nothing.
  269. break;
  270. }
  271. if ($refreshType != 0) { # 是否刷新
  272. $err = self::refreshDynamicShopItems($req, $userSecretshop); # 更新物品表
  273. if ($err) {
  274. return Resp::err($err);
  275. }
  276. $user->userSecretshop = $userSecretshop;
  277. $req->userInfo->game = $user;
  278. UserProc::updateUserInfo();
  279. }
  280. // 返回最新物品表
  281. return Resp::ok(array(# # 成功后将最新的玩家数据返回给客户端
  282. 'gold' => $user->gold,
  283. 'tili' => $user->tili,
  284. 'cash' => $user->cash,
  285. 'uss' => $userSecretshop, # # 当前神秘商城数据
  286. ));
  287. }
  288. /**
  289. * 更新神秘商城物品
  290. * @param Req $req
  291. * @param UserSecretshopModel $userSecretshop Description
  292. */
  293. private static function refreshDynamicShopItems($req, &$userSecretshop) {
  294. $userSecretshop->lastRefreshTs = now();
  295. // todo: 这里补完更新物品的函数, // 第一版: 随机
  296. $userSecretshop->currentItems = ObjectInit();
  297. for ($i = 1; $i <= 3; $i++) { # 3种类型的商品
  298. $arr = GameConfig::secretshop_goodsType_getItem($i);
  299. if (count($arr) > 0) {
  300. $err = self::Dice(GameConfig::secretshop_goodsType_getItem($i), 1, $userSecretshop); # 一个种类一次1个物品
  301. if ($err) {
  302. return $err;
  303. }
  304. }
  305. }
  306. return ErrCode::ok;
  307. }
  308. // // </editor-fold>
  309. //
  310. // <editor-fold defaultstate="collapsed" desc=" 旧版PVP 681x ">
  311. //
  312. /**
  313. * @deprecated since version 2020.5.8
  314. * @param Req $req
  315. * @param type $uid
  316. * @param type $addValue Description
  317. */
  318. private static function _addScore_by_uid($req, $uid, $addValue) {
  319. if ($addValue <= 0) { # 防御
  320. return;
  321. }
  322. $mem = $req->mem;
  323. $zoneid = $req->zoneid;
  324. $key = MemKey_GameRun::Game_PVPScoreByZone_zset($zoneid); # 积分榜
  325. $score = $mem->zscore($key, $uid);
  326. if (!$score || $score <= 0) { # 分数异常
  327. $leagueScore = GameConfig::pvp_leaguescore_getItem(1); # 新手基础分
  328. $score = $leagueScore->minScore;
  329. $mem->zadd($key, array($uid => $score)); # 重置玩家积分
  330. }
  331. $newscore = $mem->zincrby($key, $uid, $addValue); # 返回最新值
  332. $mem->zadd(MemKey_GameRun::Game_PVPScoreByZone_zset_curWeek($zoneid), array($uid => $newscore));
  333. return $newscore;
  334. }
  335. /**
  336. * @deprecated since version 2020.5.8
  337. * @param type $req
  338. * @param type $uid
  339. * @param type $subValue
  340. */
  341. private static function _subScore_by_uid($req, $uid, $subValue) {
  342. if ($subValue >= 0) { # 防御异常数值
  343. return;
  344. }
  345. $mem = $req->mem;
  346. $zoneid = $req->zoneid;
  347. $key = MemKey_GameRun::Game_PVPScoreByZone_zset($zoneid); # 积分榜
  348. $score = $mem->zscore($key, $uid);
  349. $leagueScore = GameConfig::pvp_leaguescore_getItem(1); # 新手基础分
  350. if (!$score || $score <= 0 #
  351. || ($score + $subValue) <= $leagueScore->minScore) { # 分数异常
  352. $score = $leagueScore->minScore; # 新手基础分
  353. $mem->zadd($key, array($uid => $score)); # 重置玩家积分
  354. } else {
  355. $score = $mem->zincrby($key, $uid, $subValue); # 扣除积分并返回最新值
  356. }
  357. $mem->zadd(MemKey_GameRun::Game_PVPScoreByZone_zset_curWeek($zoneid), array($uid => $score)); # 同步记录
  358. return $score;
  359. }
  360. /**
  361. * 发送上周上榜奖励
  362. * @param Req $req
  363. */
  364. public static function sendLastRankReward($req) {
  365. $mem = gMem();
  366. $zoneid = $req->zoneid;
  367. $tsweek = TimeUtil::tsWeek();
  368. $key = MemKey_GameRun::Game_PVPRankRewardRecord_set($zoneid);
  369. if (!$mem->sismember($key, $tsweek)) { # 上周榜单尚未处理, 每周只处理一次
  370. // 上周是空的情况.
  371. $lastKey = MemKey_GameRun::Game_PVPRankRewardRecord_byWeek_str($zoneid, $tsweek - 1);
  372. $mem->sadd($key, $tsweek); # 添加处理记录
  373. $key_total = MemKey_GameRun::Game_PVPScoreByZone_zset($zoneid); # 拉取上榜人员
  374. $uids = $mem->zrevrange($key_total, 0, 99);
  375. $rank = 0;
  376. foreach ($uids as $uid) {
  377. EmailProc::SendPvpRankReward($zoneid, $uid, ++$rank); # 发送奖励邮件
  378. }
  379. $mem->add($lastKey, $uids);
  380. }
  381. }
  382. /**
  383. * [6811] 竞技场 刷新对手
  384. * @param Req $req
  385. */
  386. static function pvp_Refresh($req) {
  387. // 刷新无花费, 间隔时间3秒(客户端控制得了)
  388. // 根据匹配规格获得5个对手即可(1. 排除自己, 2. 最好不要出现已经刷到过的对手)
  389. // 低于玩家当前积分 -- 2个 -5%~-20%之间
  390. // 与玩家当前积分基本持平 1个, ±5%之间
  391. // 高于玩家当前积分 -- 2个 +5%~+40%之间
  392. // $amt = $req->paras[0]; # 提取 params , 验证下花费
  393. // $pvp = new UserPVPModel($req->userInfo->game->pvp);
  394. // $ts = now();
  395. // if ($amt <= 0) { # 免费情况
  396. // if ($pvp->nextRefreshTs > $ts) { # 验证时间
  397. // return Resp::err(ErrCode::pvp_refresh_time);
  398. // }
  399. // } else { # 扣除钻石刷新的情况
  400. // $priceArr = explode(',', glc()->PVP_reFresh_Match_cost); # 价格
  401. // if (count($priceArr) < $pvp->dailyRefreshMatchTimes) { # 超过最大刷新次数
  402. // return Resp::err(ErrCode::pvp_refresh_max);
  403. // }
  404. // $costCash = $priceArr[$pvp->dailyRefreshMatchTimes++]; # 查找对应的定价, 并计次
  405. // if ($costCash != $amt) { # 跟预期值不一致,
  406. // return Resp::err(ErrCode::pvp_refresh_cost_ilegal);
  407. // }
  408. // if (!UserGameModel::Consume_Cash($req->userInfo->game, $costCash)) {# 扣除钻石失败
  409. // return Resp::err(ErrCode::notenough_cash_msg);
  410. // }
  411. // }
  412. //
  413. // $pvp->curMatches = self::getNewMatches($pvp, $req->mem, $req->uid, $req->zoneid);
  414. // $pvp->nextRefreshTs = now(glc()->PVP_refresh_Match_RecoverSeconds);
  415. // $req->userInfo->game->pvp = $pvp;
  416. // UserProc::updateUserInfo(); # 回写数据
  417. //
  418. // $ret = array(
  419. // 'nextRefreshTs' => $pvp->nextRefreshTs, # # 下次免费刷新的时间戳
  420. // 'dailyRefreshMatchTimes' => $pvp->dailyRefreshMatchTimes,
  421. // 'curMatches' => $pvp->curMatches # # 当前对手清单
  422. // );
  423. return Resp::ok($ret);
  424. }
  425. /**
  426. * [6812] 竞技场 挑战对手xx
  427. * @param Req $req
  428. */
  429. static function pvp_PK($req) {
  430. $uid = $req->uid;
  431. $target_uid = $req->paras[0]; # 对手id
  432. $result = $req->paras[1]; # 胜负结果 0负,1胜
  433. $pvp = $req->userInfo->game->pvp;
  434. // 根据挑战结果, 计算双方积分变化
  435. // 更新双方的积分变化
  436. // redis->zincr 修改各自积分
  437. // 返回
  438. // if (!property_exists($pvp->curMatches, $target_uid)//
  439. // or property_exists($pvp->curMatches->$target_uid, 'killed')) { # 对手已被ko
  440. // return Resp::err(ErrCode::pvp_wrongMather);
  441. // }
  442. // $g = glc();
  443. // if (now() - $pvp->tiliTs > $g->PVP_reCover_Tili_costSec * 5) { # 检查并扣除体力
  444. // $pvp->tiliTs = now() - $g->PVP_reCover_Tili_costSec * 5;
  445. // }
  446. // if ($pvp->tiliExtra > 0) { # 优先扣除溢出值
  447. // $pvp->tiliExtra--;
  448. // } else {
  449. // $pvp->tiliTs += $g->PVP_reCover_Tili_costSec;
  450. // if ($pvp->tiliTs > now()) {
  451. // return Resp::err(ErrCode::pvp_wrongtili);
  452. // }
  453. // }
  454. //# 发放对应奖励并记录挑战记录
  455. // $score = 0;
  456. // if ($result) { # 1 胜, 奖金币、积分、pvp币
  457. // UserGameModel::Add_Gold($req->userInfo->game, $g->PVP_PK_Win_Gold);
  458. // # × 奖励积分的公式: 获胜基础奖励积分 + 积分差距修正系数 * 积分差
  459. // # 挑战胜利, 积分改为固定值.
  460. // $winnerGainScore = $g->PVP_PK_Win_Score; # 本局得分
  461. // $score = self::_addScore_by_uid($req, $uid, intval($winnerGainScore)); # 增加积分
  462. // $pvp->pvpCoins += $g->PVP_PK_Win_PvpCoin; # 发放金币
  463. // self::_subScore_by_uid($req, $target_uid, -intval($g->PVP_PK_be_defeated_score)); # 给对手扣除积分
  464. // $pvp->contWin++; # 连胜记录
  465. // $pvp->winTimes++; # 获胜记录
  466. // $all = true; # 全部战胜标记
  467. // foreach ($pvp->curMatches as $pid => &$p) {
  468. // if ($pid == $target_uid) { #
  469. // $p->killed = 1; # 添加ko标志
  470. // if (property_exists($p, 'fog')) {
  471. // $score = self::_addScore_by_uid($req, $uid, intval($winnerGainScore)); # 神秘对手, 积分翻倍(再加一遍)
  472. // }
  473. // }
  474. // if (!property_exists($p, 'killed')) {
  475. // $all = false; # 尚未全部战胜
  476. // }
  477. // }
  478. // if ($all) { # 全部战胜之后,刷新对手
  479. // $pvp->curMatches = self::getNewMatches($pvp, $req->mem, $req->uid, $req->zoneid);
  480. // $pvp->nextRefreshTs = now(glc()->PVP_refresh_Match_RecoverSeconds);
  481. // }
  482. // } else { # 0 挑战失败 只扣除积分
  483. // # × 奖励积分的公式: 获胜基础奖励积分 + 积分差距修正系数 * 积分差
  484. // # 挑战失败, 扣除固定积分
  485. // $loserGainScore = $g->PVP_PK_Fail_Score;
  486. // $score = self::_subScore_by_uid($req, $uid, -intval($loserGainScore)); # 扣除积分
  487. // self::_addScore_by_uid($req, $target_uid, intval($g->PVP_PK_be_undefeated_score)); # 给对手发放积分
  488. // $pvp->contWin = 0; # 连胜置零
  489. // }
  490. // if (property_exists($pvp->curMatches, $target_uid) && property_exists($pvp->curMatches->$target_uid, 'fog')) {
  491. // unset($pvp->curMatches->$target_uid->fog);
  492. // }
  493. // $pvp->totalTimes++;
  494. // $leagueid = self::GetLeagueByScore($score);
  495. // if ($pvp->leagueId != $leagueid) {
  496. // $pvp->leagueId = $leagueid;
  497. // SystemProc::PVP_league($req->zoneid, $req->userInfo->game, $leagueid);
  498. // }
  499. // $pvp->actives++;
  500. // $pvp->dailyPkCnt++;
  501. // $req->userInfo->game->pvp = $pvp;
  502. ////todo: debuging...
  503. // UserProc::updateUserInfo(); # 回写数据
  504. // $ret = array(
  505. // 'contWin' => $pvp->contWin,
  506. // 'leagueId' => $pvp->leagueId,
  507. // 'newscore' => $score, # # 返回最新积分
  508. // );
  509. return Resp::ok($ret);
  510. }
  511. /**
  512. * [6813] 竞技场 设置防守队伍
  513. * @param req $req
  514. */
  515. public static function pvp_setTeam($req) {
  516. $heros = $req->paras[0]; # para: 新阵容
  517. $pvp = new UserPVPModel($req->userInfo->game->pvp);
  518. if (is_array($heros)) { # 更新阵容
  519. $pvp->defTeam->heros = $heros;
  520. }
  521. $req->userInfo->game->pvp = $pvp;
  522. UserProc::updateUserInfo(); # 回存数据
  523. return Resp::ok($pvp->defTeam); # 返回
  524. }
  525. /**
  526. * [6814] 购买挑战票
  527. * @param Req $req
  528. * @return type
  529. */
  530. static function pvp_buyticket($req) {
  531. $amt = $req->paras[0]; # 验证下本次扣除的钻石数量
  532. # 检查并扣除消耗
  533. # 增加挑战票
  534. # 回存数据
  535. # 返回
  536. // $pvp = new UserPVPModel($req->userInfo->game->pvp);
  537. // $g = glc();
  538. // $curTili = intval((now() - $pvp->tiliTs) / $g->PVP_reCover_Tili_costSec);
  539. // if ($curTili > 5) {
  540. // $curTili = 5;
  541. // }
  542. // if ($pvp->tiliExtra > 0) { # 体力溢出值大于0的时候不要购买
  543. // return Resp::err(ErrCode::pvp_tili_chargenum);
  544. // }
  545. // $priceArr = explode(',', $g->PVP_recover_tili_cost_cash);
  546. //
  547. // if (count($priceArr) < $pvp->dailyBuyTiliTimes) {
  548. // return Resp::err(ErrCode::pvp_tili_soldout);
  549. // }
  550. // $costCash = $priceArr[$pvp->dailyBuyTiliTimes++]; # 查找对应的定价, 并计次
  551. // if ($costCash != $amt) { # 跟预期值不一致,
  552. // return Resp::err(ErrCode::pvp_tili_cost_ilegal);
  553. // }
  554. // if (!UserGameModel::Consume_Cash($req->userInfo->game, $costCash)) { # 扣除钻石失败
  555. // return Resp::err(ErrCode::notenough_cash_msg);
  556. // }
  557. // if ($curTili > 0) { # 增加溢出值
  558. // $pvp->tiliExtra = $curTili;
  559. // }
  560. //
  561. // $pvp->tiliTs = now() - ( 5 * $g->PVP_reCover_Tili_costSec); # 补满
  562. // $req->userInfo->game->pvp = $pvp;
  563. // UserProc::updateUserInfo(); # 回写玩家数据
  564. // $ret = array(
  565. // 'tiliTs' => $pvp->tiliTs,
  566. // 'tiliExtra' => $pvp->tiliExtra,
  567. // 'costCash' => $costCash,
  568. // 'dailyBuyTiliTimes' => $pvp->dailyBuyTiliTimes,
  569. // 'userCash' => $req->userInfo->game->cash
  570. // );
  571. return Resp::ok($ret); # 返回
  572. }
  573. /**
  574. * [6815] 竞技场 拉取榜单数据
  575. * @param Req $req
  576. */
  577. static function pvp_getRank($req) {
  578. $maxAmt = 10; # 一次最多取10个玩家信息
  579. $zoneid = $req->zoneid;
  580. $index = $req->paras[0]; # 起始(0)
  581. $n = $req->paras[1]; # 数量, (n<=max)
  582. // if ($n < 1 || $n > $maxAmt) { # 防御非法情况
  583. // $n = $maxAmt;
  584. // }
  585. // $arr = self::getRankPlayers($req->mem, $zoneid, $index - 1, ($index + $n) - 1);
  586. //
  587. // $rankId = $index;
  588. // $result = ObjectInit();
  589. // if (count($arr)) {
  590. // foreach ($arr as $key => $value) {//$value 的数据类型是array
  591. // $value["uid"] = $key;
  592. // $result->$rankId = $value;
  593. // $rankId += 1;
  594. // }
  595. // }
  596. // $ret = array(
  597. // 'arr' => $result
  598. // );
  599. return Resp::ok($ret);
  600. }
  601. /**
  602. * [6816] 竞技场 查看战报
  603. * @param Req $req
  604. */
  605. static function pvp_getFightLogs($req) {
  606. // 提取主动挑战+被挑战记录
  607. // 更新下刷新时间
  608. // 返回
  609. return Resp::ok($ret);
  610. }
  611. // ---------------- 辅助函数 -----------------------
  612. // <editor-fold defaultstate="collapsed" desc=" 辅助 函数 ">
  613. /**
  614. * 竞技场 查询玩家的积分
  615. * @param type $uid
  616. * @return int
  617. */
  618. static function _getScore_by_uid($uid, $key) {
  619. if ("" == $uid) {
  620. CLog::err('"uid" is Null!');
  621. return 10; # 记录错误并返回一个极低分
  622. }
  623. $mem = gMem();
  624. $score = $mem->zscore($key, $uid);
  625. if (!$score || $score <= 0) { # 分数异常
  626. $score = self::pvpBaseScore; # 新手基础分
  627. $mem->zadd($key, array($uid => $score)); # 更新玩家积分
  628. }
  629. return $score;
  630. }
  631. /**
  632. * 竞技场 查询玩家的排名
  633. * @param string $uid
  634. * @param string $key
  635. * @return int
  636. */
  637. static function _getRank_by_uid($uid, $key) {
  638. $rank = self::pvpMaxRank + 1; # 未上榜
  639. if ("" == $uid) {
  640. CLog::err('"uid" is Null!');
  641. return $rank; # 记录错误并返回未上榜
  642. }
  643. $mem = gMem();
  644. $r = $mem->zrevrank($key, $uid);
  645. if (!is_null($r)) {
  646. $rank = $r; # 有名次,更新
  647. }
  648. return $rank; # 返回
  649. }
  650. /**
  651. * 清理每日计数器
  652. * @deprecated since version 2020.5.8
  653. * @param Req $req
  654. */
  655. public static function ClearDailyPkcnt($req) {
  656. $pvp = new UserPVPModel($req->userInfo->game->pvp);
  657. $pvp->dailyPkCnt = 0;
  658. $pvp->dailyBuyTiliTimes = 0;
  659. $pvp->dailyRefreshMatchTimes = 0;
  660. $pvp->dailyMatchRecord = array($req->uid); # 屏蔽自己
  661. $pvp->curMatches = self::getNewMatches($pvp, $req->mem, $req->uid, $req->zoneid); # added by: 李宁,2017年8月1日 11:45:41
  662. $pvp->nextRefreshTs = tsDay() * 86400 + glc()->PVP_refresh_Match_RecoverSeconds;
  663. $req->userInfo->game->pvp = $pvp;
  664. }
  665. /**
  666. * 依据积分获得所在阶段的id
  667. * @deprecated since version 2020.5.8
  668. * @param type $curScore
  669. * @return int
  670. */
  671. private static function GetLeagueByScore($curScore) {
  672. foreach (GameConfig::pvp_leaguescore() as $id => $lg) {
  673. isEditor() and $lg = new sm_pvp_leaguescore;
  674. if ($lg->maxScore >= $curScore && $lg->minScore <= $curScore) {
  675. return $id;
  676. }
  677. }
  678. return 1; # 找不到对应所属阶段的时候,返回 默认值 1
  679. }
  680. /**
  681. * 获取对手匹配结果
  682. * @param UserPVPModel $pvp
  683. * @param CRedisutil $mem
  684. * @param type $uid
  685. * @param type $zoneid
  686. */
  687. private static function getNewMatches($pvp, $uid, $zoneid) {
  688. $arr = array();
  689. $seasonID = self::GetCurSeasonID();
  690. $key = MemKey_GameRun::Game_PVPScoreByZoneSeason_zset($zoneid, $seasonID); # redis key
  691. self::findmatcher($zoneid, $key, $uid, $pvp, $arr); # 积分榜查找
  692. if (count($arr) < self::matchCount) { # 再不行, 准备机器人吧
  693. // todo: 这里引入gm对手数据,
  694. CLog::err('PVP刷对手数量不足, 赶快考虑加入机器人.', 'PVP');
  695. }
  696. $ret = self::GetPlayerInfosForPVP(gMem(), $zoneid, $arr);
  697. return $ret;
  698. }
  699. /**
  700. * 查找匹配的对手
  701. * @param type $zoneid
  702. * @param type $key
  703. * @param type $uid
  704. * @param UserPVPModel $pvp
  705. * @param type $arr
  706. */
  707. private static function findmatcher($zoneid, $key, $uid, &$pvp, &$arr) {
  708. $mem = gMem();
  709. $rank = $mem->zrevrank($key, $uid); # 直接查排名
  710. $zlen = $mem->zlen($key); # 数据长度
  711. $i = 0; # 计数器
  712. $b = false; # 上下标志位
  713. while (count($arr) < self::matchCount && $i < 50) { # 找够数量为止,或者查找超过100个位置, 跳出查找
  714. $index = $rank + ($b ? - $i : $i);
  715. $b = !$b;
  716. if ($b) { # 变化查找方向
  717. $i++; #
  718. }
  719. if ($index < 0 || $index >= $zlen) { # 跳过不合理位置
  720. continue;
  721. }
  722. $back = $mem->zrevrange($key, $index, $index, true); # 取1个人
  723. if (count($back) <= 0) {
  724. continue; # 跳过
  725. }
  726. $us = array_keys($back);
  727. $d_uid = $us[0];
  728. if ($d_uid == $uid) { # 避开自己
  729. continue;
  730. }
  731. $user = $mem->get(MemKey_User::Info_hash($zoneid, $d_uid));
  732. if (null == $user) { # 找不到对手数据,跳过
  733. continue;
  734. }
  735. // $fp = HeroProc::CalcTeamFightPower($zoneid, $d_uid, $user);
  736. // if ($fp > 0) {
  737. if (!in_array($d_uid, $pvp->dailyMatchRecord)) { # 已经匹配过的对手不在出现
  738. $arr[$d_uid] = $back[$d_uid];
  739. $pvp->dailyMatchRecord[] = $d_uid;
  740. }
  741. // }
  742. }
  743. }
  744. /**
  745. * 拉取玩家信息-pvp模块专用信息
  746. * @param CredisUtil $mem
  747. * @param type $zoneid
  748. * @param type $retUidsWithScore
  749. * @return type
  750. */
  751. private static function GetPlayerInfosForPVP($mem, $zoneid, $retUidsWithScore) {
  752. $retUids = array_keys($retUidsWithScore);
  753. $keysOfUserInfo = array_map(function($u)use($zoneid) {
  754. return MemKey_User::Info_hash($zoneid, $u);
  755. }, $retUids);
  756. $arrUserInfos = $mem->getMulti($keysOfUserInfo);
  757. $arr = ArrayInit();
  758. $i = 0;
  759. foreach ($arrUserInfos as $userGameInfo) { # 遍历
  760. isEditor() && $userGameInfo = new UserGameModel; # 语法辅助
  761. $uid = $retUids[$i++]; # 当前UID
  762. $teamConfig = $userGameInfo->pvp->defTeam; # 防守阵容
  763. $heros = new \stdClass(); # 英雄集合
  764. foreach ($teamConfig->heros as $i => $hid) {
  765. if ($hid > 0) {
  766. $n_hid = $hid - 10000;
  767. $heros->$n_hid = $userGameInfo->game->heros->collectHeros->$hid;
  768. }
  769. }
  770. $adversary = array(# # 拼装玩家信息
  771. 'uid' => $uid,
  772. 'name' => my_null_default($userGameInfo->game->name, ""),
  773. 'level' => my_null_default($userGameInfo->game->level, 1),
  774. 'headImg' => my_null_default($userGameInfo->game->img, ""),
  775. // 'skills' => null, # # skills暂时没有实例数据
  776. 'equipment' => array("equipments" => my_null_default($userGameInfo->game->store->equipment, new \stdClass())), # 武器
  777. 'yanling' => array("items" => my_null_default($userGameInfo->game->store->yanling, new \stdClass())), # 言灵
  778. 'heros' => my_null_default($heros, new \stdClass()), # # 英雄集合
  779. 'score' => $retUidsWithScore[$uid], # # 积分
  780. );
  781. $arr[$uid] = $adversary;
  782. }
  783. if (count($arr) <= 0) {
  784. $arr = null;
  785. }
  786. return $arr;
  787. }
  788. /**
  789. * 获取榜单玩家
  790. * @deprecated since version 2020.5.8
  791. * @param Credisutil $mem
  792. * @param int $zoneid
  793. * @param int $start
  794. * @param int $stop
  795. * @return type
  796. */
  797. private static function getRankPlayers($mem, $zoneid, $start, $stop) {
  798. $key = MemKey_GameRun::Game_PVPScoreByZone_zset($zoneid);
  799. $retUidsWithScore = $mem->zrevrange($key, $start, $stop, true);
  800. return self::GetPlayerInfosForPVP($mem, $zoneid, $retUidsWithScore);
  801. }
  802. /**
  803. * 提取英雄信息以供其他人使用
  804. * @deprecated since version 2020.5.8
  805. * @param type $userInfo
  806. * @param type $heroId
  807. * @return UserHeroModel
  808. */
  809. public static function getHeroInfoForShare($userInfo, $heroId) {
  810. if ($heroId) {
  811. $heroInfo = $userInfo->heros->collectHeros->$heroId;
  812. } else { // todo: 暂定如果没有设置共享英雄,默认取集合中第一个英雄, 征询策划是否修订规则
  813. $coll = $userInfo->heros->collectHeros;
  814. $keys = array_keys((array) $coll);
  815. if (count($keys) <= 0) {
  816. return null; // 暴力跳过, 防止出现更大的错误
  817. }
  818. $heroId = $keys[0];
  819. $heroInfo = $userInfo->heros->collectHeros->$heroId;
  820. }
  821. // 额外将英雄的装备数据替换为实例数据
  822. self::getArmor($userInfo, $heroInfo, 'weapon');
  823. self::getArmor($userInfo, $heroInfo, 'armor');
  824. self::getArmor($userInfo, $heroInfo, 'ring');
  825. return $heroInfo;
  826. }
  827. /**
  828. * @deprecated since version 2020.5.8
  829. * @param type $userInfo
  830. * @param type $hero
  831. * @param type $armor
  832. * @return type
  833. */
  834. private static function getArmor($userInfo, $hero, $armor) {
  835. if (isset($hero->equip->$armor)) {
  836. if (property_exists($hero->equip->$armor, 'itemuid')) {
  837. $armorid = $hero->equip->$armor->itemuid;
  838. if ($armorid > 0 && isset($userInfo->store->equipment->$armorid)) {
  839. $hero->equip->$armor = $userInfo->store->equipment->$armorid;
  840. return; # 找到后直接跳出
  841. }
  842. }
  843. }
  844. $hero->equip->$armor = null;
  845. }
  846. // </editor-fold>
  847. //
  848. // </editor-fold>
  849. //
  850. }