UserProc.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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_acceptcontidaysgift: # 6003 领取连续登陆奖励
  25. return UserProc::acceptContiDaysGift();
  26. case CmdCode::cmd_user_registerNewRole: # 6006 注册新角色
  27. return UserProc::RegisterNewRole();
  28. case CmdCode::cmd_user_completeNewbieGuide: # 6007 提交新手引导步骤
  29. return UserProc::completeNewbieGuide();
  30. // case CmdCode::cmd_user_setNewbieGuideCards: # 6008 发放新手引导所需卡牌
  31. // return self::SetNewbieGuideCards($req);
  32. // case CmdCode::cmd_user_setNewbieGuideOver: # 6009 跳过新手引导
  33. // return UserProc::setNewbieGuideOver();
  34. case CmdCode::cmd_user_setNickname: # 6010 设置/修改玩家昵称
  35. return self::SetUserNickname();
  36. case CmdCode::cmd_user_SetUserHeadImageBorder: # 6011 修改玩家头像框
  37. return self::SetUserImageBorder();
  38. case CmdCode::cmd_user_setUserImage: # 6012 更换玩家形象
  39. return self::SetUserImage();
  40. case CmdCode::cmd_user_changeUserHeadImg: # 6013 更换玩家头像
  41. return self::SetUserHeadImage();
  42. case CmdCode::cmd_user_stat_initvolc: # 6014 初始化火山引擎所需数据
  43. return self::InitVolcData();
  44. case CmdCode::cmd_user_ctxBack: # 6015 收集玩家反馈
  45. return self::UserCtxBack();
  46. case CmdCode::cmd_user_other_info: # 6016 拉取其他玩家信息
  47. return self::UserOtherPlayerInfo();
  48. default:
  49. Err(ErrCode::cmd_err);
  50. }
  51. }
  52. /**
  53. * 检测遗漏订单
  54. */
  55. static function checkMissOrder() {
  56. $tableName = "tpl_order_tab";
  57. if (daoInst()->tableExist($tableName)) {
  58. $arr = daoInst()->select("*")->from($tableName)
  59. ->where('uid')->eq(req()->uid)
  60. ->andWhere('zoneid')->eq(req()->zoneid)
  61. ->andWhere('status')->eq(1)
  62. ->andWhere('drawed_ts')->eq(0)
  63. ->fetchAll();
  64. if (count($arr) != null) {
  65. foreach ($arr as $item) {
  66. $result = pay_op::CheckAndDrawOrder(req()->uid, $item->cpOrderId, array(new PayProc, 'distributePayGoods'));
  67. }
  68. }
  69. }
  70. }
  71. /**
  72. * 6016 拉取其他玩家的信息.
  73. */
  74. public static function UserOtherPlayerInfo() {
  75. $zoneId = req()->zoneid;
  76. list($other_uid) = req()->paras;
  77. $g = UserProc::getUserGame($zoneId, $other_uid);
  78. my_Assert(null != $g, ErrCode::user_no_err); # 找不到指定的玩家数据
  79. return Resp::ok($g);
  80. }
  81. /**
  82. * 6015 收集玩家反馈
  83. */
  84. public static function UserCtxBack() {
  85. $userID = req()->uid;
  86. $zoneid = req()->zoneid;
  87. $name = ctx()->baseInfo->name;
  88. list($ctx, $type, $phoneId) = req()->paras;
  89. $ret = self::insertUserCtxBack($userID, $name, $zoneid, $ctx, $phoneId, $type);
  90. return Resp::ok();
  91. }
  92. /**
  93. * 收集玩家反馈
  94. * @param type $userID
  95. * @param type $zoneid
  96. * @param type $ctx
  97. * @param type $phoneId
  98. * @return type
  99. */
  100. static function insertUserCtxBack($userID, $name, $zoneid, $ctx, $phoneId, $type) {
  101. $paydb = daoInst();
  102. $sql = "INSERT INTO `tab_userctxback` (tid,uid,name,zoneid,ctx,phoneId,type) VALUES (null,'%s','%s',%d,'%s','%s','%s')";
  103. $query = sprintf($sql, $userID, $name, $zoneid, $ctx, $phoneId, $type);
  104. $result = $paydb->exec($query);
  105. return $result;
  106. }
  107. /**
  108. * 6014 初始化火山引擎所需数据
  109. */
  110. public static function InitVolcData() {
  111. list($app_name, $app_package, $app_channel, $app_version, $os_name, $os_version, $device_model, $ab_version, $traffic_type) = req()->paras;
  112. VolcUtil::Init($app_name, $app_package, $app_channel, $app_version, $os_name, $os_version, $device_model, $ab_version, $traffic_type);
  113. VolcUtil::Report(Enum_Volc_EventName::Login, array());
  114. return Resp::ok();
  115. }
  116. //
  117. // <editor-fold defaultstate="collapsed" desc="玩家信息 修改">
  118. /**
  119. * [6013]修改玩家头像
  120. */
  121. static function SetUserHeadImage() {
  122. list($headImage) = req()->paras; # 参数, 新头像
  123. ctx()->baseInfo->headImg = $headImage;
  124. UserProc::updateUserInfo();
  125. return Resp::ok(array('ret' => 'ok'));
  126. }
  127. /**
  128. * [6012]修改玩家形象
  129. */
  130. static function SetUserImage() {
  131. list($image) = req()->paras; # 参数, 新形象
  132. ctx()->baseInfo->img = $image;
  133. UserProc::updateUserInfo();
  134. return Resp::ok(array('ret' => 'ok'));
  135. }
  136. /**
  137. * [6011]修改玩家头像框
  138. */
  139. static function SetUserImageBorder() {
  140. list($imgborderId) = req()->paras; # 参数, 新头像框ID
  141. ctx()->baseInfo->imgBorderId = $imgborderId;
  142. UserProc::updateUserInfo();
  143. return Resp::ok(array('ret' => 'ok'));
  144. }
  145. /**
  146. * [6010] 设置/修改玩家昵称
  147. */
  148. static function SetUserNickname() {
  149. list($newname) = req()->paras; # 参数: 新昵称, 头像
  150. my_Assert(strlen($newname) >= 3, "名字太短了, 换个长点的吧!"); # 防御字符串长度太短.
  151. my_Assert(isset(glc()->User_SetNickname_Cost), "全局变量中未找到改名消耗钻石数量的配置字段");
  152. $amt = glc()->User_SetNickname_Cost; # 改名需要消耗钻石
  153. $ok = ctx()->base()->Consume_Cash($amt); # 扣除钻石
  154. my_Assert($ok, ErrCode::notenough_cash_msg);
  155. my_Assert(self::checkRoleNameNotExist($newname), ErrCode::user_nicknameexist); # 昵称已存在
  156. ctx()->baseInfo->name = $newname;
  157. daoInst()->update(self::role_Table)
  158. ->data(array('roleName' => $newname))
  159. ->where('userID')->eq(req()->uid)
  160. ->andWhere('zoneid')->eq(req()->zoneid)
  161. ->exec();
  162. UserProc::updateUserInfo();
  163. return Resp::ok(array('ret' => 'ok'));
  164. }
  165. // </editor-fold>
  166. //
  167. // <editor-fold defaultstate="collapsed" desc="新手引导">
  168. // /**
  169. // * [6009] 新手引导 发送n张碎片到玩家身上
  170. // */
  171. // static function SetNewbieGuideCards() {
  172. // Err(ErrCode::function_notopen_msg); # 功能已经废弃 -wg
  173. // $private = ctx()->privateState;
  174. // if (!property_exists($private, 'newbieguideCards')) { # 逻辑检查, 是否已经发放过
  175. // $private->newbieguideCards = true;
  176. // ctx()->privateState = $private; # 回写私有数据
  177. // $sp = explode(';', glc()->User_SetNewbieGuideCards); # 碎片
  178. // foreach ($sp as $cardinfo) {
  179. // $arr = explode(',', $cardinfo);
  180. // $itemid = $arr[0];
  181. // $num = $arr[1];
  182. // StoreProc::addSegmentIntoStore(ctx()->store, $itemid, $num);
  183. // }
  184. // $exp = explode(';', glc()->User_SetNewbieGuideExps); # 经验卡
  185. // foreach ($exp as $expinfo) {
  186. // $arr = explode(',', $expinfo);
  187. // $itemid = $arr[0];
  188. // $num = $arr[1];
  189. // StoreProc::PutOverlyingItemInStore($itemid, $num);
  190. // }
  191. // UserProc::updateUserInfo(); # 回存玩家数据
  192. // return Resp::ok(array('store' => ctx()->store));
  193. // }
  194. // return Resp::err(ErrCode::active_hasgetted);
  195. // }
  196. /**
  197. * [6009] 增加 设置引导结束的标志位
  198. */
  199. public static function setNewbieGuideOver() {
  200. Err(ErrCode::function_notopen_msg); # 功能已经废弃 -wg
  201. // $user = new Data_UserGame(ctx()); # user
  202. // if (!CommUtil::isPropertyExists($user, "NewbieGuideOver")) { # 防御: 变量未初始化
  203. // $user->NewbieGuideOver = 1;
  204. // }
  205. // $user->NewbieGuideOver = 1;
  206. // ctx($user);
  207. // UserProc::updateUserInfo(); # 回写数据
  208. // return Resp::ok(array(# # 返回值
  209. // 'result' => "succeed"
  210. // ));
  211. }
  212. /**
  213. * [6007] 更新初始的强制的新手引导阶段步骤
  214. * 第一阶段的引导
  215. */
  216. public static function completeNewbieGuide() {
  217. $guideIndex = req()->paras[0]; # 参数: 新手引导步骤
  218. $user = new Data_UserGame(ctx()); # user
  219. my_default_Obj($user->NewbieGuide); # 防御: 变量未初始化
  220. $NewbieGuide = $user->NewbieGuide;
  221. if (!CommUtil::isPropertyExists($NewbieGuide, "guideStep")) { # 防御: 变量未初始化
  222. $NewbieGuide->guideStep = 0;
  223. }
  224. my_Assert($guideIndex >= $NewbieGuide->guideStep, ErrCode::user_settutorialscompletedfail_err);
  225. $NewbieGuide->guideStep = $guideIndex;
  226. $user->NewbieGuide = $NewbieGuide;
  227. if ($NewbieGuide->guideStep == 2) {
  228. StatisticsProc::TargetStatistics(Enum_TargetStatistics::comNewGuide_UserNum);
  229. }
  230. ctx($user);
  231. UserProc::updateUserInfo(); # 回写数据
  232. return Resp::ok(array(
  233. "store" => $user->store, # # 目前来看只涉及到items变化
  234. "heros" => $user->heros, # # 英雄数据
  235. ));
  236. }
  237. // </editor-fold>
  238. //
  239. /**
  240. * 6006 注册新角色
  241. */
  242. public static function RegisterNewRole() {
  243. $userID = req()->uid;
  244. list($rolename, $gender, $profile_img) = req()->paras; # 参数: 昵称,性别,头像
  245. $id = gMem()->increment(MemKey_GameRun::Stat_UserCountByZone_int(req()->zoneid)); # 增加玩家数量计数
  246. $rolename = "No." . sprintf("%03d", req()->zoneid) . sprintf("%07d", $id); # 生成编号
  247. if (self::checkRoleNameNotExist($rolename)) { # 记录玩家
  248. $game = self::createUser($rolename, $gender, $profile_img);
  249. if (1 == self::regRole(req()->zoneid, $userID, $rolename, $gender, $profile_img, req()->getPlatStr())) {
  250. StatisticsProc::TargetStatistics(Enum_TargetStatistics::registerUserNum); //注册人数统计
  251. StatisticsProc::dailyTaskInit();
  252. $resp = Resp::ok($game);
  253. self::updtateUserZoneInfo();
  254. } else {
  255. $resp = Resp::err(ErrCode::err_db);
  256. }
  257. // StatProc::UserGuidStep($userinfo->user->firstLogOn, $req->zoneid, -2, 1);
  258. } else { # 昵称已存在
  259. $resp = Resp::ok(array('ret' => '用户已存在.'));
  260. }
  261. return $resp;
  262. }
  263. /**
  264. * 6000 【移动端】 获取分区列表
  265. */
  266. public static function GetZoneList() {
  267. $defaultZone = new Ins_ZoneInfo(1, 0, ""); # 新用户默认分区
  268. $bGetRecommended = false;
  269. if (count(req()->paras) > 0) { # 是否只拉取推荐分区
  270. $bGetRecommended = req()->paras[0];
  271. }
  272. $zoneList = array();
  273. $ts = now();
  274. foreach (GameConfig::zonelist() as $zoneid => $zone) {
  275. isEditor() and $zone = new \sm_zonelist();
  276. if ($zone->publicTs > $ts) {
  277. continue;
  278. }
  279. $zone->zoneid = $zoneid; # 把zoneid塞进zone数据结构中
  280. if ($bGetRecommended) {
  281. if ($zone->isRecommended > 0 && $zone->status == 1) {
  282. $zoneList[] = $zone;
  283. } else {
  284. }
  285. } else {
  286. $zoneList[] = $zone;
  287. }
  288. unset($zone->isRecommended);
  289. }
  290. UserProc::_AddTesterZonelist($zoneList); # 添加测试分区
  291. #
  292. // <editor-fold defaultstate="collapsed" desc=" 取玩家分区记录 ">
  293. $userZoneInfo = self::getUserZoneInfo(); # 玩家分区记录
  294. $isNewUser = false;
  295. if ($userZoneInfo == null) { // 这里使用推荐分区的数据,推荐分区信息,在后台编辑,编辑器可用
  296. $userZoneInfo = new Data_UserZoneInfo();
  297. $userZoneInfo->lastZone = $defaultZone; # 新用户导向默认分区
  298. $isNewUser = true;
  299. // $err = self::RegisterNewUser($req); # 新用户添加到总表中
  300. // if ($err != ErrCode::succeed) {
  301. // return ResponseVo::myErrResponse($req, $err);
  302. // }
  303. # 统计模块添加记录
  304. // StatProc::UserGuidStep(CommUtil::tsCurrent(), $req->zoneid, -1, 1); // 第一次进入游戏
  305. } else { # 转换一下格式,去掉key,只保留value的集合
  306. // $userZoneInfo->playedZones = ArrayInit();
  307. // array_merge($userZoneInfo->playedZones, array_values((array) $userZoneInfo->playedZones));
  308. }
  309. // </editor-fold>
  310. $ret = array(
  311. 'isNewUser' => $isNewUser,
  312. 'zonelist' => json_decode(json_encode($zoneList)),
  313. 'userZoneInfo' => $userZoneInfo
  314. );
  315. return Resp::ok($ret); # 返回值
  316. }
  317. private static function _AddTesterZonelist(&$zoneList) {
  318. if (config::Inst()->isTester(req()->uid)) { # 添加测试分区
  319. $zoneList[] = array('zoneid' => 999, 'name' => '内测专区', 'status' => 2, 'publicTs' => 0);
  320. }
  321. }
  322. /**
  323. * 6002 客户端下载常量配置信息
  324. * @return type
  325. */
  326. public static function downloadConstInfo() {
  327. list($clientDataVer) = req()->paras; # 客户端数据版本号,程序版本号
  328. $serverVer = GameConfig::ver(); # 最新数据版本号
  329. my_Assert($serverVer, ErrCode::err_const_no); # 找不到常量数据
  330. if ($clientDataVer == $serverVer) { # 如果版本一致,数据体抹掉,只传回版本号
  331. // $md5 = md5(json_encode($constInfo)); # 计算MD5值,多余计算md5
  332. $ret = array(
  333. 'version' => $clientDataVer,
  334. 'data' => null);
  335. return Resp::ok($ret);
  336. }
  337. $constInfo = GameConfig::client(); # 取出来的已经是base64过的压缩数据
  338. my_Assert($constInfo, ErrCode::err_const_no); # 找不到配置数据
  339. return Resp::ok(array('data' => $constInfo));
  340. }
  341. /**
  342. * 6001 客户端登录并返还玩家信息
  343. * @return Resp
  344. */
  345. public static function loginUserInfo() {
  346. $game = UserProc::getUserGame(req()->zoneid, req()->uid);
  347. if ($game == null) { # 新用户, -> 6006创建账号
  348. $ret = array(
  349. 'isNewUser' => true
  350. );
  351. return Resp::ok($ret);
  352. } else { # 2.如果玩家已存在,则处理普通登录流程
  353. if ($game->store->storage == null) {
  354. $key = 1;
  355. $key2 = 2;
  356. $game->store->storage = ObjectInit();
  357. $game->store->storage->$key = new Ins_storage();
  358. $game->store->storage->$key2 = new Ins_storage();
  359. }
  360. req()->game = $game; # 给Req挂载玩家数据
  361. UserProc::checkContidays(); # 连续登录,状态检查
  362. PayProc::m_refreshChargeOrders(); # 刷新订单, 多平台版本
  363. PayProc::checkDeltest(); # 检查内侧充值记录(函数内部会只检查一次)
  364. $college = new Info_College(ctx()->college);
  365. $college->initTaskCard();
  366. ctx()->colleg = $college;
  367. ShopProc::resetDaliySpecialPackages(); #每日特惠领奖每日重置
  368. self::checkMissOrder(); #校验是否有漏单
  369. UserProc::updateUserInfo(); # 这一步回存操作只有在 userInfo正常存在的情况下才进行
  370. $resp = Resp::ok($game); # 设置返回值
  371. // self::backupUserInfo(); # 数据回写
  372. AuctionProc::TriggerSettlement(); #结算流拍信息
  373. self::updtateUserZoneInfo(); # 1. 更新玩家分区记录
  374. }
  375. return $resp;
  376. }
  377. /**
  378. * 6003 领取连续登录奖励
  379. */
  380. public static function acceptContiDaysGift() {
  381. $resp = Resp::err(ErrCode::err_method_notimplement);
  382. return $resp;
  383. }
  384. //
  385. // <editor-fold defaultstate="collapsed" desc=" 辅助方法 ">
  386. /**
  387. * 检查昵称是否已经存在
  388. * @param string $roleName
  389. * @return boolean
  390. */
  391. static function checkRoleNameNotExist($roleName) {
  392. return false;
  393. static $sqlFormat = "SELECT count(*) as rows FROM `tab_rolename` WHERE roleName='%s';";
  394. $sql = sprintf($sqlFormat, $roleName);
  395. $n = daoInst()->query($sql)->fetch();
  396. return $n->rows <= 0;
  397. }
  398. /**
  399. * 插入玩家新角色
  400. *
  401. * @param string $zoneid
  402. * @param string $userID
  403. * @param string $nickname
  404. * @param string $gender
  405. * @param string $profile_img
  406. * @param string $plat
  407. */
  408. static function regRole($zoneid, $userID, $nickname, $gender, $profile_img, $plat) {
  409. return daoInst()->insert('tab_rolename')
  410. ->data(array(
  411. 'zoneid' => $zoneid,
  412. 'userID' => $userID,
  413. 'roleName' => $nickname,
  414. 'gender' => $gender,
  415. 'profile' => $profile_img,
  416. 'plat' => $plat
  417. ))->exec();
  418. }
  419. /**
  420. * 检测连续登录状态,重置必要字段[时间戳自动记录]
  421. */
  422. static function checkContidays($isnew = 0) {
  423. $ret = TimeUtil::totalDays() - TimeUtil::totalDays(ctx()->baseInfo->lastLogin); // 对比登录日期
  424. if ($ret > 0 || $isnew) { # 当天第一次登录
  425. self::OnNewDay($isnew);
  426. } else { # 更新下登录次数记录(任务计数器)
  427. }
  428. if ($ret == 1) { # 连续登录
  429. } else if ($ret >= 2) { # 隔天登录
  430. }
  431. ctx()->baseInfo->lastLogin = now(); # 更新下访问时间
  432. TaskProc::OnUserLogin();
  433. return $ret;
  434. }
  435. /**
  436. * 处理当天第一次登录
  437. * @param bool $isnew Description
  438. */
  439. static function OnNewDay($isnew) {
  440. self::updatePlatUserRecord($isnew); # 添加到到当天活跃玩家记录集合
  441. ActiveProc::DailyReset(); # 抽奖、每日任务状态,计数器...
  442. FightProc::ClearByDay(); # 战斗相关状态每日清理
  443. ShopProc::DailyCheck();
  444. TaskProc::ResetDailyTaskCards(); # 重置每日任务卡
  445. StoreProc::DailyRefreshWeapon(); #每日登录刷新武器商店
  446. }
  447. // <editor-fold defaultstate="collapsed" desc="创建新用户">
  448. /**
  449. * 创建用户
  450. * @return Data_UserGame
  451. */
  452. static function createUser($rolename, $gender, $profile_img) {
  453. $game = new Data_UserGame();
  454. req()->game = $game; # 更新Req挂载的玩家数据,
  455. $game->initialize(); # 初始化玩家数据
  456. $game->baseInfo->name = $rolename;
  457. $game->baseInfo->gender = $gender;
  458. $game->baseInfo->headImg = $profile_img;
  459. $game->baseInfo->firstLogin = now();
  460. #Ps 6006是没有获得到Userinfo到Req中的
  461. UserProc::checkContidays(1); # 每日状态检查
  462. // UserProc::fetchFromInteract($mem, $req); # 从interact拉取数据,Ps.初始化的过程应该还拉取不到什么有效数据
  463. UserProc::updateUserInfo(); # 回存用户数据
  464. return $game;
  465. }
  466. /**
  467. * 整理平台玩家记录集
  468. * @param int $isnew
  469. */
  470. private static function updatePlatUserRecord($isnew = 0) {
  471. $zoneid = req()->zoneid;
  472. $uid = req()->uid;
  473. $user = ctx()->baseInfo;
  474. $day = totalDays();
  475. $level = $user->level;
  476. $platUser = ObjectInit();
  477. $platUser->uid = $uid; #
  478. $platUser->name = $user->name; #
  479. $platUser->level = $level;
  480. $platUser->img = $user->headImg; # 头像字段
  481. $platUser->cash = $user->cash;
  482. $platUser->gold = $user->gold;
  483. $platUser->tili = $user->tili;
  484. $platUser->ts = now();
  485. $platUser->isnew = $isnew;
  486. gMem()->delete(MemKey_GameRun::DailyLoginUser_byUID_hash($zoneid, $day - 108));
  487. gMem()->hset(MemKey_GameRun::DailyLoginUser_byUID_hash($zoneid), $uid, $platUser);
  488. gMem()->delete(MemKey_GameRun::DailyLoginUser_byLevel_hash($zoneid, $level, $day - 108));
  489. gMem()->hset(MemKey_GameRun::DailyLoginUser_byLevel_hash($zoneid, $level), $uid, $platUser);
  490. }
  491. // </editor-fold>
  492. //
  493. // <editor-fold defaultstate="collapsed" desc="读写玩家数据">
  494. /**
  495. * 取玩家数据
  496. * @param type $zoneid
  497. * @param type $uid
  498. * @return Data_UserGame
  499. */
  500. public static function getUserGame($zoneid, $uid) {
  501. $key = MemKey_User::Info_hash($zoneid, $uid);
  502. $a = new Data_UserGame();
  503. if (null == $a->readDataFromMem($key)) { # ps.下面这一段代码和经常删号会有冲突,因此关闭了 --gwang 2022.2.28
  504. // $collection = "ylsj2019.userInfoBack";
  505. // $cursor = gMongo()->find($collection, ['key' => $key]);
  506. // if ($cursor && $cursor->valid()) {
  507. // foreach ($cursor as $v) {
  508. // $a->LoadFrom($v->value); # 加载
  509. // $a->updateDataFull($key); # 反向写回redis
  510. // break; # 其实是只有一条
  511. // }
  512. // } else {
  513. return null;
  514. // }
  515. }
  516. return new Data_UserGame($a);
  517. }
  518. /**
  519. * 更新用户数据(设置标志位,最后统一更新-gwang 2017.07.18)
  520. */
  521. public static function updateUserInfo() {
  522. my_Assert(req(), "req()为空");
  523. my_Assert(req()->game, "[" . req()->cmd . "] 玩家数据正在被清空!" . req()->uid);
  524. req()->userInfoChanged = TRUE; # 设置回写标志位
  525. }
  526. /**
  527. * 回写玩家数据
  528. * @param Data_UserGame $game
  529. */
  530. public static function setUserInfo($game) {
  531. $OK = false;
  532. if ($game) {
  533. $zoneid = req()->zoneid;
  534. $uid = req()->uid;
  535. $game->baseInfo->lastSaveTs = now();
  536. $key = MemKey_User::Info_hash($zoneid, $uid);
  537. $OK = $game->updateDataFull($key); # 向Redis回写玩家数据
  538. self::backupUserInfoMongo(); # 向MongoDB备份数据
  539. }
  540. return $OK;
  541. }
  542. // </editor-fold>
  543. //
  544. // <editor-fold defaultstate="collapsed" desc="玩家分区记录">
  545. /**
  546. * 读取玩家的分区记录
  547. * @return Data_UserZoneInfo Description
  548. */
  549. public static function getUserZoneInfo() {
  550. $ret = gMem()->get(MemKey_User::Union_PlayedZoneInfo_normal(req()->uid));
  551. return $ret;
  552. }
  553. /**
  554. * 更新玩家分区记录
  555. */
  556. public static function updtateUserZoneInfo() {
  557. $req = req();
  558. $userZoneInfo = self::getUserZoneInfo(); # 取玩家分区记录
  559. if (!$userZoneInfo) {
  560. $userZoneInfo = new Data_UserZoneInfo;
  561. }
  562. $level = 0;
  563. $zoneid = $req->zoneid;
  564. $playerName = "";
  565. $headImg = "";
  566. if (null != ctx()) { # 防御确保玩家数据不为空
  567. $level = ctx()->baseInfo->level;
  568. $playerName = ctx()->baseInfo->name;
  569. $headImg = ctx()->baseInfo->headImg;
  570. } else {
  571. Err('玩家数据为空!' . __CLASS__ . '.' . __FUNCTION__);
  572. }
  573. if (is_null($level)) {
  574. $level = 0;
  575. }
  576. $userZoneInfo->lastZone = new Ins_ZoneInfo($zoneid, $level, $playerName, $headImg); # 更新玩家分区记录
  577. $userZoneInfo->playedZones->$zoneid = $userZoneInfo->lastZone; # 玩过的分区集合
  578. gMem()->set(MemKey_User::Union_PlayedZoneInfo_normal($req->uid), $userZoneInfo); # 回写数据
  579. }
  580. // </editor-fold>
  581. // <editor-fold defaultstate="collapsed" desc=" 用户数据备份 ">
  582. /**
  583. * 备份玩家数据,(玩家数据落地),一天一份,当天记为最后一次登录时的状态而非最后一次操作的状态
  584. * @history
  585. * version 3.0.13 mysql版备份玩家数据, (性能优化后表现还不错)
  586. * 除非用脚本在redis中实现备份,否则直接写入mysql.
  587. * 主要是mysql版本利用存储过程之后性能已经和redis版相差不多.
  588. * version 2.0.0 Redis storage 从MySQL转到Redis中存储. 这个备份数据没有大规模导出硬盘的需求
  589. * 因为mysql版本实在是负载太大了, 上百毫秒. 所以改在redis中了.
  590. * version 1.0.0 Mysql storagef 1. 分表不做了, 2. 存储过程不用了 3. 先能用再说
  591. */
  592. public static function backupUserInfo() {
  593. $tsday = TimeUtil::totalDays(); # 精确到天,保留10个最近记录.
  594. $value = base64_encode(gzdeflate(JsonUtil::encode(ctx()))); # blob数据,序列化下
  595. $sql = sprintf("call insertUserInfo('%s', %d, %d, '%s');", # # 所以直接将序列化后的结果存进去吧,
  596. req()->uid, req()->zoneid, $tsday, $value); # zoneid, uid, tsday
  597. daoInst()->exec($sql); # 也可以用exec($sql)
  598. }
  599. /**
  600. * 备份玩家数据,(玩家数据落地),一天一份,当天记为最后一次登录时的状态而非最后一次操作的状态
  601. * @history
  602. * version 4.0.0 切换到MongoDB存储
  603. * version 3.0.13 mysql版备份玩家数据, (性能优化后表现还不错)
  604. * 除非用脚本在redis中实现备份,否则直接写入mysql.
  605. * 主要是mysql版本利用存储过程之后性能已经和redis版相差不多.
  606. * version 2.0.0 Redis storage 从MySQL转到Redis中存储. 这个备份数据没有大规模导出硬盘的需求
  607. * 因为mysql版本实在是负载太大了, 上百毫秒. 所以改在redis中了.
  608. * version 1.0.0 Mysql storagef 1. 分表不做了, 2. 存储过程不用了 3. 先能用再说
  609. */
  610. public static function backupUserInfoMongo() {
  611. $collectionName = "ylsj2019.userInfoBack"; # 表名
  612. $key = MemKey_User::Info_hash(req()->zoneid, req()->uid);
  613. $doc = array('key' => $key, # # 最新文档
  614. 'ts' => TimeUtil::dtCurrent(), # # 更新时间
  615. 'value' => ctx()); # 玩家数据
  616. $filter = array('key' => $key); # 指定条件
  617. gMongo()->update($collectionName, $filter, $doc, true); # 更新
  618. }
  619. // </editor-fold>
  620. //</editor-fold>
  621. //
  622. // <editor-fold defaultstate="collapsed" desc=" 统计信息 ">
  623. /**
  624. * 经验 金币 钻石 统计
  625. * @param type $cmd
  626. * @param type $cmdName
  627. * @param type $type
  628. * @param type $cur
  629. * @param type $change
  630. * @param type $end
  631. * @param type $desc
  632. */
  633. static function CollectUserBaseParam($cmd, $type, $curVal, $changeVal, $endVal, $desc) {
  634. $item = new \stdClass();
  635. $item->uid = req()->uid;
  636. $item->cmd = $cmd;
  637. $item->type = $type;
  638. $item->curVal = $curVal;
  639. $item->changeVal = $changeVal;
  640. $item->endVal = $endVal;
  641. $item->desc = $desc;
  642. $item->ts = time();
  643. $arr = array();
  644. $arr[] = $item;
  645. //$num = gMem()->rpush("userbaseParams-" . req()->uid . "-" . req()->zoneid, $arr);
  646. $num = gMem()->rpush("userbaseParams-" . req()->zoneid, $arr);
  647. }
  648. //</editor-fold>
  649. }