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. $id = gMem()->increment(MemKey_GameRun::Stat_UserCountByZone_int(req()->zoneid)); # 增加玩家数量计数
  180. $rolename = "No." . sprintf("%03d", req()->zoneid) . sprintf("%07d", $id); # 生成编号
  181. if (self::checkRoleNameNotExist($rolename)) { # 记录玩家
  182. $game = self::createUser($rolename);
  183. if (1 == self::regRole(req()->zoneid, $userID, $rolename, "", "", "")) {
  184. $resp = Resp::ok($game);
  185. self::updtateUserZoneInfo();
  186. } else {
  187. $resp = Resp::err(ErrCode::err_db);
  188. }
  189. } else { # 昵称已存在
  190. $resp = Resp::ok(array('ret' => '用户已存在.'));
  191. }
  192. UserProc::updateUserInfo(); # 这一步回存操作只有在 userInfo正常存在的情况下才进行
  193. TaskProc::OnLogin_Daily();
  194. TaskProc::OnLogin_day7();
  195. FightProc::Ranking_FightPower();
  196. return $resp;
  197. } else { # 2.如果玩家已存在,则处理普通登录流程
  198. req()->game = $game; # 给Req挂载玩家数据
  199. $game->base()->Reset_tilits(); # 修正体力ts
  200. UserProc::checkContidays(); # 连续登录,状态检查
  201. //PayProc::m_refreshChargeOrders(); # 刷新订单, 多平台版本
  202. //PayProc::checkDeltest(); # 检查内侧充值记录(函数内部会只检查一次)
  203. //self::checkMissOrder(); #校验是否有漏单
  204. UserProc::updateUserInfo(); # 这一步回存操作只有在 userInfo正常存在的情况下才进行
  205. FightProc::Ranking_FightPower();
  206. TaskProc::OnLogin_Daily();
  207. TaskProc::OnLogin_day7();
  208. $resp = Resp::ok($game); # 设置返回值
  209. self::updtateUserZoneInfo(); # 1. 更新玩家分区记录
  210. }
  211. return $resp;
  212. }
  213. //
  214. // <editor-fold defaultstate="collapsed" desc=" 辅助方法 ">
  215. /**
  216. * 检查昵称是否已经存在
  217. * @param string $roleName
  218. * @return boolean
  219. */
  220. static function checkRoleNameNotExist($roleName) {
  221. return true; # 不再检查昵称重复
  222. static $sqlFormat = "SELECT count(*) as rows FROM `tab_rolename` WHERE roleName='%s';";
  223. $sql = sprintf($sqlFormat, $roleName);
  224. $n = daoInst()->query($sql)->fetch();
  225. return $n->rows <= 0;
  226. }
  227. /**
  228. * 插入玩家新角色
  229. *
  230. * @param string $zoneid
  231. * @param string $userID
  232. * @param string $nickname
  233. * @param string $gender
  234. * @param string $profile_img
  235. * @param string $plat
  236. */
  237. static function regRole($zoneid, $userID, $nickname, $gender, $profile_img, $plat) {
  238. return daoInst()->insert('tab_rolename')
  239. ->data(array(
  240. 'zoneid' => $zoneid,
  241. 'userID' => $userID,
  242. 'roleName' => $nickname,
  243. 'gender' => $gender,
  244. 'profile' => $profile_img,
  245. 'plat' => $plat
  246. ))->exec();
  247. }
  248. /**
  249. * 检测连续登录状态,重置必要字段[时间戳自动记录]
  250. */
  251. static function checkContidays($isnew = 0) {
  252. $ret = TimeUtil::totalDays() - TimeUtil::totalDays(ctx()->baseInfo->lastLogin); // 对比登录日期
  253. if ($ret > 0 || $isnew) { # 当天第一次登录
  254. self::OnNewDay($isnew);
  255. } else { # 更新下登录次数记录(任务计数器)
  256. }
  257. if ($ret == 1) { # 连续登录
  258. } else if ($ret >= 2) { # 隔天登录
  259. }
  260. ctx()->baseInfo->lastLogin = now(); # 更新下访问时间
  261. //TapDBUtil::SOnUserLogin(); # 向tapdb上报玩家登录 2023.5.10
  262. return $ret;
  263. }
  264. /**
  265. * 处理当天第一次登录
  266. * @param bool $isnew Description
  267. */
  268. static function OnNewDay($isnew) {
  269. ShopProc::DailyShopItemRand();
  270. ShopProc::ShopDailyClear();
  271. //self::clear();
  272. FightProc::FightDailyClear();
  273. //TaskProc::initAchieveData();
  274. TaskProc::ResetTask();
  275. }
  276. // static function clear() {
  277. // $dic = GameConfig::announcement();
  278. // foreach ($dic as $mo) {
  279. // if(now() > $mo->endTs && in_array($mo->id,ctx()->privateState->announcement)){
  280. // StlUtil::arrayRemove(ctx()->privateState->announcement, $mo->id);
  281. // }
  282. // }
  283. //
  284. // }
  285. // <editor-fold defaultstate="collapsed" desc="创建新用户">
  286. /**
  287. * 创建用户
  288. * @return Data_UserGame
  289. */
  290. static function createUser($rolename) {
  291. $game = new Data_UserGame();
  292. req()->game = $game; # 更新Req挂载的玩家数据,
  293. $game->initialize(); # 初始化玩家数据
  294. $game->baseInfo->name = $rolename;
  295. //$game->baseInfo->headImg = "";
  296. $game->baseInfo->firstLogin = now();
  297. #Ps 6006是没有获得到Userinfo到Req中的
  298. UserProc::checkContidays(1); # 每日状态检查
  299. // UserProc::fetchFromInteract($mem, $req); # 从interact拉取数据,Ps.初始化的过程应该还拉取不到什么有效数据
  300. UserProc::updateUserInfo(); # 回存用户数据
  301. return $game;
  302. }
  303. /**
  304. * 整理平台玩家记录集
  305. * @param int $isnew
  306. */
  307. private static function updatePlatUserRecord($isnew = 0) {
  308. $zoneid = req()->zoneid;
  309. $uid = req()->uid;
  310. $user = ctx()->baseInfo;
  311. $day = totalDays();
  312. $level = $user->level;
  313. $platUser = ObjectInit();
  314. $platUser->uid = $uid; #
  315. $platUser->name = $user->name; #
  316. $platUser->level = $level;
  317. $platUser->img = $user->headImg; # 头像字段
  318. $platUser->cash = $user->cash;
  319. $platUser->gold = $user->gold;
  320. $platUser->tili = $user->tili;
  321. $platUser->ts = now();
  322. $platUser->isnew = $isnew;
  323. gMem()->delete(MemKey_GameRun::DailyLoginUser_byUID_hash($zoneid, $day - 108));
  324. gMem()->hset(MemKey_GameRun::DailyLoginUser_byUID_hash($zoneid), $uid, $platUser);
  325. gMem()->delete(MemKey_GameRun::DailyLoginUser_byLevel_hash($zoneid, $level, $day - 108));
  326. gMem()->hset(MemKey_GameRun::DailyLoginUser_byLevel_hash($zoneid, $level), $uid, $platUser);
  327. }
  328. // </editor-fold>
  329. //
  330. // <editor-fold defaultstate="collapsed" desc="读写玩家数据">
  331. /**
  332. * 取玩家数据
  333. * @param type $zoneid
  334. * @param type $uid
  335. * @return Data_UserGame
  336. */
  337. public static function getUserGame($zoneid, $uid) {
  338. $key = MemKey_User::Info_hash($zoneid, $uid);
  339. // $pf = req()->getPlatStr();
  340. // if ($pf == "tap") { # taptap平台
  341. // $oldkey = MemKey_User::Info_hash($zoneid, req()->getPlatOid());
  342. // if (gMem()->exists($oldkey)) {
  343. // gMem()->rename($oldkey, $key); # 做下数据迁移
  344. // }
  345. // }
  346. $a = new Data_UserGame();
  347. if (null == $a->readDataFromMem($key)) { # ps.下面这一段代码和经常删号会有冲突,因此关闭了 --gwang 2022.2.28
  348. $collection = "userInfoBack";
  349. $cursor = gMongo()->find($collection, ['key' => $key], ['sort' => array('ts' => -1), 'limit' => 1]); # 提取备份数据
  350. $cursor->rewind();
  351. if ($cursor->valid()) {
  352. $v = $cursor->current();
  353. $a->LoadFrom($v->value); # 加载
  354. $a->updateDataFull($key); # 反向写回redis
  355. } else {
  356. return null;
  357. }
  358. }
  359. return new Data_UserGame($a);
  360. }
  361. /**
  362. * 更新用户数据(设置标志位,最后统一更新-gwang 2017.07.18)
  363. */
  364. public static function updateUserInfo() {
  365. my_Assert(req(), "req()为空");
  366. my_Assert(req()->game, "[" . req()->cmd . "] 玩家数据正在被清空!" . req()->uid);
  367. req()->userInfoChanged = TRUE; # 设置回写标志位
  368. }
  369. /**
  370. * 回写玩家数据
  371. * @param Data_UserGame $game
  372. */
  373. public static function setUserInfo($game) {
  374. $OK = false;
  375. if ($game) {
  376. $zoneid = req()->zoneid;
  377. $uid = req()->uid;
  378. $game->baseInfo->lastSaveTs = now();
  379. $key = MemKey_User::Info_hash($zoneid, $uid);
  380. $OK = $game->updateDataFull($key); # 向Redis回写玩家数据
  381. if ($OK) {
  382. // CLog::info($msg);
  383. self::backupUserInfoMongo(); # 向MongoDB备份数据
  384. gMem()->expire($key, 3600); # 设置过期时间1小时
  385. } else {
  386. // redo Logic
  387. CLog::err("写入数据时版本已过期!!!");
  388. }
  389. }
  390. return $OK;
  391. }
  392. // </editor-fold>
  393. //
  394. // <editor-fold defaultstate="collapsed" desc="玩家分区记录">
  395. /**
  396. * 读取玩家的分区记录
  397. * @return Data_UserZoneInfo Description
  398. */
  399. public static function getUserZoneInfo() {
  400. $ret = gMem()->get(MemKey_User::Union_PlayedZoneInfo_normal(req()->uid));
  401. return $ret;
  402. }
  403. /**
  404. * 更新玩家分区记录
  405. */
  406. public static function updtateUserZoneInfo() {
  407. $req = req();
  408. $userZoneInfo = self::getUserZoneInfo(); # 取玩家分区记录
  409. if (!$userZoneInfo) {
  410. $userZoneInfo = new Data_UserZoneInfo;
  411. }
  412. $level = 0;
  413. $zoneid = $req->zoneid;
  414. $playerName = "";
  415. $headImg = "";
  416. if (null != ctx()) { # 防御确保玩家数据不为空
  417. $level = ctx()->baseInfo->level;
  418. $playerName = ctx()->baseInfo->name;
  419. $headImg = ctx()->baseInfo->headImg;
  420. } else {
  421. Err('玩家数据为空!' . __CLASS__ . '.' . __FUNCTION__);
  422. }
  423. if (is_null($level)) {
  424. $level = 0;
  425. }
  426. $userZoneInfo->lastZone = new Ins_ZoneInfo($zoneid, $level, $playerName, $headImg); # 更新玩家分区记录
  427. $userZoneInfo->playedZones->$zoneid = $userZoneInfo->lastZone; # 玩过的分区集合
  428. gMem()->set(MemKey_User::Union_PlayedZoneInfo_normal($req->uid), $userZoneInfo); # 回写数据
  429. }
  430. // </editor-fold>
  431. // <editor-fold defaultstate="collapsed" desc=" 用户数据备份 ">
  432. /**
  433. * 备份玩家数据,(玩家数据落地),一天一份,当天记为最后一次登录时的状态而非最后一次操作的状态
  434. * @history
  435. * version 3.0.13 mysql版备份玩家数据, (性能优化后表现还不错)
  436. * 除非用脚本在redis中实现备份,否则直接写入mysql.
  437. * 主要是mysql版本利用存储过程之后性能已经和redis版相差不多.
  438. * version 2.0.0 Redis storage 从MySQL转到Redis中存储. 这个备份数据没有大规模导出硬盘的需求
  439. * 因为mysql版本实在是负载太大了, 上百毫秒. 所以改在redis中了.
  440. * version 1.0.0 Mysql storagef 1. 分表不做了, 2. 存储过程不用了 3. 先能用再说
  441. */
  442. public static function backupUserInfo() {
  443. $tsday = TimeUtil::totalDays(); # 精确到天,保留10个最近记录.
  444. $value = base64_encode(gzdeflate(JsonUtil::encode(ctx()))); # blob数据,序列化下
  445. $sql = sprintf("call insertUserInfo('%s', %d, %d, '%s');", # # 所以直接将序列化后的结果存进去吧,
  446. req()->uid, req()->zoneid, $tsday, $value); # zoneid, uid, tsday
  447. daoInst()->exec($sql); # 也可以用exec($sql)
  448. }
  449. /**
  450. * 备份玩家数据,(玩家数据落地),一天一份,当天记为最后一次登录时的状态而非最后一次操作的状态
  451. * @history
  452. * version 4.0.0 切换到MongoDB存储
  453. * version 3.0.13 mysql版备份玩家数据, (性能优化后表现还不错)
  454. * 除非用脚本在redis中实现备份,否则直接写入mysql.
  455. * 主要是mysql版本利用存储过程之后性能已经和redis版相差不多.
  456. * version 2.0.0 Redis storage 从MySQL转到Redis中存储. 这个备份数据没有大规模导出硬盘的需求
  457. * 因为mysql版本实在是负载太大了, 上百毫秒. 所以改在redis中了.
  458. * version 1.0.0 Mysql storagef 1. 分表不做了, 2. 存储过程不用了 3. 先能用再说
  459. */
  460. public static function backupUserInfoMongo() {
  461. $collectionName = "userInfoBack"; # 表名
  462. $key = MemKey_User::Info_hash(req()->zoneid, req()->uid);
  463. $doc = array('key' => $key, # # 最新文档
  464. 'ts' => TimeUtil::dtCurrent(), # # 更新时间
  465. 'stVer' => ctx()->stVer, # # 增加版本号
  466. 'value' => ctx()); # 玩家数据
  467. $bok = gMongo()->insert($collectionName, $doc); # 插入备份
  468. // CLog::err($collectionName . "备份玩家数据" . req()->uid . ($bok ? "成功" : "失败"));
  469. if (ctx()->stVer % 100 == 1) { # 每100条记录清理一次(delete耗时比较长-gwang.2023年3月10日)
  470. $delFilter = array('key' => $key, 'stVer' => ['$lt' => ctx()->stVer - 100]); # 保留最后一百次变更记录
  471. gMongo()->delete($collectionName, $delFilter);
  472. }
  473. // $filter = array('key' => $key); # 指定条件
  474. // gMongo()->update($collectionName, $filter, $doc, true); # 更新
  475. }
  476. // </editor-fold>
  477. //</editor-fold>
  478. //
  479. }