UserProc.php 28 KB

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