UserProc.php 24 KB

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