PVPProc.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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. foreach ($team->teamDic->$curTeamId->heros as $i => $hid) {
  53. if ($hid > 0) {
  54. $n_hid = $hid - 10000;
  55. $heros->$n_hid = $uinfo->game->heros->collectHeros->$hid;
  56. }
  57. }
  58. $adversary = array(# # 拼装玩家信息
  59. 'uid' => $targetUID,
  60. 'name' => my_null_default($uinfo->game->baseInfo->name, ""),
  61. 'level' => my_null_default($uinfo->game->baseInfo->level, 1),
  62. 'headImg' => my_null_default($uinfo->game->baseInfo->headImg, ""),
  63. // 'skills' => null, # # skills暂时没有实例数据
  64. 'equipment' => array("equipments" => my_null_default($uinfo->game->store->equipment, new \stdClass())), # 武器
  65. 'yanling' => array("items" => my_null_default($uinfo->game->store->yanling, new \stdClass())), # 言灵
  66. 'heros' => my_null_default($heros, new \stdClass()), # # 英雄集合
  67. );
  68. $result = array(# # 拼装返回值
  69. "adversaryInfo" => $adversary
  70. );
  71. return Resp::ok($result);
  72. }
  73. /**
  74. * [6804] 挑战 - 记录挑战结果
  75. * @param req $req
  76. */
  77. static function LogChallengeInfo($req) {
  78. // 参数: 对手uid,对手昵称,对手头像, 对战结果, 胜利者的留言(失败时无法留言"")
  79. list($targetUID, $name, $headImg, $win, $msg) = $req->paras;
  80. $key_mine = MemKey_User::OffensiveLog_zset($req->zoneid, $req->uid);
  81. $key_target = MemKey_User::DefensiveLog_zset($req->zoneid, $targetUID);
  82. $ts = now(); # 记录时间戳
  83. $req->mem->zadd($key_mine, array(# # 组装被挑战对手信息
  84. JsonUtil::encode(array(
  85. 'uid' => my_null_default($targetUID, "-"),
  86. 'name' => my_null_default($name, ""),
  87. 'headImg' => my_null_default($headImg, ""),
  88. 'win' => my_null_default($win, false),
  89. 'msg' => my_null_default($msg, ""),
  90. 'ts' => $ts
  91. )) => $ts));
  92. $req->mem->zremrangebyrank($key_mine, self::maxLogCount, -1); # 保留50条数据
  93. $req->mem->zadd($key_target, array(# # 组装挑战者信息
  94. JsonUtil::encode(array(
  95. 'uid' => $req->uid,
  96. 'name' => $req->userInfo->game->baseInfo->name,
  97. 'headImg' => $req->userInfo->game->baseInfo->headImg,
  98. 'win' => !my_null_default($win, false),
  99. 'msg' => my_null_default($msg, ""),
  100. 'ts' => $ts
  101. )) => $ts));
  102. $req->mem->zremrangebyrank($key_target, self::maxLogCount, -1); # 保留50条数据
  103. // 暂无发放奖励流程
  104. // 更新每日任务
  105. TaskProc::OnRankChalenge();
  106. UserProc::updateUserInfo();
  107. // 返回
  108. return Resp::ok(); # 返回成功
  109. }
  110. /**
  111. * [6805] 挑战 - 拉取挑战记录
  112. * @param req $req
  113. */
  114. static function GetChagllengeLog($req) {
  115. // 参数:无
  116. $key_off = MemKey_User::OffensiveLog_zset($req->zoneid, $req->uid);
  117. $key_def = MemKey_User::DefensiveLog_zset($req->zoneid, $req->uid);
  118. // 拉取自己的挑战记录
  119. $offLog = $req->mem->zrange($key_off, 0, self::maxLogCount);
  120. $defLog = $req->mem->zrange($key_def, 0, self::maxLogCount);
  121. // Ps. 挑战记录分为2个榜, 且按照时间戳记录,晚于指定时间戳的判定为未读消息,挑战记录最多记录50条
  122. // if (!CommUtil::isPropertyExists($req->userInfo->game->privateState, "lastCheckDefLog")) {
  123. $req->userInfo->game->privateState->lastCheckDefLog_ts = now(); # 记录时间戳
  124. // }
  125. UserProc::updateUserInfo(); # 回写数据
  126. // 记录拉取时间戳(在主界面有个未读消息条数显示, 需要靠最后拉取时间戳对比, 时间戳之后的消息是未读消息)
  127. // 回传数据记录
  128. array_walk($offLog, function (&$i) { # 解码一下
  129. $i = JsonUtil::decode($i);
  130. });
  131. array_walk($defLog, function (&$i) { # 解码一下
  132. $i = JsonUtil::decode($i);
  133. });
  134. return Resp::ok(array(
  135. 'offLog' => $offLog,
  136. 'defLog' => $defLog
  137. ));
  138. }
  139. // <editor-fold defaultstate="collapsed" desc=" 竞技商店 ">
  140. /**
  141. * 王刚 16:23:39 (2020.5.9)
  142. 商店现在的模式定位:
  143. 商店显示所有物品, 刷新时是重置购买/售罄记录
  144. 刘海 16:24:23 (2020.5.9)
  145. 没错
  146. */
  147. /**
  148. * [6820] 竞技商店 主界面
  149. * @param req $req
  150. */
  151. public static function pvpShopMain($req) {
  152. $pvp = new Info_UserPVP($req->userInfo->game->pvp); # PVP信息
  153. if ($pvp->shopRefreshTs < now()) { # 检查刷新时间<now => 刷新商品列表
  154. $pvp->shopRefreshTs = now() + glc()->PVP_shop_refresh_interval; # 更新刷新时间
  155. $pvp->curShopItems = GameConfig::pvp_shop(); # 重刷道具
  156. $req->userInfo->game->pvp = $pvp; # 回写
  157. UserProc::updateUserInfo();
  158. }
  159. return Resp::ok($pvp); // 返回
  160. }
  161. /**
  162. * [6821] 竞技 商店 购买道具
  163. * @param req $req
  164. */
  165. public static function pvpShopBuy($req) {
  166. $index = $req->paras[0]; # 参数:道具索引(typeId)
  167. $pvp = new Info_UserPVP($req->userInfo->game->pvp); # PVP 数据
  168. my_Assert(CommUtil::isPropertyExists($pvp->curShopItems, $index), ErrCode::err_innerfault); # 没有找到改商品
  169. // isEditor() && $citem = new \sm_pvp_shop();
  170. $citem = $pvp->curShopItems->$index; # 查询物品数据
  171. my_Assert($citem->sold == 0, ErrCode::pvp_item_soldout); # 防御道具已售罄
  172. // var_dump($citem);
  173. my_Assert($citem->priceType == 5, ErrCode::pay_price_err); # 防御定价异常
  174. my_Assert($pvp->pvpCoins > $citem->price, ErrCode::pvp_coinnotenough); # pvp币不足
  175. $citem->sold += 1; # 设置已售罄/已购买标志
  176. $pvp->pvpCoins -= $citem->price; # 扣除竞技币
  177. StoreProc::AddMultiItemInStore($req, $citem->goods); // 发放道具
  178. $req->userInfo->game->pvp = $pvp; // 回写数据
  179. UserProc::updateUserInfo();
  180. return Resp::ok(array('pvp' => $pvp, 'store' => $req->userInfo->game->store)); # 返回
  181. }
  182. /**
  183. * [6822] 竞技 商店 刷新道具
  184. * @param req $req
  185. */
  186. public static function pvpShopRefresh($req) {
  187. // 扣除刷新消耗
  188. $pvp = new Info_UserPVP($req->userInfo->game->pvp);
  189. $costCash = glc()->PVP_shop_refresh_cash;
  190. my_Assert(Data_UserGame::Consume_Cash($req->userInfo->game->baseInfo, $costCash), ErrCode::notenough_cash_msg);
  191. // $pvp->shopRefreshTs = now() + glc()->PVP_shop_refresh_interval; # 更新刷新时间
  192. $pvp->curShopItems = GameConfig::pvp_shop(); # 重刷道具
  193. $req->userInfo->game->pvp = $pvp; # 回写
  194. UserProc::updateUserInfo(); # 回写玩家数据
  195. return Resp::ok($pvp); # 返回
  196. }
  197. // // </editor-fold>
  198. //
  199. // <editor-fold defaultstate="collapsed" desc=" 竞技场 681x">
  200. //
  201. /**
  202. * 辅助方法:取当前赛季的编号(从赛季起始开始算起)
  203. * @return int
  204. */
  205. public static function GetCurSeasonID() {
  206. $n = ceil((now() - self::pvpStartTs ) / self::pvpSeasonLengt); # 进一取整
  207. return $n;
  208. }
  209. /**
  210. * 辅助方法:取指定赛季的结束时间戳
  211. * @param int $seasonID
  212. * @return int
  213. */
  214. public static function GetSeasonEndTs($seasonID) {
  215. $ts = self::pvpStartTs + $seasonID * self::pvpSeasonLengt; # 从起始点开始 + n个赛季时常
  216. return $ts;
  217. }
  218. /**
  219. * [6810] 竞技场 拉取主界面信息
  220. * @param Req $req
  221. */
  222. static function pvpMainInfo($req) {
  223. $uid = $req->uid; # 快速访问UID
  224. $zoneid = $req->zoneid; # 快速访问zoneid
  225. $pvp = new Info_UserPVP($req->userInfo->game->pvp); # 设计玩家pvp数据结构
  226. $pvp->refreshDailyData(); # 刷新免费挑战次数
  227. $seasonId = self::GetCurSeasonID(); # 当前赛季ID
  228. $key = MemKey_GameRun::Game_PVPScoreByZoneSeason_zset($zoneid, $seasonId); # 积分总榜
  229. $score = self::_getScore_by_uid($uid, $key); # 玩家积分
  230. $rank = self::_getRank_by_uid($uid, $key); # 玩家排名
  231. $fPower = HeroProc::CalcUserFightPower($zoneid, $uid, $req->userInfo->game); # 玩家总战力?还是当前防守队伍的战斗力?
  232. $numNewLog = 0; // todo: 真正查询是否有新战报
  233. $matches = self::getNewMatches($pvp, $uid, $zoneid); # 获得新的匹配对手
  234. $pvp->sendRewardEmail($zoneid, $uid, $seasonId); # 发奖励邮件
  235. $req->userInfo->game->pvp = $pvp;
  236. UserProc::updateUserInfo();
  237. $ret = array(# # 组装 返回值结构
  238. 'score' => $score, # # 自己的积分
  239. 'rank' => $rank, # # 自己的排名
  240. 'pvpCoins' => $pvp->pvpCoins, # # 自己的竞技币
  241. 'fPower' => $fPower, # # 自己的总战力
  242. 'fightTicket' => $pvp->fightTicket, # # 自己的挑战票
  243. 'defTeam' => $pvp->defTeam, # # 自己的防守队伍
  244. 'bHasNewFightLog' => $numNewLog, # # 是否有战报刷新
  245. 'matches' => my_null_default($matches, array()), # # 对手列表
  246. );
  247. return Resp::ok($ret); # 返回
  248. }
  249. /**
  250. * [6811] 竞技场 刷新对手
  251. * @param Req $req
  252. */
  253. static function pvp_Refresh($req) {
  254. // 刷新无花费, 间隔时间3秒(客户端控制得了)
  255. $pvp = new Info_UserPVP($req->userInfo->game->pvp);
  256. $ts = now();
  257. my_Assert($pvp->nextRefreshTs < $ts, ErrCode::pvp_refresh_time); # 验证时间间隔
  258. $pvp->curMatches = self::getNewMatches($pvp, $req->uid, $req->zoneid);
  259. $pvp->nextRefreshTs = now(3);
  260. $req->userInfo->game->pvp = $pvp;
  261. UserProc::updateUserInfo(); # 回写数据
  262. $ret = array(
  263. 'curMatches' => my_null_default($pvp->curMatches, array()), # # 当前对手清单
  264. );
  265. return Resp::ok($ret);
  266. }
  267. /**
  268. * [6812] 竞技场 挑战对手xx
  269. * @param Req $req
  270. */
  271. static function pvp_PK($req) {
  272. $uid = $req->uid;
  273. $zoneid = $req->zoneid;
  274. $baseInfo = $req->userInfo->game->baseInfo;
  275. list($target_uid, $result, $target_name, $target_HeadImg) = $req->paras; # 对手id,胜负结果 0负,1胜
  276. $pvp = $req->userInfo->game->pvp;
  277. if ($pvp->freeFightTickets > 0) { # 有免费挑战票,先扣除免费的
  278. $pvp->freeFightTickets -= 1;
  279. } else {
  280. my_Assert($pvp->fightTicket > 0, ErrCode::pvp_no_tickets); # 防御: 挑战票不足
  281. $pvp->fightTicket -= 1; # 扣除挑战票
  282. }
  283. $season = self::GetCurSeasonID(); # 当前赛季
  284. $key = MemKey_GameRun::Game_PVPScoreByZoneSeason_zset($zoneid, $season); # redis key
  285. $RA = self::_getScore_by_uid($uid, $key); # A的积分
  286. $RB = self::_getScore_by_uid($target_uid, $key); # B的积分
  287. $EA = 1 / (1 + pow(10, ($RA - $RB) / 400)); # A的胜率期望值
  288. $EB = 1 / (1 + pow(10, ($RB - $RA) / 400)); # B的胜率期望值
  289. $K = 32; # 浮动系数, 暂定为32
  290. $SA = $result ? 1 : 0; # 我的胜负结果
  291. $SB = $result ? 0 : 1; # 对手的胜负结果
  292. $R_A = intval($RA + $K * ($SA - $EA )); # 我的最终积分
  293. $R_B = intval($RB + $K * ($SB - $EB)); # 对手的最终积分
  294. #
  295. $myOldRank = self::_getRank_by_uid($uid, $key); # 记录下战前排名
  296. self::_addScore_by_uid($zoneid, $uid, $R_A - $RA); # 更新A的积分
  297. $B_Change = $result ? $R_B - $RB : 0; # 计算对方的积分变化:对方输了的情况下扣分,我输了的情况下,不加分
  298. if ($B_Change != 0) { #
  299. self::_addScore_by_uid($zoneid, $target_uid, $B_Change); # 更新B的积分
  300. }
  301. $myNewRank = self::_getRank_by_uid($uid, $key); # 查询战后排名
  302. if ($result) {
  303. $pvp->totalWin += 1;
  304. TaskProc::OnPvPWinN($pvp->totalWin);
  305. TaskProc::OnPvPScoreN($R_A);
  306. }
  307. $req->userInfo->game->pvp = $pvp;
  308. TaskProc::OnPvp(); # 每日PVP挑战即可
  309. UserProc::updateUserInfo(); # 回写数据
  310. // 写挑战记录
  311. $key_mine = MemKey_User::PVP_OffensiveLog_zset($zoneid, $uid); # 我的主动挑战记录
  312. self::_Log_PVP_PK_Info($key_mine, $target_uid, $target_name, $target_HeadImg, $result, $R_A - $RA); # 自己的挑战记录
  313. $key_target = MemKey_User::PVP_DefensiveLog_zset($zoneid, $target_uid); # 对手的被挑战记录
  314. self::_Log_PVP_PK_Info($key_target, $uid, $baseInfo->name, $baseInfo->headImg, !$result, $B_Change); # 对手的被挑战记录
  315. $ret = array(# # 组装返回值
  316. 'freeFightTickets' => $pvp->freeFightTickets, # # 自己剩余免费票
  317. 'fightTicket' => $pvp->fightTicket, # # 自己剩余挑战票
  318. 'RA' => $RA, # # 自己挑战之前积分
  319. 'RB' => $RB, # # 对手挑战之前积分
  320. 'R_A' => $R_A, # # 自己最新积分
  321. 'R_B' => $R_B, # # 对手最新积分
  322. 'rank_diff' => $myNewRank - $myOldRank, # # 自己的排名变化
  323. 'rank_new' => $myNewRank, # # 最新排名
  324. );
  325. return Resp::ok($ret); # 返回
  326. }
  327. /**
  328. * [6813] 竞技场 设置防守队伍
  329. * @param req $req
  330. */
  331. public static function pvp_setTeam($req) {
  332. $heros = $req->paras[0]; # para: 新阵容
  333. $pvp = new Info_UserPVP($req->userInfo->game->pvp);
  334. my_Assert(is_array($heros), ErrCode::paras_err); # 参数检查
  335. $pvp->defTeam = $heros; # 更新阵容
  336. $req->userInfo->game->pvp = $pvp;
  337. UserProc::updateUserInfo(); # 回存数据
  338. return Resp::ok($pvp); # 返回
  339. }
  340. /**
  341. * [6814] 购买挑战票
  342. * @param Req $req
  343. * @return type
  344. */
  345. static function pvp_buyticket($req) {
  346. $amt = $req->paras[0]; # 购买数量
  347. # 检查并扣除消耗
  348. # 增加挑战票
  349. # 回存数据
  350. # 返回
  351. my_Assert($amt > 0, ErrCode::paras_err); # 数量>0
  352. $pvp = new Info_UserPVP($req->userInfo->game->pvp);
  353. $g = glc();
  354. $costCash = $g->PVP_pk_ticket_price * $amt; # 计算消耗钻石
  355. my_Assert($costCash > 0, ErrCode::pvp_ticket_cost_ilegal); # 定价数据异常
  356. if (!Data_UserGame::Consume_Cash($req->userInfo->game->baseInfo, $costCash)) { # 扣除钻石失败
  357. return Resp::err(ErrCode::notenough_cash_msg);
  358. }
  359. $pvp->fightTicket += $amt; # 发放挑战票
  360. $req->userInfo->game->pvp = $pvp;
  361. UserProc::updateUserInfo(); # 回写玩家数据
  362. $ret = array(
  363. 'fightTicket' => $pvp->fightTicket,
  364. 'costCash' => $costCash,
  365. 'userCash' => $req->userInfo->game->baseInfo->cash
  366. );
  367. return Resp::ok($ret); # 返回
  368. }
  369. /**
  370. * [6815] 竞技场 拉取榜单数据
  371. * @param Req $req
  372. */
  373. static function pvp_getRank($req) {
  374. $maxAmt = 10; # 一次最多取10个玩家信息
  375. $zoneid = $req->zoneid;
  376. $index = $req->paras[0]; # 起始(0)
  377. $n = $req->paras[1]; # 数量, (n<=max)
  378. #
  379. if ($n < 1 || $n > $maxAmt) { # 防御非法情况
  380. $n = $maxAmt;
  381. }
  382. $arr = self::getRankPlayers($req->mem, $zoneid, $index - 1, ($index + $n) - 1);
  383. //
  384. $rankId = $index;
  385. $result = ObjectInit();
  386. if (count($arr)) {
  387. foreach ($arr as $key => $value) {// $value 的数据类型是array
  388. $value["uid"] = $key;
  389. $result->$rankId = $value;
  390. $rankId += 1;
  391. }
  392. }
  393. $key_rank = MemKey_GameRun::Game_PVPScoreByZoneSeason_zset($zoneid, self::GetCurSeasonID());
  394. $myRank = self::_getRank_by_uid($req->uid, $key_rank);
  395. $myScore = self::_getScore_by_uid($uid, $key_rank);
  396. $ret = array(
  397. 'dic' => $result,
  398. 'myRank' => $myRank,
  399. 'myScore' => $myScore
  400. );
  401. return Resp::ok($ret);
  402. }
  403. /**
  404. * [6816] 竞技场 查看战报
  405. * @param Req $req
  406. */
  407. static function pvp_getFightLogs($req) {
  408. // 提取主动挑战+被挑战记录
  409. // 更新下刷新时间
  410. // 返回
  411. // 参数:无
  412. $key_off = MemKey_User::PVP_OffensiveLog_zset($req->zoneid, $req->uid);
  413. $key_def = MemKey_User::PVP_DefensiveLog_zset($req->zoneid, $req->uid);
  414. // 拉取自己的挑战记录
  415. $offLog = $req->mem->zrange($key_off, 0, self::maxLogCount); # 主动挑战数据
  416. $defLog = $req->mem->zrange($key_def, 0, self::maxLogCount); # 防守数据
  417. // Ps. 挑战记录分为2个榜, 且按照时间戳记录,晚于指定时间戳的判定为未读消息,挑战记录最多记录50条
  418. $pvp = new Info_UserPVP($req->userInfo->game->pvp); # 玩家竞技场数据
  419. $pvp->lastCheckDefLog_ts = now(); # 记录时间戳
  420. UserProc::updateUserInfo(); # 回写数据
  421. array_walk($offLog, function (&$i) { # 解码一下
  422. $i = JsonUtil::decode($i);
  423. });
  424. array_walk($defLog, function (&$i) { # 解码一下
  425. $i = JsonUtil::decode($i);
  426. });
  427. return Resp::ok(array(
  428. 'offLog' => $offLog,
  429. 'defLog' => $defLog
  430. ));
  431. }
  432. // ---------------- 辅助函数 -----------------------
  433. // <editor-fold defaultstate="collapsed" desc=" 辅助 函数 ">
  434. /**
  435. * 竞技场 - 记录挑战结果
  436. */
  437. private static function _Log_PVP_PK_Info($key, $oppUID, $name, $headImg, $win, $score) {
  438. $ts = now(); # 记录时间戳
  439. gMem()->zadd($key, array(# # 组装被挑战对手信息
  440. JsonUtil::encode(array(
  441. 'uid' => my_null_default($oppUID, "-"), # # 对手uid
  442. 'name' => my_null_default($name, ""), # # 对手昵称
  443. 'headImg' => my_null_default($headImg, ""), # # 对手头像
  444. 'win' => my_null_default($win, false), # # 胜负结果
  445. 'msg' => "竞技场中没有留言羞辱对手的设定.", # # 胜利者留言
  446. 'scoreInfo' => my_null_default($score, 0), # # 积分变动
  447. 'ts' => $ts, # # 时间戳
  448. )) => $ts));
  449. gMem()->zremrangebyrank($key, self::maxLogCount, -1); # 保留50条数据
  450. }
  451. /**
  452. * 修改积分
  453. * @param int $zoneid
  454. * @param string $uid
  455. * @param int $addValue 可以是负值
  456. */
  457. private static function _addScore_by_uid($zoneid, $uid, $addValue) {
  458. $mem = gMem();
  459. $seasonId = self::GetCurSeasonID();
  460. $key = MemKey_GameRun::Game_PVPScoreByZoneSeason_zset($zoneid, $seasonId); # 积分榜
  461. $score = $mem->zscore($key, $uid);
  462. if (is_null($score) || $score <= 0) { # 分数异常, 理论上不会出现负分
  463. $score = self::pvpBaseScore; # 新手基础分
  464. $mem->zadd($key, array($uid => $score)); # 重置玩家积分
  465. }
  466. $newscore = $mem->zincrby($key, $uid, $addValue); # 返回最新值
  467. // var_dump($newscore);
  468. return $newscore;
  469. }
  470. /**
  471. * 竞技场 查询玩家的积分
  472. * @param type $uid
  473. * @return int
  474. */
  475. static function _getScore_by_uid($uid, $key) {
  476. if ("" == $uid) {
  477. CLog::err('"uid" is Null!');
  478. return 10; # 记录错误并返回一个极低分
  479. }
  480. $mem = gMem();
  481. // var_dump($key);
  482. $score = $mem->zscore($key, $uid);
  483. if (is_null($score) || $score <= 0) { # 分数异常
  484. $score = self::pvpBaseScore; # 新手基础分
  485. $mem->zadd($key, array($uid => $score)); # 更新玩家积分
  486. }
  487. return $score;
  488. }
  489. /**
  490. * 竞技场 查询玩家的排名
  491. * @param string $uid
  492. * @param string $key
  493. * @return int
  494. */
  495. static function _getRank_by_uid($uid, $key) {
  496. $rank = self::pvpMaxRank + 1; # 未上榜
  497. if ("" == $uid) {
  498. CLog::err('"uid" is Null!');
  499. return $rank; # 记录错误并返回未上榜
  500. }
  501. $mem = gMem();
  502. $r = $mem->zrevrank($key, $uid);
  503. if (!is_null($r)) {
  504. $rank = $r + 1; # 有名次,更新(zset从0开始排序)
  505. }
  506. return $rank; # 返回
  507. }
  508. /**
  509. * 获取对手匹配结果
  510. * @param Info_UserPVP $pvp
  511. * @param CRedisutil $mem
  512. * @param type $uid
  513. * @param type $zoneid
  514. */
  515. private static function getNewMatches($pvp, $uid, $zoneid) {
  516. $seasonID = self::GetCurSeasonID();
  517. $key = MemKey_GameRun::Game_PVPScoreByZoneSeason_zset($zoneid, $seasonID); # redis key
  518. $arr = self::findmatcher($key, $uid); # 积分榜查找
  519. if (count($arr) < self::matchCount) { # 再不行, 准备机器人吧
  520. CLog::err('PVP刷对手数量不足, 赶快考虑加入机器人.', 'PVP'); // todo: 这里引入gm对手数据,
  521. }
  522. $ret = self::GetPlayerInfosForPVP(gMem(), $zoneid, $arr);
  523. return $ret;
  524. }
  525. /**
  526. * 查找匹配的对手
  527. * @param type $key
  528. * @param type $uid
  529. */
  530. private static function findmatcher($key, $uid) {
  531. // 根据匹配规格获得5个对手即可(1. 排除自己, 2. 最好不要出现已经刷到过的对手)
  532. // 低于玩家当前积分 -- 1个 -5%~-20%之间
  533. // 与玩家当前积分基本持平 2个, ±5%之间
  534. // 高于玩家当前积分 -- 1个 +5%~+40%之间
  535. $score = self::_getScore_by_uid($uid, $key); # 查积分
  536. // 计算 比自己弱的 上线下线
  537. $bH = ceil($score * (1 - 0.05)); # 低于当前玩家积分5%
  538. $bL = ceil($score * (1 - 0.2)); # 低于当前玩家积分20%
  539. $aL = ceil($score * (1 + 0.05)); # 高于当前玩家积分5%
  540. $aH = ceil($score * (1 + 0.2)); # 高于当前玩家积分20%
  541. $bPlayerUIDs = gMem()->zrevrangebyscore($key, $bH, $bL, true, true, 0, 10); # 取低于玩家积分的uids1个
  542. $mPlayerUIDs = gMem()->zrevrangebyscore($key, $aL, $bH, true, true, 0, 13); # 取玩家相当的uid2个(以防包含玩家自己)
  543. $aPlayerUIDs = gMem()->zrevrangebyscore($key, $aH, $aL, true, true, 0, 10); # 取高于玩家的uids1个
  544. if (array_key_exists($uid, $mPlayerUIDs)) { # 如果积分相近的那一组包含自己
  545. unset($mPlayerUIDs[$uid]); # 剔除掉
  546. } else { # 或者不包含自己
  547. array_pop($mPlayerUIDs); # 那多一个人,踢掉一个
  548. }
  549. $bPlayerUIDs = self::array_random_assoc($bPlayerUIDs, 1);
  550. $mPlayerUIDs = self::array_random_assoc($mPlayerUIDs, 2);
  551. $aPlayerUIDs = self::array_random_assoc($aPlayerUIDs, 1);
  552. $uidWithScore = array_merge($bPlayerUIDs, $mPlayerUIDs, $aPlayerUIDs); # 合并为玩家列表
  553. return $uidWithScore; # 返回uid集合
  554. }
  555. /**
  556. * 随机关联数组
  557. * @param type $arr
  558. * @param type $num
  559. * @return type
  560. */
  561. function array_random_assoc($arr, $num = 1) {
  562. $keys = array_keys($arr);
  563. shuffle($keys);
  564. if ($num > count($arr)) {
  565. $num = count($keys);
  566. }
  567. $r = array();
  568. for ($i = 0; $i < $num; $i++) {
  569. $r[$keys[$i]] = $arr[$keys[$i]];
  570. }
  571. return $r;
  572. }
  573. /**
  574. * 获取榜单玩家
  575. * @param Credisutil $mem
  576. * @param int $zoneid
  577. * @param int $start
  578. * @param int $stop
  579. * @return type
  580. */
  581. private static function getRankPlayers($mem, $zoneid, $start, $stop) {
  582. $key = MemKey_GameRun::Game_PVPScoreByZoneSeason_zset($zoneid, self::GetCurSeasonID());
  583. $retUidsWithScore = $mem->zrevrange($key, $start, $stop, true);
  584. return self::GetPlayerInfosForPVP($mem, $zoneid, $retUidsWithScore);
  585. }
  586. // private static function get
  587. /**
  588. * 拉取玩家信息-pvp模块专用信息
  589. * @param CredisUtil $mem
  590. * @param type $zoneid
  591. * @param type $retUidsWithScore
  592. * @return type
  593. */
  594. private static function GetPlayerInfosForPVP($mem, $zoneid, $retUidsWithScore) {
  595. $arr = ArrayInit();
  596. foreach ($retUidsWithScore as $uid => $score) {
  597. // isEditor() && $userGameInfo = new loyalsoft\UserInfoMo;
  598. $userGameInfo = UserProc::getUserInfo($mem, $zoneid, $uid); # 玩家数据
  599. // var_dump($userGameInfo->game->pvp);
  600. $teamConfig = $userGameInfo->game->pvp->defTeam; # 防守阵容
  601. // var_dump($teamConfig);
  602. $heros = new \stdClass(); # 英雄集合
  603. foreach ($teamConfig as $hid) {
  604. if ($hid > 0) {
  605. $n_hid = $hid - 10000;
  606. $heros->$n_hid = $userGameInfo->game->heros->collectHeros->$hid;
  607. }
  608. }
  609. $fpower = HeroProc::CalcUserFightPower($zoneid, $uid, $userGameInfo->game); # 计算总战力
  610. $adversary = array(# # 拼装玩家信息
  611. 'uid' => $uid,
  612. 'name' => my_null_default($userGameInfo->game->baseInfo->name, ""),
  613. 'level' => my_null_default($userGameInfo->game->baseInfo->level, 1),
  614. 'headImg' => my_null_default($userGameInfo->game->baseInfo->headImg, ""),
  615. // 'skills' => null, # # skills暂时没有实例数据
  616. 'equipment' => array("equipments" => my_null_default($userGameInfo->game->store->equipment, new \stdClass())), # 武器
  617. 'yanling' => array("items" => my_null_default($userGameInfo->game->store->yanling, new \stdClass())), # 言灵
  618. 'heros' => my_null_default($heros, new \stdClass()), # # 英雄集合
  619. 'score' => $score, # # 积分
  620. 'fpower' => $fpower, # # 总战力
  621. );
  622. // $arr[$uid] = $adversary;
  623. $arr[] = $adversary;
  624. }
  625. if (count($arr) <= 0) {
  626. $arr = null;
  627. }
  628. return $arr;
  629. }
  630. // </editor-fold>
  631. //
  632. // </editor-fold>
  633. //
  634. }