200 lines
7.4 KiB
PHP
200 lines
7.4 KiB
PHP
<?php
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: zn
|
|
* Date: 2017/8/9
|
|
* Time: 11:51
|
|
*/
|
|
|
|
namespace common\modules\sign;
|
|
|
|
|
|
use common\modules\achievement\AchievementUtil;
|
|
use common\modules\dailytask\DailyTaskUtil;
|
|
use common\modules\safe\SafeUtil;
|
|
use common\modules\sign\vo\Sign;
|
|
use common\modules\sign\vo\SignContinueGift;
|
|
use common\modules\systemlog\SystemLogUtil;
|
|
use common\services\GameConfigServices;
|
|
use common\services\ItemServices;
|
|
use common\services\ResultStatusServices;
|
|
use common\services\StatusException;
|
|
use common\utils\CommUtil;
|
|
use common\utils\MoneyUtil;
|
|
use common\utils\RedisKeyUtil;
|
|
|
|
/**
|
|
* 签到
|
|
* Class SignUtil
|
|
* @package common\modules\levelgift
|
|
*/
|
|
class SignUtil
|
|
{
|
|
const REMEDY = 1;//补签
|
|
const NOT_REMEDY = 0;//不是补签
|
|
const REMEDY_LIST = [
|
|
self::REMEDY,
|
|
self::NOT_REMEDY,
|
|
];
|
|
|
|
public static function sign($userID)
|
|
{
|
|
return SafeUtil::dbOperate(function ($userID) {
|
|
return SafeUtil::redisLockOperate(function ($userID) {
|
|
|
|
$time = time();
|
|
$day = CommUtil::dayStr($time);
|
|
$week = CommUtil::getWeek($time);
|
|
$sign = Sign::findByUserIDAndDay($userID, $day);
|
|
if (!empty($sign)) throw new StatusException(ResultStatusServices::RS_HAS_SIGN);
|
|
|
|
$weekIndex = CommUtil::getWeekIndex($time);
|
|
$config = GameConfigServices::getParamByPath('sign.' . ($weekIndex + 1));
|
|
if (empty($config)) throw new StatusException(ResultStatusServices::RS_NOT_FIND_IN_CONFIG);
|
|
|
|
$getList = [];
|
|
|
|
//日奖励
|
|
$getItem = MoneyUtil::parseItem($config['days_id'], 1, true, $userID, SystemLogUtil::FROM_TYPE_SIGN_GIFT, ['config' => $config]);
|
|
if (!empty($getItem)) array_push($getList, $getItem);
|
|
|
|
$sign = new Sign();
|
|
$sign->user_id = $userID;
|
|
$sign->sign_time = $time;
|
|
$sign->is_remedy = self::NOT_REMEDY;
|
|
$sign->week = $week;
|
|
$sign->day = $day;
|
|
if (!$sign->save()) throw new StatusException(ResultStatusServices::RS_SAVE_DB_ERROR);
|
|
|
|
//任务记录
|
|
DailyTaskUtil::addTaskCount($userID, DailyTaskUtil::TYPE_SIGN, null, 1);
|
|
|
|
AchievementUtil::addCount($userID, AchievementUtil::ID_SIGN, 1);
|
|
|
|
// //累积奖励
|
|
// $list = Sign::findAllByUserIDAndWeek($userID, $week, SORT_DESC);
|
|
// $continueCount = 0;
|
|
// $lastIndex = 0;
|
|
// for ($i = 0; $i < count($list); $i++) {
|
|
// $index = CommUtil::getWeekIndex($list[$i]->sign_time);
|
|
// if ($i == 0) $lastIndex = $index;
|
|
//
|
|
// if ($lastIndex - $index > 1) break;
|
|
//
|
|
// $continueCount++;
|
|
// }
|
|
//
|
|
// $config = GameConfigServices::getParamByPath('sign.' . $continueCount);
|
|
// if (empty($config)) return $getList;
|
|
//
|
|
// $allGetList = MoneyUtil::parseAllItem($config['accumulation_id'], 1, true, $userID, SystemLogUtil::FROM_TYPE_SIGN_CONTINUE_GIFT, ['config' => $config]);
|
|
// $getList = array_merge($getList, $allGetList);
|
|
|
|
return $getList;
|
|
|
|
}, [$userID], RedisKeyUtil::lockSign($userID));
|
|
}, [$userID]);
|
|
}
|
|
|
|
/**
|
|
* 补签
|
|
* @param $userID
|
|
* @throws StatusException
|
|
*/
|
|
public static function remedy($userID)
|
|
{
|
|
SafeUtil::dbOperate(function ($userID) {
|
|
SafeUtil::redisLockOperate(function ($userID) {
|
|
$week = CommUtil::getWeek(time());
|
|
|
|
//扣道具
|
|
$itemID = GameConfigServices::getItemIDByType2(ItemServices::ITEM_TYPE2_DEMEDY);
|
|
$log = SystemLogUtil::get($userID, SystemLogUtil::TYPE_ITEM, $itemID, -1, SystemLogUtil::FROM_TYPE_SIGN_DEMEDY, []);
|
|
MoneyUtil::checkAndModifyNum($userID, $itemID, false, -1, $log);
|
|
|
|
//补签
|
|
$list = Sign::findAllByUserIDAndWeek($userID, $week, SORT_DESC);
|
|
$time = time() - 3600 * 24;
|
|
$weekIndex = CommUtil::getWeekIndex($time);
|
|
$hasRemedy = false;
|
|
for ($i = 0; $i < count($list); $i++) {
|
|
$index = CommUtil::getWeekIndex($list[$i]->sign_time);
|
|
if ($index == CommUtil::getWeekIndex(time())) continue;
|
|
|
|
if ($weekIndex == $index) {
|
|
$weekIndex--;
|
|
$time -= 3600 * 24;//减一天时间
|
|
continue;
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
if ($weekIndex >= 0) {
|
|
//补签
|
|
$sign = new Sign();
|
|
$sign->user_id = $userID;
|
|
$sign->sign_time = $time;
|
|
$sign->is_remedy = self::REMEDY;
|
|
$sign->week = $week;
|
|
$sign->day = CommUtil::dayStr($time);
|
|
if (!$sign->save()) throw new StatusException(ResultStatusServices::RS_SAVE_DB_ERROR);
|
|
|
|
$hasRemedy = true;
|
|
}
|
|
|
|
if (!$hasRemedy) throw new StatusException(ResultStatusServices::RS_SIGN_NOT_NEED_REMEDY);
|
|
|
|
}, [$userID], RedisKeyUtil::lockSign($userID));
|
|
}, [$userID]);
|
|
}
|
|
|
|
/**
|
|
* 领取连续签到奖励
|
|
* @param $userID
|
|
* @return mixed
|
|
*/
|
|
public static function getContinueSignGift($userID, $count)
|
|
{
|
|
return SafeUtil::dbOperate(function ($userID, $count) {
|
|
return SafeUtil::redisLockOperate(function ($userID, $count) {
|
|
|
|
$count = (int)$count;
|
|
if ($count <= 0) throw new StatusException(ResultStatusServices::RS_PARAM_ERROR);
|
|
|
|
$week = CommUtil::getWeek(time());
|
|
|
|
//累积奖励
|
|
$list = Sign::findAllByUserIDAndWeek($userID, $week, SORT_DESC);
|
|
$continueCount = 0;
|
|
$lastIndex = 0;
|
|
for ($i = 0; $i < count($list); $i++) {
|
|
$index = CommUtil::getWeekIndex($list[$i]->sign_time);
|
|
if ($i == 0) $lastIndex = $index;
|
|
|
|
if ($lastIndex - $index > 1) break;
|
|
|
|
$lastIndex = $index;
|
|
$continueCount++;
|
|
}
|
|
|
|
if ($count > $continueCount) throw new StatusException(ResultStatusServices::RS_PARAM_ERROR);
|
|
|
|
if (!empty(SignContinueGift::findByUserIDAndWeekAndCount($userID, $week, $count))) throw new StatusException(ResultStatusServices::RS_PARAM_ERROR);
|
|
|
|
$config = GameConfigServices::getParamByPath('sign.' . $count);
|
|
if (empty($config)) throw new StatusException(ResultStatusServices::RS_NOT_FIND_IN_CONFIG);
|
|
|
|
$signContinueGift = new SignContinueGift();
|
|
$signContinueGift->user_id = $userID;
|
|
$signContinueGift->week = $week;
|
|
$signContinueGift->count = $count;
|
|
if (!$signContinueGift->save()) throw new StatusException(ResultStatusServices::RS_SAVE_DB_ERROR);
|
|
|
|
$getList = MoneyUtil::parseAllItem($config['accumulation_id'], 1, true, $userID, SystemLogUtil::FROM_TYPE_SIGN_CONTINUE_GIFT, ['config' => $config]);
|
|
return $getList;
|
|
|
|
}, [$userID, $count], RedisKeyUtil::lockSign($userID));
|
|
}, [$userID, $count]);
|
|
}
|
|
} |