UserProc.php 21 KB

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