UserProc.php 21 KB

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