| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- <?php
- namespace App\Module\Game\Models;
- use App\Module\Game\Enums\CONSUME_TYPE;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use UCore\ModelCore;
- /**
- * 消耗项
- *
- * field start
- * @property int $id 主键
- * @property int $group_id 消耗组ID,外键关联game_consume_groups表
- * @property int $consume_type 消耗类型(1:物品, 2:货币)
- * @property int $target_id 目标ID(物品ID、货币ID等,根据consume_type解释)
- * @property int $param1 参数1(根据consume_type不同含义,如物品的品质、货币的来源等)
- * @property int $param2 参数2(根据consume_type不同含义,如物品的绑定状态、货币的类型等)
- * @property int $quantity 数量
- * @property array $extra_data 额外数据(JSON格式,可存储特定消耗类型的额外参数)
- * @property \Carbon\Carbon $created_at 创建时间
- * @property \Carbon\Carbon $updated_at 更新时间
- * field end
- */
- class GameConsumeItem extends ModelCore
- {
- /**
- * 与模型关联的表名
- *
- * @var string
- */
- protected $table = 'game_consume_items';
- // attrlist start
- protected $fillable = [
- 'id',
- 'group_id',
- 'consume_type',
- 'target_id',
- 'param1',
- 'param2',
- 'quantity',
- 'extra_data',
- ];
- // attrlist end
- /**
- * 应该被转换为原生类型的属性
- *
- * @var array
- */
- protected $casts = [
- 'consume_type' => 'integer',
- 'target_id' => 'integer',
- 'param1' => 'integer',
- 'param2' => 'integer',
- 'quantity' => 'integer',
- 'extra_data' => 'json',
- ];
- /**
- * 获取消耗项所属的消耗组
- *
- * @return BelongsTo
- */
- public function consumeGroup(): BelongsTo
- {
- return $this->belongsTo(GameConsumeGroup::class, 'group_id', 'id');
- }
- /**
- * 获取消耗项名称(访问器)
- *
- * @return string
- */
- public function getNameAttribute(): string
- {
- return $this->getTargetName();
- }
- /**
- * 获取目标名称
- *
- * @return string
- */
- public function getTargetName(): string
- {
- switch ($this->consume_type) {
- case CONSUME_TYPE::ITEM->value:
- try {
- $itemModel = \App\Module\GameItems\Models\Item::find($this->target_id);
- return $itemModel ? $itemModel->name : "物品 (ID: {$this->target_id})";
- } catch (\Exception $e) {
- return "物品 (ID: {$this->target_id})";
- }
- case CONSUME_TYPE::CURRENCY->value:
- try {
- $currency = \App\Module\Fund\Models\FundCurrencyModel::find($this->target_id);
- return $currency ? $currency->name : "货币 (ID: {$this->target_id})";
- } catch (\Exception $e) {
- return "货币 (ID: {$this->target_id})";
- }
- case CONSUME_TYPE::FUND_CONFIG->value:
- try {
- $fund = \App\Module\Fund\Models\FundConfigModel::find($this->target_id);
- return $fund ? $fund->name : "账户种类 (ID: {$this->target_id})";
- } catch (\Exception $e) {
- return "账户种类 (ID: {$this->target_id})";
- }
- case CONSUME_TYPE::FUND_CONFIGS->value:
- try {
- $names = [];
- // 获取主账户种类名称
- if ($this->target_id > 0) {
- $fund = \App\Module\Fund\Models\FundConfigModel::find($this->target_id);
- $names[] = $fund ? $fund->name : "账户种类 (ID: {$this->target_id})";
- }
- // 获取额外账户种类名称
- if (!empty($this->extra_data) && is_array($this->extra_data) &&
- isset($this->extra_data['fund_config_ids']) && is_array($this->extra_data['fund_config_ids'])) {
- foreach ($this->extra_data['fund_config_ids'] as $id) {
- if (is_numeric($id) && $id > 0 && $id != $this->target_id) {
- $fund = \App\Module\Fund\Models\FundConfigModel::find($id);
- $names[] = $fund ? $fund->name : "账户种类 (ID: {$id})";
- }
- }
- }
- return empty($names) ? "多账户种类 (未配置)" : "多账户种类: " . implode(', ', $names);
- } catch (\Exception $e) {
- return "多账户种类 (ID: {$this->target_id})";
- }
- default:
- return "未知消耗类型 (ID: {$this->target_id})";
- }
- }
- /**
- * 转换为 Deduct protobuf 对象
- *
- * @return \Uraus\Kku\Common\Deduct
- */
- public function toDeductObject(): \Uraus\Kku\Common\Deduct
- {
- $deduct = new \Uraus\Kku\Common\Deduct();
- switch ($this->consume_type) {
- case CONSUME_TYPE::ITEM->value:
- // 创建物品扣除对象
- $deductItem = new \Uraus\Kku\Common\DeductItem([
- 'item_id' => $this->target_id,
- 'instance_id' => 0, // 默认为0,表示不指定实例
- 'quantity' => $this->quantity,
- ]);
- $deduct->setItems([$deductItem]);
- break;
- case CONSUME_TYPE::CURRENCY->value:
- case CONSUME_TYPE::FUND_CONFIG->value:
- // 创建代币扣除对象
- $deductCoin = new \Uraus\Kku\Common\DeductCoin([
- 'type' => $this->target_id,
- 'quantity' => $this->quantity,
- ]);
- $deduct->setCoins([$deductCoin]);
- break;
- case CONSUME_TYPE::FUND_CONFIGS->value:
- // 创建多个代币扣除对象
- $deductCoins = [];
- // 获取账户种类ID列表
- $fundConfigIds = [];
- if ($this->target_id > 0) {
- $fundConfigIds[] = $this->target_id;
- }
- if (!empty($this->extra_data) && is_array($this->extra_data) &&
- isset($this->extra_data['fund_config_ids']) && is_array($this->extra_data['fund_config_ids'])) {
- foreach ($this->extra_data['fund_config_ids'] as $id) {
- if (is_numeric($id) && $id > 0 && !in_array($id, $fundConfigIds)) {
- $fundConfigIds[] = (int)$id;
- }
- }
- }
- // 为每个账户种类创建扣除对象
- foreach ($fundConfigIds as $fundConfigId) {
- $deductCoins[] = new \Uraus\Kku\Common\DeductCoin([
- 'type' => $fundConfigId,
- 'quantity' => $this->quantity, // 注意:这里是总量,实际扣除时会按顺序分配
- ]);
- }
- $deduct->setCoins($deductCoins);
- break;
- }
- return $deduct;
- }
- }
|