| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459 |
- <?php
- namespace App\Module\Game\Services;
- use App\Module\Fund\Logic\User as FundLogic;
- use App\Module\Game\Enums\CONDITION_OPERATOR;
- use App\Module\Game\Enums\CONDITION_TYPE;
- use App\Module\Game\Models\GameConditionGroup;
- use App\Module\Game\Dtos\ConditionGroupDto;
- use App\Module\Game\Models\GameConditionItem;
- use App\Module\GameItems\Services\ItemService;
- use App\Module\Pet\Services\PetService;
- use Illuminate\Support\Facades\Log;
- /**
- * 条件服务类
- *
- * 提供条件组相关的服务,包括检查用户是否满足条件等功能
- */
- class ConditionService
- {
- /**
- * 获取条件组信息
- *
- * @param int|string $conditionGroupCode 条件组ID或编码
- * @param bool $withItems 是否包含条件项
- * @return ConditionGroupDto|null 条件组DTO,不存在时返回null
- */
- public static function getConditionGroup($conditionGroupCode, bool $withItems = true): ?ConditionGroupDto
- {
- try {
- // 获取条件组
- $conditionGroup = is_numeric($conditionGroupCode)
- ? GameConditionGroup::with($withItems ? ['conditionItems'] : [])->find($conditionGroupCode)
- : GameConditionGroup::with($withItems ? ['conditionItems'] : [])->where('code', $conditionGroupCode)->first();
- if (!$conditionGroup) {
- return null;
- }
- return ConditionGroupDto::fromModel($conditionGroup, $withItems);
- } catch (\Exception $e) {
- Log::error('获取条件组失败', [
- 'condition_group_code' => $conditionGroupCode,
- 'error' => $e->getMessage()
- ]);
- return null;
- }
- }
- /**
- * 检查用户是否满足条件组的条件
- *
- * @param int $userId 用户ID
- * @param string|int $conditionGroupCode 条件组编码或ID
- * @return array 检查结果,包含success字段表示是否满足条件,message字段表示错误信息
- */
- public static function checkCondition(int $userId, $conditionGroupCode): array
- {
- try {
- // 获取条件组
- $conditionGroup = is_numeric($conditionGroupCode)
- ? GameConditionGroup::find($conditionGroupCode)
- : GameConditionGroup::where('code', $conditionGroupCode)->first();
-
- if (!$conditionGroup) {
- return [
- 'success' => false,
- 'message' => "条件组不存在: {$conditionGroupCode}"
- ];
- }
-
- // 获取条件组中的所有条件项
- $conditionItems = $conditionGroup->conditionItems;
-
- if ($conditionItems->isEmpty()) {
- return [
- 'success' => true,
- 'message' => '条件组中没有条件项'
- ];
- }
-
- // 根据逻辑类型检查条件
- $logicType = $conditionGroup->logic_type;
- $failedConditions = [];
-
- foreach ($conditionItems as $item) {
- $checkResult = self::checkConditionItem($userId, $item);
-
- if ($logicType == GameConditionGroup::LOGIC_TYPE_ALL && !$checkResult['success']) {
- // 全部满足模式下,有一个不满足就返回失败
- return [
- 'success' => false,
- 'message' => $checkResult['message'],
- 'condition_item' => $item->id,
- 'details' => $checkResult
- ];
- } elseif ($logicType == GameConditionGroup::LOGIC_TYPE_ANY && $checkResult['success']) {
- // 任一满足模式下,有一个满足就返回成功
- return [
- 'success' => true,
- 'message' => '满足条件',
- 'condition_item' => $item->id,
- 'details' => $checkResult
- ];
- }
-
- if (!$checkResult['success']) {
- $failedConditions[] = [
- 'condition_item' => $item->id,
- 'details' => $checkResult
- ];
- }
- }
-
- // 处理最终结果
- if ($logicType == GameConditionGroup::LOGIC_TYPE_ALL) {
- // 全部满足模式下,走到这里说明全部满足
- return [
- 'success' => true,
- 'message' => '满足所有条件'
- ];
- } else {
- // 任一满足模式下,走到这里说明没有一个满足
- return [
- 'success' => false,
- 'message' => '不满足任何条件',
- 'failed_conditions' => $failedConditions
- ];
- }
- } catch (\Exception $e) {
- Log::error('检查条件失败', [
- 'user_id' => $userId,
- 'condition_group' => $conditionGroupCode,
- 'error' => $e->getMessage()
- ]);
-
- return [
- 'success' => false,
- 'message' => '检查条件时发生错误: ' . $e->getMessage()
- ];
- }
- }
-
- /**
- * 检查单个条件项
- *
- * @param int $userId 用户ID
- * @param GameConditionItem $conditionItem 条件项
- * @return array 检查结果
- */
- protected static function checkConditionItem(int $userId, GameConditionItem $conditionItem): array
- {
- switch ($conditionItem->condition_type) {
- case CONDITION_TYPE::LAND_LEVEL->value:
- return self::checkLandLevelCondition($userId, $conditionItem);
-
- case CONDITION_TYPE::HOUSE_LEVEL->value:
- return self::checkHouseLevelCondition($userId, $conditionItem);
-
- case CONDITION_TYPE::PET_LEVEL->value:
- return self::checkPetLevelCondition($userId, $conditionItem);
-
- case CONDITION_TYPE::ITEM_COUNT->value:
- return self::checkItemCountCondition($userId, $conditionItem);
-
- case CONDITION_TYPE::CURRENCY_COUNT->value:
- return self::checkCurrencyCountCondition($userId, $conditionItem);
-
- default:
- return [
- 'success' => false,
- 'message' => "不支持的条件类型: {$conditionItem->condition_type}"
- ];
- }
- }
-
- /**
- * 检查土地等级条件
- *
- * @param int $userId 用户ID
- * @param GameConditionItem $conditionItem 条件项
- * @return array 检查结果
- */
- protected static function checkLandLevelCondition(int $userId, GameConditionItem $conditionItem): array
- {
- try {
- // 获取用户指定类型的土地
- $landType = $conditionItem->target_id;
- $lands = \App\Module\Farm\Services\LandService::getUserLands($userId);
-
- // 计算指定类型的土地数量
- $count = 0;
- foreach ($lands as $land) {
- if ($land->landType == $landType) {
- $count++;
- }
- }
-
- // 执行比较
- $result = CONDITION_OPERATOR::compare($count, $conditionItem->value, $conditionItem->operator);
-
- if ($result) {
- return [
- 'success' => true,
- 'message' => "土地等级条件满足",
- 'actual' => $count,
- 'expected' => $conditionItem->value,
- 'operator' => $conditionItem->getOperatorSymbol()
- ];
- } else {
- return [
- 'success' => false,
- 'message' => "土地等级条件不满足,需要 {$conditionItem->getOperatorSymbol()} {$conditionItem->value},实际 {$count}",
- 'actual' => $count,
- 'expected' => $conditionItem->value,
- 'operator' => $conditionItem->getOperatorSymbol()
- ];
- }
- } catch (\Exception $e) {
- Log::error('检查土地等级条件失败', [
- 'user_id' => $userId,
- 'condition_item' => $conditionItem->id,
- 'error' => $e->getMessage()
- ]);
-
- return [
- 'success' => false,
- 'message' => '检查土地等级条件时发生错误: ' . $e->getMessage()
- ];
- }
- }
-
- /**
- * 检查房屋等级条件
- *
- * @param int $userId 用户ID
- * @param GameConditionItem $conditionItem 条件项
- * @return array 检查结果
- */
- protected static function checkHouseLevelCondition(int $userId, GameConditionItem $conditionItem): array
- {
- try {
- // 获取用户房屋等级
- $farmUser = \App\Module\Farm\Models\FarmUser::where('user_id', $userId)->first();
-
- if (!$farmUser) {
- return [
- 'success' => false,
- 'message' => "用户没有房屋"
- ];
- }
-
- $houseLevel = $farmUser->house_level;
-
- // 执行比较
- $result = CONDITION_OPERATOR::compare($houseLevel, $conditionItem->value, $conditionItem->operator);
-
- if ($result) {
- return [
- 'success' => true,
- 'message' => "房屋等级条件满足",
- 'actual' => $houseLevel,
- 'expected' => $conditionItem->value,
- 'operator' => $conditionItem->getOperatorSymbol()
- ];
- } else {
- return [
- 'success' => false,
- 'message' => "房屋等级条件不满足,需要 {$conditionItem->getOperatorSymbol()} {$conditionItem->value},实际 {$houseLevel}",
- 'actual' => $houseLevel,
- 'expected' => $conditionItem->value,
- 'operator' => $conditionItem->getOperatorSymbol()
- ];
- }
- } catch (\Exception $e) {
- Log::error('检查房屋等级条件失败', [
- 'user_id' => $userId,
- 'condition_item' => $conditionItem->id,
- 'error' => $e->getMessage()
- ]);
-
- return [
- 'success' => false,
- 'message' => '检查房屋等级条件时发生错误: ' . $e->getMessage()
- ];
- }
- }
-
- /**
- * 检查宠物等级条件
- *
- * @param int $userId 用户ID
- * @param GameConditionItem $conditionItem 条件项
- * @return array 检查结果
- */
- protected static function checkPetLevelCondition(int $userId, GameConditionItem $conditionItem): array
- {
- try {
- // 获取用户宠物
- $petId = $conditionItem->target_id;
- $userPet = PetService::getUserPet($userId, $petId);
-
- if (!$userPet) {
- return [
- 'success' => false,
- 'message' => "用户没有该宠物"
- ];
- }
-
- $petLevel = $userPet->level;
-
- // 执行比较
- $result = CONDITION_OPERATOR::compare($petLevel, $conditionItem->value, $conditionItem->operator);
-
- if ($result) {
- return [
- 'success' => true,
- 'message' => "宠物等级条件满足",
- 'actual' => $petLevel,
- 'expected' => $conditionItem->value,
- 'operator' => $conditionItem->getOperatorSymbol()
- ];
- } else {
- return [
- 'success' => false,
- 'message' => "宠物等级条件不满足,需要 {$conditionItem->getOperatorSymbol()} {$conditionItem->value},实际 {$petLevel}",
- 'actual' => $petLevel,
- 'expected' => $conditionItem->value,
- 'operator' => $conditionItem->getOperatorSymbol()
- ];
- }
- } catch (\Exception $e) {
- Log::error('检查宠物等级条件失败', [
- 'user_id' => $userId,
- 'condition_item' => $conditionItem->id,
- 'error' => $e->getMessage()
- ]);
-
- return [
- 'success' => false,
- 'message' => '检查宠物等级条件时发生错误: ' . $e->getMessage()
- ];
- }
- }
-
- /**
- * 检查物品持有数条件
- *
- * @param int $userId 用户ID
- * @param GameConditionItem $conditionItem 条件项
- * @return array 检查结果
- */
- protected static function checkItemCountCondition(int $userId, GameConditionItem $conditionItem): array
- {
- try {
- // 获取用户物品
- $itemId = $conditionItem->target_id;
- $userItems = ItemService::getUserItems($userId, ['item_id' => $itemId]);
-
- // 计算物品总数
- $totalQuantity = 0;
- foreach ($userItems as $userItem) {
- $totalQuantity += $userItem->quantity;
- }
-
- // 执行比较
- $result = CONDITION_OPERATOR::compare($totalQuantity, $conditionItem->value, $conditionItem->operator);
-
- if ($result) {
- return [
- 'success' => true,
- 'message' => "物品持有数条件满足",
- 'actual' => $totalQuantity,
- 'expected' => $conditionItem->value,
- 'operator' => $conditionItem->getOperatorSymbol()
- ];
- } else {
- return [
- 'success' => false,
- 'message' => "物品持有数条件不满足,需要 {$conditionItem->getOperatorSymbol()} {$conditionItem->value},实际 {$totalQuantity}",
- 'actual' => $totalQuantity,
- 'expected' => $conditionItem->value,
- 'operator' => $conditionItem->getOperatorSymbol()
- ];
- }
- } catch (\Exception $e) {
- Log::error('检查物品持有数条件失败', [
- 'user_id' => $userId,
- 'condition_item' => $conditionItem->id,
- 'error' => $e->getMessage()
- ]);
-
- return [
- 'success' => false,
- 'message' => '检查物品持有数条件时发生错误: ' . $e->getMessage()
- ];
- }
- }
-
- /**
- * 检查代币持有数条件
- *
- * @param int $userId 用户ID
- * @param GameConditionItem $conditionItem 条件项
- * @return array 检查结果
- */
- protected static function checkCurrencyCountCondition(int $userId, GameConditionItem $conditionItem): array
- {
- try {
- // 获取用户货币账户
- $currencyId = $conditionItem->target_id;
- $account = FundLogic::get_account($userId, $currencyId);
-
- // 检查账户是否存在
- if ($account === false) {
- return [
- 'success' => false,
- 'message' => "用户没有该货币账户"
- ];
- }
-
- $balance = $account->balance;
-
- // 执行比较
- $result = CONDITION_OPERATOR::compare($balance, $conditionItem->value, $conditionItem->operator);
-
- if ($result) {
- return [
- 'success' => true,
- 'message' => "代币持有数条件满足",
- 'actual' => $balance,
- 'expected' => $conditionItem->value,
- 'operator' => $conditionItem->getOperatorSymbol()
- ];
- } else {
- return [
- 'success' => false,
- 'message' => "代币持有数条件不满足,需要 {$conditionItem->getOperatorSymbol()} {$conditionItem->value},实际 {$balance}",
- 'actual' => $balance,
- 'expected' => $conditionItem->value,
- 'operator' => $conditionItem->getOperatorSymbol()
- ];
- }
- } catch (\Exception $e) {
- Log::error('检查代币持有数条件失败', [
- 'user_id' => $userId,
- 'condition_item' => $conditionItem->id,
- 'error' => $e->getMessage()
- ]);
-
- return [
- 'success' => false,
- 'message' => '检查代币持有数条件时发生错误: ' . $e->getMessage()
- ];
- }
- }
- }
|