123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736 |
- <?php
- namespace loyalsoft;
- require_once __DIR__ . '/../service_call/pay/official/pay_op.php';
- /**
- * Description of UserProc
- * 玩家数据处理流程
- *
- */
- class UserProc {
- const role_Table = 'tab_rolename';
- /**
- * 逻辑分发
- * 所有的Proc中必须有这样一个方法
- * @param Req $req
- */
- public static function procMain($req) {
- switch ($req->cmd) {
- case CmdCode::cmd_user_getzonelist: # 6000 分区列表
- return UserProc::GetZoneList();
- case CmdCode::cmd_user_loginuserinfo: # 6001 登录/新玩家直接注册并登录
- return UserProc::loginUserInfo();
- case CmdCode::cmd_user_gameconstinfo: # 6002 下载游戏配置
- return UserProc::downloadConstInfo();
- case CmdCode::cmd_user_setAnimation: # 6004 片头播放记录
- return UserProc::setAnimation();
- case CmdCode::cmd_user_replaceHeadImg: # 6005 替换头像
- return UserProc::replaceHeadImg();
- case CmdCode::cmd_user_delUserUid: # 6006 删除账号
- return UserProc::delUserUid();
- case CmdCode::cmd_user_removeNewHeadImgTip: # 6007 移除新头像标志
- return UserProc::removeNewHeadImgTip();
- case CmdCode::cmd_user_readAnnouncement: # 6008 读公告记录
- return UserProc::readAnnouncement();
- case CmdCode::cmd_user_clearFunUnlockInfo: # 6009 重置功能解锁记录信息
- return UserProc::clearFunUnlockInfo();
- case CmdCode::user_rename: # 6010 改名
- return self::ReName();
- default:
- Err(ErrCode::cmd_err);
- }
- }
- /**
- * 6010 玩家改名
- */
- public static function ReName() {
- list($newName) = req()->paras;
- $historyNames = ctx()->privateData()->HistoryNames;
- $n = count($historyNames);
- $arr = explode(',', glc()->Rename_Cost); # 花费数组
- $cost = ($n >= count($arr) ) ? $arr[count($arr) - 1] : $arr[$n]; # 本次改名花费
- my_Assert((ctx()->privateData()->lastRenameTs + glc()->Rename_Cooldown) < now(), "改名功能冷却中");
- my_Assert(ctx()->base()->Consume_Cash($cost), "元宝不足!");
- my_Assert(self::checkRoleNameNotExist($newName), "昵称已存在, 请重新命名.");
- if (1 == self::regRole(req()->zoneid, req()->uid, $newName, "", "", "")) {
- $historyNames[] = ctx()->base()->name;
- ctx()->privateData()->HistoryNames = $historyNames;
- ctx()->privateState->lastRenameTs = now();
- ctx()->base()->name = $newName;
- self::updateUserInfo(); # 回存数据
- FightProc::UpdateRankUserName(req()->uid, $newName); # 刷新排行榜上的昵称
- return Resp::ok();
- }
- return Resp::err(ErrCode::err_db);
- }
- /**
- * 6009 重置功能解锁记录信息
- */
- public static function clearFunUnlockInfo() {
- list($type, $id) = req()->paras;
- if ($type == 1 && in_array($id, ctx()->privateState->funUnlockRecord)) {
- StlUtil::arrayRemove(ctx()->privateState->funUnlockRecord, $id);
- } elseif ($type == 2) {
- $mo = GameConfig::skills_getItem($id);
- if (in_array($mo->typeId, ctx()->privateState->skillUnlockRecord)) {
- StlUtil::arrayRemove(ctx()->privateState->skillUnlockRecord, $mo->typeId);
- }
- } elseif ($type == 3) {
- ctx()->privateState->oldLevel = 0;
- ctx()->privateState->upLevel = 0;
- }
- UserProc::updateUserInfo();
- return Resp::ok(array());
- }
- /**
- * 6008 读公告记录
- */
- public static function readAnnouncement() {
- list($id) = req()->paras;
- if (!in_array($id, ctx()->privateState->announcement_drawed)) {
- ctx()->privateState->announcement_drawed[] = $id;
- }
- UserProc::updateUserInfo();
- return Resp::ok(array());
- }
- /**
- * 6007 移除新头像标志
- */
- public static function removeNewHeadImgTip() {
- //list($gateId) = req()->paras;
- $dic = ctx()->heros->Dic;
- foreach ($dic as $heroId => $ins_Hero) {
- if ($ins_Hero->isUnlock == 1 && $ins_Hero->isNewHeadImgTip == 1) {
- ctx()->heros->Dic->$heroId->isNewHeadImgTip = 0;
- }
- }
- UserProc::updateUserInfo();
- return Resp::ok(array());
- }
- /**
- * 6006 删除账号
- * @return type
- */
- public static function delUserUid() {
- $mem = gMem();
- $list = self::GetUserDataKeys(req()->uid, req()->zoneid); # 玩家数据key
- foreach ($list as $key) {
- if ($mem->exists($key)) {
- $mem->delete($key);
- }
- }
- // <editor-fold defaultstate="collapsed" desc="清理mongodb中的数据">
- self::deleteUserMapData(req()->uid, req()->zoneid);
- // </editor-fold>
- FightProc::DeleteRankInvalidUser(req()->uid);
- self::delRegRole(req()->zoneid, req()->uid, ctx()->baseInfo->name);
- $ret = array();
- return Resp::ok($ret);
- }
- private static function GetUserDataKeys($uid, $zoneid) {
- $list = array();
- $zoneKey = MemKey_User::Union_PlayedZoneInfo_normal($uid); # 分区信息
- $list[] = $zoneKey;
- $publicKey = MemKey_User::Union_PublicState_hash($uid); # 公共信息
- $list[] = $publicKey;
- $gameInfoKey = MemKey_User::Info_hash($zoneid, $uid); # 游戏数据主体
- $list[] = $gameInfoKey;
- $CurIdKey = MemKey_User::Mail_CurId_int($zoneid, $uid); # 当前邮件编号
- $list[] = $CurIdKey;
- $SysRecordKey = MemKey_User::Mail_SysRecord_set($zoneid, $uid); # 当前已经领取过的系统邮件记录
- $list[] = $SysRecordKey;
- $QueueKey = MemKey_User::Mail_Queue_hash($zoneid, $uid); # 邮件列表
- $list[] = $QueueKey;
- return $list;
- }
- /**
- * 删除账号-区别内外网
- * @param type $uid
- * @param type $type
- */
- static public function deleteUserMapData($uid, $zoneid) {
- gMongo()->delete("playerMapInfo", array('Uid' => $uid, 'ZoneId' => intval($zoneid))); # 地图
- gMongo()->delete("PlayerInfo", array('Uid' => $uid, 'ZoneId' => intval($zoneid)));
- gMongo()->delete("userInfoBack", array('key' => MemKey_User::Info_hash($zoneid, $uid)));
- }
- /**
- * 6005 替换头像
- * @return type
- */
- public static function replaceHeadImg() {
- list($img) = req()->paras;
- ctx()->baseInfo->headImg = $img;
- FightProc::UpdateRankUserHeadImg(req()->uid, $img);
- UserProc::updateUserInfo();
- $ret = array();
- return Resp::ok($ret);
- }
- /**
- * 6004 设置片头播放记录
- * @return type
- */
- public static function setAnimation() {
- list($tag) = req()->paras;
- if (ctx()->baseInfo->animation == 0 && $tag > 0) {
- ctx()->baseInfo->animation = 1;
- }
- UserProc::updateUserInfo();
- $ret = array();
- return Resp::ok($ret);
- }
- /**
- * 检测遗漏订单
- */
- static function checkMissOrder() {
- $tableName = "tpl_order_tab";
- if (daoInst()->tableExist($tableName)) {
- $arr = daoInst()->select("*")->from($tableName)
- ->where('uid')->eq(req()->uid)
- ->andWhere('zoneid')->eq(req()->zoneid)
- ->andWhere('status')->eq(1)
- ->andWhere('drawed_ts')->eq(0)
- ->fetchAll();
- if (count($arr) != null) {
- foreach ($arr as $item) {
- $result = pay_op::CheckAndDrawOrder(req()->uid, $item->cpOrderId, array(new PayProc, 'distributePayGoods'));
- }
- }
- }
- }
- /**
- * 6016 拉取其他玩家的信息.
- */
- public static function UserOtherPlayerInfo() {
- $zoneId = req()->zoneid;
- list($other_uid) = req()->paras;
- $g = UserProc::getUserGame($zoneId, $other_uid);
- my_Assert(null != $g, ErrCode::user_no_err); # 找不到指定的玩家数据
- return Resp::ok($g);
- }
- /**
- * 6000 【移动端】 获取分区列表
- */
- public static function GetZoneList() {
- $defaultZone = new Ins_ZoneInfo(1, 0, ""); # 新用户默认分区
- $bGetRecommended = false;
- if (count(req()->paras) > 0) { # 是否只拉取推荐分区
- $bGetRecommended = req()->paras[0];
- }
- $zoneList = array();
- $ts = now();
- // foreach (GameConfig::zonelist() as $zoneid => $zone) {
- // isEditor() and $zone = new \sm_zonelist();
- // if ($zone->publicTs > $ts) {
- // continue;
- // }
- // $zone->zoneid = $zoneid; # 把zoneid塞进zone数据结构中
- // if ($bGetRecommended) {
- // if ($zone->isRecommended > 0 && $zone->status == 1) {
- // $zoneList[] = $zone;
- // } else {
- //
- // }
- // } else {
- // $zoneList[] = $zone;
- // }
- // unset($zone->isRecommended);
- // }
- // UserProc::_AddTesterZonelist($zoneList); # 添加测试分区
- #
- // <editor-fold defaultstate="collapsed" desc=" 取玩家分区记录 ">
- $userZoneInfo = self::getUserZoneInfo(); # 玩家分区记录
- $isNewUser = false;
- if ($userZoneInfo == null) { // 这里使用推荐分区的数据,推荐分区信息,在后台编辑,编辑器可用
- $userZoneInfo = new Data_UserZoneInfo();
- $userZoneInfo->lastZone = $defaultZone; # 新用户导向默认分区
- $isNewUser = true;
- } else { # 转换一下格式,去掉key,只保留value的集合
- // $userZoneInfo->playedZones = ArrayInit();
- // array_merge($userZoneInfo->playedZones, array_values((array) $userZoneInfo->playedZones));
- }
- // </editor-fold>
- $ret = array(
- 'isNewUser' => $isNewUser,
- 'zonelist' => json_decode(json_encode($zoneList)),
- 'userZoneInfo' => $userZoneInfo
- );
- return Resp::ok($ret); # 返回值
- }
- private static function _AddTesterZonelist(&$zoneList) {
- if (config::Inst()->isTester(req()->uid)) { # 添加测试分区
- $zoneList[] = array('zoneid' => 999, 'name' => '内测专区', 'status' => 2, 'publicTs' => 0);
- }
- }
- /**
- * 6002 客户端下载常量配置信息
- * @return type
- */
- public static function downloadConstInfo() {
- list($clientDataVer) = req()->paras; # 客户端数据版本号,程序版本号
- $serverVer = GameConfig::ver(); # 最新数据版本号
- my_Assert($serverVer, ErrCode::err_const_no); # 找不到常量数据
- $url = config::CDN_host() . "/cfg/" . req()->CV . "/Client.bytes?" . $serverVer;
- $ret = array(
- 'version' => $serverVer,
- 'url' => $clientDataVer == $serverVer ? "" : $url, # # 如果版本一致,url传空,只传回版本号
- 'data' => null);
- return Resp::ok($ret);
- // $md5 = md5(json_encode($constInfo)); # 计算MD5值,多余计算md5
- // $constInfo = GameConfig::client(); # 取出来的已经是base64过的压缩数据
- // my_Assert($constInfo, ErrCode::err_const_no); # 找不到配置数据
- }
- /**
- * 6001 客户端登录并返还玩家信息
- * @return Resp
- */
- public static function loginUserInfo() {
- $game = UserProc::getUserGame(req()->zoneid, req()->uid);
- if ($game == null) { # 新用户, -> 6006创建账号
- $userID = req()->uid;
- list($nickName) = req()->paras;
- $id = gMem()->increment(MemKey_GameRun::Stat_UserCountByZone_int(req()->zoneid)); # 增加玩家数量计数
- $rolename = "No." . sprintf("%03d", req()->zoneid) . sprintf("%07d", $id); # 生成编号
- $rolename = $nickName; # 采用客户端传过来的值创建账号, 2024.6.24
- if (self::checkRoleNameNotExist($rolename)) { # 记录玩家
- $game = self::createUser($rolename);
- if (1 == self::regRole(req()->zoneid, $userID, $rolename, "", "", "")) {
- $resp = Resp::ok($game);
- self::updtateUserZoneInfo();
- } else {
- $resp = Resp::err(ErrCode::err_db);
- }
- } else { # 昵称已存在
- $resp = Resp::ok(array('ret' => '用户已存在.'));
- }
- $game->RegenNewToken();
- $game->baseInfo->Reset_tilits();
- self::OnLogin_DateDeal();
- UserProc::updateUserInfo(); # 这一步回存操作只有在 userInfo正常存在的情况下才进行
- return $resp;
- } else { # 2.如果玩家已存在,则处理普通登录流程
- req()->game = $game; # 给Req挂载玩家数据
- $game->base()->Reset_tilits(); # 修正体力ts
- UserProc::checkContidays(); # 连续登录,状态检查
- //PayProc::m_refreshChargeOrders(); # 刷新订单, 多平台版本
- //PayProc::checkDeltest(); # 检查内侧充值记录(函数内部会只检查一次)
- //self::checkMissOrder(); #校验是否有漏单
- $game->RegenNewToken();
- UserProc::updateUserInfo(); # 这一步回存操作只有在 userInfo正常存在的情况下才进行
- self::OnLogin_DateDeal();
- //ctx()->privateState->firstRechargeUI_OpenTip = 1;
- if (ctx()->baseInfo->charge_amt == 0) {
- ctx()->privateState->firstRechargeUI_OpenTip = 1;
- } else {
- $num = ctx()->privateState->firstRecharge_receiveTag;
- if (!in_array($num, ctx()->privateState->firstRechargeRewardRecord)) {
- ctx()->privateState->firstRechargeUI_OpenTip = 1;
- }
- }
- $resp = Resp::ok($game); # 设置返回值
- self::updtateUserZoneInfo(); # 1. 更新玩家分区记录
- }
- return $resp;
- }
- //
- // <editor-fold defaultstate="collapsed" desc=" 辅助方法 ">
- /**
- * 检查昵称是否已经存在
- * @param string $roleName
- * @return boolean
- */
- static function checkRoleNameNotExist($roleName) {
- // return true; # 不再检查昵称重复
- static $sqlFormat = "SELECT count(*) as cnt FROM `tab_rolename` WHERE roleName='%s';";
- $sql = sprintf($sqlFormat, $roleName);
- $n = daoInst()->query($sql)->fetch();
- // var_dump($n);
- return $n->cnt <= 0;
- }
- /**
- * 插入玩家新角色
- *
- * @param string $zoneid
- * @param string $userID
- * @param string $nickname
- * @param string $gender
- * @param string $profile_img
- * @param string $plat
- */
- static function regRole($zoneid, $userID, $nickname, $gender, $profile_img, $plat) {
- return daoInst()->insert('tab_rolename')
- ->data(array(
- 'zoneid' => $zoneid,
- 'userID' => $userID,
- 'roleName' => $nickname,
- 'gender' => $gender,
- 'profile' => $profile_img,
- 'plat' => $plat
- ))->exec();
- }
- /**
- *
- * @param type $zoneid
- * @param type $userID
- * @param type $nickname
- * @param type $gender
- * @param type $profile_img
- * @param type $plat
- * @return type
- */
- static function delRegRole($zoneid, $userID, $nickname) {
- return daoInst()->del('tab_rolename')
- ->data(array(
- 'zoneid' => $zoneid,
- 'userID' => $userID,
- 'roleName' => $nickname,
- ))->exec();
- }
- /**
- * 检测连续登录状态,重置必要字段[时间戳自动记录]
- */
- static function checkContidays($isnew = 0) {
- $ret = TimeUtil::totalDays() - TimeUtil::totalDays(ctx()->baseInfo->lastLogin); // 对比登录日期
- if ($ret > 0 || $isnew) { # 当天第一次登录
- self::OnNewDay($isnew);
- } else { # 更新下登录次数记录(任务计数器)
- }
- if ($ret == 1) { # 连续登录
- } else if ($ret >= 2) { # 隔天登录
- }
- ctx()->baseInfo->lastLogin = now(); # 更新下访问时间
- //TapDBUtil::SOnUserLogin(); # 向tapdb上报玩家登录 2023.5.10
- return $ret;
- }
- /**
- * 处理当天第一次登录
- * @param bool $isnew Description
- */
- static function OnNewDay($isnew) {
- ShopProc::DailyShopItemRand();
- ShopProc::ShopDailyClear();
- //self::clear();
- FightProc::FightDailyClear();
- //TaskProc::initAchieveData();
- TaskProc::ResetTask();
- PayProc::setFirstRechargeLoginTag();
- self::ActiveRefershTsDeal();
- ActiveProc::DailyResetDay7Task();
- }
-
- static function ActiveRefershTsDeal() {
- //一天一刷
- TaskProc::DailyTaskReset();
- TaskProc::ClearDay7Task();
-
- //一周一刷
- $weekNum = TimeUtil::totalWeeks();
- $lastWeekNum = TimeUtil::totalWeeks(ctx()->baseInfo->lastLogin);
- if($weekNum - $lastWeekNum >= 1){
- TaskProc::WeekTaskReset();
- }
-
- //两周一刷
- if($weekNum - $lastWeekNum >= 2){//暂时还没有对应活动
-
- }
- //2天一刷
- $curDay = TimeUtil::totalDays();
- $nextDay = TimeUtil::totalDays(ctx()->privateState->nextDayLogin);
- if($curDay - $nextDay>= 2){
- ctx()->privateState->nextDayLogin = now();
- ActiveProc::ResetLimitTsBuy();
- ctx()->privateState->limitTsBuy_RewardRecord = array();
- }
- }
- static function OnLogin_DateDeal() {
- EmailProc::refreshSysMail(req()->zoneid, req()->uid);
- EmailProc::IsExistRedTip();
- FightProc::isExistNoDrawed_FightPower();
- FightProc::isExistNoDrawed_MainGate();
- FightProc::Ranking_FightPower();
- TaskProc::OnLogin_Daily();
- TaskProc::OnLogin_day7();
- TaskProc::checkMainTask();
- FightProc::SubFunDateInit_Config();
- }
- // static function clear() {
- // $dic = GameConfig::announcement();
- // foreach ($dic as $mo) {
- // if(now() > $mo->endTs && in_array($mo->id,ctx()->privateState->announcement)){
- // StlUtil::arrayRemove(ctx()->privateState->announcement, $mo->id);
- // }
- // }
- //
- // }
- // <editor-fold defaultstate="collapsed" desc="创建新用户">
- /**
- * 创建用户
- * @return Data_UserGame
- */
- static function createUser($rolename) {
- $game = new Data_UserGame();
- req()->game = $game; # 更新Req挂载的玩家数据,
- $game->initialize(); # 初始化玩家数据
- $game->baseInfo->name = $rolename;
- //$game->baseInfo->headImg = "";
- $game->baseInfo->firstLogin = now();
- #Ps 6006是没有获得到Userinfo到Req中的
- UserProc::checkContidays(1); # 每日状态检查
- // UserProc::fetchFromInteract($mem, $req); # 从interact拉取数据,Ps.初始化的过程应该还拉取不到什么有效数据
- UserProc::updateUserInfo(); # 回存用户数据
- return $game;
- }
- /**
- * 整理平台玩家记录集
- * @param int $isnew
- */
- private static function updatePlatUserRecord($isnew = 0) {
- $zoneid = req()->zoneid;
- $uid = req()->uid;
- $user = ctx()->baseInfo;
- $day = totalDays();
- $level = $user->level;
- $platUser = ObjectInit();
- $platUser->uid = $uid; #
- $platUser->name = $user->name; #
- $platUser->level = $level;
- $platUser->img = $user->headImg; # 头像字段
- $platUser->cash = $user->cash;
- $platUser->gold = $user->gold;
- $platUser->tili = $user->tili;
- $platUser->ts = now();
- $platUser->isnew = $isnew;
- gMem()->delete(MemKey_GameRun::DailyLoginUser_byUID_hash($zoneid, $day - 108));
- gMem()->hset(MemKey_GameRun::DailyLoginUser_byUID_hash($zoneid), $uid, $platUser);
- gMem()->delete(MemKey_GameRun::DailyLoginUser_byLevel_hash($zoneid, $level, $day - 108));
- gMem()->hset(MemKey_GameRun::DailyLoginUser_byLevel_hash($zoneid, $level), $uid, $platUser);
- }
- // </editor-fold>
- //
- // <editor-fold defaultstate="collapsed" desc="读写玩家数据">
- /**
- * 取玩家数据
- * @param type $zoneid
- * @param type $uid
- * @return Data_UserGame
- */
- public static function getUserGame($zoneid, $uid) {
- $key = MemKey_User::Info_hash($zoneid, $uid);
- // $pf = req()->getPlatStr();
- // if ($pf == "tap") { # taptap平台
- // $oldkey = MemKey_User::Info_hash($zoneid, req()->getPlatOid());
- // if (gMem()->exists($oldkey)) {
- // gMem()->rename($oldkey, $key); # 做下数据迁移
- // }
- // }
- $a = new Data_UserGame();
- if (null == $a->readDataFromMem($key)) { # ps.下面这一段代码和经常删号会有冲突,因此关闭了 --gwang 2022.2.28
- $collection = "userInfoBack";
- $cursor = gMongo()->find($collection, ['key' => $key], ['sort' => array('ts' => -1), 'limit' => 1]); # 提取备份数据
- $cursor->rewind();
- if ($cursor->valid()) {
- $v = $cursor->current();
- $a->LoadFrom($v->value); # 加载
- $a->updateDataFull($key); # 反向写回redis
- } else {
- return null;
- }
- }
- return new Data_UserGame($a);
- }
- /**
- * 更新用户数据(设置标志位,最后统一更新-gwang 2017.07.18)
- */
- public static function updateUserInfo() {
- my_Assert(req(), "req()为空");
- my_Assert(req()->game, "[" . req()->cmd . "] 玩家数据正在被清空!" . req()->uid);
- req()->userInfoChanged = TRUE; # 设置回写标志位
- }
- /**
- * 回写玩家数据
- * @param Data_UserGame $game
- */
- public static function setUserInfo($game) {
- $OK = false;
- if ($game) {
- $zoneid = req()->zoneid;
- $uid = req()->uid;
- $game->baseInfo->lastSaveTs = now();
- $key = MemKey_User::Info_hash($zoneid, $uid);
- $OK = $game->updateDataFull($key); # 向Redis回写玩家数据
- if ($OK) {
- // CLog::info($msg);
- self::backupUserInfoMongo(); # 向MongoDB备份数据
- gMem()->expire($key, 3600); # 设置过期时间1小时
- } else {
- // redo Logic
- CLog::err("写入数据时版本已过期!!!");
- }
- }
- return $OK;
- }
- // </editor-fold>
- //
- // <editor-fold defaultstate="collapsed" desc="玩家分区记录">
- /**
- * 读取玩家的分区记录
- * @return Data_UserZoneInfo Description
- */
- public static function getUserZoneInfo() {
- $ret = gMem()->get(MemKey_User::Union_PlayedZoneInfo_normal(req()->uid));
- return $ret;
- }
- /**
- * 更新玩家分区记录
- */
- public static function updtateUserZoneInfo() {
- $req = req();
- $userZoneInfo = self::getUserZoneInfo(); # 取玩家分区记录
- if (!$userZoneInfo) {
- $userZoneInfo = new Data_UserZoneInfo;
- }
- $level = 0;
- $zoneid = $req->zoneid;
- $playerName = "";
- $headImg = "";
- if (null != ctx()) { # 防御确保玩家数据不为空
- $level = ctx()->baseInfo->level;
- $playerName = ctx()->baseInfo->name;
- $headImg = ctx()->baseInfo->headImg;
- } else {
- Err('玩家数据为空!' . __CLASS__ . '.' . __FUNCTION__);
- }
- if (is_null($level)) {
- $level = 0;
- }
- $userZoneInfo->lastZone = new Ins_ZoneInfo($zoneid, $level, $playerName, $headImg); # 更新玩家分区记录
- $userZoneInfo->playedZones->$zoneid = $userZoneInfo->lastZone; # 玩过的分区集合
- gMem()->set(MemKey_User::Union_PlayedZoneInfo_normal($req->uid), $userZoneInfo); # 回写数据
- }
- // </editor-fold>
- // <editor-fold defaultstate="collapsed" desc=" 用户数据备份 ">
- /**
- * 备份玩家数据,(玩家数据落地),一天一份,当天记为最后一次登录时的状态而非最后一次操作的状态
- * @history
- * version 3.0.13 mysql版备份玩家数据, (性能优化后表现还不错)
- * 除非用脚本在redis中实现备份,否则直接写入mysql.
- * 主要是mysql版本利用存储过程之后性能已经和redis版相差不多.
- * version 2.0.0 Redis storage 从MySQL转到Redis中存储. 这个备份数据没有大规模导出硬盘的需求
- * 因为mysql版本实在是负载太大了, 上百毫秒. 所以改在redis中了.
- * version 1.0.0 Mysql storagef 1. 分表不做了, 2. 存储过程不用了 3. 先能用再说
- */
- public static function backupUserInfo() {
- $tsday = TimeUtil::totalDays(); # 精确到天,保留10个最近记录.
- $value = base64_encode(gzdeflate(JsonUtil::encode(ctx()))); # blob数据,序列化下
- $sql = sprintf("call insertUserInfo('%s', %d, %d, '%s');", # # 所以直接将序列化后的结果存进去吧,
- req()->uid, req()->zoneid, $tsday, $value); # zoneid, uid, tsday
- daoInst()->exec($sql); # 也可以用exec($sql)
- }
- /**
- * 备份玩家数据,(玩家数据落地),一天一份,当天记为最后一次登录时的状态而非最后一次操作的状态
- * @history
- * version 4.0.0 切换到MongoDB存储
- * version 3.0.13 mysql版备份玩家数据, (性能优化后表现还不错)
- * 除非用脚本在redis中实现备份,否则直接写入mysql.
- * 主要是mysql版本利用存储过程之后性能已经和redis版相差不多.
- * version 2.0.0 Redis storage 从MySQL转到Redis中存储. 这个备份数据没有大规模导出硬盘的需求
- * 因为mysql版本实在是负载太大了, 上百毫秒. 所以改在redis中了.
- * version 1.0.0 Mysql storagef 1. 分表不做了, 2. 存储过程不用了 3. 先能用再说
- */
- public static function backupUserInfoMongo() {
- $collectionName = "userInfoBack"; # 表名
- $key = MemKey_User::Info_hash(req()->zoneid, req()->uid);
- $doc = array('key' => $key, # # 最新文档
- 'ts' => TimeUtil::dtCurrent(), # # 更新时间
- 'stVer' => ctx()->stVer, # # 增加版本号
- 'value' => ctx()); # 玩家数据
- $bok = gMongo()->insert($collectionName, $doc); # 插入备份
- // CLog::err($collectionName . "备份玩家数据" . req()->uid . ($bok ? "成功" : "失败"));
- if (ctx()->stVer % 100 == 1) { # 每100条记录清理一次(delete耗时比较长-gwang.2023年3月10日)
- $delFilter = array('key' => $key, 'stVer' => ['$lt' => ctx()->stVer - 100]); # 保留最后一百次变更记录
- gMongo()->delete($collectionName, $delFilter);
- }
- // $filter = array('key' => $key); # 指定条件
- // gMongo()->update($collectionName, $filter, $doc, true); # 更新
- }
- // </editor-fold>
- //</editor-fold>
- //
- }
|