60 lines
1.7 KiB
PHP
60 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace common\services;
|
|
|
|
use common\modules\systemlog\SystemLog;
|
|
use common\modules\systemlog\SystemLogUtil;
|
|
use common\utils\RedisKeyUtil;
|
|
use RedisLock\RedisLock;
|
|
use Yii;
|
|
|
|
class NewbieGuideService
|
|
{
|
|
const NEWBIE_SEED_ITEM_ID = 101018;
|
|
const NEWBIE_CROP_ID = 18;
|
|
const NEWBIE_REWARD_ITEM_ID = 105002;
|
|
const NEWBIE_REWARD_GOLD = 10;
|
|
|
|
public static function grantSeedIfNeeded($userID)
|
|
{
|
|
$userID = (int)$userID;
|
|
if ($userID <= 0) return ResultStatusServices::RS_USER_NOT_LOGIN;
|
|
|
|
$lock = new RedisLock(Yii::$app->redis, RedisKeyUtil::lockUser($userID), RedisLock::FLAG_CATCH_EXCEPTIONS);
|
|
if (!$lock->acquire(10)) return ResultStatusServices::RS_SAME_TIME_OPERATION;
|
|
|
|
try {
|
|
if (self::hasGrantedSeed($userID)) return ResultStatusServices::RS_OPERATION_SUCCESS;
|
|
|
|
$log = SystemLogUtil::get(
|
|
$userID,
|
|
SystemLogUtil::TYPE_ITEM,
|
|
self::NEWBIE_SEED_ITEM_ID,
|
|
1,
|
|
SystemLogUtil::FROM_TYPE_NEWBIE_SEED,
|
|
[]
|
|
);
|
|
|
|
return ItemServices::checkAndModifyNum($userID, self::NEWBIE_SEED_ITEM_ID, true, 1, $log);
|
|
} finally {
|
|
$lock->release();
|
|
}
|
|
}
|
|
|
|
public static function hasGrantedSeed($userID)
|
|
{
|
|
return SystemLog::find()
|
|
->where([
|
|
'user_id' => (int)$userID,
|
|
'from_type' => SystemLogUtil::FROM_TYPE_NEWBIE_SEED,
|
|
'value' => self::NEWBIE_SEED_ITEM_ID,
|
|
])
|
|
->exists();
|
|
}
|
|
|
|
public static function isNewbieCrop($cropID)
|
|
{
|
|
return (int)$cropID === self::NEWBIE_CROP_ID;
|
|
}
|
|
}
|