UserProc.php 22 KB

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