| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?php
- namespace App\Module\Game\Commands;
- use App\Module\Game\Services\RewardCollectorService;
- use App\Module\Game\Services\RewardService;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- /**
- * 测试神像奖励收集命令
- *
- * 用于测试神像奖励是否能正确收集到Response.Reward中
- */
- class TestGodRewardCommand extends Command
- {
- /**
- * 命令签名
- *
- * @var string
- */
- protected $signature = 'game:test-god-reward {user_id=10001} {reward_group_id=16}';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '测试神像奖励收集功能';
- /**
- * 执行命令
- *
- * @return int
- */
- public function handle(): int
- {
- $this->info('开始测试神像奖励收集功能...');
- // 测试手动添加神像奖励
- $this->testManualGodReward();
- // 测试通过奖励组发放神像奖励
- $this->testRewardGroupGodReward();
- $this->info('神像奖励收集功能测试完成!');
- return 0;
- }
- /**
- * 测试手动添加神像奖励
- */
- protected function testManualGodReward(): void
- {
- $this->info('测试手动添加神像奖励...');
- // 清空收集器
- RewardCollectorService::clearRewards();
- // 手动添加神像奖励
- RewardCollectorService::addGodReward(2, 86400, 1); // 雨露之神,24小时,1次
- // 检查收集器中的数据
- if (RewardCollectorService::hasRewards()) {
- $rewardData = RewardCollectorService::getRewards();
- $this->line("收集器中的奖励数据:");
- $this->line(" 物品奖励数量: " . count($rewardData['items']));
- $this->line(" 代币奖励数量: " . count($rewardData['coins']));
- $this->line(" 神像奖励数量: " . count($rewardData['gods']));
- if (!empty($rewardData['gods'])) {
- $this->line("✓ 神像奖励收集成功!");
- foreach ($rewardData['gods'] as $god) {
- $this->line(" 神像类型: {$god['type']}, 时间差值: {$god['diff']}秒, 数量: {$god['quantity']}");
- }
- } else {
- $this->error("✗ 神像奖励收集失败!");
- }
- } else {
- $this->error("✗ 收集器中没有任何奖励数据!");
- }
- }
- /**
- * 测试通过奖励组发放神像奖励
- */
- protected function testRewardGroupGodReward(): void
- {
- $userId = (int) $this->argument('user_id');
- $rewardGroupId = (int) $this->argument('reward_group_id');
- $this->info("测试通过奖励组发放神像奖励...");
- $this->line("用户ID: {$userId}");
- $this->line("奖励组ID: {$rewardGroupId}");
- try {
- // 开启事务
- DB::beginTransaction();
- // 清空之前的收集器数据
- RewardCollectorService::clearRewards();
- // 发放奖励
- $result = RewardService::grantReward(
- $userId,
- $rewardGroupId,
- 'test_command',
- 1
- );
- if (!$result->success) {
- $this->error("✗ 奖励发放失败: " . $result->errorMessage);
- DB::rollBack();
- return;
- }
- $this->line("✓ 奖励发放成功!");
- // 检查收集器中的数据
- if (RewardCollectorService::hasRewards()) {
- $rewardData = RewardCollectorService::getRewards();
- $this->line("收集器中的奖励数据:");
- $this->line(" 物品奖励数量: " . count($rewardData['items']));
- $this->line(" 代币奖励数量: " . count($rewardData['coins']));
- $this->line(" 神像奖励数量: " . count($rewardData['gods']));
- if (!empty($rewardData['gods'])) {
- $this->line("✓ 神像奖励收集成功!");
- foreach ($rewardData['gods'] as $god) {
- $this->line(" 神像类型: {$god['type']}, 时间差值: {$god['diff']}秒, 数量: {$god['quantity']}");
- }
- } else {
- $this->warn("⚠ 没有收集到神像奖励数据(可能奖励组中没有神像奖励)");
- }
- } else {
- $this->warn("⚠ 收集器中没有任何奖励数据!");
- }
- // 提交事务
- DB::commit();
- } catch (\Exception $e) {
- DB::rollBack();
- $this->error("✗ 测试失败: " . $e->getMessage());
- $this->line("错误堆栈: " . $e->getTraceAsString());
- }
- }
- }
|