| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- <?php
- namespace App\Module\AppGame\Handler\Land;
- use App\Module\AppGame\Handler\BaseHandler;
- use App\Module\Farm\Services\CropService;
- use App\Module\Farm\Validations\FertilizerValidation;
- use App\Module\GameItems\Services\ItemService;
- use Google\Protobuf\Internal\Message;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Uraus\Kku\Request\RequestLandFertilizer;
- use Uraus\Kku\Response\ResponseLandFertilizer;
- use UCore\Exception\LogicException;
- /**
- * 处理施肥操作请求
- */
- class FertilizerHandler extends BaseHandler
- {
- /**
- * 是否需要登录
- *
- * @var bool
- */
- protected bool $need_login = true;
- /**
- * 处理施肥操作请求
- *
- * @param RequestLandFertilizer $data 施肥操作请求数据
- * @return Message 施肥操作响应
- */
- public function handle(Message $data): Message
- {
- // 获取请求参数
- $landId = $data->getLandId();
- $itemId = $data->getItemId();
- $userId = $this->user_id;
- Log::info('施肥操作开始', [
- 'user_id' => $userId,
- 'land_id' => $landId,
- 'item_id' => $itemId,
- ]);
- // 使用FertilizerValidation进行数据验证
- Log::info('FertilizerHandler: 开始验证', [
- 'user_id' => $userId,
- 'land_id' => $landId,
- 'item_id' => $itemId,
- ]);
- $validation = new FertilizerValidation();
- $validationData = [
- 'user_id' => $userId,
- 'land_id' => $landId,
- 'item_id' => $itemId,
- ];
- Log::info('FertilizerHandler: 调用validation->validate()');
- $validationResult = $validation->validate($validationData);
- Log::info('FertilizerHandler: 验证结果', [
- 'result' => $validationResult,
- 'errors' => $validation->getErrors()
- ]);
- if (!$validationResult) {
- $errors = $validation->getErrors();
- $errorMessage = implode(', ', $errors);
- Log::warning('施肥验证失败', [
- 'user_id' => $userId,
- 'land_id' => $landId,
- 'item_id' => $itemId,
- 'errors' => $errors,
- ]);
- throw new LogicException($errorMessage);
- }
- try {
- DB::beginTransaction();
- // 从验证结果中获取数据,避免重复查询
- $cropGrowthTime = $validation->crop_growth_time;
- // 使用肥料(使用已验证的数据)
- $result = CropService::useFertilizer($userId, $landId, $cropGrowthTime);
- if ($result->error) {
- throw new LogicException("施肥失败:" . $result->message);
- }
- // 消耗物品
- ItemService::consumeItem($userId, $itemId, null, 1, [
- 'source_type' => 'land_fertilizer',
- 'source_id' => $landId,
- 'details' => ['land_id' => $landId]
- ]);
- DB::commit();
- Log::info('施肥操作成功', [
- 'user_id' => $userId,
- 'land_id' => $landId,
- 'item_id' => $itemId,
- 'crop_growth_time' => $cropGrowthTime,
- ]);
- // 更新作物生长阶段
- CropService::updateGrowthStage($result->data['crop_id']);
- } catch (\Exception $e) {
- DB::rollBack();
- Log::error('施肥操作失败', [
- 'user_id' => $userId,
- 'land_id' => $landId,
- 'item_id' => $itemId,
- 'error' => $e->getMessage(),
- ]);
- throw $e;
- }
- // 创建响应对象
- $response = new ResponseLandFertilizer();
- return $response;
- }
- }
|