FriendProc.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. <?php
  2. namespace loyalsoft;
  3. /**
  4. * 好友模块各种操作类型-枚举值
  5. */
  6. class enum_Friend_op extends Enum {
  7. const __default = 0;
  8. // 删除好友
  9. const remove = 1;
  10. // 加好友
  11. const apply = 2;
  12. // 接受请求
  13. const accepted = 3;
  14. // 示好, 送心...
  15. const gift = 4;
  16. // 加好友请求的发送记录
  17. const applied = 5;
  18. // 示好,送心的记录
  19. const giftrecord = 6;
  20. }
  21. /**
  22. * 好友处理流程
  23. * @version
  24. * 1.0.0 Created at 2017-3-6. by --gwang
  25. * @author gwang (mail@wanggangzero.cn)
  26. * @copyright ? 2017-3-6, SJZ LoyalSoft Corporation & gwang. All rights reserved.
  27. */
  28. class FriendProc {
  29. /**
  30. * 好友发送消息的最大长度(超过的截断)
  31. */
  32. const MaxMessageLength = 80;
  33. /**
  34. * 逻辑分发
  35. * 所有的Proc中必须有这样一个方法
  36. * @param type $req
  37. */
  38. static function procMain($req) {
  39. switch ($req->cmd) {
  40. case CmdCode::cmd_user_friend_search: # [6110]查找好友
  41. return FriendProc::SearchFriend($req);
  42. case CmdCode::cmd_user_friend_suggestList: # [6111]添加好友界面获取一批推荐好友列表
  43. return FriendProc::GetSuggestList($req);
  44. case CmdCode::cmd_user_friend_get_list: # [6106]获取好友列表 ok下发好友个人公会信息
  45. return FriendProc::getAppFriends($req);
  46. case CmdCode::cmd_user_friend_get_infos: # [6107]获取详细信息
  47. return FriendProc::getAppFriendInfos($req);
  48. // ------------------------------------------
  49. case CmdCode::cmd_user_friend_gift_getlist: # [6113]拉取好友赠送礼物的记录
  50. return FriendProc::getAppFriendGiftList($req);
  51. case CmdCode::cmd_user_friend_gift_redraw: # [6114]领取好友赠送的礼物[并回赠]
  52. return FriendProc::getAppFriendGift($req);
  53. case CmdCode::cmd_user_friend_gift_send: # [6115]向好友发送礼物,示好
  54. return FriendProc::sendGiftToAppFriend($req);
  55. case CmdCode::cmd_user_friend_unduckmsg:
  56. return FriendProc::CheckUnduckMessage($req);
  57. //--------------------------------------------
  58. case CmdCode::cmd_user_friend_apply: # [6101]加好友
  59. return FriendProc::ApplyToAddNewFriend($req);
  60. case CmdCode::cmd_user_friend_accept: # [6102]接受好友请求
  61. return FriendProc::Accept($req);
  62. case CmdCode::cmd_user_friend_deny: # [6103]拒绝好友请求
  63. return FriendProc::Deny($req);
  64. case CmdCode::cmd_user_friend_remove: # [6104]删除好友
  65. return FriendProc::Remove($req);
  66. case CmdCode::cmd_user_friend_getapplylist: # [6105]好友请求列表
  67. return FriendProc::GetApplyList($req);
  68. //-----------------------------------------------
  69. case CmdCode::cmd_user_friend_leave_message: # [6108]给好友发消息
  70. return FriendProc::SendMessage($req);
  71. case CmdCode::cmd_user_friend_get_message: # [6109]刷新消息
  72. return FriendProc::GetNewMessage($req);
  73. //-----------------------------------------------
  74. case CmdCode::cmd_user_friend_reload: # [6112]更新好友
  75. return FriendProc::Reload($req);
  76. case CmdCode::cmd_user_friend_LookGuildInfo: # [6117]查看好友所在部落信息
  77. return FriendProc::LookGuildInfo($req);
  78. case CmdCode::cmd_user_friend_LookFriendInfo: # [6118]查看好友简介
  79. return FriendProc::LookFriendInfo($req);
  80. default:
  81. return Resp::err(ErrCode::cmd_err);
  82. }
  83. }
  84. /**
  85. * 查看好友的部落信息
  86. * @param type $req
  87. */
  88. static function LookGuildInfo($req) {
  89. $friendId = $req->paras[0];
  90. $zoneid = $req->zoneid;
  91. $mem = $req->mem;
  92. $sql = "select * from `tab_rolename` where userID = '$friendId'";
  93. $rows = daoInst()->query($sql)->fetchAll();
  94. my_Assert(count($rows) > 0, ErrCode::err_user_no); # 找不到玩家数据
  95. $friendGuildInfo = GuildProc::getMemUserGuildInfo($mem, $friendId, $zoneid);
  96. my_Assert($friendGuildInfo != null, "好友公会数据为空");
  97. my_Assert($friendGuildInfo->guildId > 0, ErrCode::err_guild_userguildinfono);
  98. $guildInfo = GuildProc::getMemGuildInfo($mem, $friendGuildInfo->guildId, $zoneid);
  99. my_Assert($guildInfo != null, ErrCode::err_guild_guildinfono); # 找不到公会
  100. return Resp::ok($guildInfo);
  101. }
  102. /**
  103. * 查看好友简介
  104. * @param type $req
  105. */
  106. static function LookFriendInfo($req) {
  107. $friendId = $req->paras[0]; //好友id
  108. $mem = $req->mem;
  109. $sql = "select * from `tab_rolename` where userID = '$friendId'";
  110. $rows = daoInst()->query($sql)->fetchAll();
  111. my_Assert(count($rows) > 0, ErrCode::err_user_no); # 找不到玩家角色记录
  112. $friendInfo = self::initfriendInfo($mem, $rows[0]['userID'], $rows[0]['zoneid']);
  113. my_Assert($friendInfo != null, ErrCode::err_user_no); # 好友数据初始化成功
  114. $friendInfo->fightPower = GuildProc::initFightTeamInfo($mem, $rows[0]['userID'], $rows[0]['zoneid']);
  115. return Resp::ok(array("friendInfo" => $friendInfo,));
  116. }
  117. /**
  118. * 检查玩家的好友信息是否有未处理的
  119. * @param type $req
  120. */
  121. public static function CheckUnduckMessage($req) {
  122. $mem = $req->mem;
  123. $zoneid = $req->zoneid;
  124. $uid = $req->uid;
  125. $result = -1;
  126. // 别人的赠送列表
  127. $fgiftlist = self::getOps($mem, $uid, $zoneid, enum_Friend_op::gift);
  128. if (count((array) $fgiftlist) <= 0) {
  129. $result = 0;
  130. } else {
  131. foreach ($fgiftlist as $key => $value) {
  132. if (count((array) $value) <= 0) {
  133. continue;
  134. } else {
  135. $result = 1;
  136. break;
  137. }
  138. }
  139. }
  140. //找别人发来的申请加好友的列表
  141. if ($result == 0) {
  142. $ret = FriendProc::getOps($req->mem, $req->uid, $req->zoneid, enum_Friend_op::apply);
  143. $result = (count((array) $ret) > 0) ? 1 : 0;
  144. }
  145. return Resp::ok(array('ret' => $result));
  146. }
  147. /**
  148. * [6112] 核算好友的离线操作
  149. * @param Req $req
  150. */
  151. public static function Reload($req) {
  152. $mem = $req->mem;
  153. $uid = $req->uid;
  154. $zoneid = $req->zoneid;
  155. // 刷新接受请求列表, 将已接受的好友添加到
  156. $allOps = self::getOps($mem, $uid, $zoneid);
  157. $op = new enum_Friend_op(enum_Friend_op::accepted);
  158. $ops = $allOps->$op;
  159. if (count($ops) > 0) {
  160. foreach ($ops as $u => $ts) { # 立即将对方添加到好友列表
  161. self::_AddFriend($mem, $uid, $zoneid, $u);
  162. }
  163. $ops = "{}";
  164. $allOps->$op = $ops;
  165. }
  166. // 刷新删除列表, 看谁和我解除了好友关系, 删掉他
  167. $op = new enum_Friend_op(enum_Friend_op::remove);
  168. $ops = $allOps->$op;
  169. if (count($ops) > 0) {
  170. foreach ($ops as $u => $ts) {
  171. self::_RemoveFriend($mem, $uid, $zoneid, $u);
  172. }
  173. $allOps->$op = "{}";
  174. }
  175. $g = glc();
  176. self::filterOutDateRecords($allOps, # # 清理过期的请求记录,
  177. new enum_Friend_op(enum_Friend_op::applied), $g->User_Friends_Apply_ExpireTs);
  178. self::filterOutDateRecords($allOps, # # 刷新别人发来的好友请求列表, 过期而没有处理的也删掉(莫名其妙的消失,玩家会不会不爽?要不要加上倒计时?).
  179. new enum_Friend_op(enum_Friend_op::apply), $g->User_Friends_Apply_ExpireTs);
  180. self::saveOps($mem, $uid, $zoneid, $allOps, 0); # 回存数据
  181. // 清理过期的互动记录, 不清除, 一个好友最多保留3条记录
  182. // 更新好友数量
  183. return Resp::ok(array("ret" => "ok"));
  184. }
  185. private static function filterOutDateRecords($allOps, $op, $outts) {
  186. $ops = $allOps->$op;
  187. if (count((array) $ops) > 0) {
  188. $ops = array_filter((array) $ops, function ($ts)use($outts) {
  189. return (intval($ts) + $outts) > now();
  190. });
  191. $allOps->$op = (is_null($ops) || 0 == count((array) $ops)) ? "{}" : $ops;
  192. }
  193. }
  194. //
  195. // <editor-fold defaultstate="collapsed" desc=" 聊天/留言 ">
  196. //
  197. /**
  198. * [6108]发送消息/留言
  199. * @param Req $req
  200. */
  201. public static function SendMessage($req) {
  202. // todo:检查是否好友
  203. $mem = $req->mem;
  204. $uid_from = $req->uid;
  205. $uid_to = $req->paras[0];
  206. $msg = $req->paras[1];
  207. if (strlen($msg) > self::MaxMessageLength) {
  208. $msg = substr($msg, 0, self::MaxMessageLength) . "...";
  209. }
  210. $key = MemKey_User::FriendsMsgQueue_hash($req->zoneid, $uid_to);
  211. $arr = $mem->hget($key, $uid_from);
  212. if (null == $arr) {
  213. $arr = ArrayInit();
  214. }
  215. $ts = now();
  216. array_push($arr, "{$msg}{$ts}");
  217. if (count($arr) > glc()->User_Friends_MessageCount_Max) {
  218. array_shift($arr);
  219. }
  220. $mem->hset($key, $uid_from, $arr);
  221. //
  222. $mts = isset($req->paras[2]) ? $req->paras[2] : 0;
  223. $newmsg = self::_GetNewMessage($mem, $req->zoneid, $uid_from, $uid_to, $mts);
  224. return Resp::ok(array("ret" => "ok", 'newmsg' => $newmsg));
  225. }
  226. /**
  227. * [6109]拉取最新消息/留言
  228. *
  229. * @param Req $req
  230. */
  231. public static function GetNewMessage($req) {
  232. if (count($req->paras) < 2) {
  233. return Resp::err(ErrCode::parasnotenough_msg);
  234. }
  235. $uid_filter = $req->paras[0];
  236. $ts = $req->paras[1];
  237. $arr = self::_GetNewMessage($req->mem, $req->zoneid, $req->uid, $uid_filter, $ts);
  238. return Resp::ok($arr);
  239. }
  240. /**
  241. * 取留言消息
  242. * @param CredisUtil $mem
  243. * @param int $zoneid
  244. * @param string $uid
  245. * @param string $f_uid
  246. * @param ts $ts
  247. * @return asoc_array
  248. */
  249. private static function _GetNewMessage($mem, $zoneid, $uid, $f_uid, $ts) {
  250. $key = MemKey_User::FriendsMsgQueue_hash($zoneid, $uid);
  251. $arr = array($f_uid => $mem->hget($key, $f_uid));
  252. // 依据ts时间戳,剔除客户端不需要的消息,然后传送.
  253. foreach ($arr as &$msgq) {
  254. if (null == $msgq) { // 防御
  255. continue;
  256. }
  257. $msgq = array_filter($msgq, function($msg) use($ts) {
  258. return $ts < substr($msg, -10); // 时间戳10位
  259. });
  260. }
  261. return $arr;
  262. }
  263. //
  264. //
  265. // </editor-fold>
  266. //
  267. // <editor-fold defaultstate="collapsed" desc=" 待定 ">
  268. //
  269. /**
  270. * 设置默认同意好友请求
  271. * @param Req $req
  272. */
  273. public static function SetAutoAccept($req) {
  274. }
  275. // </editor-fold>
  276. //
  277. // <editor-fold defaultstate="collapsed" desc=" 好友操作 ">
  278. //
  279. /**
  280. * [6105] 拉取好友请求列表
  281. * @param Req $req
  282. */
  283. public static function GetApplyList($req) {
  284. $ret = FriendProc::getOps($req->mem, $req->uid, $req->zoneid, enum_Friend_op::apply);
  285. return Resp::ok(array("ret" => (count((array) $ret) > 0) ? $ret : array()));
  286. }
  287. /**
  288. * [6101]添加好友
  289. * @param Req $req
  290. */
  291. public static function ApplyToAddNewFriend($req) { // 取目标玩家的ops列表插入请求
  292. $mem = $req->mem;
  293. $uid_to = $req->paras[0];
  294. $uid_from = $req->uid;
  295. if ($uid_from == $uid_to) {
  296. return Resp::err(ErrCode::paras_err, "添加的好友ID非法!");
  297. }
  298. $err = self::AddOp($mem, $req->zoneid, $uid_to, enum_Friend_op::apply, $uid_from);
  299. if (ErrCode::ok != $err) {
  300. return Resp::err($err);
  301. }
  302. $err = self::AddOp($mem, $req->zoneid, $req->uid, enum_Friend_op::applied, $uid_to);
  303. if (ErrCode::ok != $err) {
  304. return Resp::err($err);
  305. }
  306. return Resp::ok(array("ret" => "ok"));
  307. }
  308. /**
  309. * [6102]接受好友请求
  310. * @param Req $req
  311. */
  312. public static function Accept($req) { // 将对方立即放到自己的好友列表中, 并给对方发送接受的op, 删除请求记录
  313. $mem = $req->mem;
  314. $zoneid = $req->zoneid;
  315. $uid_my = $req->uid;
  316. $uid_arr = explode(',', $req->paras[0]);
  317. foreach ($uid_arr as $uid_to) {
  318. $myOp = self::getOps($mem, $req->uid, $zoneid, enum_Friend_op::apply); // 自己的请求列表
  319. unset($myOp->$uid_to); // 删除请求记录
  320. self::saveOps($mem, $req->uid, $zoneid, $myOp, enum_Friend_op::apply); // 更新自己的请求列表
  321. $err = self::AddOp($mem, $zoneid, $uid_to, enum_Friend_op::accepted, $uid_my); // 向对方的已接受列表中添加记录
  322. if ($err != ErrCode::ok) { // 出错
  323. return Resp::err($err);
  324. }
  325. self::_AddFriend($mem, $uid_my, $zoneid, $uid_to);
  326. // $mem->sadd(MemKey_User::FriendsList_set($zoneid, $req->uid), $uid_to); // 立即将对方添加到好友列表
  327. }
  328. return Resp::ok(array("ret" => "ok"));
  329. }
  330. /**
  331. * [6103]拒接好友请求
  332. * @param Req $req
  333. */
  334. public static function Deny($req) { // 直接 删除请求记录, 不通知对方了
  335. $mem = $req->mem;
  336. $zoneid = $req->zoneid;
  337. $uid_my = $req->uid;
  338. $uid_to = $req->paras[0];
  339. // 从自己的列表中删除就行了,不用通知对方
  340. $myOp = self::getOps($mem, $uid_my, $zoneid, enum_Friend_op::apply); // 自己的请求列表
  341. unset($myOp->$uid_to);
  342. self::saveOps($mem, $uid_my, $zoneid, $myOp, enum_Friend_op::apply); // 更新自己的请求列表
  343. return Resp::ok(array("ret" => "ok"));
  344. }
  345. /**
  346. * [6104]删除好友
  347. * @param Req $req
  348. */
  349. public static function Remove($req) {// 将对方立即从自己的好友列表中删除, 并给对方发送remove的op
  350. $mem = $req->mem;
  351. $zoneid = $req->zoneid;
  352. $uid_to = $req->paras[0]; // 删除谁
  353. $uid_my = $req->uid;
  354. $tOp = self::getOps($mem, $uid_to, $zoneid, enum_Friend_op::remove); // 对方的删除列表
  355. $ts = now();
  356. $userInfo_my = UserProc::getUserInfo($mem, $zoneid, $uid_my);
  357. $name = $userInfo_my->game->name;
  358. $tOp->$uid_my = $ts . "_" . $name;
  359. self::saveOps($mem, $uid_to, $zoneid, $tOp, enum_Friend_op::remove); // 更新对方的删除列表
  360. self::_RemoveFriend($mem, $uid_my, $zoneid, $uid_to);
  361. return Resp::ok(array("ret" => "ok"));
  362. }
  363. //
  364. // </editor-fold>
  365. //
  366. // <editor-fold defaultstate="collapsed" desc=" 搜索 ">
  367. /**
  368. * [6110]搜索好友
  369. * @param Req $req
  370. */
  371. static function SearchFriend($req) {
  372. //客户端参数解析
  373. if (count($req->paras) <= 1) { // 参数非法
  374. return Resp::err(ErrCode::parasnotenough_msg);
  375. }
  376. $zoneid = $req->zoneid;
  377. $filter = $req->paras[0];
  378. $start = 0;
  379. if (count($req->paras) > 1) {
  380. $start = $req->paras[1];
  381. }
  382. $max = 8; // 每次查询返回的数据条目数量
  383. $sql = "select * from `tab_rolename` where (`userID`='$filter' OR `roleName` like '%$filter%') and zoneid = $zoneid limit $start, $max ;";
  384. $paydb = CPayInit();
  385. $rows = $paydb->fetch_array($sql);
  386. $ret = ArrayInit();
  387. foreach ($rows as $row) {
  388. $uid = $row['userID'];
  389. $zone_all = $row['zoneid'];
  390. if ($uid == $req->uid) {
  391. continue;
  392. }
  393. $friendInfo = self::initfriendInfo($req->mem, $uid, $zone_all);
  394. if ($friendInfo != null) {
  395. $ret[] = $friendInfo;
  396. }
  397. }
  398. $ops = self::getOps($req->mem, $req->uid, $req->zoneid, enum_Friend_op::applied);
  399. return Resp::ok(array('fList' => $ret, 'applied' => $ops));
  400. }
  401. static function initfriendInfo($mem, $uid, $zoneid) {
  402. $userInfo = UserProc::getUserInfo($mem, $zoneid, $uid);
  403. if ($userInfo == null) {
  404. return null;
  405. }
  406. $userGuildInfo = GuildProc::getMemUserGuildInfo($mem, $uid, $zoneid);
  407. if ($userGuildInfo == null) {
  408. $userGuildInfo = ObjectInit();
  409. $userGuildInfo->guildName = "";
  410. $userGuildInfo->guildId = 0;
  411. }
  412. $key_cur = MemKey_GameRun::Game_PVPScoreByZone_zset($zoneid);
  413. $score = $mem->zscore($key_cur, $uid);
  414. $userGuildInfo->cupScore = $score == null ? 0 : $score;
  415. $friendObj = new initfriendInfoModel(null, $uid, $userInfo->game->name, $userInfo->game->level, $userInfo->game->img, #
  416. $userInfo->game->imgBorderId, $userGuildInfo->guildName, $userGuildInfo->guildId, $userGuildInfo->cupScore);
  417. return $friendObj;
  418. }
  419. /**
  420. * [6111]添加好友界面获取一批推荐好友列表
  421. * @param Req $req
  422. */
  423. static function GetSuggestList($req) {
  424. //客户端参数解析
  425. if (count($req->paras) < 1) { // 参数非法
  426. return Resp::err(ErrCode::parasnotenough_msg);
  427. }
  428. $zoneid = $req->zoneid;
  429. $start = $req->paras[0]; // 第几页
  430. $max = 8; // 每次查询返回的数据条目数量
  431. $sql = "select * from `tab_rolename` where `zoneid`=$zoneid order by ts desc limit $start, $max ;";
  432. $paydb = CPayInit();
  433. $rows = $paydb->fetch_array($sql);
  434. $arr = ArrayInit();
  435. foreach ($rows as $row) {
  436. if ($row['userID'] == $req->uid) {
  437. continue;
  438. }
  439. unset($row["tid"]);
  440. unset($row["zoneid"]);
  441. $arr[] = $row['userID'];
  442. }
  443. $applidops = self::getOps($req->mem, $req->uid, $req->zoneid, enum_Friend_op::applied);
  444. $ret = array('fList' => array_unique($arr), 'applied' => $applidops);
  445. return Resp::ok($ret);
  446. }
  447. // </editor-fold>
  448. //
  449. // <editor-fold defaultstate="collapsed" desc=" 好友列表 ">
  450. /**
  451. * [6106]获取好友ID列表
  452. * @param Req $req
  453. * @return int
  454. */
  455. static function getAppFriends($req) {
  456. self::Reload($req);
  457. $mem = $req->mem;
  458. $zoneid = $req->zoneid;
  459. //客户端参数解析
  460. $friendIds = self::_GetFriendList($mem, $req->uid, $zoneid); // 取好友列表
  461. $userArr = ArrayInit();
  462. foreach ($friendIds as $index => $uid) {
  463. $friendInfo = self::initfriendInfo($mem, $uid, $zoneid);
  464. if ($friendInfo == null) {
  465. continue;
  466. }
  467. $userArr[] = $friendInfo;
  468. }
  469. //$mygiftops = self::getOps($mem, $req->uid, $req->zoneid, enum_Friend_op::giftrecord);
  470. $resp = Resp::ok(array(
  471. #"friends" => $friendIds,
  472. 'userGuild' => $userArr,
  473. ));
  474. return $resp;
  475. }
  476. /**
  477. * 查找好友数量
  478. * @param Req $req
  479. */
  480. public static function getCurrentFriendsCount($req) {
  481. return $req->mem->scard(MemKey_User::FriendsList_set($req->zoneid, $req->uid));
  482. }
  483. /**
  484. * [6107] 获取好友信息列表
  485. * @param Req $req
  486. * @return type
  487. */
  488. static function getAppFriendInfos($req) {
  489. $mem = $req->mem;
  490. //客户端参数解析
  491. $zoneid = $req->zoneid;
  492. $fuids = explode(',', $req->paras[0]); // 逗号分割uid字符串
  493. $max = 8; # 一次最多取八个数据
  494. $n = count($fuids);
  495. // var_dump($fuids);
  496. if ($n > 0 && $n <= $max) {
  497. $fuidkeys = array_map(function ($i) use($zoneid) {
  498. return MemKey_User::Info($zoneid, $i);
  499. }, $fuids);
  500. $arr = $mem->getMulti($fuidkeys);
  501. $friendInfos = ArrayInit();
  502. $n = 0;
  503. foreach ($arr as $userInfo) {
  504. isEditor() && $userInfo = new UserGameModel;
  505. isEditor() && $shareInfo = new UserHeroModel;
  506. $uid = $fuids[$n++];
  507. if (!$userInfo # 玩家数据不为空
  508. || !property_exists($userInfo, 'heros') # 存在heros数据
  509. || !$userInfo->heros) { # heros数据不为空
  510. continue;
  511. }
  512. $sharedHeroId = $userInfo->heros->firendSupportHeroUID;
  513. if ($sharedHeroId) {
  514. $shareInfo = $userInfo->heros->collectHeros->$sharedHeroId;
  515. } else { // todo: 暂定如果没有设置共享英雄,默认取集合中第一个英雄, 征询策划是否修订规则
  516. $coll = $userInfo->heros->collectHeros;
  517. $keys = array_keys((array) $coll);
  518. if (count($keys) <= 0) {
  519. continue;
  520. }
  521. $sharedHeroId = $keys[0];
  522. $shareInfo = $userInfo->heros->collectHeros->$sharedHeroId;
  523. }
  524. // 额外将英雄的装备数据替换为实例数据
  525. self::getArmor($userInfo, $shareInfo, 'weapon');
  526. self::getArmor($userInfo, $shareInfo, 'armor');
  527. self::getArmor($userInfo, $shareInfo, 'ring');
  528. $friendInfos[$uid] = array('sharedHero' => $shareInfo,
  529. 'lastLoginTs' => isset($userInfo->lastLogin) ? $userInfo->lastLogin : 0,
  530. 'level' => $userInfo->level,
  531. 'nickname' => $userInfo->name
  532. );
  533. }
  534. if (count($friendInfos) <= 0) {
  535. $friendInfos = null;
  536. }
  537. $resp = Resp::ok(array('ret' => $friendInfos));
  538. } else {
  539. $resp = Resp::err(ErrCode::paras_err);
  540. }
  541. return $resp;
  542. }
  543. static function getArmor($userInfo, $hero, $armor) {
  544. $armorid = $hero->equip->$armor->itemuid;
  545. if ($armorid > 0 && isset($userInfo->store->equipment->$armorid)) {
  546. $hero->equip->$armor = $userInfo->store->equipment->$armorid;
  547. } else {
  548. $hero->equip->$armor = null;
  549. }
  550. }
  551. // </editor-fold>
  552. //
  553. // <editor-fold defaultstate="collapsed" desc=" 示好 ">
  554. //
  555. /**
  556. * [6115]向好友发送礼物(示好)
  557. * @param Req $req
  558. */
  559. static function sendGiftToAppFriend($req) {
  560. $mem = $req->mem;
  561. $zoneid = $req->zoneid;
  562. $uid = $req->uid;
  563. //客户端参数解析
  564. $fuids = explode(',', $req->paras[0]); // 逗号分割的uid数组
  565. // $pr = $req->paras[1]; // other参数 preserved
  566. $n = count($fuids);
  567. if ($n <= 0) {
  568. return Resp::ok(array("ret" => 0));
  569. }
  570. $mygiftrecord = self::getOps($mem, $uid, $zoneid, enum_Friend_op::giftrecord);
  571. $m = 0;
  572. for ($i = 0; $i < $n; $i++) {
  573. $fid = $fuids[$i];
  574. $tsDay = tsDay();
  575. if (property_exists($mygiftrecord, $fid) // 判断对方是否能够领取礼物(当天已经发送过,则不再发送)
  576. && $mygiftrecord->$fid >= $tsDay) {
  577. continue;
  578. }
  579. $mygiftrecord->$fid = $tsDay; # 更新发送记录
  580. $fgiftinfo = self::getOps($mem, $fid, $zoneid, enum_Friend_op::gift);
  581. if (null == $fgiftinfo) { // 防御
  582. $fgiftinfo = ObjectInit(); // 没找到好友数据, 则创建
  583. }
  584. // 给对方身上插入操作记录
  585. if (!isset($fgiftinfo->$uid) or ! is_array($fgiftinfo->$uid)) {//防御
  586. $fgiftinfo->$uid = ArrayInit();
  587. }
  588. $arr = $fgiftinfo->$uid;
  589. $arr[] = tsDay();
  590. if (count($arr) > 3) { // 最多保留3条记录
  591. sort($arr);
  592. array_shift($arr);
  593. }
  594. $fgiftinfo->$uid = $arr; // 更新下
  595. self::saveOps($mem, $fid, $zoneid, $fgiftinfo, enum_Friend_op::gift);
  596. $m++;
  597. }
  598. // 回存数据
  599. self::saveOps($mem, $uid, $zoneid, $mygiftrecord, enum_Friend_op::giftrecord);
  600. // 其他检查比如任务、成就系统
  601. // ...
  602. // 返回结果
  603. return Resp::ok(array("ret" => $m));
  604. }
  605. /**
  606. * [6113]查询游戏中哪些好友给我发送了礼物的列表
  607. * @param Req $req
  608. */
  609. static function getAppFriendGiftList($req) {
  610. $mem = $req->mem;
  611. $zoneid = $req->zoneid;
  612. $uid = $req->uid;
  613. // 别人的赠送列表
  614. $fgiftlist = self::getOps($mem, $uid, $zoneid, enum_Friend_op::gift);
  615. if (count((array) $fgiftlist) <= 0) {
  616. $fgiftlist = NULL;
  617. }
  618. return Resp::ok(array('ret' => $fgiftlist));
  619. }
  620. /**
  621. * [6114]领取来自好友的礼物[并回赠]
  622. * @param Req $req
  623. */
  624. static function getAppFriendGift($req) {
  625. $mem = $req->mem;
  626. $zoneid = $req->zoneid;
  627. $uid = $req->uid;
  628. //客户端参数解析
  629. $fuids = explode(',', $req->paras[0]); // 逗号分割的uidtsday数组
  630. $bSendback = true; // 是否回赠
  631. $arr = array();
  632. array_map(function($f)use(&$arr) {
  633. $arr[substr($f, -5)] = substr($f, 0, -5); // tsday()占5个字符
  634. }, $fuids);
  635. $fgiftlist = self::getOps($mem, $uid, $zoneid, enum_Friend_op::gift);
  636. $recieved = 0;
  637. foreach ($arr as $ts => $fid) {
  638. if (isset($fgiftlist->$fid) && in_array($ts, $fgiftlist->$fid)) {
  639. StlUtil::arrayRemove($fgiftlist->$fid, $ts);
  640. $recieved++;
  641. }
  642. }
  643. $req->userInfo->game->friendPoint += $recieved;
  644. self::saveOps($mem, $uid, $zoneid, $fgiftlist, enum_Friend_op::gift);
  645. UserProc::updateUserInfo();
  646. if ($bSendback && $recieved > 0) {
  647. $uids_send = array_unique(array_values($arr));
  648. $req->paras[0] = implode(',', $uids_send);
  649. // var_dump($uids_send);
  650. self::sendGiftToAppFriend($req);
  651. }
  652. // 当天领取好友礼物有没有上限? 比如100个心.
  653. // 检查是否存在对应的礼物记录,
  654. // 发送礼物
  655. // 删除礼物记录
  656. // 回写数据
  657. // 返回值
  658. return Resp::ok(array("ret" => $recieved));
  659. }
  660. /**
  661. * 玩家初始化
  662. * @param Req $req
  663. */
  664. static function Init($req) {
  665. $mem = $req->mem;
  666. $z = $req->zoneid;
  667. $uid = $req->uid;
  668. // $mem->sadd(MemKey_User::FriendsList_set($z, $uid), "");
  669. $mem->hset(MemKey_User::FriendsMsgQueue_hash($z, $uid), "sys", "");
  670. $mem->hmset(MemKey_User::FriendsOps_hash($z, $uid), array("apply" => "{}",
  671. "applied" => "{}",
  672. "gift" => "{}",
  673. "accepted" => "{}",
  674. "remove" => "{}",
  675. 'giftrecord' => "{}"));
  676. }
  677. //
  678. // </editor-fold>
  679. //
  680. //-------------以下是辅助方法-----------------
  681. //
  682. /**
  683. * 验证是否好友
  684. * @param Req $req
  685. */
  686. public static function isFriendship($zoneid, $myUid, $fUid) {
  687. return gMem()->sismember(MemKey_User::FriendsList_set($zoneid, $myUid), $fUid);
  688. }
  689. /**
  690. * 添加fuid到好友列表
  691. */
  692. private static function _AddFriend($mem, $uid, $zoneid, $fuid) {
  693. $mem->sadd(MemKey_User::FriendsList_set($zoneid, $uid), $fuid);
  694. }
  695. /**
  696. * 从好友列表中删除fuid
  697. */
  698. private static function _RemoveFriend($mem, $uid, $zoneid, $fuid) {
  699. $mem->sremove(MemKey_User::FriendsList_set($zoneid, $uid), $fuid);
  700. }
  701. private static function _GetFriendList($mem, $uid, $zoneid) {
  702. $arr = $mem->smembers(MemKey_User::FriendsList_set($zoneid, $uid));
  703. if (!$arr) { # 数据为空
  704. $arr = ArrayInit();
  705. }
  706. $friendIds = array_values(array_filter($arr, function ($a) {
  707. return $a != "";
  708. }));
  709. return $friendIds;
  710. }
  711. //
  712. // <editor-fold defaultstate="collapsed" desc=" 读写玩家好友数据 ">
  713. /**
  714. * 添加操作
  715. * @param enum_Friend_op $op
  716. * @param Req $req
  717. */
  718. private static function AddOp($mem, $zoneid, $uid, $op, $uid_from) {
  719. $enop = new enum_Friend_op($op);
  720. if (!$enop->isValidValue($op)) {
  721. return ErrCode::paras_err;
  722. }
  723. $ts = now();
  724. $tOp = FriendProc::getOps($mem, $uid, $zoneid, $op);
  725. $userInfo = UserProc::getUserInfo($mem, $zoneid, $uid_from);
  726. $userName = $userInfo->game->name;
  727. $tOp->$uid_from = $ts . "_" . $userName; # 可以按照时间戳排序
  728. return FriendProc::saveOps($mem, $uid, $zoneid, $tOp, $op);
  729. }
  730. /**
  731. * 查询好友异步操作队列
  732. * @param CRedisUtil $mem
  733. * @param string $uid
  734. * @param int $zoneid
  735. * @param enum_Friend_op $op
  736. * @return mixed
  737. */
  738. private static function getOps($mem, $uid, $zoneid, $op = 0) {
  739. $ret = ObjectInit();
  740. if ($op == enum_Friend_op::__default) {
  741. $ret = $mem->hgetall(MemKey_User::FriendsOps_hash($zoneid, $uid));
  742. } else {
  743. $ret = $mem->hget(MemKey_User::FriendsOps_hash($zoneid, $uid), new enum_Friend_op($op));
  744. }
  745. if (is_null($ret)) {
  746. $ret = ObjectInit();
  747. }
  748. return $ret;
  749. }
  750. /**
  751. * 回写好友异步操作队列
  752. * @param CRedisUtil $mem
  753. * @param string $uid
  754. * @param int $zoneid
  755. * @param mixed $data
  756. * @param int $op
  757. */
  758. private static function saveOps($mem, $uid, $zoneid, $data, $op) {
  759. $enop = new enum_Friend_op($op);
  760. if (!$enop->isValidValue($op)) {
  761. return ErrCode::paras_err;
  762. }
  763. if ($op == enum_Friend_op::__default) { # 全部数据
  764. $mem->hmset(MemKey_User::FriendsOps_hash($zoneid, $uid), $data);
  765. } else {
  766. if (is_array($data) && count($data) == 0) {
  767. $data = "{}";
  768. }
  769. $mem->hset(MemKey_User::FriendsOps_hash($zoneid, $uid), $enop, $data);
  770. }
  771. return ErrCode::ok;
  772. }
  773. //
  774. // // </editor-fold>
  775. //
  776. }
  777. class initfriendInfoModel extends Object_ext {
  778. public $uid;
  779. public $name;
  780. public $level;
  781. public $img;
  782. public $imgBorderId;
  783. public $guildName;
  784. public $guildId;
  785. public $cupScore;
  786. function __construct($tag, $uid, $name, $level, $img, $imgBorderId, $guildName, $guildId, $cupScore) {
  787. if (1 == func_num_args() && $tag) { # 默认的Object初始化方法
  788. parent::__construct($tag);
  789. } else {
  790. $this->uid = $uid;
  791. $this->name = $name;
  792. $this->level = $level;
  793. $this->img = $img;
  794. $this->imgBorderId = $imgBorderId;
  795. $this->guildName = $guildName;
  796. $this->guildId = $guildId;
  797. $this->cupScore = $cupScore;
  798. }
  799. }
  800. }