155 lines
4.7 KiB
PHP
155 lines
4.7 KiB
PHP
<?php
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: zn
|
|
* Date: 2017/6/2
|
|
* Time: 10:35
|
|
*/
|
|
|
|
namespace common\services;
|
|
|
|
|
|
use common\models\Friend;
|
|
use common\models\Interaction;
|
|
use common\models\Muchang;
|
|
use common\modules\achievement\AchievementUtil;
|
|
use common\modules\vip\vo\CardUtil;
|
|
use common\utils\EnumUtil;
|
|
use common\utils\RedisKeyUtil;
|
|
use RedisLock\RedisLock;
|
|
use Yii;
|
|
use yii\base\Object;
|
|
|
|
/**
|
|
* 农田帮助类
|
|
* Class LandService
|
|
* @package common\services
|
|
*/
|
|
class MuchangService extends Object
|
|
{
|
|
|
|
|
|
/**
|
|
* 播种
|
|
* @param $farmID
|
|
* @param $position
|
|
* @param $itemID
|
|
* @return int
|
|
*/
|
|
public static function sowSeeds(Muchang $MuchangDbData,$itemID,$userId)
|
|
{
|
|
|
|
|
|
try{
|
|
$itemConfig = GameConfigServices::getParamByPath('item_list.' . $itemID);
|
|
if (empty($itemConfig)) return ResultStatusServices::RS_NOT_FIND_IN_CONFIG;
|
|
|
|
$cropConfig = GameConfigServices::getParamByPath('crop_list.' . $itemConfig['effect_id']);
|
|
if (empty($cropConfig)) return ResultStatusServices::RS_NOT_FIND_IN_CONFIG;
|
|
|
|
//修改农田数据
|
|
$MuchangDbData->crop_config_id = $cropConfig['id'];
|
|
$MuchangDbData->start_time = time();
|
|
$MuchangDbData->updated_at = time();
|
|
$MuchangDbData->created_at = time();
|
|
$MuchangDbData->type = 104;
|
|
$MuchangDbData->user_id = $userId;
|
|
$MuchangDbData->gather_time = time() + intval($cropConfig['seedling_time']) + intval($cropConfig['grow_time']) + intval($cropConfig['ripe_time']);
|
|
|
|
$rs= $MuchangDbData->save();
|
|
} finally {
|
|
|
|
}
|
|
return $rs;
|
|
}
|
|
|
|
|
|
/**
|
|
* 更新所有动物状态
|
|
* @param $farmID
|
|
* @param $farmLevel
|
|
* @return int
|
|
*/
|
|
public static function updateAllCropStatus($userID)
|
|
{
|
|
$MuDbList = Muchang::findAll(['user_id' => $userID]);
|
|
foreach ($MuDbList as $mu) {
|
|
|
|
//检查是否需要更新
|
|
if ($mu->is_ripe == 0) {
|
|
$cropConfig = GameConfigServices::getParamByPath('crop_list.' . $mu->crop_config_id);
|
|
|
|
|
|
//配置表生长周期是单个周期的时间差,为了方便计算,把生长周期变成从播种开始的时间
|
|
$time= $mu['start_time'] + $cropConfig['seedling_time'] + $cropConfig['grow_time'] + $cropConfig['ripe_time'];
|
|
if(time()>$time){
|
|
//更新单个动物状态
|
|
|
|
$output=$cropConfig['output'];
|
|
$meat=$cropConfig['meat'];
|
|
$muchangDbData = Muchang::findOne(['id' => $mu['id']]);
|
|
$muchangDbData->num=$meat;
|
|
$muchangDbData->output=$output;
|
|
$muchangDbData->is_ripe=1;
|
|
$muchangDbData->is_output=1;
|
|
$muchangDbData->save();
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
return ResultStatusServices::RS_OPERATION_SUCCESS;
|
|
}
|
|
|
|
/**
|
|
* 偷菜
|
|
* @param $farmID
|
|
* @param $position
|
|
* @param $stealCount
|
|
* @return int
|
|
*/
|
|
public static function steal($id, &$stealCount)
|
|
{
|
|
//加锁
|
|
$lock = new RedisLock(Yii::$app->redis, RedisKeyUtil::lockLand($id,$id), RedisLock::FLAG_CATCH_EXCEPTIONS);
|
|
if (!$lock->acquire(10)) return ResultStatusServices::RS_SAME_TIME_OPERATION;
|
|
|
|
try {
|
|
$muchangDbData = Muchang::findByID($id);
|
|
if (!$muchangDbData) return ResultStatusServices::RS_NOT_FIND_IN_DB;
|
|
|
|
//检查农作物
|
|
if ($muchangDbData->crop_config_id == 0) return ResultStatusServices::RS_LAND_NOT_CROP;
|
|
|
|
//农作物是否成熟状态
|
|
if ($muchangDbData->is_ripe != 1) return ResultStatusServices::RS_CROP_NOT_RIPE;
|
|
|
|
//农作物是否被收获过
|
|
if ($muchangDbData->has_gather == 1) return ResultStatusServices::RS_CROP_HAD_GATHER;
|
|
|
|
//可偷取数量
|
|
$stealCount = GameConfigServices::getCommonNumByID(27);
|
|
//保底产量比例
|
|
$baodibili = GameConfigServices::getCommonNumByID(26);
|
|
//收获保底数量
|
|
if ($muchangDbData->output - $stealCount < ($muchangDbData->output*$baodibili*0.01))
|
|
$stealCount = $muchangDbData->output*$baodibili*0.01;
|
|
|
|
//没有可偷取的数量
|
|
if ($stealCount <= 0)
|
|
return ResultStatusServices::RS_CROP_STEAL_NOT_NUM;
|
|
|
|
//偷取
|
|
$muchangDbData->output -= $stealCount;
|
|
$muchangDbData->steal_count += $stealCount;
|
|
//保存
|
|
if (!$muchangDbData->save()) ResultStatusServices::RS_SAVE_DB_ERROR;
|
|
|
|
} finally {
|
|
$lock->release();
|
|
}
|
|
|
|
return ResultStatusServices::RS_OPERATION_SUCCESS;
|
|
}
|
|
|
|
} |