gamesModel.class.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. <?php
  2. /**
  3. *
  4. */
  5. class gamesModel extends Model
  6. {
  7. public static $colors = [
  8. 'spade' => 0,
  9. 'heart' => 1,
  10. 'club' => 2,
  11. 'diamond' => 3,
  12. ];
  13. public static $figures = [
  14. 'A' => 1,
  15. '2' => 2,
  16. '3' => 3,
  17. '4' => 4,
  18. '5' => 5,
  19. '6' => 6,
  20. '7' => 7,
  21. '8' => 8,
  22. '9' => 9,
  23. '10' => 10,
  24. 'J' => 11,
  25. 'Q' => 12,
  26. 'K' => 13,
  27. ];
  28. public static function parseCards($cards)
  29. {
  30. $new_cards = array();
  31. foreach ($cards as $v) {
  32. $new_cards[] = array(self::$colors[$v[0]], self::$figures[$v[1]]);
  33. }
  34. return $new_cards;
  35. }
  36. public function crontab()
  37. {
  38. /**
  39. * 封装各方法
  40. * @return [type] [description]
  41. */
  42. $microtime = microtime(1);
  43. fanwe_require(APP_ROOT_PATH . 'mapi/lib/redis/BaseRedisService.php');
  44. fanwe_require(APP_ROOT_PATH . 'mapi/lib/redis/GamesRedisService.php');
  45. $redis = new GamesRedisService();
  46. if ($redis->isLock()) {
  47. return array('is_lock');
  48. }
  49. $redis->lock();
  50. fanwe_require(APP_ROOT_PATH . 'mapi/lib/redis/VideoRedisService.php');
  51. $video_redis = new VideoRedisService();
  52. $game_log_model = Model::build('game_log');
  53. $time = NOW_TIME;
  54. $games = $game_log_model->field('id,game_id,create_time,long_time')->select(['status' => 1]);
  55. $game_types = array();
  56. $return_data = array('time' => 0);
  57. /**
  58. * 游戏处理
  59. */
  60. if ($games) {
  61. $m_config = load_auto_cache("m_config");
  62. $game_commission = +$m_config['game_commission'];
  63. $podcast_commission = intval(defined('PODCAST_COMMISSION') && PODCAST_COMMISSION);
  64. $user_model = Model::build('user');
  65. $games_model = Model::build('games');
  66. $coin_log_model = Model::build('coin_log');
  67. $banker_log_model = Model::build('banker_log');
  68. $user_game_log_model = Model::build('user_game_log');
  69. fanwe_require(APP_ROOT_PATH . 'mapi/lib/tools/Poker.class.php');
  70. fanwe_require(APP_ROOT_PATH . 'mapi/lib/tools/NiuNiu.class.php');
  71. fanwe_require(APP_ROOT_PATH . 'mapi/lib/tools/DeZhou.class.php');
  72. fanwe_require(APP_ROOT_PATH . 'mapi/lib/tools/Dice.class.php');
  73. fanwe_require(APP_ROOT_PATH . 'mapi/lib/redis/UserRedisService.php');
  74. $user_redis = new UserRedisService();
  75. foreach ($games as $game) {
  76. $game_log_id = $game['id'];
  77. $game_id = $game['game_id'];
  78. $game_log = $redis->get($game_log_id, 'video_id,podcast_id,group_id,public_cards,banker_id');
  79. $video_id = $game_log['video_id'];
  80. $podcast_id = $game_log['podcast_id'];
  81. $banker_id = $game_log['banker_id'];
  82. if (!isset($game_types[$game_id])) {
  83. $game_types[$game_id] = $games_model->field('commission_rate,rate,option,class,principal,ticket_rate')->selectOne(['id' => $game_id]);
  84. }
  85. $game_type = $game_types[$game_id];
  86. $option = json_decode($game_type['option'], 1);
  87. if (!in_array($game_type['class'], ['Poker', 'NiuNiu', 'DeZhou', 'Dice'])) {
  88. break;
  89. }
  90. $game_object = new $game_type['class']();
  91. if ($game['create_time'] + $game['long_time'] <= $time) {
  92. $live_in = $video_redis->getOne_db($video_id, 'live_in');
  93. if ($redis->isVideoLock($video_id)) {
  94. break;
  95. }
  96. $redis->lockVideo($video_id);
  97. if ($live_in && $game['long_time']) {
  98. $sql_time = microtime(1);
  99. // 计算投注结果
  100. $sum = [];
  101. $sum_v = [];
  102. $table = DB_PREFIX . 'user_game_log';
  103. foreach ($option as $key => $value) {
  104. $res = intval($user_game_log_model->sum('money', ['game_log_id' => $game_log_id, 'bet' => $key]));
  105. $sum[] = $res;
  106. $sum_v[] = $res * $value;
  107. }
  108. $rate = $user_redis->getOne_db($podcast_id, 'rate');
  109. $rate = $rate ? $rate : (+$game_type['rate']);
  110. $cheat = rand(1, 100) < $rate && !$banker_id;
  111. $dices = [];
  112. $cards_data = [];
  113. switch ($game_id) {
  114. case 1:
  115. case 2:
  116. $cards_data = $this->formatCardsData($game_object->play());
  117. if ($cheat) {
  118. $cards_data = $this->sortGame($sum_v, $cards_data);
  119. } else {
  120. shuffle($cards_data);
  121. }
  122. break;
  123. case 3:
  124. $game_object->flop(json_decode($game_log['public_cards'], 1));
  125. $data = $this->formatCardsData($game_object->play());
  126. $cards = self::parseCards($game_object->gp);
  127. $gp = array(
  128. 'win' => false,
  129. 'cards' => $cards,
  130. 'type' => 0,
  131. );
  132. if ($game_object->compare($res[0], $res[1]) == $game_object->compare($res[1], $res[0])) {
  133. $data[0]['win'] = false;
  134. $gp['win'] = true;
  135. shuffle($data);
  136. } else {
  137. if ($cheat && $sum_v[2] > $sum_v[0]) {
  138. $value = $data[0];
  139. $data[0] = $data[1];
  140. $data[1] = $value;
  141. } else {
  142. shuffle($data);
  143. }
  144. }
  145. $cards_data = array(
  146. $data[0],
  147. $gp,
  148. $data[1],
  149. );
  150. break;
  151. case 4:
  152. if ($cheat) {
  153. $key = array_search(min($sum_v), $sum_v);
  154. $total = $key == 1 ? 7 : ($key ? rand(8, 12) : rand(2, 6));
  155. } else {
  156. $total = false;
  157. }
  158. $dices = $game_object->play(2, $total);
  159. break;
  160. default:
  161. break;
  162. }
  163. // 计算得胜结果
  164. $result = 0;
  165. $data = array('status' => 2);
  166. if ($cards_data) {
  167. foreach ($cards_data as $key => $value) {
  168. if ($value['win']) {
  169. $result = $key + 1;
  170. }
  171. unset($cards_data[$key]['win']);
  172. $data['option_win' . ($key + 1)] = intval($value['win']);
  173. $data['option_cards' . ($key + 1)] = json_encode($value['cards']);
  174. $data['option_type' . ($key + 1)] = $value['type'];
  175. }
  176. } else if ($dices) {
  177. $res = sum($dices);
  178. switch ($res) {
  179. case 7:
  180. $result = 2;
  181. break;
  182. default:
  183. $result = $res > 7 ? 3 : 1;
  184. break;
  185. }
  186. }
  187. $times = $option[$result];
  188. $redis->set($game_log_id, $data);
  189. // MySQL结束游戏
  190. // 计算主播收入
  191. $commission_rate = $game_type['commission_rate'];
  192. $income = array_sum($sum) - $times * $sum[$result - 1];
  193. $suit_patterns = json_encode($cards_data);
  194. $bet = json_encode($sum);
  195. $podcast_income = $income > 0 ? intval($income * $commission_rate / 100) : 0;
  196. $final_income = $banker_id ? ($income > 0 ? intval($income * $game_commission / 100) : 0) : $income - $podcast_income;
  197. $commission = 0;
  198. Connect::beginTransaction();
  199. // 游戏记录结算
  200. if ($game_log_model->multiAddLog($game_log_id, $result, $bet, $suit_patterns, $podcast_income, $banker_id ? 0 : $final_income) === false) {
  201. Connect::rollback();
  202. $return_data[$game_log_id] = 'error:' . __LINE__ . ' sql:' . $game_log_model->getLastSql();
  203. break;
  204. }
  205. // 玩家收入
  206. if ($sum[$result - 1]) {
  207. // 获得下注总金额三倍返还
  208. if ($user_model->multiAddCoin($game_log_id, $result, $times) === false) {
  209. Connect::rollback();
  210. $return_data[$game_log_id] = 'error:' . __LINE__ . ' sql:' . $user_model->getLastSql();
  211. break;
  212. }
  213. // 批量插入金币记录
  214. if ($coin_log_model->multiAddLog($game_log_id, $result, $times) === false) {
  215. Connect::rollback();
  216. $return_data[$game_log_id] = 'error:' . __LINE__ . ' sql:' . $coin_log_model->getLastSql();
  217. break;
  218. }
  219. if ($game_commission) {
  220. if ($user_model->multiAddCoin($game_log_id, $result, -$game_commission / 100 * $times) === false) {
  221. Connect::rollback();
  222. $return_data[$game_log_id] = 'error:' . __LINE__ . ' sql:' . $user_model->getLastSql();
  223. break;
  224. }
  225. // 批量插入金币记录
  226. if ($coin_log_model->multiAddLog($game_log_id, $result, -$game_commission / 100 * $times, '玩家收入平台抽成') === false) {
  227. Connect::rollback();
  228. $return_data[$game_log_id] = 'error:' . __LINE__ . ' sql:' . $coin_log_model->getLastSql();
  229. break;
  230. }
  231. }
  232. if ($podcast_commission) {
  233. if ($user_model->multiAddCoin($game_log_id, $result, -$commission_rate / 100 * $times) === false) {
  234. Connect::rollback();
  235. $return_data[$game_log_id] = 'error:' . __LINE__ . ' sql:' . $user_model->getLastSql();
  236. break;
  237. }
  238. // 批量插入金币记录
  239. if ($coin_log_model->multiAddLog($game_log_id, $result, -$commission_rate / 100 * $times, '玩家收入主播抽成') === false) {
  240. Connect::rollback();
  241. $return_data[$game_log_id] = 'error:' . __LINE__ . ' sql:' . $coin_log_model->getLastSql();
  242. break;
  243. }
  244. $commission = $commission_rate / 100 * $times * $sum[$result - 1];
  245. if (defined('OPEN_DIAMOND_GAME_MODULE') && OPEN_DIAMOND_GAME_MODULE == 1) {
  246. $ticket = intval($commission * $game_type['ticket_rate'] / 100);
  247. $res = $user_model->update(['ticket' => ['ticket + ' . $ticket]], ['id' => $podcast_id]);
  248. $video_redis->inc_field($video_id, 'vote_number', $ticket);
  249. if ($res) {
  250. $log_data = [
  251. 'log_info' => '主播游戏赢家抽成',
  252. 'log_time' => NOW_TIME,
  253. 'log_admin_id' => 0,
  254. 'money' => 0,
  255. 'user_id' => 1,
  256. 'type' => 7,
  257. 'prop_id' => 0,
  258. 'score' => 0,
  259. 'point' => 0,
  260. 'podcast_id' => $podcast_id,
  261. 'diamonds' => 0,
  262. 'ticket' => $ticket,
  263. 'video_id' => $video_id,
  264. ];
  265. Model::build('user_log')->insert($log_data);
  266. }
  267. if (defined('GAME_DISTRIBUTION') && GAME_DISTRIBUTION) {
  268. if (Model::build('game_distribution')->addLog($podcast_id, $video_id, $game_log_id, $ticket, '游戏直播分销') === false) {
  269. Connect::rollback();
  270. $return_data[$game_log_id] = 'error:' . __LINE__ . ' sql:' . $user_game_log_model->getLastSql();
  271. break;
  272. }
  273. }
  274. } else {
  275. $res = $user_model->coin($podcast_id, $commission);
  276. $account_diamonds = $user_model->coin($podcast_id);
  277. if ($res) {
  278. //会员账户 金币变更日志表
  279. if ($coin_log_model->addLog($podcast_id, $game_log_id, $commission, $account_diamonds, '主播游戏赢家抽成') === false) {
  280. Connect::rollback();
  281. $return_data[$game_log_id] = 'error:' . __LINE__;
  282. break;
  283. }
  284. }
  285. if (defined('GAME_DISTRIBUTION') && GAME_DISTRIBUTION) {
  286. if (Model::build('game_distribution')->addLog($podcast_id, $video_id, $game_log_id, $commission, '游戏直播分销') === false) {
  287. Connect::rollback();
  288. $return_data[$game_log_id] = 'error:' . __LINE__ . ' sql:' . $user_game_log_model->getLastSql();
  289. break;
  290. }
  291. }
  292. }
  293. }
  294. $win_times = $times * (1 - ($game_commission + $commission_rate * $podcast_commission) / 100);
  295. // 批量插入获胜记录
  296. if ($user_game_log_model->multiAddLog($game_log_id, $result, $win_times, $podcast_id) === false) {
  297. Connect::rollback();
  298. $return_data[$game_log_id] = 'error:' . __LINE__ . ' sql:' . $user_game_log_model->getLastSql();
  299. break;
  300. }
  301. }
  302. // 主播收入增加
  303. if ($podcast_income) {
  304. if (defined('OPEN_DIAMOND_GAME_MODULE') && OPEN_DIAMOND_GAME_MODULE == 1) {
  305. $ticket = intval($podcast_income * $game_type['ticket_rate'] / 100);
  306. $res = $user_model->update(['ticket' => ['ticket + ' . $ticket]], ['id' => $podcast_id]);
  307. $video_redis->inc_field($video_id, 'vote_number', $ticket);
  308. if ($res) {
  309. $log_data = [
  310. 'log_info' => '游戏直播收入',
  311. 'log_time' => NOW_TIME,
  312. 'log_admin_id' => 0,
  313. 'money' => 0,
  314. 'user_id' => $podcast_id,
  315. 'type' => 7,
  316. 'prop_id' => 0,
  317. 'score' => 0,
  318. 'point' => 0,
  319. 'podcast_id' => $podcast_id,
  320. 'diamonds' => 0,
  321. 'ticket' => $ticket,
  322. 'video_id' => $video_id,
  323. ];
  324. Model::build('user_log')->insert($log_data);
  325. }
  326. if (defined('GAME_DISTRIBUTION') && GAME_DISTRIBUTION) {
  327. if (Model::build('game_distribution')->addLog($podcast_id, $video_id, $game_log_id, $ticket, '游戏直播分销') === false) {
  328. Connect::rollback();
  329. $return_data[$game_log_id] = 'error:' . __LINE__ . ' sql:' . $user_game_log_model->getLastSql();
  330. break;
  331. }
  332. }
  333. } else {
  334. $res = $user_model->coin($podcast_id, $podcast_income);
  335. $account_diamonds = $user_model->coin($podcast_id);
  336. if ($res) {
  337. //会员账户 金币变更日志表
  338. if ($coin_log_model->addLog($podcast_id, $game_log_id, $podcast_income, $account_diamonds, '游戏直播收入') === false) {
  339. Connect::rollback();
  340. $return_data[$game_log_id] = 'error:' . __LINE__;
  341. break;
  342. }
  343. }
  344. if (defined('GAME_DISTRIBUTION') && GAME_DISTRIBUTION) {
  345. if (Model::build('game_distribution')->addLog($podcast_id, $video_id, $game_log_id, $podcast_income, '游戏直播分销') === false) {
  346. Connect::rollback();
  347. $return_data[$game_log_id] = 'error:' . __LINE__ . ' sql:' . $user_game_log_model->getLastSql();
  348. break;
  349. }
  350. }
  351. }
  352. }
  353. // 主播收入增加
  354. if ($podcast_income + $commission) {
  355. if ($user_game_log_model->addLog($game_log_id, $podcast_id, $podcast_income + $commission) === false) {
  356. Connect::rollback();
  357. $return_data[$game_log_id] = 'error:' . __LINE__;
  358. break;
  359. }
  360. }
  361. $stop_banker = false;
  362. // 庄家收入
  363. if ($banker_id) {
  364. $banker_income = $income;
  365. if ($income > 0) {
  366. $banker_income = intval((100 - $game_commission - $commission_rate) / 100 * $income);
  367. }
  368. $res = $banker_log_model->update(['coin' => ["`coin`+$banker_income"]], ['user_id' => $banker_id, 'video_id' => $video_id, 'status' => 3]);
  369. $video_redis->inc_field($video_id, 'coin', $banker_income);
  370. $coin = $video_redis->getOne_db($video_id, 'coin');
  371. if ($res) {
  372. if ($user_game_log_model->addLog($game_log_id, $banker_id, $banker_income) === false) {
  373. Connect::rollback();
  374. $return_data[$game_log_id] = 'error:' . __LINE__;
  375. break;
  376. }
  377. }
  378. // 强制下庄
  379. if ($coin < $game_type['principal']) {
  380. $stop_banker = true;
  381. }
  382. }
  383. Connect::commit();
  384. $sql_time = microtime(1) - $sql_time;
  385. $ids = array();
  386. if ($sum[$result - 1]) {
  387. $res = $user_game_log_model->field('user_id')->group('user_id')->select(array('game_log_id' => $game_log_id));
  388. foreach ($res as $value) {
  389. $ids[] = $value['user_id'];
  390. }
  391. }
  392. if ($podcast_income) {
  393. $ids[] = $podcast_id;
  394. }
  395. if (!empty($ids)) {
  396. user_deal_to_reids($ids);
  397. }
  398. $banker = $video_redis->getRow_db($video_id, [
  399. 'banker_status',
  400. 'banker_id',
  401. 'banker_log_id',
  402. 'banker_name',
  403. 'banker_img',
  404. 'coin',
  405. ]);
  406. $tim_time = microtime(1);
  407. // 新推送
  408. $ext = array(
  409. 'type' => 39,
  410. 'desc' => '',
  411. 'room_id' => $video_id,
  412. 'time' => 0,
  413. 'game_id' => $game_id,
  414. 'game_log_id' => $game_log_id,
  415. 'game_status' => 2,
  416. 'game_action' => 4,
  417. 'podcast_income' => $podcast_income,
  418. 'game_data' => array(
  419. 'win' => $result,
  420. 'bet' => $sum,
  421. 'dices' => $dices,
  422. 'cards_data' => $cards_data,
  423. ),
  424. 'banker_status' => intval($banker['banker_status']),
  425. 'banker' => [
  426. 'banker_id' => intval($banker['banker_id']),
  427. 'banker_log_id' => intval($banker['banker_log_id']),
  428. 'banker_name' => $banker['banker_name'] ? $banker['banker_name'] : '',
  429. 'banker_img' => $banker['banker_img'] ? $banker['banker_img'] : '',
  430. 'coin' => intval($banker['coin']),
  431. 'max_bet' => $banker['coin'] / (max($option) - 1),
  432. ],
  433. );
  434. $res = timSystemNotify($game_log['group_id'], $ext);
  435. if ($stop_banker) {
  436. if ($banker_log_model->returnCoin(['video_id' => $video_id, 'status' => 3], '底金不足,玩家下庄') == false) {
  437. $return_data[$game_log_id] = 'error:' . __LINE__ . $banker_log_model->getLastSql();
  438. break;
  439. }
  440. $banker_ext = [
  441. 'type' => 43,
  442. 'desc' => '',
  443. 'room_id' => $video_id,
  444. 'action' => 4,
  445. 'banker_status' => 0,
  446. 'data' => [
  447. 'banker' => [
  448. 'banker_id' => intval($banker['banker_id']),
  449. 'banker_log_id' => intval($banker['banker_log_id']),
  450. 'banker_name' => $banker['banker_name'] ? $banker['banker_name'] : '',
  451. 'banker_img' => $banker['banker_img'] ? $banker['banker_img'] : '',
  452. 'coin' => intval($banker['coin']),
  453. ],
  454. ],
  455. ];
  456. $data = [
  457. 'banker_id' => 0,
  458. 'banker_status' => 0,
  459. "banker_log_id" => 0,
  460. "banker_name" => '',
  461. "banker_img" => '',
  462. 'coin' => 0,
  463. ];
  464. $video_redis->update_db($video_id, $data);
  465. $banker_res = timSystemNotify($game_log['group_id'], $banker_ext);
  466. }
  467. $tim_time = microtime(1) - $tim_time;
  468. $return_data[$game_log_id] = array(
  469. 'type' => 'result',
  470. 'id' => $game_log_id,
  471. 'data' => $ext,
  472. 'res' => $res,
  473. 'sql_time' => $sql_time,
  474. 'tim_time' => $tim_time,
  475. 'banker_ext' => $banker_ext,
  476. 'banker_res' => $banker_res,
  477. );
  478. } else {
  479. // 返还投注
  480. Connect::beginTransaction();
  481. if ($game_log_model->update(array('status' => 2), array('id' => $game_log_id)) === false) {
  482. Connect::rollback();
  483. $return_data[$game_log_id] = 'error:' . __LINE__;
  484. break;
  485. }
  486. $redis->set($game_log_id, array('status' => 2));
  487. if ($user_model->returnCoin($game_log_id) === false) {
  488. Connect::rollback();
  489. $return_data[$game_log_id] = 'error:' . __LINE__;
  490. break;
  491. }
  492. if ($coin_log_model->returnCoin($game_log_id) === false) {
  493. Connect::rollback();
  494. $return_data[$game_log_id] = 'error:' . __LINE__;
  495. break;
  496. }
  497. if ($GLOBALS['db']->affected_rows()) {
  498. $res = $user_game_log_model->field('user_id')->group('user_id')->select(array('game_log_id' => $game_log_id));
  499. $ids = array();
  500. foreach ($res as $value) {
  501. $ids[] = $value['user_id'];
  502. }
  503. user_deal_to_reids($ids);
  504. }
  505. Connect::commit();
  506. $data = [
  507. 'game_log_id' => 0,
  508. 'banker_id' => 0,
  509. 'banker_status' => 0,
  510. "banker_log_id" => 0,
  511. "banker_name" => '',
  512. "banker_img" => '',
  513. 'coin' => 0,
  514. ];
  515. $video_redis->update_db($video_id, $data);
  516. $ext = array(
  517. 'type' => 34,
  518. 'desc' => '',
  519. );
  520. $res = timSystemNotify($game_log['group_id'], $ext);
  521. $return_data[$game_log_id] = array(
  522. 'type' => 'end_return',
  523. 'id' => $game_log_id,
  524. 'res' => $res,
  525. );
  526. }
  527. $redis->unLockVideo($video_id);
  528. } else if ($game['create_time'] + $game['long_time'] > $time && !$banker_id) {
  529. // 机器人下注
  530. $robot_num = $video_redis->getOne_db($video_id, 'robot_num');
  531. if ($robot_num) {
  532. $rest = $game['create_time'] + $game['long_time'] - $time;
  533. if (rand(1, 300) < $game_type['rate'] && ($time - $game['create_time'] > 5)) {
  534. $option = json_decode($game_type['option'], 1);
  535. $data = array();
  536. $op = array_rand($option);
  537. for ($i = 0; $i < $robot_num; $i++) {
  538. if (isset($data[$op])) {
  539. $data[$op] += rand(0, 10) * 10;
  540. } else {
  541. $data[$op] = rand(0, 10) * 10;
  542. }
  543. }
  544. foreach ($data as $key => $value) {
  545. $redis->inc($game_log_id, 'option' . $key, $value);
  546. }
  547. $data = $redis->get($game_log_id, array('option1', 'option2', 'option3'));
  548. $bet = array();
  549. for ($i = 1; $i <= 3; $i++) {
  550. $bet[] = intval($data['option' . $i]);
  551. }
  552. $ext = array(
  553. 'type' => 39,
  554. 'room_id' => $video_id,
  555. 'desc' => '',
  556. 'time' => $rest,
  557. 'game_id' => $game_id,
  558. 'game_log_id' => $game_log_id,
  559. 'game_status' => 1,
  560. 'game_action' => 2,
  561. 'game_data' => array(
  562. 'bet' => $bet,
  563. ),
  564. );
  565. $res = timSystemNotify($game_log['group_id'], $ext);
  566. $return_data[$game_log_id] = array(
  567. 'type' => 'robot',
  568. 'id' => $game_log_id,
  569. 'ext' => $ext,
  570. 'res' => $res,
  571. );
  572. }
  573. }
  574. }
  575. }
  576. }
  577. $this->clearGameLog();
  578. if (defined('OPEN_BANKER_MODULE') && OPEN_BANKER_MODULE == 1) {
  579. $this->clearBankerLog();
  580. }
  581. $return_data['time'] = microtime(1) - $microtime;
  582. $redis->unLock();
  583. return $return_data;
  584. }
  585. /**
  586. * 将关闭的直播游戏上庄记录移入历史记录
  587. * @param string $value [description]
  588. * @return [type] [description]
  589. */
  590. public function clearBankerLog($value = '')
  591. {
  592. $video_table = DB_PREFIX . 'video';
  593. $table = DB_PREFIX . 'banker_log';
  594. $history = DB_PREFIX . 'banker_log_history';
  595. $res = $GLOBALS['db']->getOne("SELECT COUNT(1) FROM `$table` WHERE `video_id` NOT IN(SELECT id FROM `$video_table` WHERE live_in = 1)");
  596. if ($res) {
  597. $res = $GLOBALS['db']->getAll("SELECT `video_id` FROM `$table` WHERE `video_id` NOT IN(SELECT id FROM `$video_table` WHERE live_in = 1) AND `status` IN (1,3) GROUP BY `video_id`");
  598. $model = Model::build('banker_log');
  599. foreach ($res as $key => $value) {
  600. $data = [
  601. 'banker_id' => 0,
  602. 'banker_status' => 0,
  603. "banker_log_id" => 0,
  604. "banker_name" => '',
  605. "banker_img" => '',
  606. 'coin' => 0,
  607. ];
  608. $video_redis->update_db($value['video_id'], $data);
  609. $res = $model->returnCoin(['video_id' => $value['video_id'], 'status' => ['in', [1, 3]]], '主播退出,退还上庄金额');
  610. if ($res === false) {
  611. Connect::rollback();
  612. $return_data['clear_log'] = array('error:' . __LINE__ . ':' . $model->getLastSql());
  613. }
  614. }
  615. Connect::beginTransaction();
  616. $res = Connect::exec("INSERT INTO `$history`(SELECT * FROM `$table` WHERE `video_id` NOT IN(SELECT id FROM `$video_table` WHERE live_in = 1) AND `status` IN (2,4))");
  617. if ($res === false) {
  618. Connect::rollback();
  619. $return_data['clear_log'] = array('error:' . __LINE__);
  620. }
  621. $res = Connect::exec("DELETE FROM `$table` WHERE `video_id` NOT IN(SELECT id FROM `$video_table` WHERE live_in = 1) AND `status` IN (2,4)");
  622. if ($res === false) {
  623. Connect::rollback();
  624. $return_data['clear_log'] = array('error:' . __LINE__);
  625. }
  626. Connect::commit();
  627. }
  628. }
  629. /**
  630. * 将关闭的直播游戏记录移入历史记录
  631. * @return [type] [description]
  632. */
  633. public function clearGameLog()
  634. {
  635. $table = DB_PREFIX . 'game_log';
  636. $video_table = DB_PREFIX . 'video';
  637. $res = $GLOBALS['db']->getOne("SELECT COUNT(1) FROM `$table` WHERE podcast_id NOT IN(SELECT user_id FROM `$video_table` WHERE live_in = 1) AND `status`=2");
  638. if ($res) {
  639. $history = DB_PREFIX . 'game_log_history';
  640. $log_table = DB_PREFIX . 'user_game_log';
  641. $log_history = DB_PREFIX . 'user_game_log_history';
  642. Connect::beginTransaction();
  643. /**
  644. * 游戏记录迁移
  645. * @var [type]
  646. */
  647. $res = Connect::exec("INSERT INTO `$history`(SELECT * FROM `$table` WHERE podcast_id NOT IN(SELECT user_id FROM `$video_table` WHERE live_in = 1) AND `status`=2)");
  648. if ($res === false) {
  649. Connect::rollback();
  650. $return_data['clear_log'] = array('error:' . __LINE__);
  651. }
  652. $res = Connect::exec("DELETE FROM `$table` WHERE podcast_id NOT IN (SELECT user_id FROM `$video_table` WHERE live_in = 1) AND `status`=2");
  653. if ($res === false) {
  654. Connect::rollback();
  655. $return_data['clear_log'] = array('error:' . __LINE__);
  656. }
  657. /**
  658. * 下注记录迁移
  659. * @var [type]
  660. */
  661. $res = Connect::exec("INSERT INTO `$log_history`(SELECT * FROM `$log_table` WHERE `game_log_id` NOT IN(SELECT `id` FROM `$table`))");
  662. if ($res === false) {
  663. Connect::rollback();
  664. $return_data['clear_log'] = array('error:' . __LINE__);
  665. }
  666. $res = Connect::exec("DELETE FROM `$log_table` WHERE `game_log_id` NOT IN (SELECT `id` FROM `$table`)");
  667. if ($res === false) {
  668. Connect::rollback();
  669. $return_data['clear_log'] = array('error:' . __LINE__);
  670. }
  671. Connect::commit();
  672. }
  673. }
  674. public function sortGame($sum, $res)
  675. {
  676. $min = min($sum);
  677. asort($sum);
  678. $data = array();
  679. foreach ($sum as $k => $v) {
  680. if ($v == $min) {
  681. $data[] = array_shift($res);
  682. shuffle($data);
  683. shuffle($res);
  684. } else {
  685. $data[] = array_pop($res);
  686. }
  687. }
  688. $result = array();
  689. $i = 0;
  690. foreach ($sum as $k => $v) {
  691. $result[$k] = $data[$i];
  692. $i++;
  693. }
  694. ksort($result);
  695. return $result;
  696. }
  697. public function formatCardsData($cards_data)
  698. {
  699. $data = [];
  700. foreach ($cards_data as $key => $value) {
  701. $cards = self::parseCards($value['cards']);
  702. $data[] = array(
  703. 'win' => !$key,
  704. 'cards' => $cards,
  705. 'type' => $value['check']['type'],
  706. );
  707. }
  708. return $data;
  709. }
  710. }