| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- <?php
- namespace App\Module\Game\Models;
- use Illuminate\Database\Eloquent\Relations\HasMany;
- use UCore\ModelCore;
- /**
- * 奖励组
- *
- * field start
- * @property int $id 主键
- * @property string $name 奖励组名称
- * @property string $code 奖励组编码(唯一)
- * @property string $description 奖励组描述
- * @property bool $is_random 是否随机发放(0:全部发放, 1:随机发放)
- * @property int $random_count 随机发放时的奖励数量
- * @property int $reward_mode 奖励模式(1:权重选择模式, 2:独立概率模式)
- * @property \Carbon\Carbon $created_at 创建时间
- * @property \Carbon\Carbon $updated_at 更新时间
- * field end
- */
- class GameRewardGroup extends ModelCore
- {
- /**
- * 与模型关联的表名
- *
- * @var string
- */
- protected $table = 'game_reward_groups';
- // attrlist start
- protected $fillable = [
- 'id',
- 'name',
- 'code',
- 'description',
- 'is_random',
- 'random_count',
- 'reward_mode',
- ];
- // attrlist end
- /**
- * 应该被转换为原生类型的属性
- *
- * @var array
- */
- protected $casts = [
- 'is_random' => '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 '<span class="text-muted">暂无奖励项</span>';
- }
- $details = [];
- foreach ($this->rewardItems as $item) {
- $detail = $this->formatSingleRewardItem($item);
- $details[] = $detail;
- }
- return '<div class="reward-details">' . implode('<br>', $details) . '</div>';
- }
- /**
- * 格式化标签用于显示
- *
- * @return string
- */
- public function formatTags(): string
- {
- if ($this->tags->isEmpty()) {
- return '<span class="text-muted">无标签</span>';
- }
- $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(
- '<span class="badge badge-info">%s</span> %s × %s (权重: %.2f, %s)',
- $rewardTypeName,
- $targetName,
- $quantityText,
- $weight,
- $guaranteed
- );
- }
- /**
- * 格式化数量文本
- *
- * @param GameRewardItem $item
- * @return string
- */
- private 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;
- }
- }
- // 如果只设置了最小数量
- if ($item->min_quantity !== null) {
- return $item->min_quantity . '+';
- }
- // 如果只设置了最大数量
- if ($item->max_quantity !== null) {
- return '1-' . $item->max_quantity;
- }
- // 默认使用固定数量
- 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})";
- }
- }
- }
|