BigWinner.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace App\Game;
  3. use App\Facade\TableName;
  4. use App\Game\Services\RouteService;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Support\Facades\DB;
  7. use Carbon\Carbon;
  8. use Illuminate\Support\Facades\Redis;
  9. class BigWinner extends Model
  10. {
  11. protected $connection = 'mysql';
  12. protected $table = 'webgame.big_winners'; // 指定模型对应的表名
  13. // 指定可以被批量赋值的属性
  14. protected $fillable = ['img', 'gtype', 'uid', 'nick', 'lv', 'win', 'wintime'];
  15. // 指定日期时间转换
  16. protected $dates = ['wintime'];
  17. public static function FindWinnerFromGame()
  18. {
  19. $rekey='check_bigwinner';
  20. if(Redis::ttl($rekey)>60){
  21. return;
  22. }
  23. // if(Redis::exists($rekey))return;
  24. Redis::set($rekey, 1);
  25. Redis::expire($rekey, 360);
  26. // 获取当前时间和24小时前的时间
  27. $now = Carbon::now();
  28. $startTime = $now->subHours(24)->toDateTimeString();
  29. $currentMonth = $now->format('Ym');
  30. if($now->subHours(24)->format("Ym")!=$currentMonth&& $now->format('H')<4){
  31. $currentMonth=$now->subHours(24)->format("Ym");
  32. }
  33. $scoreTableName = TableName::QPTreasureDB() . 'YN_RecordScoreInfo_' . $currentMonth;
  34. $accountTableName = TableName::QPAccountsDB() . 'AccountsInfo'; // 假设 AccountInfo 表名是固定的
  35. $sql = "
  36. WITH RankedScores AS (
  37. SELECT
  38. rs.UserID,
  39. rs.ChangeScore,
  40. rs.UpdateTime,
  41. ROW_NUMBER() OVER (PARTITION BY rs.UserID ORDER BY rs.ChangeScore DESC) AS rn
  42. FROM $scoreTableName AS rs
  43. WHERE rs.UpdateTime >= ?
  44. )
  45. SELECT TOP 50
  46. rs.UserID as uid,
  47. rs.ChangeScore as win,
  48. rs.UpdateTime as wintime,
  49. ai.NickName as nick,
  50. ai.FaceID as img
  51. FROM RankedScores AS rs
  52. JOIN $accountTableName AS ai ON rs.UserID = ai.UserID
  53. WHERE rs.rn = 1 and rs.ChangeScore>10
  54. ORDER BY rs.ChangeScore DESC";
  55. try {
  56. $topUsers = DB::select($sql, [$startTime]);
  57. if (count($topUsers) > 20) {
  58. DB::connection('mysql')->statement('TRUNCATE TABLE ' . (new BigWinner())->table);
  59. }
  60. $userids = [];
  61. foreach ($topUsers as &$user) {
  62. $user = (array)$user;
  63. if (isset($userids[$user['uid']])) continue;
  64. $userids[$user['uid']] = 1;
  65. $user['win'] = $user['win'] / 100;
  66. $user['img'] = GlobalUserInfo::faceidToAvatar($user['img']);
  67. BigWinner::query()->insert($user);
  68. // (new BigWinner($user))->save();
  69. }
  70. self::clearCache();
  71. }catch (\Exception $e) {
  72. \Log::error($e);
  73. }
  74. }
  75. public static function clearCache()
  76. {
  77. // $state=RouteService::getStateConfig();
  78. $cacheKey = 'BigWinner24680';
  79. Redis::del($cacheKey);
  80. }
  81. public static function getCache()
  82. {
  83. $cacheKey = 'BigWinner24680';
  84. $cache=Redis::get($cacheKey);
  85. if($cache)return json_decode($cache, true);
  86. $gtypes=self::select('gtype')->distinct()->pluck('gtype');
  87. $data=self::orderBy('wintime', 'desc')->get();
  88. $cache=compact('gtypes','data');
  89. Redis::set($cacheKey,json_encode($cache));
  90. return $cache;
  91. }
  92. }