GameConsumeItem.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. case CONSUME_TYPE::FUND_CONFIGS->value:
  103. try {
  104. $names = [];
  105. // 获取主账户种类名称
  106. if ($this->target_id > 0) {
  107. $fund = \App\Module\Fund\Models\FundConfigModel::find($this->target_id);
  108. $names[] = $fund ? $fund->name : "账户种类 (ID: {$this->target_id})";
  109. }
  110. // 获取额外账户种类名称
  111. if (!empty($this->extra_data) && is_array($this->extra_data) &&
  112. isset($this->extra_data['fund_config_ids']) && is_array($this->extra_data['fund_config_ids'])) {
  113. foreach ($this->extra_data['fund_config_ids'] as $id) {
  114. if (is_numeric($id) && $id > 0 && $id != $this->target_id) {
  115. $fund = \App\Module\Fund\Models\FundConfigModel::find($id);
  116. $names[] = $fund ? $fund->name : "账户种类 (ID: {$id})";
  117. }
  118. }
  119. }
  120. return empty($names) ? "多账户种类 (未配置)" : "多账户种类: " . implode(', ', $names);
  121. } catch (\Exception $e) {
  122. return "多账户种类 (ID: {$this->target_id})";
  123. }
  124. default:
  125. return "未知消耗类型 (ID: {$this->target_id})";
  126. }
  127. }
  128. /**
  129. * 转换为 Deduct protobuf 对象
  130. *
  131. * @return \Uraus\Kku\Common\Deduct
  132. */
  133. public function toDeductObject(): \Uraus\Kku\Common\Deduct
  134. {
  135. $deduct = new \Uraus\Kku\Common\Deduct();
  136. switch ($this->consume_type) {
  137. case CONSUME_TYPE::ITEM->value:
  138. // 创建物品扣除对象
  139. $deductItem = new \Uraus\Kku\Common\DeductItem([
  140. 'item_id' => $this->target_id,
  141. 'instance_id' => 0, // 默认为0,表示不指定实例
  142. 'quantity' => $this->quantity,
  143. ]);
  144. $deduct->setItems([$deductItem]);
  145. break;
  146. case CONSUME_TYPE::CURRENCY->value:
  147. case CONSUME_TYPE::FUND_CONFIG->value:
  148. // 创建代币扣除对象
  149. $deductCoin = new \Uraus\Kku\Common\DeductCoin([
  150. 'type' => $this->target_id,
  151. 'quantity' => $this->quantity,
  152. ]);
  153. $deduct->setCoins([$deductCoin]);
  154. break;
  155. case CONSUME_TYPE::FUND_CONFIGS->value:
  156. // 创建多个代币扣除对象
  157. $deductCoins = [];
  158. // 获取账户种类ID列表
  159. $fundConfigIds = [];
  160. if ($this->target_id > 0) {
  161. $fundConfigIds[] = $this->target_id;
  162. }
  163. if (!empty($this->extra_data) && is_array($this->extra_data) &&
  164. isset($this->extra_data['fund_config_ids']) && is_array($this->extra_data['fund_config_ids'])) {
  165. foreach ($this->extra_data['fund_config_ids'] as $id) {
  166. if (is_numeric($id) && $id > 0 && !in_array($id, $fundConfigIds)) {
  167. $fundConfigIds[] = (int)$id;
  168. }
  169. }
  170. }
  171. // 为每个账户种类创建扣除对象
  172. foreach ($fundConfigIds as $fundConfigId) {
  173. $deductCoins[] = new \Uraus\Kku\Common\DeductCoin([
  174. 'type' => $fundConfigId,
  175. 'quantity' => $this->quantity, // 注意:这里是总量,实际扣除时会按顺序分配
  176. ]);
  177. }
  178. $deduct->setCoins($deductCoins);
  179. break;
  180. }
  181. return $deduct;
  182. }
  183. }