UserProc.php 32 KB

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