2
0

RecordServerGameCountYesterday.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Facade\TableName;
  4. use Carbon\Carbon;
  5. use Illuminate\Console\Command;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Facades\Redis;
  8. class RecordServerGameCountYesterday extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'record_server_game_count_yesterday';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = '按天统计游戏人数-游戏房间去重过的用户';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return mixed
  35. */
  36. public function handle()
  37. {
  38. // 今日执行昨日数据
  39. $DateID = Carbon::yesterday()->format('Ymd');
  40. // 查找按天去重玩过游戏的人
  41. $list = DB::connection('write')->table(TableName::QPRecordDB() . 'RecordUserGameDayCount')
  42. ->where('DateID', $DateID)
  43. ->selectRaw('count(UserID) UserCount,GameID,sum(Cnt) Cnt')
  44. ->groupBy('GameID')
  45. ->get();
  46. foreach ($list as $value) {
  47. DB::connection('write')->table(TableName::QPRecordDB() . 'RecordServerGameCount')
  48. ->updateOrInsert(['DateID' => $DateID, 'GameID' => $value->GameID], ['DateID' => $DateID, 'GameID' => $value->GameID, 'Cnt' => $value->Cnt, 'UserCount' => $value->UserCount]);
  49. }
  50. // 计算并更新游戏RTP数据
  51. $this->calculateAndStoreGameRtp();
  52. }
  53. /**
  54. * 计算并存储游戏RTP数据
  55. * 从RecordGameRoomDayInfo和RoomStockDay两张表中获取最近60天的数据
  56. * 根据GameID分类求和PayTotalBet和PayWinLost,计算RTP并存储到Redis
  57. */
  58. private function calculateAndStoreGameRtp()
  59. {
  60. try {
  61. // 计算最近60天的DateID范围
  62. $endDate = date('Ymd', strtotime('-1 day')); // 昨天
  63. $startDate = date('Ymd', strtotime('-60 days')); // 60天前
  64. // 使用JOIN查询两张表中DateID和GameID相同,SortID=1的数据
  65. // 按GameID分组求和PayTotalBet和PayWinLost
  66. $gameStats = DB::connection('write')
  67. ->table(TableName::QPRecordDB() . 'RecordGameRoomDayInfo as r')
  68. ->join(TableName::QPPlatformDB() . 'RoomStockDay as rs', function($join) {
  69. $join->on('r.DateID', '=', 'rs.DateID')
  70. ->on('r.GameID', '=', 'rs.GameID')
  71. ->on('r.SortID', '=', 'rs.SortID');
  72. })
  73. ->where('r.SortID', 1)
  74. ->whereBetween('r.DateID', [$startDate, $endDate])
  75. ->select(
  76. 'r.GameID',
  77. DB::raw('SUM(r.PayTotalBet) as PayTotalBet'),
  78. DB::raw('SUM(rs.PayWinLost) as PayWinLost')
  79. )
  80. ->groupBy('r.GameID')
  81. ->get();
  82. // 计算所有数据的总和
  83. $totalPayTotalBet = 0;
  84. $totalPayWinLost = 0;
  85. foreach ($gameStats as $item) {
  86. $totalPayTotalBet += ($item->PayTotalBet ?? 0);
  87. $totalPayWinLost += ($item->PayWinLost ?? 0);
  88. }
  89. // 计算所有数据相加的RTP值作为g0
  90. $totalRtp = intval((($totalPayTotalBet - $totalPayWinLost) / max($totalPayTotalBet, 1)) * 100);
  91. $gameRtp = ["g0" => $totalRtp];
  92. // 计算每个GameID的RTP
  93. foreach ($gameStats as $item) {
  94. $gameId = $item->GameID;
  95. $payTotalBet = $item->PayTotalBet ?? 0;
  96. $payWinLost = $item->PayWinLost ?? 0;
  97. // RTP计算公式: ((PayTotalBet-PayWinLost)/max(PayTotalBet,1))*100
  98. $rtp = intval((($payTotalBet - $payWinLost) / max($payTotalBet, 1)) * 100);
  99. $rtp = ($rtp<=80)?80:$rtp;
  100. $rtp = ($rtp>=120)?120:$rtp;
  101. $gameRtp["g" . $gameId] = $rtp;
  102. }
  103. // 将结果存储到Redis的SomeConfigSpecial键中
  104. Redis::set("SomeConfigSpecial", json_encode($gameRtp));
  105. // $this->info('游戏RTP数据计算完成并已存储到Redis');
  106. \Log::info('游戏RTP数据更新成功', [
  107. 'date_range' => [$startDate, $endDate],
  108. 'game_count' => count($gameRtp) - 1, // 减去默认的g0
  109. 'rtp_data' => $gameRtp
  110. ]);
  111. } catch (\Exception $e) {
  112. // $this->error('计算游戏RTP数据时发生错误: ' . $e->getMessage());
  113. \Log::error('计算游戏RTP数据失败', [
  114. 'error' => $e->getMessage(),
  115. 'trace' => $e->getTraceAsString()
  116. ]);
  117. }
  118. }
  119. }