| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- <?php
- namespace App\Module\Game\Commands;
- use App\Module\Game\Services\RewardService;
- use App\Module\Game\Models\GameRewardGroup;
- use App\Module\Game\Models\GameRewardItem;
- use App\Module\Game\Enums\REWARD_TYPE;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\DB;
- use Exception;
- /**
- * 测试奖励系统命令
- *
- * 用于测试奖励发放功能是否正常工作
- */
- class TestRewardSystemCommand extends Command
- {
- /**
- * 命令签名
- *
- * @var string
- */
- protected $signature = 'game:test-reward-system {--user-id=1 : 测试用户ID}';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '测试奖励系统功能';
- /**
- * 执行命令
- *
- * @return int
- */
- public function handle(): int
- {
- $userId = (int) $this->option('user-id');
- $this->info("开始测试奖励系统功能,用户ID: {$userId}");
- try {
- // 创建测试奖励组
- $this->createTestRewardGroup();
- // 测试物品奖励发放
- $this->testItemReward($userId);
- // 测试账户种类奖励发放
- $this->testFundConfigReward($userId);
- // 测试币种奖励发放
- $this->testCurrencyReward($userId);
- // 测试宠物经验奖励发放
- $this->testPetExpReward($userId);
- // 测试宠物体力奖励发放
- $this->testPetEnergyReward($userId);
- // 测试其他类型奖励发放
- $this->testOtherReward($userId);
- $this->info('奖励系统功能测试完成!');
- return 0;
- } catch (Exception $e) {
- $this->error('测试失败: ' . $e->getMessage());
- return 1;
- }
- }
- /**
- * 创建测试奖励组
- */
- protected function createTestRewardGroup(): void
- {
- $this->info('创建测试奖励组...');
- // 检查是否已存在测试奖励组
- $existingGroup = GameRewardGroup::where('code', 'TEST_REWARD_GROUP')->first();
- if ($existingGroup) {
- $this->line('测试奖励组已存在,跳过创建');
- return;
- }
- DB::transaction(function () {
- // 创建奖励组
- $group = GameRewardGroup::create([
- 'name' => '测试奖励组',
- 'code' => 'TEST_REWARD_GROUP',
- 'description' => '用于测试奖励系统的奖励组',
- 'is_random' => false,
- 'random_count' => 1,
- 'is_active' => true
- ]);
- // 创建物品奖励项
- GameRewardItem::create([
- 'group_id' => $group->id,
- 'reward_type' => REWARD_TYPE::ITEM->value,
- 'target_id' => 1001, // 假设物品ID为1001
- 'param1' => 0,
- 'param2' => 0,
- 'quantity' => 10,
- 'weight' => 1.0,
- 'is_guaranteed' => true,
- 'extra_data' => []
- ]);
- // 创建账户种类奖励项
- GameRewardItem::create([
- 'group_id' => $group->id,
- 'reward_type' => REWARD_TYPE::FUND_CONFIG->value,
- 'target_id' => 1, // 假设账户种类ID为1
- 'param1' => 0,
- 'param2' => 0,
- 'quantity' => 100,
- 'weight' => 1.0,
- 'is_guaranteed' => true,
- 'extra_data' => []
- ]);
- $this->line('✓ 测试奖励组创建完成');
- });
- }
- /**
- * 测试物品奖励发放
- */
- protected function testItemReward(int $userId): void
- {
- $this->info('测试物品奖励发放...');
- try {
- DB::transaction(function () use ($userId) {
- $result = RewardService::grantReward(
- $userId,
- 'TEST_REWARD_GROUP',
- 'test_command', // 来源类型:测试命令
- time() // 来源ID:当前时间戳作为唯一标识
- );
- if ($result->success) {
- $this->line('✓ 奖励发放成功');
- $this->line(' 发放的奖励项数量: ' . count($result->items));
- $this->line(' 来源类型: ' . $result->sourceType);
- $this->line(' 来源ID: ' . $result->sourceId);
- foreach ($result->items as $item) {
- $this->line(" 奖励类型: {$item->rewardType}, 目标ID: {$item->targetId}, 数量: {$item->quantity}");
- }
- } else {
- $this->error('✗ 奖励发放失败: ' . $result->errorMessage);
- }
- });
- } catch (Exception $e) {
- $this->error('✗ 奖励发放异常: ' . $e->getMessage());
- }
- }
- /**
- * 测试账户种类奖励发放
- */
- protected function testFundConfigReward(int $userId): void
- {
- $this->info('测试账户种类奖励发放...');
- $this->line('注意:此测试需要确保用户账户已创建且fund_config表中有对应记录');
- // 实际测试代码可以在这里实现
- }
- /**
- * 测试币种奖励发放
- */
- protected function testCurrencyReward(int $userId): void
- {
- $this->info('测试币种奖励发放...');
- $this->line('注意:此测试需要确保fund_currency和fund_config表中有对应记录');
- // 实际测试代码可以在这里实现
- }
- /**
- * 测试宠物经验奖励发放
- */
- protected function testPetExpReward(int $userId): void
- {
- $this->info('测试宠物经验奖励发放...');
- $this->line('注意:此测试需要确保用户有宠物');
- // 实际测试代码可以在这里实现
- }
- /**
- * 测试宠物体力奖励发放
- */
- protected function testPetEnergyReward(int $userId): void
- {
- $this->info('测试宠物体力奖励发放...');
- $this->line('注意:此测试需要确保用户有宠物');
- // 实际测试代码可以在这里实现
- }
- /**
- * 测试其他类型奖励发放
- */
- protected function testOtherReward(int $userId): void
- {
- $this->info('测试其他类型奖励发放...');
- $this->line('其他类型奖励目前只记录日志,不执行具体操作');
- // 实际测试代码可以在这里实现
- }
- }
|