'boolean', 'random_count' => 'integer', 'reward_mode' => 'integer', ]; /** * 获取奖励组中的所有奖励项 * * @return HasMany */ public function rewardItems(): HasMany { return $this->hasMany(GameRewardItem::class, 'group_id', 'id'); } /** * 获取关联的标签(多态关联) * * @return \Illuminate\Database\Eloquent\Relations\MorphToMany */ public function tags() { return $this->morphToMany(GameTag::class, 'taggable', 'game_tag_relations', 'taggable_id', 'tag_id'); } /** * 格式化奖励详情用于显示 * * @return string */ public function formatRewardDetails(): string { if ($this->rewardItems->isEmpty()) { return '暂无奖励项'; } $details = []; foreach ($this->rewardItems as $item) { $detail = $this->formatSingleRewardItem($item); $details[] = $detail; } return '
' . implode('
', $details) . '
'; } /** * 格式化标签用于显示 * * @return string */ public function formatTags(): string { if ($this->tags->isEmpty()) { return '无标签'; } $tagHtml = []; foreach ($this->tags as $tag) { $tagHtml[] = $tag->formatTag(); } return implode(' ', $tagHtml); } /** * 格式化单个奖励项 * * @param GameRewardItem $item * @return string */ private function formatSingleRewardItem(GameRewardItem $item): string { $rewardTypeName = \App\Module\Game\Enums\REWARD_TYPE::getName($item->reward_type); $targetName = $this->getTargetName($item); // 处理随机数量显示 $quantityText = $this->formatQuantityText($item); $weight = $item->weight; $guaranteed = $item->is_guaranteed ? '必中' : '非必中'; return sprintf( '%s %s × %s (权重: %.2f, %s)', $rewardTypeName, $targetName, $quantityText, $weight, $guaranteed ); } /** * 格式化数量文本 * * @param GameRewardItem $item * @return string */ private function formatQuantityText(GameRewardItem $item): string { // 如果设置了最小和最大数量,显示范围 if ($item->min_quantity > 0 && $item->max_quantity > 0) { if ($item->min_quantity == $item->max_quantity) { return (string)$item->min_quantity; } else { return $item->min_quantity . '-' . $item->max_quantity; } } if( $item->min_quantity >0 || $item->max_quantity > 0){ // 如果只设置了最小数量 if ($item->min_quantity == 0) { return ' 数量错误 ,缺少 最大值'; } // 如果只设置了最大数量 if ($item->max_quantity == 0) { return ' 数量错误 ,缺少 最小值'; } } // 默认使用固定数量 return (string)$item->quantity; } /** * 获取目标名称 * * @param GameRewardItem $item * @return string */ private function getTargetName(GameRewardItem $item): string { // dump($item); switch ($item->reward_type) { case \App\Module\Game\Enums\REWARD_TYPE::ITEM->value: try { $itemModel = \App\Module\GameItems\Models\Item::find($item->target_id); return $itemModel ? $itemModel->name : "物品 (ID: {$item->target_id})"; } catch (\Exception $e) { return "物品 (ID: {$item->target_id})"; } case \App\Module\Game\Enums\REWARD_TYPE::CURRENCY->value: try { $currency = \App\Module\Fund\Models\FundCurrencyModel::find($item->target_id); return $currency ? $currency->name : "货币 (ID: {$item->target_id})"; } catch (\Exception $e) { return "货币 (ID: {$item->target_id})"; } case \App\Module\Game\Enums\REWARD_TYPE::FUND_CONFIG->value: try { $fund = \App\Module\Fund\Models\FundConfigModel::find($item->target_id); return $fund ? $fund->name : "账户种类 (ID: {$item->target_id})"; } catch (\Exception $e) { return "账户种类 (ID: {$item->target_id})"; } case \App\Module\Game\Enums\REWARD_TYPE::PET_EXP->value: return "宠物经验 (宠物ID: {$item->target_id})"; case \App\Module\Game\Enums\REWARD_TYPE::PET_ENERGY->value: return "宠物体力 (宠物ID: {$item->target_id})"; case \App\Module\Game\Enums\REWARD_TYPE::PET->value: $pet = \App\Module\Pet\Models\PetConfig::find($item->target_id); return "宠物 (宠物: {$pet->name})"; case \App\Module\Game\Enums\REWARD_TYPE::FARM_SHRINE->value: try { $shrineName = \App\Module\Farm\Enums\BUFF_TYPE::getName($item->target_id); $durationHours = $item->param2 > 0 ? $item->param2 : 24; return "{$shrineName} ({$durationHours}小时)"; } catch (\Exception $e) { return "神像 (类型: {$item->target_id})"; } case \App\Module\Game\Enums\REWARD_TYPE::OTHER->value: return "其他奖励 (ID: {$item->target_id})"; default: return "未知奖励类型 (ID: {$item->target_id})"; } } }