UserProc.php 23 KB

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