123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- <?php
- namespace loyalsoft;
- require_once __DIR__ . '/ActiveProc/CipheredBase32.php'; # 算法库
- /**
- * 活动模块
- */
- class ActiveProc {
- /**
- * 兑换码礼包使用记录
- */
- const tab_toke_gift = 'tab_token_gift';
- /**
- * 逻辑分发
- * 所有的Proc中必须有这样一个方法
- * @param Req $req
- */
- public static function procMain($req) {
- switch ($req->cmd) {
- case CmdCode::active_day7_drawreward: # 6502 领取奖励
- return self::Day7_DrawReward($req);
- case CmdCode::active_getzonePublicTs: # 6503 查询开服时间戳
- return ActiveProc::GetZonePublicTS($req);
- case CmdCode::Task_Tili: # 6508 体力变化
- return ActiveProc::RecoveryTili($req);
- case CmdCode::active_token_drawReward: # 6512 兑换码礼包
- return self::drawActivePackageByCode($req);
- case CmdCode::active_draw_onlinegift: # 6513 领取在线礼包
- return self::drawOnlineGift();
- case CmdCode::active_draw_reggift: # 6514 领取全服注册礼包
- return self::drawRegGift($req);
- case CmdCode::active_presentTili: # 6515 领取在线赠送体力
- return self::drawTiliGift($req);
- case CmdCode::active_get_regnum: # 6516 查询当前全服注册人数
- return self::getRegNumber($req);
- default:
- return Resp::err(ErrCode::cmd_err);
- }
- }
- /**
- * 每天重置
- * @param type $req
- */
- static function DailyReset($req) {
- self::DailyResetDay7Task($req);
- self::ClearOnlineGiftTs($req);
- self::ClearDailyTiliGift($req);
- }
- // <editor-fold defaultstate="collapsed" desc=" 赠送体力 ">
- /**
- * 领取 赠送体力
- * @param Req $req
- * @return type
- */
- static function drawTiliGift($req) {
- $user = new UserGameModel($req->userInfo->game);
- $privateState = new PrivateStateModel($user->privateState);
- $hour = date('G');
- if (in_array($hour, $privateState->dailyDrawedTiliGift)) { # 判断是否已领取
- return Resp::err(ErrCode::active_hasgetted);
- }
- $arr = explode(',', glc()->activity_presentTili_times); # 判断是否满足领取条件
- if (!in_array($hour, $arr)) {
- return Resp::err(ErrCode::active_illegal); # 不满足条件
- }
- UserGameModel::Add_tili($req, glc()->activity_presentTili_number); # 发放奖励
- $privateState->dailyDrawedTiliGift[] = $hour;
- $req->userInfo->game->privateState = $privateState;
- UserProc::updateUserInfo(); # 回存
- return Resp::ok(array(# # 返回值
- 'key' => 'ok',
- 'record' => $privateState->dailyDrawedTiliGift,
- 'tili' => $req->userInfo->game->baseInfo->tili,
- 'time' => $req->userInfo->game->privateState->TiliTime,
- ));
- }
- ////
- // </editor-fold>
- // <editor-fold defaultstate="collapsed" desc=" 全服注册礼包 ">
- /**
- * 查询当前全服注册人数
- * @param type $req
- */
- static function getRegNumber($req) {
- return Resp::ok(array(
- 'num' => gMem()->get(MemKey_GameRun::Stat_UserCountByZone_int($req->zoneid))
- ));
- }
- /**
- * 领取全服注册礼包
- * @param Req $req
- */
- static function drawRegGift($req) {
- $user = new UserGameModel($req->userInfo->game);
- $privateState = new PrivateStateModel($user->privateState);
- $giftId = $req->paras[0]; # 礼包id
- $giftModel = GameConfig::activity_reggift_getItem($giftId); # 取礼包常亮数据
- if (null == $giftModel) {
- return Resp::err(ErrCode::err_const_no);
- }
- if (in_array($giftId, $privateState->drawedRegGift)) { # 判断是否已领取
- return Resp::err(ErrCode::active_hasgetted);
- }
- // 判断是否满足领取条件
- if (gMem()->get(MemKey_GameRun::Stat_UserCountByZone_int($req->zoneid)) < $giftModel->regNumber) {
- return Resp::err(ErrCode::active_illegal); # 不满足条件
- }
- $err = StoreProc::AddMultiItemInStore($req, $giftModel->rewardstr); # 发放奖励
- if ($err) {
- return Resp::err($err); # 失败
- }
- $privateState->drawedRegGift[] = $giftId;
- $user->privateState = $privateState;
- $req->userInfo->game = $user;
- UserProc::updateUserInfo(); # 回存
- return Resp::ok(array(# # 返回值
- 'key' => 'ok',
- 'store' => $req->userInfo->game->store
- ));
- }
- // </editor-fold>
- // <editor-fold defaultstate="collapsed" desc=" 在线礼包 ">
- /**
- * 6513 领取连续在线礼包
- * @return type
- */
- static function drawOnlineGift() {
- $req = req();
- $user = $req->userInfo->game;
- $privateState = new PrivateStateModel($user->privateState);
- $giftid = $req->paras[0]; # 提取参数: 礼包的编号
- $giftData = GameConfig::activity_onlinegift_getItem($giftid); # 在线礼包常量数据
- if ($giftData == NULL) {
- return Resp::err(ErrCode::onlinegift_constno_err);
- }
- if ($privateState->onlineGiftts + $giftData->onlineSec > now()) { # 时间未到,不可以领取
- return Resp::err(ErrCode::onlinegift_timenotenough);
- }
- if (!CommUtil::isPropertyExists($privateState, "onlineGiftID")) {#初始化 这个字段
- $privateState->onlineGiftID = 1;
- } else {
- if ($privateState->onlineGiftID == 0) {
- $privateState->onlineGiftID = 1;
- }
- }
- if ($privateState->onlineGiftID != $giftid) { # 判断礼包ID是否正确,可以领取,
- return Resp::err(ErrCode::onlinegift_wrongid, $privateState->onlineGiftID . '--->' . $giftid);
- }
- $privateState->onlineGiftts = now(); # 更新时间戳
- $privateState->onlineGiftID = $giftid + 1; # 已领礼包ID++
- $err = StoreProc::AddMultiItemInStore($req, $giftData->rewardstr); # 发放奖励
- if ($err) {
- return Resp::err($err, '发放奖励失败');
- }
- $user->privateState = $privateState;
- // var_dump($privateState);
- $req->userInfo->game = $user;
- UserProc::updateUserInfo(); # 回写用户数据
- return Resp::ok(array(
- 'onlineGiftts' => $privateState->onlineGiftts,
- 'onlineGiftID' => $privateState->onlineGiftID,
- 'store' => $req->userInfo->game->store
- )); # 设置返回值
- }
- /**
- * 清理/重置 在线礼包时间戳
- * @param Req $req
- */
- static private function ClearOnlineGiftTs($req) {
- $req->userInfo->game->privateState->onlineGiftts = 0;
- $req->userInfo->game->privateState->onlineGiftID = 1;
- }
- // </editor-fold>
- // <editor-fold defaultstate="collapsed" desc=" 兑换码 ">
- /**
- * 凭兑换码领取礼包
- * @param Req $req
- */
- static function drawActivePackageByCode($req) {
- $user = new UserGameModel($req->userInfo->game);
- $privateState = new PrivateStateModel($user->privateState);
- $activeId = $req->paras[0]; # 取参数 活动id
- $active = GameConfig::activity_getItem($activeId); # 活动数据
- if (null == $active) {
- return Resp::err(ErrCode::active_const_no_err);
- }
- if ($active->startts > now() or $active->endts < now()) { # 校验开放时间
- return Resp::err(ErrCode::active_time);
- }
- $codestring = $req->paras[1]; # 取参数 激活码
- if (!preg_match("/^[a-kmnp-z2-9]{10}$/", $codestring)) { # 长度校验(10个字符)
- return Resp::err(ErrCode::active_activecode_format);
- }
- $activeCode = CipheredBase32::Decode($codestring); # 解码
- $codePlatStr = GameConstants::GetPlatStringByActivteCode($activeCode); # platstr
- if (GameConstants::AllPlatStr !== $codePlatStr # 忽略全平台礼包
- && $req->userInfo->getPlatStr() !== $codePlatStr) { # 平台字符串必须相符
- return Resp::err(ErrCode::active_activecode_plat, $codePlatStr); # # 平台错误
- }
- if (!is_int($activeCode->number) # 检查 兑换码的编号范围0~50000
- || $activeCode->number < 1 || $activeCode->number > 50000) {
- return Resp::err(ErrCode::active_activecode_format);
- }
- $packageID = $activeCode->package; # 礼包id
- $packageInfo = GameConfig::tokenGift_getItem($packageID); # 礼包常量数据
- if ($packageInfo == null) { # 无效
- return Resp::err(ErrCode::err_const_no);
- }
- if ($packageInfo->expirets < now() || $packageInfo->startTs > now()) { # 激活码已经失效,或者礼包尚未开启
- return Resp::err(ErrCode::active_activecode_outtime);
- }
- if (in_array($packageID, $privateState->usedTokens)) { # 已经领取过该礼包了
- return Resp::err(ErrCode::active_hasgetted);
- }
- if (self::checkActiveCodeIsUsed($activeCode)) { # 检查 该激活码是否已经使用过了
- return Resp::err(ErrCode::active_activecode_used);
- } # 发放礼包,
- $err = StoreProc::AddMultiItemInStore($req, $packageInfo->reward);
- if ($err) {
- return Resp::err($err);
- }
- $req->userInfo->game->privateState->usedTokens[] = $packageID; # 记录领取记录
- $ok = self::setActiveCodeUserRecord($activeCode, $req->uid); # 插入数据库
- if (!$ok) {
- return Resp::err(ErrCode::err_db, daoInst()->getError(TRUE)); # # 数据库操作失败- 重试
- }
- UserProc::updateUserInfo(); # 回存玩家数据
- $ret = array(# # 返回值
- "plat" => $codePlatStr,
- "packageId" => $activeCode->package,
- "reward" => $packageInfo->reward,
- 'store' => $req->userInfo->game->store,
- );
- return Resp::ok($ret); # 返回成功信息
- }
- /**
- * 检查兑换码是否已经使用过了
- * @param ActiveCode $activeCode
- * @return boolean true : 已经用过了, false : 激活码尚未使用过.
- */
- static function checkActiveCodeIsUsed($activeCode) {
- $sql = sprintf("`plat`='%d' and `package`='%d' and `number`='%d'", # # where 子句
- $activeCode->plat, $activeCode->package, $activeCode->number);
- $rows = daoInst()
- ->select()
- ->from(self::tab_toke_gift) # 表名
- ->where($sql)
- ->count();
- return $rows > 0;
- }
- /**
- * 将兑换码添加到使用记录中
- * @param ActiveCode $actvieCode
- * @param string $uid
- * @return boolean true: 操作成功, false: 操作失败
- */
- static function setActiveCodeUserRecord($actvieCode, $uid) {
- $actvieCode->uid = $uid; # 添加uid
- $ret = daoInst()
- ->insert(self::tab_toke_gift) # 表名
- ->data($actvieCode)
- ->exec();
- return $ret > 0;
- }
- // </editor-fold>
- /**
- * 查询当前分区开放时间戳
- * @param Req $req
- */
- private static function GetZonePublicTS($req) {
- $zoneInfo = GameConfig::zonelist_getItem($req->zoneid);
- return Resp::ok($zoneInfo->publicTs);
- }
- // <editor-fold defaultstate="collapsed" desc=" 体力 ">
- /**
- *
- * @param req $req
- * @return type
- */
- public static function RecoveryTili($req) {
- $useTili = $req->paras[0];
- ActiveProc::ChangeTili($useTili);
- UserProc::updateUserInfo();
- $result = array(
- 'time' => $req->userInfo->game->privateState->TiliTime,
- 'tili' => $req->userInfo->game->baseInfo->tili
- );
- $resp = Resp::ok($result);
- return $resp;
- }
- /**
- * 清理每天的赠送体力领取记录
- * @param Req $req
- */
- private static function ClearDailyTiliGift($req) {
- $req->userInfo->game->privateState->dailyDrawedTiliGift = ArrayInit();
- }
- /**
- * 修改玩家体力
- * @param int $useTili $useTili>0代表增加体力, $useTili<0代表消耗体力
- * @return type
- */
- public static function ChangeTili($useTili) {
- $req = req();
- $user = $req->userInfo->game;
- $moreTili = 0; # 超上限的体力值
- // 如果没有tilitime这个值,说明是以前老用户新登录的,为了纠错在判断里面补值.(正常用户不会进入判断内部)
- if (!CommUtil::isPropertyExists($user->privateState, "TiliTime")) {
- $user->privateState->TiliTime = time() - 120;
- }
- if ($user->baseInfo->tili > 120) { # 如果该账户体力值本身是大于上限的,获取一下超出的体力值
- $moreTili = $user->baseInfo->tili - 120; # 用moreTili保存一下额外的体力值
- }
- $useTime = time() - $user->privateState->TiliTime; # 距离上次修改体力值的时间
- if ($useTime > 300) { # 该账户超过了单位体力时间,需要重新计算体力值
- if ($moreTili <= 0) {
- $user->baseInfo->tili += (int) ( (int) ($useTime / 300));
- if ($user->baseInfo->tili > 120) { # 体力值上限
- $user->baseInfo->tili = 120;
- } // 这代码真烂,以后用的时候再修复吧... -gwang 20200110154858
- }
- $user->privateState->TiliTime = time();
- }
- if ($useTili < 0) { # 如果是消耗体力
- my_Assert(abs($useTili) < $user->baseInfo->tili, ErrCode::notenough_tili); # 体力不足
- $user->baseInfo->tili += $useTili; # 扣除体力
- } else if ($useTili > 0) { # 如果是购买增加体力
- $user->baseInfo->tili += $useTili;
- }
- return ErrCode::ok;
- }
- // </editor-fold>
- //
- //
- // <editor-fold defaultstate="collapsed" desc=" 7日签到 ">
- /**
- * 重置7日签到相关计数器
- * @param Req $req
- */
- static function DailyResetDay7Task($req) {
- $req->userInfo->game->gates->Times = 0;
- if (count($req->userInfo->game->privateState->LoginDays) < 7) {
- $req->userInfo->game->privateState->LoginDays[] = tsDay();
- }
- }
- /**
- * [6502] 领取七日签到奖励
- * @param req $req
- */
- static function Day7_DrawReward($req) {
- // 设计7日数据结构
- // 检查领取条件
- // 结束
- }
- // </editor-fold>
- //
- }
|