GamesRedisService.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. class GamesRedisService extends BaseRedisService
  3. {
  4. /**
  5. * @var string 游戏数据前缀
  6. */
  7. public $video_games_db;
  8. /**
  9. * GamesRedisService constructor.
  10. */
  11. public function __construct()
  12. {
  13. parent::__construct();
  14. $this->video_games_db = $this->prefix . 'games:';
  15. }
  16. public function bet($id, $bet, $money, $user_id)
  17. {
  18. $this->inc($id, "option$bet", $money);
  19. $this->inc($id, "option$bet" . ':' . $user_id, $money);
  20. }
  21. public function getBet($id, $bet_array, $user_id = false)
  22. {
  23. $field = array();
  24. foreach ($bet_array as $i) {
  25. $field[] = 'option' . $i;
  26. if ($user_id) {
  27. $field[] = 'option' . $i . ':' . $user_id;
  28. }
  29. }
  30. $data = $this->get($id, $field);
  31. $bet = array();
  32. $user_bet = array();
  33. foreach ($bet_array as $i) {
  34. $bet[] = intval($data['option' . $i]);
  35. if ($user_id) {
  36. $user_bet[] = intval($data['option' . $i . ':' . $user_id]);
  37. }
  38. }
  39. return $user_id ? [$bet, $user_bet] : $bet;
  40. }
  41. public function isLock()
  42. {
  43. $lock = $this->redis->hMGet($this->video_games_db . 'global', array('lock'));
  44. return $lock['lock'] > NOW_TIME;
  45. }
  46. public function lock($time = 5)
  47. {
  48. return $this->redis->hMset($this->video_games_db . 'global', array('lock' => NOW_TIME + $time));
  49. }
  50. public function unLock()
  51. {
  52. return $this->redis->hMset($this->video_games_db . 'global', array('lock' => 0));
  53. }
  54. public function isVideoLock($video_id)
  55. {
  56. $lock = $this->redis->hMGet($this->video_games_db . 'vedio:' . $video_id, array('lock'));
  57. return $lock['lock'] > NOW_TIME;
  58. }
  59. public function lockVideo($video_id, $time = 5)
  60. {
  61. return $this->redis->hMset($this->video_games_db . 'vedio:' . $video_id, array('lock' => NOW_TIME + $time));
  62. }
  63. public function unLockVideo($video_id)
  64. {
  65. return $this->redis->hMset($this->video_games_db . 'vedio:' . $video_id, array('lock' => 0));
  66. }
  67. /**
  68. * @param int $id 游戏id
  69. * @param array $data
  70. * @return bool|int
  71. */
  72. public function set($id, $data)
  73. {
  74. $id = intval($id);
  75. if (!$id) {
  76. return false;
  77. }
  78. $redis = $this->redis->multi();
  79. $redis->hMset($this->video_games_db . $id, $data);
  80. $res = $redis->exec();
  81. if (isset($res[0]) && $res[0] !== false) {
  82. return $id;
  83. } else {
  84. return false;
  85. }
  86. }
  87. public function get($id, $field = '')
  88. {
  89. $id = intval($id);
  90. if (!$id) {
  91. return false;
  92. }
  93. if ($field) {
  94. if (is_string($field)) {
  95. $field = explode(',', $field);
  96. }
  97. return $this->redis->hMGet($this->video_games_db . $id, $field);
  98. } else {
  99. return $this->redis->hGetAll($this->video_games_db . $id);
  100. }
  101. }
  102. public function inc($id, $key, $value)
  103. {
  104. $id = intval($id);
  105. $value = intval($value);
  106. if (!$id) {
  107. return false;
  108. }
  109. return $this->redis->hIncrBy($this->video_games_db . $id, $key, $value);
  110. }
  111. public function del($id)
  112. {
  113. $id = intval($id);
  114. if (!$id) {
  115. return false;
  116. }
  117. return $this->redis->hDel($this->video_games_db . $id, $this->redis->hKeys($this->video_games_db . $id));
  118. }
  119. }