|
|
@@ -0,0 +1,318 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Module\Game\Services;
|
|
|
+
|
|
|
+use App\Module\Game\Enums\REWARD_TYPE;
|
|
|
+use App\Module\Game\Dtos\RewardItemDto;
|
|
|
+use App\Module\Game\Models\GameRewardItem;
|
|
|
+use App\Module\GameItems\Models\Item;
|
|
|
+use App\Module\Fund\Models\FundCurrencyModel;
|
|
|
+use App\Module\Fund\Models\FundConfigModel;
|
|
|
+use App\Module\Pet\Models\PetConfig;
|
|
|
+use App\Module\Pet\Models\PetUser;
|
|
|
+use App\Module\Farm\Enums\BUFF_TYPE;
|
|
|
+use Exception;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 奖励类型描述器
|
|
|
+ *
|
|
|
+ * 统一处理所有奖励类型的描述、名称获取等逻辑
|
|
|
+ * 避免在各个类中重复实现相同的逻辑
|
|
|
+ */
|
|
|
+class RewardTypeDescriptor
|
|
|
+{
|
|
|
+ /**
|
|
|
+ * 获取奖励目标的名称
|
|
|
+ *
|
|
|
+ * @param int $rewardType 奖励类型
|
|
|
+ * @param int $targetId 目标ID
|
|
|
+ * @param int $param1 参数1
|
|
|
+ * @param int $param2 参数2
|
|
|
+ * @return string 目标名称
|
|
|
+ */
|
|
|
+ public static function getTargetName(int $rewardType, int $targetId, int $param1 = 0, int $param2 = 0): string
|
|
|
+ {
|
|
|
+ switch ($rewardType) {
|
|
|
+ case REWARD_TYPE::ITEM->value:
|
|
|
+ return self::getItemName($targetId);
|
|
|
+
|
|
|
+ case REWARD_TYPE::CURRENCY->value:
|
|
|
+ return self::getCurrencyName($targetId);
|
|
|
+
|
|
|
+ case REWARD_TYPE::FUND_CONFIG->value:
|
|
|
+ return self::getFundConfigName($targetId);
|
|
|
+
|
|
|
+ case REWARD_TYPE::PET_EXP->value:
|
|
|
+ return self::getPetExpName($targetId);
|
|
|
+
|
|
|
+ case REWARD_TYPE::PET_ENERGY->value:
|
|
|
+ return self::getPetEnergyName($targetId);
|
|
|
+
|
|
|
+ case REWARD_TYPE::PET->value:
|
|
|
+ return self::getPetName($targetId);
|
|
|
+
|
|
|
+ case REWARD_TYPE::PET_POWER->value:
|
|
|
+ return self::getPetPowerName($targetId);
|
|
|
+
|
|
|
+ case REWARD_TYPE::FARM_SHRINE->value:
|
|
|
+ return self::getFarmShrineName($targetId, $param2);
|
|
|
+
|
|
|
+ case REWARD_TYPE::SKIN->value:
|
|
|
+ return self::getSkinName($targetId);
|
|
|
+
|
|
|
+ case REWARD_TYPE::OTHER->value:
|
|
|
+ return "其他奖励 (ID: {$targetId})";
|
|
|
+
|
|
|
+ default:
|
|
|
+ return "未知奖励类型 (ID: {$targetId})";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从RewardItemDto获取目标名称
|
|
|
+ *
|
|
|
+ * @param RewardItemDto $item 奖励项DTO
|
|
|
+ * @return string 目标名称
|
|
|
+ */
|
|
|
+ public static function getTargetNameFromDto(RewardItemDto $item): string
|
|
|
+ {
|
|
|
+ return self::getTargetName($item->rewardType, $item->targetId, $item->param1, $item->param2);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从GameRewardItem模型获取目标名称
|
|
|
+ *
|
|
|
+ * @param GameRewardItem $item 奖励项模型
|
|
|
+ * @return string 目标名称
|
|
|
+ */
|
|
|
+ public static function getTargetNameFromModel(GameRewardItem $item): string
|
|
|
+ {
|
|
|
+ return self::getTargetName($item->reward_type, $item->target_id, $item->param1, $item->param2);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 格式化奖励项显示
|
|
|
+ *
|
|
|
+ * @param int $rewardType 奖励类型
|
|
|
+ * @param int $targetId 目标ID
|
|
|
+ * @param int $quantity 数量
|
|
|
+ * @param int $param1 参数1
|
|
|
+ * @param int $param2 参数2
|
|
|
+ * @param bool $withBadge 是否包含徽章样式
|
|
|
+ * @return string 格式化后的显示文本
|
|
|
+ */
|
|
|
+ public static function formatRewardDisplay(int $rewardType, int $targetId, int $quantity, int $param1 = 0, int $param2 = 0, bool $withBadge = true): string
|
|
|
+ {
|
|
|
+ $rewardTypeName = REWARD_TYPE::getName($rewardType);
|
|
|
+ $targetName = self::getTargetName($rewardType, $targetId, $param1, $param2);
|
|
|
+
|
|
|
+ if ($withBadge) {
|
|
|
+ return "<span class=\"badge badge-info\">{$rewardTypeName}</span> {$targetName} × {$quantity}";
|
|
|
+ } else {
|
|
|
+ return "• {$rewardTypeName}: {$targetName} × {$quantity}";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从RewardItemDto格式化显示
|
|
|
+ *
|
|
|
+ * @param RewardItemDto $item 奖励项DTO
|
|
|
+ * @param bool $withBadge 是否包含徽章样式
|
|
|
+ * @return string 格式化后的显示文本
|
|
|
+ */
|
|
|
+ public static function formatRewardDisplayFromDto(RewardItemDto $item, bool $withBadge = true): string
|
|
|
+ {
|
|
|
+ return self::formatRewardDisplay(
|
|
|
+ $item->rewardType,
|
|
|
+ $item->targetId,
|
|
|
+ $item->quantity,
|
|
|
+ $item->param1,
|
|
|
+ $item->param2,
|
|
|
+ $withBadge
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从GameRewardItem模型格式化显示
|
|
|
+ *
|
|
|
+ * @param GameRewardItem $item 奖励项模型
|
|
|
+ * @param bool $withBadge 是否包含徽章样式
|
|
|
+ * @return string 格式化后的显示文本
|
|
|
+ */
|
|
|
+ public static function formatRewardDisplayFromModel(GameRewardItem $item, bool $withBadge = true): string
|
|
|
+ {
|
|
|
+ // 处理随机数量显示
|
|
|
+ $quantityText = self::formatQuantityText($item);
|
|
|
+
|
|
|
+ $rewardTypeName = REWARD_TYPE::getName($item->reward_type);
|
|
|
+ $targetName = self::getTargetNameFromModel($item);
|
|
|
+
|
|
|
+ if ($withBadge) {
|
|
|
+ return "<span class=\"badge badge-info\">{$rewardTypeName}</span> {$targetName} × {$quantityText}";
|
|
|
+ } else {
|
|
|
+ return "• {$rewardTypeName}: {$targetName} × {$quantityText}";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 格式化数量文本(支持随机数量)
|
|
|
+ *
|
|
|
+ * @param GameRewardItem $item 奖励项模型
|
|
|
+ * @return string 数量文本
|
|
|
+ */
|
|
|
+ public static function formatQuantityText(GameRewardItem $item): string
|
|
|
+ {
|
|
|
+ if ($item->min_quantity !== null && $item->max_quantity !== null) {
|
|
|
+ if ($item->min_quantity == $item->max_quantity) {
|
|
|
+ return (string)$item->min_quantity;
|
|
|
+ } else {
|
|
|
+ return "{$item->min_quantity}-{$item->max_quantity}";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return (string)$item->quantity;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取物品名称
|
|
|
+ *
|
|
|
+ * @param int $itemId 物品ID
|
|
|
+ * @return string 物品名称
|
|
|
+ */
|
|
|
+ private static function getItemName(int $itemId): string
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $item = Item::find($itemId);
|
|
|
+ return $item ? $item->name : "物品 (ID: {$itemId})";
|
|
|
+ } catch (Exception $e) {
|
|
|
+ return "物品 (ID: {$itemId})";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取货币名称
|
|
|
+ *
|
|
|
+ * @param int $currencyId 货币ID
|
|
|
+ * @return string 货币名称
|
|
|
+ */
|
|
|
+ private static function getCurrencyName(int $currencyId): string
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $currency = FundCurrencyModel::find($currencyId);
|
|
|
+ return $currency ? $currency->name : "货币 (ID: {$currencyId})";
|
|
|
+ } catch (Exception $e) {
|
|
|
+ return "货币 (ID: {$currencyId})";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取账户种类名称
|
|
|
+ *
|
|
|
+ * @param int $fundConfigId 账户种类ID
|
|
|
+ * @return string 账户种类名称
|
|
|
+ */
|
|
|
+ private static function getFundConfigName(int $fundConfigId): string
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $fund = FundConfigModel::find($fundConfigId);
|
|
|
+ return $fund ? $fund->name : "代币账户 (ID: {$fundConfigId})";
|
|
|
+ } catch (Exception $e) {
|
|
|
+ return "代币账户 (ID: {$fundConfigId})";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取宠物经验名称
|
|
|
+ *
|
|
|
+ * @param int $petId 宠物ID
|
|
|
+ * @return string 宠物经验名称
|
|
|
+ */
|
|
|
+ private static function getPetExpName(int $petId): string
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $pet = PetConfig::find($petId);
|
|
|
+ return $pet ? "{$pet->name}经验" : "宠物经验 (宠物ID: {$petId})";
|
|
|
+ } catch (Exception $e) {
|
|
|
+ return "宠物经验 (宠物ID: {$petId})";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取宠物体力名称
|
|
|
+ *
|
|
|
+ * @param int $petId 宠物ID
|
|
|
+ * @return string 宠物体力名称
|
|
|
+ */
|
|
|
+ private static function getPetEnergyName(int $petId): string
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $pet = PetConfig::find($petId);
|
|
|
+ return $pet ? "{$pet->name}体力" : "宠物体力 (宠物ID: {$petId})";
|
|
|
+ } catch (Exception $e) {
|
|
|
+ return "宠物体力 (宠物ID: {$petId})";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取宠物名称
|
|
|
+ *
|
|
|
+ * @param int $petConfigId 宠物配置ID
|
|
|
+ * @return string 宠物名称
|
|
|
+ */
|
|
|
+ private static function getPetName(int $petConfigId): string
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $pet = PetConfig::find($petConfigId);
|
|
|
+ return $pet ? "宠物 ({$pet->name})" : "宠物 (配置ID: {$petConfigId})";
|
|
|
+ } catch (Exception $e) {
|
|
|
+ return "宠物 (配置ID: {$petConfigId})";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取宠物体力名称(用户宠物)
|
|
|
+ *
|
|
|
+ * @param int $petUserId 用户宠物ID
|
|
|
+ * @return string 宠物体力名称
|
|
|
+ */
|
|
|
+ private static function getPetPowerName(int $petUserId): string
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $petUser = PetUser::find($petUserId);
|
|
|
+ return $petUser ? "宠物体力 ({$petUser->name})" : "宠物体力 (用户宠物ID: {$petUserId})";
|
|
|
+ } catch (Exception $e) {
|
|
|
+ return "宠物体力 (用户宠物ID: {$petUserId})";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取农场神像名称
|
|
|
+ *
|
|
|
+ * @param int $shrineType 神像类型
|
|
|
+ * @param int $durationHours 持续时间(小时)
|
|
|
+ * @return string 神像名称
|
|
|
+ */
|
|
|
+ private static function getFarmShrineName(int $shrineType, int $durationHours = 24): string
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $shrineName = BUFF_TYPE::getName($shrineType);
|
|
|
+ $duration = $durationHours > 0 ? $durationHours : 24;
|
|
|
+ return "{$shrineName} ({$duration}小时)";
|
|
|
+ } catch (Exception $e) {
|
|
|
+ return "神像 (类型: {$shrineType})";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取皮肤名称
|
|
|
+ *
|
|
|
+ * @param int $skinId 皮肤ID
|
|
|
+ * @return string 皮肤名称
|
|
|
+ */
|
|
|
+ private static function getSkinName(int $skinId): string
|
|
|
+ {
|
|
|
+ return SkinService::getSkinName($skinId);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|