UserProc.php 30 KB

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