Game.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. <?php
  2. /**
  3. * 游戏表
  4. * @author solu
  5. */
  6. class Game extends Model {
  7. protected $tableName = 'game';
  8. protected $dbKey = 'dw_baccarat';
  9. const REDIS_PRE_BLOCK_ID = 'globals:reveal_block_id_cache:';
  10. const REDIS_PLAYER_ONLINE_SET = 'globals:baccarat:player_online_set';
  11. const MAX_PLAYER_ONLINE_COUNT = 100000;
  12. const PLAYER_TIME_OUT = 1800; // 玩家在线超时时间
  13. const STATUS_PLAYING = 0; // 进行中
  14. const STATUS_OPENING = 1; // 开奖中
  15. const STATUS_FINISH = 2; // 已开奖
  16. const WIN_PLAYER = 0;
  17. const WIN_BANKER = 1;
  18. const WIN_TIE = 2;
  19. /**
  20. * 计算游戏结果
  21. * @param $createBlockNum
  22. * @param $ttl
  23. * @throws Exception
  24. * @return array
  25. */
  26. public static function revealGameData($createBlockNum, $ttl) {
  27. $objRedis = dwRedis::init(Eos::REDIS_SERV);
  28. $revealBlockNum = $createBlockNum + $ttl * 2; // 每秒造两个块
  29. $blockNum = $revealBlockNum;
  30. $blockInfo = [];
  31. $banker = []; // 庄
  32. $player = []; // 闲
  33. $bankerNum = []; // 庄
  34. $playerNum = []; // 闲
  35. // 最多六张牌
  36. // 闲
  37. list($num, $color, $blockId) = self::blockNum2Poker($blockNum++, $objRedis);
  38. $poker = self::toPoker($num, $color);
  39. $player[] = $poker;
  40. $playerNum[] = $num;
  41. $blockInfo['player'][] = compact('blockId', 'poker');
  42. $eventData = [
  43. 'type' => 'player',
  44. 'poker' => $poker,
  45. 'num' => $num,
  46. 'color' => $color,
  47. 'block_num' => ($blockNum - 1),
  48. 'block_id' => $blockId,
  49. ];
  50. Eos::pubEvent('baccarat:new_poker', $eventData);
  51. // 庄
  52. list($num, $color, $blockId) = self::blockNum2Poker($blockNum++, $objRedis);
  53. $poker = self::toPoker($num, $color);
  54. $banker[] = $poker;
  55. $bankerNum[] = $num;
  56. $blockInfo['banker'][] = compact('blockId', 'poker');
  57. $eventData = [
  58. 'type' => 'banker',
  59. 'poker' => $poker,
  60. 'num' => $num,
  61. 'color' => $color,
  62. 'block_num' => ($blockNum - 1),
  63. 'block_id' => $blockId,
  64. ];
  65. Eos::pubEvent('baccarat:new_poker', $eventData);
  66. // 闲
  67. list($num, $color, $blockId) = self::blockNum2Poker($blockNum++, $objRedis);
  68. $poker = self::toPoker($num, $color);
  69. $player[] = $poker;
  70. $playerNum[] = $num;
  71. $blockInfo['player'][] = compact('blockId', 'poker');
  72. $eventData = [
  73. 'type' => 'player',
  74. 'poker' => $poker,
  75. 'num' => $num,
  76. 'color' => $color,
  77. 'block_num' => ($blockNum - 1),
  78. 'block_id' => $blockId,
  79. ];
  80. Eos::pubEvent('baccarat:new_poker', $eventData);
  81. // 庄
  82. list($num, $color, $blockId) = self::blockNum2Poker($blockNum++, $objRedis);
  83. $poker = self::toPoker($num, $color);
  84. $banker[] = $poker;
  85. $bankerNum[] = $num;
  86. $blockInfo['banker'][] = compact('blockId', 'poker');
  87. $eventData = [
  88. 'type' => 'banker',
  89. 'poker' => $poker,
  90. 'num' => $num,
  91. 'color' => $color,
  92. 'block_num' => ($blockNum - 1),
  93. 'block_id' => $blockId,
  94. ];
  95. Eos::pubEvent('baccarat:new_poker', $eventData);
  96. // 算对子
  97. $playerPair = self::isPair($playerNum); // 闲对
  98. $bankerPair = self::isPair($bankerNum); // 庄对
  99. // 算点数
  100. $playerPoint = self::pokerPoint($playerNum);
  101. $playerPointSource = $playerPoint;
  102. $bankerPoint = self::pokerPoint($bankerNum);
  103. // 闲判断加牌
  104. $playerExtNum = 0;
  105. if ($playerPoint < 6 && $bankerPoint < 8) {
  106. list($playerExtNum, $color, $blockId) = self::blockNum2Poker($blockNum++, $objRedis);
  107. $poker = self::toPoker($playerExtNum, $color);
  108. $player[] = $poker;
  109. $playerNum[] = $playerExtNum;
  110. $blockInfo['player'][] = compact('blockId', 'poker');
  111. $playerPoint = self::pokerPoint($playerNum);
  112. $eventData = [
  113. 'type' => 'player',
  114. 'poker' => $poker,
  115. 'num' => $playerExtNum,
  116. 'color' => $color,
  117. 'block_num' => ($blockNum - 1),
  118. 'block_id' => $blockId,
  119. ];
  120. Eos::pubEvent('baccarat:new_poker', $eventData);
  121. }
  122. // 庄判断加牌
  123. if (self::addPokerBanker($playerPointSource, $bankerPoint, $playerExtNum)) {
  124. list($num, $color, $blockId) = self::blockNum2Poker($blockNum, $objRedis);
  125. $poker = self::toPoker($num, $color);
  126. $banker[] = $poker;
  127. $bankerNum[] = $num;
  128. $blockInfo['banker'][] = compact('blockId', 'poker');
  129. $bankerPoint = self::pokerPoint($bankerNum);
  130. $eventData = [
  131. 'type' => 'banker',
  132. 'poker' => $poker,
  133. 'num' => $num,
  134. 'color' => $color,
  135. 'block_num' => $blockNum,
  136. 'block_id' => $blockId,
  137. ];
  138. Eos::pubEvent('baccarat:new_poker', $eventData);
  139. }
  140. if ($bankerPoint > $playerPoint) {
  141. $win = self::WIN_BANKER; // 庄赢
  142. } elseif ($playerPoint > $bankerPoint) {
  143. $win = self::WIN_PLAYER; // 闲赢
  144. } else {
  145. $win = self::WIN_TIE;
  146. }
  147. $revealInfo = compact('win', 'player', 'banker', 'playerPair', 'bankerPair', 'playerNum', 'bankerNum', 'playerPoint', 'bankerPoint', 'revealBlockNum', 'blockInfo');
  148. return [$win, $player, $banker, $playerPair, $bankerPair, $revealBlockNum, $revealInfo];
  149. }
  150. /**
  151. * 判断庄是否需要加牌
  152. * @param $playerPoint
  153. * @param $bankerPoint
  154. * @param $playerExtNum
  155. * @wiki https://zh.wikipedia.org/wiki/百家樂#博牌規則
  156. * @return bool
  157. */
  158. private static function addPokerBanker($playerPoint, $bankerPoint, $playerExtNum) {
  159. $playerExtNum > 9 && $playerExtNum = 0;
  160. $add = false;
  161. if ($playerPoint >= 8) {
  162. return false;
  163. }
  164. if ($bankerPoint < 3) {
  165. $add = true;
  166. } elseif ($bankerPoint == 3 && $playerExtNum != 8) {
  167. $add = true;
  168. } elseif ($bankerPoint == 4 && !in_array($playerExtNum, [0, 1, 8, 9])) {
  169. $add = true;
  170. } elseif ($bankerPoint == 5 && !in_array($playerExtNum, [0, 1, 2, 3, 8, 9])) {
  171. $add = true;
  172. } elseif ($bankerPoint == 6 && $playerPoint > 0 && $playerPoint < 6 && in_array($playerExtNum, [6, 7])) {
  173. $add = true;
  174. }
  175. return $add;
  176. }
  177. /**
  178. * 算点数
  179. * @param array $pokerNums
  180. * @return int
  181. */
  182. private static function pokerPoint(array $pokerNums) {
  183. $total = 0;
  184. // 10,J,Q,K 当 0 处理
  185. foreach ($pokerNums as $num) {
  186. $num > 9 && $num = 0;
  187. $total += $num;
  188. }
  189. return $total % 10; // 超过10取各位
  190. }
  191. /**
  192. * 对子
  193. * @param array $nums
  194. * @return bool
  195. */
  196. private static function isPair(array $nums) {
  197. return count(array_unique($nums)) === 1;
  198. }
  199. /**
  200. * @param $blockNum
  201. * @param null $objRedis
  202. * @return array
  203. * @throws Exception
  204. */
  205. private static function blockNum2Poker($blockNum, $objRedis = null) {
  206. $blockId = self::getBlockIdWithCache($blockNum, $objRedis);
  207. if (!$blockId) {
  208. throw new Exception("num:{$blockNum}, 获取blockId失败");
  209. }
  210. list($num, $color) = self::code2Poker($blockId);
  211. return [$num, $color, $blockId];
  212. }
  213. public static function code2Poker($code) {
  214. $code2 = preg_replace('/[^0-9a-d]/i', '', $code);
  215. $len = strlen($code2);
  216. // 取num
  217. $num = 0;
  218. $i = $len - 1;
  219. for (; $i > 0; $i--) {
  220. $c = $code2[$i];
  221. $num = hexdec($c);
  222. if ($num > 0) {
  223. break;
  224. }
  225. }
  226. // 取花色
  227. $color = 0;
  228. $i--;
  229. for (; $i > 0; $i--) {
  230. $c = $code2[$i];
  231. $color = hexdec($c);
  232. if ($color >= 0 && $color <= 3) {
  233. break;
  234. }
  235. }
  236. return [$num, $color];
  237. }
  238. /**
  239. * 牌序号值
  240. * @param $num
  241. * @param $color
  242. * @return int
  243. */
  244. public static function toPoker($num, $color) {
  245. if ($num < 1 || $num > 13) {
  246. return 0;
  247. }
  248. if ($color < 0 || $color > 3) {
  249. return 0;
  250. }
  251. return intval($color*13 + $num);
  252. }
  253. /**
  254. * 牌面值
  255. * @param $poker
  256. * @return int
  257. */
  258. public static function toPokerNum($poker) {
  259. return (($poker - 1) % 13) + 1;
  260. }
  261. /**
  262. * 牌花色
  263. * @param $poker
  264. * @return int
  265. */
  266. public static function toPockColor($poker) {
  267. return intval(($poker - 1) / 13);
  268. }
  269. private static function getBlockIdWithCache($blockNum, $objRedis = null) {
  270. !$objRedis && $objRedis = dwRedis::init(Eos::REDIS_SERV);
  271. $key = self::REDIS_PRE_BLOCK_ID . $blockNum;
  272. $blockId = $objRedis->get($key);
  273. if (!$blockId) {
  274. $blockId = Eos::getBlockId($blockNum);
  275. if ($blockId) {
  276. $objRedis->setex($key, 300, $blockId);
  277. }
  278. }
  279. return $blockId;
  280. }
  281. /**
  282. * 近N期开奖信息
  283. * @param int $limit
  284. * @return array
  285. */
  286. public function winData($limit = 50) {
  287. $list = $this->objTable->getAll(['game_state' => self::STATUS_FINISH], [
  288. '_field' => 'id, winner, banker_pair, player_pair, reveal_trx_id, reveal_block_num, reveal_info',
  289. '_sortKey' => 'id DESC',
  290. '_limit' => $limit,
  291. ]);
  292. foreach ($list as $k => $v) {
  293. $v['id'] = intval($v['id']);
  294. $v['winner'] = intval($v['winner']);
  295. $v['banker_pair'] = intval($v['banker_pair']);
  296. $v['player_pair'] = intval($v['player_pair']);
  297. $v['reveal_block_num'] = intval($v['reveal_block_num']);
  298. $revealInfo = json_decode($v['reveal_info'], true);
  299. unset($v['reveal_info']);
  300. $v['block_info'] = $revealInfo['blockInfo'];
  301. $list[$k] = $v;
  302. }
  303. return $list ?: [];
  304. }
  305. /**
  306. * 游戏信息
  307. * @param $old
  308. * @param $new
  309. * @param $player
  310. * @return array
  311. */
  312. public function info($old, $new, $player) {
  313. $data = [];
  314. $keyword = [
  315. '_field' => 'id, ttl, game_state, create_time, update_time_int, reveal_info',
  316. ];
  317. $games = $this->objTable->getAll(['id' => [$old, $new]], $keyword);
  318. $games = arrayFormatKey($games, 'id');
  319. $data['old'] = $games[$old] ?: [];
  320. $data['old']['reveal_info'] = json_decode($data['old']['reveal_info'], true);
  321. $data['new'] = $games[$new] ?: [];
  322. unset($data['new']['reveal_info']);
  323. $data['new']['timeout'] = max(strtotime($data['new']['create_time']) + $data['new']['ttl'] - time(), 0);
  324. $row = null;
  325. if ($player) {
  326. $objOffer = new Offer();
  327. $row = $objOffer->objTable->getRow(['game_id' => $old, 'player' => $player, 'state' => self::STATUS_FINISH], [
  328. '_field' => 'sum(offerall_int) offerall, sum(win_int) win',
  329. ]);
  330. }
  331. $data['old']['offer'] = $row ?: null;
  332. return $data;
  333. }
  334. /**
  335. * 在线玩家队列
  336. * @author solu
  337. * @param $player
  338. */
  339. public static function setPlayerOnline($player) {
  340. $objRedis = dwRedis::init(Eos::REDIS_SERV);
  341. $time = time();
  342. $objRedis->zAdd(self::REDIS_PLAYER_ONLINE_SET, $time, $player);
  343. $len = $objRedis->zCard(self::REDIS_PLAYER_ONLINE_SET);
  344. if ($len > self::MAX_PLAYER_ONLINE_COUNT) {
  345. $out = $len - self::MAX_PLAYER_ONLINE_COUNT;
  346. $objRedis->zRemRangeByRank(self::REDIS_PLAYER_ONLINE_SET, 0, $out-1);
  347. }
  348. }
  349. /**
  350. * 在线信息
  351. * @author solu
  352. * @param $limit
  353. * @return array
  354. */
  355. public static function getOnlineInfo($limit = 10) {
  356. $objRedis = dwRedis::init(Eos::REDIS_SERV);
  357. self::trimOnline();
  358. $total = $objRedis->zCard(self::REDIS_PLAYER_ONLINE_SET);
  359. $list = $objRedis->zRevRange(self::REDIS_PLAYER_ONLINE_SET, 0, $limit);
  360. return [$total, $list];
  361. }
  362. /**
  363. * 整理在线玩家
  364. * @author solu
  365. */
  366. public static function trimOnline() {
  367. $objRedis = dwRedis::init(Eos::REDIS_SERV);
  368. $timeout = time() - self::PLAYER_TIME_OUT;
  369. return $objRedis->zRemRangeByScore(self::REDIS_PLAYER_ONLINE_SET, '-inf', $timeout);
  370. }
  371. /**
  372. * 游戏信息
  373. * @author solu
  374. * @param array $gameIds
  375. * @return array
  376. */
  377. public function getGameRevealInfo(array $gameIds) {
  378. $items = $this->objTable->getAll(['id' => $gameIds], ['_field' => 'id, game_state, reveal_info']);
  379. $data = [];
  380. foreach ($items as $v) {
  381. $id = $v['id'];
  382. $v['reveal_info'] = $v['reveal_info'] ? json_decode($v['reveal_info'], true) : null;
  383. $data[$id] = $v;
  384. }
  385. return $data;
  386. }
  387. }