UserProc.php 27 KB

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