UserProc.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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. UserProc::updateUserInfo(); # 这一步回存操作只有在 userInfo正常存在的情况下才进行
  260. TaskProc::OnLogin_Daily();
  261. TaskProc::OnLogin_day7();
  262. FightProc::Ranking_FightPower();
  263. return $resp;
  264. } else { # 2.如果玩家已存在,则处理普通登录流程
  265. req()->game = $game; # 给Req挂载玩家数据
  266. $game->base()->Reset_tilits(); # 修正体力ts
  267. UserProc::checkContidays(); # 连续登录,状态检查
  268. //PayProc::m_refreshChargeOrders(); # 刷新订单, 多平台版本
  269. //PayProc::checkDeltest(); # 检查内侧充值记录(函数内部会只检查一次)
  270. //self::checkMissOrder(); #校验是否有漏单
  271. $game->RegenNewToken();
  272. UserProc::updateUserInfo(); # 这一步回存操作只有在 userInfo正常存在的情况下才进行
  273. FightProc::Ranking_FightPower();
  274. TaskProc::OnLogin_Daily();
  275. TaskProc::OnLogin_day7();
  276. $resp = Resp::ok($game); # 设置返回值
  277. self::updtateUserZoneInfo(); # 1. 更新玩家分区记录
  278. }
  279. return $resp;
  280. }
  281. //
  282. // <editor-fold defaultstate="collapsed" desc=" 辅助方法 ">
  283. /**
  284. * 检查昵称是否已经存在
  285. * @param string $roleName
  286. * @return boolean
  287. */
  288. static function checkRoleNameNotExist($roleName) {
  289. return true; # 不再检查昵称重复
  290. static $sqlFormat = "SELECT count(*) as rows FROM `tab_rolename` WHERE roleName='%s';";
  291. $sql = sprintf($sqlFormat, $roleName);
  292. $n = daoInst()->query($sql)->fetch();
  293. return $n->rows <= 0;
  294. }
  295. /**
  296. * 插入玩家新角色
  297. *
  298. * @param string $zoneid
  299. * @param string $userID
  300. * @param string $nickname
  301. * @param string $gender
  302. * @param string $profile_img
  303. * @param string $plat
  304. */
  305. static function regRole($zoneid, $userID, $nickname, $gender, $profile_img, $plat) {
  306. return daoInst()->insert('tab_rolename')
  307. ->data(array(
  308. 'zoneid' => $zoneid,
  309. 'userID' => $userID,
  310. 'roleName' => $nickname,
  311. 'gender' => $gender,
  312. 'profile' => $profile_img,
  313. 'plat' => $plat
  314. ))->exec();
  315. }
  316. /**
  317. *
  318. * @param type $zoneid
  319. * @param type $userID
  320. * @param type $nickname
  321. * @param type $gender
  322. * @param type $profile_img
  323. * @param type $plat
  324. * @return type
  325. */
  326. static function delRegRole($zoneid, $userID, $nickname) {
  327. return daoInst()->del('tab_rolename')
  328. ->data(array(
  329. 'zoneid' => $zoneid,
  330. 'userID' => $userID,
  331. 'roleName' => $nickname,
  332. ))->exec();
  333. }
  334. /**
  335. * 检测连续登录状态,重置必要字段[时间戳自动记录]
  336. */
  337. static function checkContidays($isnew = 0) {
  338. $ret = TimeUtil::totalDays() - TimeUtil::totalDays(ctx()->baseInfo->lastLogin); // 对比登录日期
  339. if ($ret > 0 || $isnew) { # 当天第一次登录
  340. self::OnNewDay($isnew);
  341. } else { # 更新下登录次数记录(任务计数器)
  342. }
  343. if ($ret == 1) { # 连续登录
  344. } else if ($ret >= 2) { # 隔天登录
  345. }
  346. ctx()->baseInfo->lastLogin = now(); # 更新下访问时间
  347. //TapDBUtil::SOnUserLogin(); # 向tapdb上报玩家登录 2023.5.10
  348. return $ret;
  349. }
  350. /**
  351. * 处理当天第一次登录
  352. * @param bool $isnew Description
  353. */
  354. static function OnNewDay($isnew) {
  355. ShopProc::DailyShopItemRand();
  356. ShopProc::ShopDailyClear();
  357. //self::clear();
  358. FightProc::FightDailyClear();
  359. //TaskProc::initAchieveData();
  360. TaskProc::ResetTask();
  361. }
  362. // static function clear() {
  363. // $dic = GameConfig::announcement();
  364. // foreach ($dic as $mo) {
  365. // if(now() > $mo->endTs && in_array($mo->id,ctx()->privateState->announcement)){
  366. // StlUtil::arrayRemove(ctx()->privateState->announcement, $mo->id);
  367. // }
  368. // }
  369. //
  370. // }
  371. // <editor-fold defaultstate="collapsed" desc="创建新用户">
  372. /**
  373. * 创建用户
  374. * @return Data_UserGame
  375. */
  376. static function createUser($rolename) {
  377. $game = new Data_UserGame();
  378. req()->game = $game; # 更新Req挂载的玩家数据,
  379. $game->initialize(); # 初始化玩家数据
  380. $game->baseInfo->name = $rolename;
  381. //$game->baseInfo->headImg = "";
  382. $game->baseInfo->firstLogin = now();
  383. #Ps 6006是没有获得到Userinfo到Req中的
  384. UserProc::checkContidays(1); # 每日状态检查
  385. // UserProc::fetchFromInteract($mem, $req); # 从interact拉取数据,Ps.初始化的过程应该还拉取不到什么有效数据
  386. UserProc::updateUserInfo(); # 回存用户数据
  387. return $game;
  388. }
  389. /**
  390. * 整理平台玩家记录集
  391. * @param int $isnew
  392. */
  393. private static function updatePlatUserRecord($isnew = 0) {
  394. $zoneid = req()->zoneid;
  395. $uid = req()->uid;
  396. $user = ctx()->baseInfo;
  397. $day = totalDays();
  398. $level = $user->level;
  399. $platUser = ObjectInit();
  400. $platUser->uid = $uid; #
  401. $platUser->name = $user->name; #
  402. $platUser->level = $level;
  403. $platUser->img = $user->headImg; # 头像字段
  404. $platUser->cash = $user->cash;
  405. $platUser->gold = $user->gold;
  406. $platUser->tili = $user->tili;
  407. $platUser->ts = now();
  408. $platUser->isnew = $isnew;
  409. gMem()->delete(MemKey_GameRun::DailyLoginUser_byUID_hash($zoneid, $day - 108));
  410. gMem()->hset(MemKey_GameRun::DailyLoginUser_byUID_hash($zoneid), $uid, $platUser);
  411. gMem()->delete(MemKey_GameRun::DailyLoginUser_byLevel_hash($zoneid, $level, $day - 108));
  412. gMem()->hset(MemKey_GameRun::DailyLoginUser_byLevel_hash($zoneid, $level), $uid, $platUser);
  413. }
  414. // </editor-fold>
  415. //
  416. // <editor-fold defaultstate="collapsed" desc="读写玩家数据">
  417. /**
  418. * 取玩家数据
  419. * @param type $zoneid
  420. * @param type $uid
  421. * @return Data_UserGame
  422. */
  423. public static function getUserGame($zoneid, $uid) {
  424. $key = MemKey_User::Info_hash($zoneid, $uid);
  425. // $pf = req()->getPlatStr();
  426. // if ($pf == "tap") { # taptap平台
  427. // $oldkey = MemKey_User::Info_hash($zoneid, req()->getPlatOid());
  428. // if (gMem()->exists($oldkey)) {
  429. // gMem()->rename($oldkey, $key); # 做下数据迁移
  430. // }
  431. // }
  432. $a = new Data_UserGame();
  433. if (null == $a->readDataFromMem($key)) { # ps.下面这一段代码和经常删号会有冲突,因此关闭了 --gwang 2022.2.28
  434. $collection = "userInfoBack";
  435. $cursor = gMongo()->find($collection, ['key' => $key], ['sort' => array('ts' => -1), 'limit' => 1]); # 提取备份数据
  436. $cursor->rewind();
  437. if ($cursor->valid()) {
  438. $v = $cursor->current();
  439. $a->LoadFrom($v->value); # 加载
  440. $a->updateDataFull($key); # 反向写回redis
  441. } else {
  442. return null;
  443. }
  444. }
  445. return new Data_UserGame($a);
  446. }
  447. /**
  448. * 更新用户数据(设置标志位,最后统一更新-gwang 2017.07.18)
  449. */
  450. public static function updateUserInfo() {
  451. my_Assert(req(), "req()为空");
  452. my_Assert(req()->game, "[" . req()->cmd . "] 玩家数据正在被清空!" . req()->uid);
  453. req()->userInfoChanged = TRUE; # 设置回写标志位
  454. }
  455. /**
  456. * 回写玩家数据
  457. * @param Data_UserGame $game
  458. */
  459. public static function setUserInfo($game) {
  460. $OK = false;
  461. if ($game) {
  462. $zoneid = req()->zoneid;
  463. $uid = req()->uid;
  464. $game->baseInfo->lastSaveTs = now();
  465. $key = MemKey_User::Info_hash($zoneid, $uid);
  466. $OK = $game->updateDataFull($key); # 向Redis回写玩家数据
  467. if ($OK) {
  468. // CLog::info($msg);
  469. self::backupUserInfoMongo(); # 向MongoDB备份数据
  470. gMem()->expire($key, 3600); # 设置过期时间1小时
  471. } else {
  472. // redo Logic
  473. CLog::err("写入数据时版本已过期!!!");
  474. }
  475. }
  476. return $OK;
  477. }
  478. // </editor-fold>
  479. //
  480. // <editor-fold defaultstate="collapsed" desc="玩家分区记录">
  481. /**
  482. * 读取玩家的分区记录
  483. * @return Data_UserZoneInfo Description
  484. */
  485. public static function getUserZoneInfo() {
  486. $ret = gMem()->get(MemKey_User::Union_PlayedZoneInfo_normal(req()->uid));
  487. return $ret;
  488. }
  489. /**
  490. * 更新玩家分区记录
  491. */
  492. public static function updtateUserZoneInfo() {
  493. $req = req();
  494. $userZoneInfo = self::getUserZoneInfo(); # 取玩家分区记录
  495. if (!$userZoneInfo) {
  496. $userZoneInfo = new Data_UserZoneInfo;
  497. }
  498. $level = 0;
  499. $zoneid = $req->zoneid;
  500. $playerName = "";
  501. $headImg = "";
  502. if (null != ctx()) { # 防御确保玩家数据不为空
  503. $level = ctx()->baseInfo->level;
  504. $playerName = ctx()->baseInfo->name;
  505. $headImg = ctx()->baseInfo->headImg;
  506. } else {
  507. Err('玩家数据为空!' . __CLASS__ . '.' . __FUNCTION__);
  508. }
  509. if (is_null($level)) {
  510. $level = 0;
  511. }
  512. $userZoneInfo->lastZone = new Ins_ZoneInfo($zoneid, $level, $playerName, $headImg); # 更新玩家分区记录
  513. $userZoneInfo->playedZones->$zoneid = $userZoneInfo->lastZone; # 玩过的分区集合
  514. gMem()->set(MemKey_User::Union_PlayedZoneInfo_normal($req->uid), $userZoneInfo); # 回写数据
  515. }
  516. // </editor-fold>
  517. // <editor-fold defaultstate="collapsed" desc=" 用户数据备份 ">
  518. /**
  519. * 备份玩家数据,(玩家数据落地),一天一份,当天记为最后一次登录时的状态而非最后一次操作的状态
  520. * @history
  521. * version 3.0.13 mysql版备份玩家数据, (性能优化后表现还不错)
  522. * 除非用脚本在redis中实现备份,否则直接写入mysql.
  523. * 主要是mysql版本利用存储过程之后性能已经和redis版相差不多.
  524. * version 2.0.0 Redis storage 从MySQL转到Redis中存储. 这个备份数据没有大规模导出硬盘的需求
  525. * 因为mysql版本实在是负载太大了, 上百毫秒. 所以改在redis中了.
  526. * version 1.0.0 Mysql storagef 1. 分表不做了, 2. 存储过程不用了 3. 先能用再说
  527. */
  528. public static function backupUserInfo() {
  529. $tsday = TimeUtil::totalDays(); # 精确到天,保留10个最近记录.
  530. $value = base64_encode(gzdeflate(JsonUtil::encode(ctx()))); # blob数据,序列化下
  531. $sql = sprintf("call insertUserInfo('%s', %d, %d, '%s');", # # 所以直接将序列化后的结果存进去吧,
  532. req()->uid, req()->zoneid, $tsday, $value); # zoneid, uid, tsday
  533. daoInst()->exec($sql); # 也可以用exec($sql)
  534. }
  535. /**
  536. * 备份玩家数据,(玩家数据落地),一天一份,当天记为最后一次登录时的状态而非最后一次操作的状态
  537. * @history
  538. * version 4.0.0 切换到MongoDB存储
  539. * version 3.0.13 mysql版备份玩家数据, (性能优化后表现还不错)
  540. * 除非用脚本在redis中实现备份,否则直接写入mysql.
  541. * 主要是mysql版本利用存储过程之后性能已经和redis版相差不多.
  542. * version 2.0.0 Redis storage 从MySQL转到Redis中存储. 这个备份数据没有大规模导出硬盘的需求
  543. * 因为mysql版本实在是负载太大了, 上百毫秒. 所以改在redis中了.
  544. * version 1.0.0 Mysql storagef 1. 分表不做了, 2. 存储过程不用了 3. 先能用再说
  545. */
  546. public static function backupUserInfoMongo() {
  547. $collectionName = "userInfoBack"; # 表名
  548. $key = MemKey_User::Info_hash(req()->zoneid, req()->uid);
  549. $doc = array('key' => $key, # # 最新文档
  550. 'ts' => TimeUtil::dtCurrent(), # # 更新时间
  551. 'stVer' => ctx()->stVer, # # 增加版本号
  552. 'value' => ctx()); # 玩家数据
  553. $bok = gMongo()->insert($collectionName, $doc); # 插入备份
  554. // CLog::err($collectionName . "备份玩家数据" . req()->uid . ($bok ? "成功" : "失败"));
  555. if (ctx()->stVer % 100 == 1) { # 每100条记录清理一次(delete耗时比较长-gwang.2023年3月10日)
  556. $delFilter = array('key' => $key, 'stVer' => ['$lt' => ctx()->stVer - 100]); # 保留最后一百次变更记录
  557. gMongo()->delete($collectionName, $delFilter);
  558. }
  559. // $filter = array('key' => $key); # 指定条件
  560. // gMongo()->update($collectionName, $filter, $doc, true); # 更新
  561. }
  562. // </editor-fold>
  563. //</editor-fold>
  564. //
  565. }