UserProc.php 26 KB

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