notfff 7 months ago
parent
commit
3032d9c90f

+ 1 - 1
.env.example

@@ -4,7 +4,7 @@ APP_KEY=
 APP_DEBUG=true
 APP_TIMEZONE=UTC
 APP_URL=http://localhost
-UNITTEST_URL=http://localhost:8000
+UNITTEST_URL=http://kku_laravel.local.gd
 
 APP_LOCALE=en
 APP_FALLBACK_LOCALE=en

+ 0 - 17
app/Module/AppGame/Handler/Land/FertilizerHandler.php

@@ -97,23 +97,6 @@ class FertilizerHandler extends BaseHandler
             // 更新作物生长阶段(在事务外执行)
             CropService::updateGrowthStage($result->data['crop_id']);
 
-        } catch (LogicException $e) {
-            // 业务逻辑异常,需要回滚事务
-            if (DB::transactionLevel() > 0) {
-                DB::rollBack();
-            }
-
-            // 设置错误响应
-            $this->response->setCode(400);
-            $this->response->setMsg($e->getMessage());
-
-            Log::warning('用户施肥失败', [
-                'user_id' => $userId,
-                'land_id' => $landId,
-                'item_id' => $itemId,
-                'error' => $e->getMessage(),
-                'trace' => $e->getTraceAsString()
-            ]);
         } catch (\Exception $e) {
             // 系统异常,需要回滚事务
             if (DB::transactionLevel() > 0) {

+ 10 - 20
app/Module/Farm/Logics/CropLogic.php

@@ -357,19 +357,18 @@ class CropLogic
             }
 
             // 防错误机制:基本状态检查,避免意外执行
-            if ($land->status !== LAND_STATUS::PLANTING->valueInt()) {
-                Log::warning('土地状态异常,但继续执行施肥', [
-                    'land_id' => $landId,
-                    'expected_status' => LAND_STATUS::PLANTING->valueInt(),
-                    'actual_status' => $land->status
-                ]);
-            }
+            // 不进行土地状态验证,只进行作物状态验证
+//            if ($land->status !== LAND_STATUS::PLANTING->valueInt()) {
+//                Log::warning('土地状态异常,但继续执行施肥', [
+//                    'land_id' => $landId,
+//                    'expected_status' => LAND_STATUS::PLANTING->valueInt(),
+//                    'actual_status' => $land->status
+//                ]);
+//            }
 
             if ($crop->fertilized) {
-                Log::warning('作物已施肥,但继续执行', [
-                    'crop_id' => $crop->id,
-                    'fertilized' => $crop->fertilized
-                ]);
+                throw new \Exception('作物已施肥');
+
             }
 
             // 更新作物信息
@@ -407,15 +406,6 @@ class CropLogic
 
             $crop->save();
 
-            // 触发土地状态变更事件,确保前端能够获取到土地状态变更
-            // 虽然土地状态没有实际变化,但我们需要通知前端作物状态已更新
-            event(new LandStatusChangedEvent(
-                $userId,
-                $landId,
-                $land->status,  // 旧状态
-                $land->status   // 新状态(实际上没有变化,但需要触发事件)
-            ));
-
             Log::info('使用化肥成功', [
                 'user_id' => $userId,
                 'land_id' => $landId,

+ 5 - 4
app/Module/Farm/Validators/FertilizerUsageValidator.php

@@ -43,10 +43,11 @@ class FertilizerUsageValidator extends Validator
             }
 
             // 检查土地状态是否为种植中
-            if ($land->status !== LAND_STATUS::PLANTING->valueInt()) {
-                $this->addError('土地状态不允许使用肥料');
-                return false;
-            }
+            // 不需要对土地状态进行验证
+//            if ($land->status !== LAND_STATUS::PLANTING->valueInt()) {
+//                $this->addError('土地状态不允许使用肥料');
+//                return false;
+//            }
 
             // 获取作物信息
             $crop = FarmCrop::where('land_id', $value)->first();

+ 10 - 3
tests/Dev/TestShifei.php

@@ -3,22 +3,29 @@
 namespace Dev;
 
 use Tests\TestCase;
+use Tests\Unit\ProtoJsonRequestTest;
 
 /**
  * 测试施肥
  *
  */
-class TestShifei extends TestCase
+class TestShifei extends ProtoJsonRequestTest
 {
 
     public $token = '';
 
     public function test_1()
     {
-
+        $resp = $this->protobufRequest();
+        // 必要的验证
+        dump($resp->serializeToJsonString());
     }
 
-    private function requestProtobuf()
+    /**
+     * 获取请求的Protobuf数据
+     * @return string
+     */
+    public function requestProtobufJson():string
     {
 
         return '{

+ 13 - 0
tests/Unit/ProtoJsonRequest.php

@@ -0,0 +1,13 @@
+<?php
+
+namespace Tests\Unit;
+
+use Google\Protobuf\Internal\Message;
+
+interface ProtoJsonRequest
+{
+
+    public function requestProtobufJson():string;
+
+
+}

+ 8 - 10
tests/Unit/ProtoJsonRequestTest.php

@@ -12,7 +12,7 @@ use Uraus\Kku\Response;
  * protobuf请求测试
  *
  */
-abstract class ProtoJsonRequestTest extends \Tests\TestCase implements ProtoRequest
+abstract class ProtoJsonRequestTest extends \Tests\TestCase implements ProtoJsonRequest
 {
 
     protected Client $client;
@@ -47,21 +47,19 @@ abstract class ProtoJsonRequestTest extends \Tests\TestCase implements ProtoRequ
         Log::info('准备 测试数据');
 
         // 1. 准备 Protobuf 请求数据
-        $requestData    = $this->create_request_protobuf();
-        $name           = substr(get_class($requestData), 17);
-        $requestDatabin = $requestData->serializeToString();
+        $requestJson    = $this->requestProtobufJson();
         Log::info('序列化请求数据完成', [
-            'data_length'  => strlen($requestDatabin),
-            'request_data' => $requestData->serializeToJsonString()
+            'data_length'  => strlen($requestJson),
+            'request_data' => $requestJson
         ]);
 
         // 2. 发送真实的 HTTP 请求
         Log::info('发送 HTTP 请求');
         $response = $this->client->post('/gameapi', [
-            'body'    => $requestDatabin,
+            'body'    => $requestJson,
             'headers' => [
-                'Content-Type' => 'application/x-protobuf',
-                'Accept'       => 'application/x-protobuf'
+                'Content-Type' => 'application/json',
+                'Accept'       => 'application/json'
             ]
         ]);
 
@@ -80,7 +78,7 @@ abstract class ProtoJsonRequestTest extends \Tests\TestCase implements ProtoRequ
         ]);
 
         $protoResponse = new Response();
-        $protoResponse->mergeFromString($responseContent);
+        $protoResponse->mergeFromJsonString($responseContent);
 
         Log::info('测试完成');