Ranking.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace Collection\Lib;
  3. use Dever;
  4. class Ranking
  5. {
  6. # 积分回调,增加积分榜单
  7. public function score($log)
  8. {
  9. if ($log['type_id'] > 0) {
  10. $this->up($log['uid'], $log['type_id'], 4, $log['num']);
  11. }
  12. }
  13. # 获取最新的总榜
  14. public function getInfo($info_id, $type = 1)
  15. {
  16. $where['info_id'] = $info_id;
  17. $where['type'] = $type;
  18. $info = Dever::db('collection/ranking')->getOne($where);
  19. return $info;
  20. }
  21. # 获取最新的一期
  22. public function getPeriodsNew($info_id, $type = 1)
  23. {
  24. $info = $this->getInfo($info_id, $type);
  25. if ($info) {
  26. $where['info_id'] = $info_id;
  27. $where['ranking_id'] = $info['id'];
  28. $info = Dever::db('collection/ranking_periods')->getNew($where);
  29. return $info;
  30. }
  31. return false;
  32. }
  33. # 获取当前期的数据
  34. public function getData($periods_id)
  35. {
  36. $where['periods_id'] = $periods_id;
  37. $data = Dever::db('collection/ranking_data')->getAll($where);
  38. return $data;
  39. }
  40. # 加入门票榜单
  41. public function up($uid, $info_id, $type = 1, $num = 1)
  42. {
  43. $periods = $this->getPeriodsNew($info_id, $type);
  44. if ($periods) {
  45. $update['uid'] = $uid;
  46. $update['periods_id'] = $periods['id'];
  47. $data = Dever::db('collection/ranking_data')->one($update);
  48. $update['info_id'] = $info_id;
  49. $update['ranking_id'] = $periods['ranking_id'];
  50. if ($data) {
  51. $update['where_id'] = $data['id'];
  52. if ($num < 0) {
  53. $update['num'] = $data['num'] - $num;
  54. } else {
  55. $update['num'] = $data['num'] + $num;
  56. }
  57. Dever::db('collection/ranking_data')->update($update);
  58. } else {
  59. $update['num'] = $num;
  60. Dever::db('collection/ranking_data')->insert($update);
  61. }
  62. }
  63. }
  64. # 加入到cron里 检测所有榜单是否有新的期数,如果没有,自动添加新的一期 collection/lib/ranking.cron
  65. public function cron_api()
  66. {
  67. $ranking = Dever::db('collection/ranking')->getAll();
  68. if ($ranking) {
  69. $cur = time();
  70. foreach ($ranking as $k => $v) {
  71. $where['ranking_id'] = $v['id'];
  72. $info = Dever::db('collection/ranking_periods')->getNew($where);
  73. if (!$info) {
  74. # 如果没有,就建立新的期数
  75. $this->createPeriods(1, $cur, $v);
  76. } else {
  77. # 如果有,检查结束时间到没到期,如果到了,就把这一期置为已结束,重新建立新的期数
  78. if ($info['end'] > 0 && $cur > $info['end']) {
  79. Dever::db('collection/ranking_periods')->update(array('where_id' => $info['id'], 'status' => 2));
  80. $this->createPeriods($info['periods'] + 1, $cur, $v);
  81. }
  82. }
  83. }
  84. }
  85. }
  86. private function createPeriods($periods, $cur, $info)
  87. {
  88. # 查看总榜的结束时间是否结束
  89. if ($info['end'] && $cur > $info['end']) {
  90. Dever::db('collection/ranking')->update(array('where_id' => $info['id'], 'status' => 2));
  91. return;
  92. }
  93. $data['info_id'] = $info['info_id'];
  94. $data['ranking_id'] = $info['id'];
  95. $data['name'] = '第' . $periods . '期';
  96. $data['periods'] = $periods;
  97. $data['users'] = 0;
  98. $data['status'] = 1;
  99. $data['start'] = $cur;
  100. $day = 86400;
  101. if ($info['periods'] == 1) {
  102. # 天
  103. $time = $day;
  104. } elseif ($info['periods'] == 2) {
  105. # 周
  106. $time = $day * 7;
  107. } elseif ($info['periods'] == 3) {
  108. # 月
  109. $time = $day * 30;
  110. } elseif ($info['periods'] == 4) {
  111. # 年
  112. $time = $day * 365;
  113. } elseif ($info['periods'] == 5) {
  114. # 永久
  115. $time = 0;
  116. }
  117. if ($time > 0) {
  118. $data['end'] = $data['start'] + $time;
  119. } else {
  120. $data['end'] = 0;
  121. }
  122. Dever::db('collection/ranking_periods')->insert($data);
  123. }
  124. }