UserProc.php 21 KB

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