UserProc.php 21 KB

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