GameConsumeItem.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace App\Module\Game\Models;
  3. use App\Module\Game\Enums\CONSUME_TYPE;
  4. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  5. use UCore\ModelCore;
  6. /**
  7. * 消耗项
  8. *
  9. * field start
  10. * @property int $id 主键
  11. * @property int $group_id 消耗组ID,外键关联game_consume_groups表
  12. * @property int $consume_type 消耗类型(1:物品, 2:货币)
  13. * @property int $target_id 目标ID(物品ID、货币ID等,根据consume_type解释)
  14. * @property int $param1 参数1(根据consume_type不同含义,如物品的品质、货币的来源等)
  15. * @property int $param2 参数2(根据consume_type不同含义,如物品的绑定状态、货币的类型等)
  16. * @property int $quantity 数量
  17. * @property array $extra_data 额外数据(JSON格式,可存储特定消耗类型的额外参数)
  18. * @property \Carbon\Carbon $created_at 创建时间
  19. * @property \Carbon\Carbon $updated_at 更新时间
  20. * field end
  21. */
  22. class GameConsumeItem extends ModelCore
  23. {
  24. /**
  25. * 与模型关联的表名
  26. *
  27. * @var string
  28. */
  29. protected $table = 'game_consume_items';
  30. // attrlist start
  31. protected $fillable = [
  32. 'id',
  33. 'group_id',
  34. 'consume_type',
  35. 'target_id',
  36. 'param1',
  37. 'param2',
  38. 'quantity',
  39. 'extra_data',
  40. ];
  41. // attrlist end
  42. /**
  43. * 应该被转换为原生类型的属性
  44. *
  45. * @var array
  46. */
  47. protected $casts = [
  48. 'consume_type' => 'integer',
  49. 'target_id' => 'integer',
  50. 'param1' => 'integer',
  51. 'param2' => 'integer',
  52. 'quantity' => 'integer',
  53. 'extra_data' => 'json',
  54. ];
  55. /**
  56. * 获取消耗项所属的消耗组
  57. *
  58. * @return BelongsTo
  59. */
  60. public function consumeGroup(): BelongsTo
  61. {
  62. return $this->belongsTo(GameConsumeGroup::class, 'group_id', 'id');
  63. }
  64. /**
  65. * 获取消耗项名称(访问器)
  66. *
  67. * @return string
  68. */
  69. public function getNameAttribute(): string
  70. {
  71. return $this->getTargetName();
  72. }
  73. /**
  74. * 获取目标名称
  75. *
  76. * @return string
  77. */
  78. public function getTargetName(): string
  79. {
  80. switch ($this->consume_type) {
  81. case CONSUME_TYPE::ITEM->value:
  82. try {
  83. $itemModel = \App\Module\GameItems\Models\Item::find($this->target_id);
  84. return $itemModel ? $itemModel->name : "物品 (ID: {$this->target_id})";
  85. } catch (\Exception $e) {
  86. return "物品 (ID: {$this->target_id})";
  87. }
  88. case CONSUME_TYPE::CURRENCY->value:
  89. try {
  90. $currency = \App\Module\Fund\Models\FundCurrencyModel::find($this->target_id);
  91. return $currency ? $currency->name : "货币 (ID: {$this->target_id})";
  92. } catch (\Exception $e) {
  93. return "货币 (ID: {$this->target_id})";
  94. }
  95. case CONSUME_TYPE::FUND_CONFIG->value:
  96. try {
  97. $fund = \App\Module\Fund\Models\FundConfigModel::find($this->target_id);
  98. return $fund ? $fund->name : "账户种类 (ID: {$this->target_id})";
  99. } catch (\Exception $e) {
  100. return "账户种类 (ID: {$this->target_id})";
  101. }
  102. default:
  103. return "未知消耗类型 (ID: {$this->target_id})";
  104. }
  105. }
  106. /**
  107. * 转换为 Deduct protobuf 对象
  108. *
  109. * @return \Uraus\Kku\Common\Deduct
  110. */
  111. public function toDeductObject(): \Uraus\Kku\Common\Deduct
  112. {
  113. $deduct = new \Uraus\Kku\Common\Deduct();
  114. switch ($this->consume_type) {
  115. case CONSUME_TYPE::ITEM->value:
  116. // 创建物品扣除对象
  117. $deductItem = new \Uraus\Kku\Common\DeductItem([
  118. 'item_id' => $this->target_id,
  119. 'instance_id' => 0, // 默认为0,表示不指定实例
  120. 'quantity' => $this->quantity,
  121. ]);
  122. $deduct->setItems([$deductItem]);
  123. break;
  124. case CONSUME_TYPE::CURRENCY->value:
  125. case CONSUME_TYPE::FUND_CONFIG->value:
  126. // 创建代币扣除对象
  127. $deductCoin = new \Uraus\Kku\Common\DeductCoin([
  128. 'type' => $this->target_id,
  129. 'quantity' => $this->quantity,
  130. ]);
  131. $deduct->setCoins([$deductCoin]);
  132. break;
  133. }
  134. return $deduct;
  135. }
  136. }