UserProc.php 29 KB

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