UserProc.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. <?php
  2. namespace loyalsoft;
  3. require_once __DIR__ . '/../service_call/pay/official/pay_op.php';
  4. /**
  5. * Description of UserProc
  6. * 玩家数据处理流程
  7. *
  8. */
  9. class UserProc {
  10. const role_Table = 'tab_rolename';
  11. /**
  12. * 逻辑分发
  13. * 所有的Proc中必须有这样一个方法
  14. * @param Req $req
  15. */
  16. public static function procMain($req) {
  17. switch ($req->cmd) {
  18. case CmdCode::cmd_user_getzonelist: # 6000 分区列表
  19. return UserProc::GetZoneList();
  20. case CmdCode::cmd_user_loginuserinfo: # 6001 登录/新玩家直接注册并登录
  21. return UserProc::loginUserInfo();
  22. case CmdCode::cmd_user_gameconstinfo: # 6002 下载游戏配置
  23. return UserProc::downloadConstInfo();
  24. case CmdCode::cmd_user_setAnimation: # 6004 片头播放记录
  25. return UserProc::setAnimation();
  26. case CmdCode::cmd_user_replaceHeadImg: # 6005 替换头像
  27. return UserProc::replaceHeadImg();
  28. case CmdCode::cmd_user_delUserUid: # 6006 删除账号
  29. return UserProc::delUserUid();
  30. case CmdCode::cmd_user_removeNewHeadImgTip: # 6007 移除新头像标志
  31. return UserProc::removeNewHeadImgTip();
  32. case CmdCode::cmd_user_readAnnouncement: # 6008 读公告记录
  33. return UserProc::readAnnouncement();
  34. case CmdCode::cmd_user_clearFunUnlockInfo: # 6009 重置功能解锁记录信息
  35. return UserProc::clearFunUnlockInfo();
  36. default:
  37. Err(ErrCode::cmd_err);
  38. }
  39. }
  40. /**
  41. * 6009 重置功能解锁记录信息
  42. */
  43. public static function clearFunUnlockInfo() {
  44. list($type,$id) = req()->paras;
  45. if($type == 1 && in_array($id,ctx()->privateState->funUnlockRecord)){
  46. StlUtil::arrayRemove(ctx()->privateState->funUnlockRecord,$id);
  47. } elseif ($type == 2 && in_array($id,ctx()->privateState->skillUnlockRecord)) {
  48. StlUtil::arrayRemove(ctx()->privateState->skillUnlockRecord,$id);
  49. } elseif ($type == 3) {
  50. ctx()->privateState->oldLevel = 0;
  51. ctx()->privateState->upLevel = 0;
  52. }
  53. UserProc::updateUserInfo();
  54. return Resp::ok(array());
  55. }
  56. /**
  57. * 6008 读公告记录
  58. */
  59. public static function readAnnouncement() {
  60. list($id) = req()->paras;
  61. if (!in_array($id, ctx()->privateState->announcement_drawed)) {
  62. ctx()->privateState->announcement_drawed[] = $id;
  63. }
  64. UserProc::updateUserInfo();
  65. return Resp::ok(array());
  66. }
  67. /**
  68. * 6007 移除新头像标志
  69. */
  70. public static function removeNewHeadImgTip() {
  71. //list($gateId) = req()->paras;
  72. $dic = ctx()->heros->Dic;
  73. foreach ($dic as $heroId => $ins_Hero) {
  74. if ($ins_Hero->isUnlock == 1 && $ins_Hero->isNewHeadImgTip == 1) {
  75. ctx()->heros->Dic->$heroId->isNewHeadImgTip = 0;
  76. }
  77. }
  78. UserProc::updateUserInfo();
  79. return Resp::ok(array());
  80. }
  81. /**
  82. * 6006 删除账号
  83. * @return type
  84. */
  85. public static function delUserUid() {
  86. $mem = gMem();
  87. $list = self::GetUserDataKeys(req()->uid, req()->zoneid); # 玩家数据key
  88. foreach ($list as $key) {
  89. if ($mem->exists($key)) {
  90. $mem->delete($key);
  91. }
  92. }
  93. // <editor-fold defaultstate="collapsed" desc="清理mongodb中的数据">
  94. self::deleteUserMapData(req()->uid, req()->zoneid);
  95. // </editor-fold>
  96. FightProc::DeleteRankInvalidUser(req()->uid);
  97. self::delRegRole(req()->zoneid, req()->uid, ctx()->baseInfo->name);
  98. $ret = array();
  99. return Resp::ok($ret);
  100. }
  101. private static function GetUserDataKeys($uid, $zoneid) {
  102. $list = array();
  103. $zoneKey = MemKey_User::Union_PlayedZoneInfo_normal($uid); # 分区信息
  104. $list[] = $zoneKey;
  105. $publicKey = MemKey_User::Union_PublicState_hash($uid); # 公共信息
  106. $list[] = $publicKey;
  107. $gameInfoKey = MemKey_User::Info_hash($zoneid, $uid); # 游戏数据主体
  108. $list[] = $gameInfoKey;
  109. $CurIdKey = MemKey_User::Mail_CurId_int($zoneid, $uid); # 当前邮件编号
  110. $list[] = $CurIdKey;
  111. $SysRecordKey = MemKey_User::Mail_SysRecord_set($zoneid, $uid); # 当前已经领取过的系统邮件记录
  112. $list[] = $SysRecordKey;
  113. $QueueKey = MemKey_User::Mail_Queue_hash($zoneid, $uid); # 邮件列表
  114. $list[] = $QueueKey;
  115. return $list;
  116. }
  117. /**
  118. * 删除账号-区别内外网
  119. * @param type $uid
  120. * @param type $type
  121. */
  122. static public function deleteUserMapData($uid, $zoneid) {
  123. gMongo()->delete("playerMapInfo", array('Uid' => $uid, 'ZoneId' => intval($zoneid))); # 地图
  124. gMongo()->delete("PlayerInfo", array('Uid' => $uid, 'ZoneId' => intval($zoneid)));
  125. gMongo()->delete("userInfoBack", array('key' => MemKey_User::Info_hash($zoneid, $uid)));
  126. }
  127. /**
  128. * 6005 替换头像
  129. * @return type
  130. */
  131. public static function replaceHeadImg() {
  132. list($img) = req()->paras;
  133. ctx()->baseInfo->headImg = $img;
  134. FightProc::UpdateRankUserHeadImg(req()->uid, $img);
  135. UserProc::updateUserInfo();
  136. $ret = array();
  137. return Resp::ok($ret);
  138. }
  139. /**
  140. * 6004 设置片头播放记录
  141. * @return type
  142. */
  143. public static function setAnimation() {
  144. list($tag) = req()->paras;
  145. if (ctx()->baseInfo->animation == 0 && $tag > 0) {
  146. ctx()->baseInfo->animation = 1;
  147. }
  148. UserProc::updateUserInfo();
  149. $ret = array();
  150. return Resp::ok($ret);
  151. }
  152. /**
  153. * 检测遗漏订单
  154. */
  155. static function checkMissOrder() {
  156. $tableName = "tpl_order_tab";
  157. if (daoInst()->tableExist($tableName)) {
  158. $arr = daoInst()->select("*")->from($tableName)
  159. ->where('uid')->eq(req()->uid)
  160. ->andWhere('zoneid')->eq(req()->zoneid)
  161. ->andWhere('status')->eq(1)
  162. ->andWhere('drawed_ts')->eq(0)
  163. ->fetchAll();
  164. if (count($arr) != null) {
  165. foreach ($arr as $item) {
  166. $result = pay_op::CheckAndDrawOrder(req()->uid, $item->cpOrderId, array(new PayProc, 'distributePayGoods'));
  167. }
  168. }
  169. }
  170. }
  171. /**
  172. * 6016 拉取其他玩家的信息.
  173. */
  174. public static function UserOtherPlayerInfo() {
  175. $zoneId = req()->zoneid;
  176. list($other_uid) = req()->paras;
  177. $g = UserProc::getUserGame($zoneId, $other_uid);
  178. my_Assert(null != $g, ErrCode::user_no_err); # 找不到指定的玩家数据
  179. return Resp::ok($g);
  180. }
  181. /**
  182. * 6000 【移动端】 获取分区列表
  183. */
  184. public static function GetZoneList() {
  185. $defaultZone = new Ins_ZoneInfo(1, 0, ""); # 新用户默认分区
  186. $bGetRecommended = false;
  187. if (count(req()->paras) > 0) { # 是否只拉取推荐分区
  188. $bGetRecommended = req()->paras[0];
  189. }
  190. $zoneList = array();
  191. $ts = now();
  192. // foreach (GameConfig::zonelist() as $zoneid => $zone) {
  193. // isEditor() and $zone = new \sm_zonelist();
  194. // if ($zone->publicTs > $ts) {
  195. // continue;
  196. // }
  197. // $zone->zoneid = $zoneid; # 把zoneid塞进zone数据结构中
  198. // if ($bGetRecommended) {
  199. // if ($zone->isRecommended > 0 && $zone->status == 1) {
  200. // $zoneList[] = $zone;
  201. // } else {
  202. //
  203. // }
  204. // } else {
  205. // $zoneList[] = $zone;
  206. // }
  207. // unset($zone->isRecommended);
  208. // }
  209. // UserProc::_AddTesterZonelist($zoneList); # 添加测试分区
  210. #
  211. // <editor-fold defaultstate="collapsed" desc=" 取玩家分区记录 ">
  212. $userZoneInfo = self::getUserZoneInfo(); # 玩家分区记录
  213. $isNewUser = false;
  214. if ($userZoneInfo == null) { // 这里使用推荐分区的数据,推荐分区信息,在后台编辑,编辑器可用
  215. $userZoneInfo = new Data_UserZoneInfo();
  216. $userZoneInfo->lastZone = $defaultZone; # 新用户导向默认分区
  217. $isNewUser = true;
  218. } else { # 转换一下格式,去掉key,只保留value的集合
  219. // $userZoneInfo->playedZones = ArrayInit();
  220. // array_merge($userZoneInfo->playedZones, array_values((array) $userZoneInfo->playedZones));
  221. }
  222. // </editor-fold>
  223. $ret = array(
  224. 'isNewUser' => $isNewUser,
  225. 'zonelist' => json_decode(json_encode($zoneList)),
  226. 'userZoneInfo' => $userZoneInfo
  227. );
  228. return Resp::ok($ret); # 返回值
  229. }
  230. private static function _AddTesterZonelist(&$zoneList) {
  231. if (config::Inst()->isTester(req()->uid)) { # 添加测试分区
  232. $zoneList[] = array('zoneid' => 999, 'name' => '内测专区', 'status' => 2, 'publicTs' => 0);
  233. }
  234. }
  235. /**
  236. * 6002 客户端下载常量配置信息
  237. * @return type
  238. */
  239. public static function downloadConstInfo() {
  240. list($clientDataVer) = req()->paras; # 客户端数据版本号,程序版本号
  241. $serverVer = GameConfig::ver(); # 最新数据版本号
  242. my_Assert($serverVer, ErrCode::err_const_no); # 找不到常量数据
  243. $url = config::CDN_host() . "/cfg/" . req()->CV . "/Client.bytes?" . $serverVer;
  244. $ret = array(
  245. 'version' => $serverVer,
  246. 'url' => $clientDataVer == $serverVer ? "" : $url, # # 如果版本一致,url传空,只传回版本号
  247. 'data' => null);
  248. return Resp::ok($ret);
  249. // $md5 = md5(json_encode($constInfo)); # 计算MD5值,多余计算md5
  250. // $constInfo = GameConfig::client(); # 取出来的已经是base64过的压缩数据
  251. // my_Assert($constInfo, ErrCode::err_const_no); # 找不到配置数据
  252. }
  253. /**
  254. * 6001 客户端登录并返还玩家信息
  255. * @return Resp
  256. */
  257. public static function loginUserInfo() {
  258. $game = UserProc::getUserGame(req()->zoneid, req()->uid);
  259. if ($game == null) { # 新用户, -> 6006创建账号
  260. $userID = req()->uid;
  261. list($nickName) = req()->paras;
  262. $id = gMem()->increment(MemKey_GameRun::Stat_UserCountByZone_int(req()->zoneid)); # 增加玩家数量计数
  263. $rolename = "No." . sprintf("%03d", req()->zoneid) . sprintf("%07d", $id); # 生成编号
  264. $rolename = $nickName; # 采用客户端传过来的值创建账号, 2024.6.24
  265. if (self::checkRoleNameNotExist($rolename)) { # 记录玩家
  266. $game = self::createUser($rolename);
  267. if (1 == self::regRole(req()->zoneid, $userID, $rolename, "", "", "")) {
  268. $resp = Resp::ok($game);
  269. self::updtateUserZoneInfo();
  270. } else {
  271. $resp = Resp::err(ErrCode::err_db);
  272. }
  273. } else { # 昵称已存在
  274. $resp = Resp::ok(array('ret' => '用户已存在.'));
  275. }
  276. $game->RegenNewToken();
  277. $game->baseInfo->Reset_tilits();
  278. self::OnLogin_DateDeal();
  279. UserProc::updateUserInfo(); # 这一步回存操作只有在 userInfo正常存在的情况下才进行
  280. return $resp;
  281. } else { # 2.如果玩家已存在,则处理普通登录流程
  282. req()->game = $game; # 给Req挂载玩家数据
  283. $game->base()->Reset_tilits(); # 修正体力ts
  284. UserProc::checkContidays(); # 连续登录,状态检查
  285. //PayProc::m_refreshChargeOrders(); # 刷新订单, 多平台版本
  286. //PayProc::checkDeltest(); # 检查内侧充值记录(函数内部会只检查一次)
  287. //self::checkMissOrder(); #校验是否有漏单
  288. $game->RegenNewToken();
  289. UserProc::updateUserInfo(); # 这一步回存操作只有在 userInfo正常存在的情况下才进行
  290. self::OnLogin_DateDeal();
  291. $resp = Resp::ok($game); # 设置返回值
  292. self::updtateUserZoneInfo(); # 1. 更新玩家分区记录
  293. }
  294. return $resp;
  295. }
  296. //
  297. // <editor-fold defaultstate="collapsed" desc=" 辅助方法 ">
  298. /**
  299. * 检查昵称是否已经存在
  300. * @param string $roleName
  301. * @return boolean
  302. */
  303. static function checkRoleNameNotExist($roleName) {
  304. return true; # 不再检查昵称重复
  305. static $sqlFormat = "SELECT count(*) as rows FROM `tab_rolename` WHERE roleName='%s';";
  306. $sql = sprintf($sqlFormat, $roleName);
  307. $n = daoInst()->query($sql)->fetch();
  308. return $n->rows <= 0;
  309. }
  310. /**
  311. * 插入玩家新角色
  312. *
  313. * @param string $zoneid
  314. * @param string $userID
  315. * @param string $nickname
  316. * @param string $gender
  317. * @param string $profile_img
  318. * @param string $plat
  319. */
  320. static function regRole($zoneid, $userID, $nickname, $gender, $profile_img, $plat) {
  321. return daoInst()->insert('tab_rolename')
  322. ->data(array(
  323. 'zoneid' => $zoneid,
  324. 'userID' => $userID,
  325. 'roleName' => $nickname,
  326. 'gender' => $gender,
  327. 'profile' => $profile_img,
  328. 'plat' => $plat
  329. ))->exec();
  330. }
  331. /**
  332. *
  333. * @param type $zoneid
  334. * @param type $userID
  335. * @param type $nickname
  336. * @param type $gender
  337. * @param type $profile_img
  338. * @param type $plat
  339. * @return type
  340. */
  341. static function delRegRole($zoneid, $userID, $nickname) {
  342. return daoInst()->del('tab_rolename')
  343. ->data(array(
  344. 'zoneid' => $zoneid,
  345. 'userID' => $userID,
  346. 'roleName' => $nickname,
  347. ))->exec();
  348. }
  349. /**
  350. * 检测连续登录状态,重置必要字段[时间戳自动记录]
  351. */
  352. static function checkContidays($isnew = 0) {
  353. $ret = TimeUtil::totalDays() - TimeUtil::totalDays(ctx()->baseInfo->lastLogin); // 对比登录日期
  354. if ($ret > 0 || $isnew) { # 当天第一次登录
  355. self::OnNewDay($isnew);
  356. } else { # 更新下登录次数记录(任务计数器)
  357. }
  358. if ($ret == 1) { # 连续登录
  359. } else if ($ret >= 2) { # 隔天登录
  360. }
  361. ctx()->baseInfo->lastLogin = now(); # 更新下访问时间
  362. //TapDBUtil::SOnUserLogin(); # 向tapdb上报玩家登录 2023.5.10
  363. return $ret;
  364. }
  365. /**
  366. * 处理当天第一次登录
  367. * @param bool $isnew Description
  368. */
  369. static function OnNewDay($isnew) {
  370. ShopProc::DailyShopItemRand();
  371. ShopProc::ShopDailyClear();
  372. //self::clear();
  373. FightProc::FightDailyClear();
  374. //TaskProc::initAchieveData();
  375. TaskProc::ResetTask();
  376. }
  377. static function OnLogin_DateDeal() {
  378. EmailProc::refreshSysMail(req()->zoneid, req()->uid);
  379. EmailProc::IsExistRedTip();
  380. FightProc::isExistNoDrawed_FightPower();
  381. FightProc::isExistNoDrawed_MainGate();
  382. FightProc::Ranking_FightPower();
  383. TaskProc::OnLogin_Daily();
  384. TaskProc::OnLogin_day7();
  385. }
  386. // static function clear() {
  387. // $dic = GameConfig::announcement();
  388. // foreach ($dic as $mo) {
  389. // if(now() > $mo->endTs && in_array($mo->id,ctx()->privateState->announcement)){
  390. // StlUtil::arrayRemove(ctx()->privateState->announcement, $mo->id);
  391. // }
  392. // }
  393. //
  394. // }
  395. // <editor-fold defaultstate="collapsed" desc="创建新用户">
  396. /**
  397. * 创建用户
  398. * @return Data_UserGame
  399. */
  400. static function createUser($rolename) {
  401. $game = new Data_UserGame();
  402. req()->game = $game; # 更新Req挂载的玩家数据,
  403. $game->initialize(); # 初始化玩家数据
  404. $game->baseInfo->name = $rolename;
  405. //$game->baseInfo->headImg = "";
  406. $game->baseInfo->firstLogin = now();
  407. #Ps 6006是没有获得到Userinfo到Req中的
  408. UserProc::checkContidays(1); # 每日状态检查
  409. // UserProc::fetchFromInteract($mem, $req); # 从interact拉取数据,Ps.初始化的过程应该还拉取不到什么有效数据
  410. UserProc::updateUserInfo(); # 回存用户数据
  411. return $game;
  412. }
  413. /**
  414. * 整理平台玩家记录集
  415. * @param int $isnew
  416. */
  417. private static function updatePlatUserRecord($isnew = 0) {
  418. $zoneid = req()->zoneid;
  419. $uid = req()->uid;
  420. $user = ctx()->baseInfo;
  421. $day = totalDays();
  422. $level = $user->level;
  423. $platUser = ObjectInit();
  424. $platUser->uid = $uid; #
  425. $platUser->name = $user->name; #
  426. $platUser->level = $level;
  427. $platUser->img = $user->headImg; # 头像字段
  428. $platUser->cash = $user->cash;
  429. $platUser->gold = $user->gold;
  430. $platUser->tili = $user->tili;
  431. $platUser->ts = now();
  432. $platUser->isnew = $isnew;
  433. gMem()->delete(MemKey_GameRun::DailyLoginUser_byUID_hash($zoneid, $day - 108));
  434. gMem()->hset(MemKey_GameRun::DailyLoginUser_byUID_hash($zoneid), $uid, $platUser);
  435. gMem()->delete(MemKey_GameRun::DailyLoginUser_byLevel_hash($zoneid, $level, $day - 108));
  436. gMem()->hset(MemKey_GameRun::DailyLoginUser_byLevel_hash($zoneid, $level), $uid, $platUser);
  437. }
  438. // </editor-fold>
  439. //
  440. // <editor-fold defaultstate="collapsed" desc="读写玩家数据">
  441. /**
  442. * 取玩家数据
  443. * @param type $zoneid
  444. * @param type $uid
  445. * @return Data_UserGame
  446. */
  447. public static function getUserGame($zoneid, $uid) {
  448. $key = MemKey_User::Info_hash($zoneid, $uid);
  449. // $pf = req()->getPlatStr();
  450. // if ($pf == "tap") { # taptap平台
  451. // $oldkey = MemKey_User::Info_hash($zoneid, req()->getPlatOid());
  452. // if (gMem()->exists($oldkey)) {
  453. // gMem()->rename($oldkey, $key); # 做下数据迁移
  454. // }
  455. // }
  456. $a = new Data_UserGame();
  457. if (null == $a->readDataFromMem($key)) { # ps.下面这一段代码和经常删号会有冲突,因此关闭了 --gwang 2022.2.28
  458. $collection = "userInfoBack";
  459. $cursor = gMongo()->find($collection, ['key' => $key], ['sort' => array('ts' => -1), 'limit' => 1]); # 提取备份数据
  460. $cursor->rewind();
  461. if ($cursor->valid()) {
  462. $v = $cursor->current();
  463. $a->LoadFrom($v->value); # 加载
  464. $a->updateDataFull($key); # 反向写回redis
  465. } else {
  466. return null;
  467. }
  468. }
  469. return new Data_UserGame($a);
  470. }
  471. /**
  472. * 更新用户数据(设置标志位,最后统一更新-gwang 2017.07.18)
  473. */
  474. public static function updateUserInfo() {
  475. my_Assert(req(), "req()为空");
  476. my_Assert(req()->game, "[" . req()->cmd . "] 玩家数据正在被清空!" . req()->uid);
  477. req()->userInfoChanged = TRUE; # 设置回写标志位
  478. }
  479. /**
  480. * 回写玩家数据
  481. * @param Data_UserGame $game
  482. */
  483. public static function setUserInfo($game) {
  484. $OK = false;
  485. if ($game) {
  486. $zoneid = req()->zoneid;
  487. $uid = req()->uid;
  488. $game->baseInfo->lastSaveTs = now();
  489. $key = MemKey_User::Info_hash($zoneid, $uid);
  490. $OK = $game->updateDataFull($key); # 向Redis回写玩家数据
  491. if ($OK) {
  492. // CLog::info($msg);
  493. self::backupUserInfoMongo(); # 向MongoDB备份数据
  494. gMem()->expire($key, 3600); # 设置过期时间1小时
  495. } else {
  496. // redo Logic
  497. CLog::err("写入数据时版本已过期!!!");
  498. }
  499. }
  500. return $OK;
  501. }
  502. // </editor-fold>
  503. //
  504. // <editor-fold defaultstate="collapsed" desc="玩家分区记录">
  505. /**
  506. * 读取玩家的分区记录
  507. * @return Data_UserZoneInfo Description
  508. */
  509. public static function getUserZoneInfo() {
  510. $ret = gMem()->get(MemKey_User::Union_PlayedZoneInfo_normal(req()->uid));
  511. return $ret;
  512. }
  513. /**
  514. * 更新玩家分区记录
  515. */
  516. public static function updtateUserZoneInfo() {
  517. $req = req();
  518. $userZoneInfo = self::getUserZoneInfo(); # 取玩家分区记录
  519. if (!$userZoneInfo) {
  520. $userZoneInfo = new Data_UserZoneInfo;
  521. }
  522. $level = 0;
  523. $zoneid = $req->zoneid;
  524. $playerName = "";
  525. $headImg = "";
  526. if (null != ctx()) { # 防御确保玩家数据不为空
  527. $level = ctx()->baseInfo->level;
  528. $playerName = ctx()->baseInfo->name;
  529. $headImg = ctx()->baseInfo->headImg;
  530. } else {
  531. Err('玩家数据为空!' . __CLASS__ . '.' . __FUNCTION__);
  532. }
  533. if (is_null($level)) {
  534. $level = 0;
  535. }
  536. $userZoneInfo->lastZone = new Ins_ZoneInfo($zoneid, $level, $playerName, $headImg); # 更新玩家分区记录
  537. $userZoneInfo->playedZones->$zoneid = $userZoneInfo->lastZone; # 玩过的分区集合
  538. gMem()->set(MemKey_User::Union_PlayedZoneInfo_normal($req->uid), $userZoneInfo); # 回写数据
  539. }
  540. // </editor-fold>
  541. // <editor-fold defaultstate="collapsed" desc=" 用户数据备份 ">
  542. /**
  543. * 备份玩家数据,(玩家数据落地),一天一份,当天记为最后一次登录时的状态而非最后一次操作的状态
  544. * @history
  545. * version 3.0.13 mysql版备份玩家数据, (性能优化后表现还不错)
  546. * 除非用脚本在redis中实现备份,否则直接写入mysql.
  547. * 主要是mysql版本利用存储过程之后性能已经和redis版相差不多.
  548. * version 2.0.0 Redis storage 从MySQL转到Redis中存储. 这个备份数据没有大规模导出硬盘的需求
  549. * 因为mysql版本实在是负载太大了, 上百毫秒. 所以改在redis中了.
  550. * version 1.0.0 Mysql storagef 1. 分表不做了, 2. 存储过程不用了 3. 先能用再说
  551. */
  552. public static function backupUserInfo() {
  553. $tsday = TimeUtil::totalDays(); # 精确到天,保留10个最近记录.
  554. $value = base64_encode(gzdeflate(JsonUtil::encode(ctx()))); # blob数据,序列化下
  555. $sql = sprintf("call insertUserInfo('%s', %d, %d, '%s');", # # 所以直接将序列化后的结果存进去吧,
  556. req()->uid, req()->zoneid, $tsday, $value); # zoneid, uid, tsday
  557. daoInst()->exec($sql); # 也可以用exec($sql)
  558. }
  559. /**
  560. * 备份玩家数据,(玩家数据落地),一天一份,当天记为最后一次登录时的状态而非最后一次操作的状态
  561. * @history
  562. * version 4.0.0 切换到MongoDB存储
  563. * version 3.0.13 mysql版备份玩家数据, (性能优化后表现还不错)
  564. * 除非用脚本在redis中实现备份,否则直接写入mysql.
  565. * 主要是mysql版本利用存储过程之后性能已经和redis版相差不多.
  566. * version 2.0.0 Redis storage 从MySQL转到Redis中存储. 这个备份数据没有大规模导出硬盘的需求
  567. * 因为mysql版本实在是负载太大了, 上百毫秒. 所以改在redis中了.
  568. * version 1.0.0 Mysql storagef 1. 分表不做了, 2. 存储过程不用了 3. 先能用再说
  569. */
  570. public static function backupUserInfoMongo() {
  571. $collectionName = "userInfoBack"; # 表名
  572. $key = MemKey_User::Info_hash(req()->zoneid, req()->uid);
  573. $doc = array('key' => $key, # # 最新文档
  574. 'ts' => TimeUtil::dtCurrent(), # # 更新时间
  575. 'stVer' => ctx()->stVer, # # 增加版本号
  576. 'value' => ctx()); # 玩家数据
  577. $bok = gMongo()->insert($collectionName, $doc); # 插入备份
  578. // CLog::err($collectionName . "备份玩家数据" . req()->uid . ($bok ? "成功" : "失败"));
  579. if (ctx()->stVer % 100 == 1) { # 每100条记录清理一次(delete耗时比较长-gwang.2023年3月10日)
  580. $delFilter = array('key' => $key, 'stVer' => ['$lt' => ctx()->stVer - 100]); # 保留最后一百次变更记录
  581. gMongo()->delete($collectionName, $delFilter);
  582. }
  583. // $filter = array('key' => $key); # 指定条件
  584. // gMongo()->update($collectionName, $filter, $doc, true); # 更新
  585. }
  586. // </editor-fold>
  587. //</editor-fold>
  588. //
  589. }