Eos.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. <?php
  2. /**
  3. * EOS相关操作
  4. * User: ben
  5. * Date: 2018/9/19
  6. * Time: 下午2:59
  7. */
  8. class Eos extends EosBase {
  9. const REDIS_SERV = 'dw_baccarat';
  10. const KEY_BALANCE_LIST = "globals:dw_baccarat:balance_list";
  11. const PRE_SIGN_MAP = "globals:dw_baccarat:sign_map:";
  12. const PRE_CREATE_TRANS_MAP = "globals:dw_baccarat:create_trans_map:";
  13. const PRE_ROOM_REWARD = 'globals:dw_baccarat:room_reward:';
  14. const ROOM_LIMIT = 10;
  15. const LIVE_LIMIT = 20;
  16. static $objRedisMap = [];
  17. function __construct() {
  18. // 解锁钱包
  19. $this->unlockWallet();
  20. }
  21. static function pubEvent($event, $data, $cacheKey = self::REDIS_SERV) {
  22. // dwRedis::cleanInstance();
  23. // $objRedis = dwRedis::init('dw_box');
  24. if (!self::$objRedisMap[$cacheKey]) {
  25. self::$objRedisMap[$cacheKey] = dwRedis::initNewOne($cacheKey);
  26. }
  27. if (is_array($data)) {
  28. $data = json_encode($data);
  29. }
  30. return self::$objRedisMap[$cacheKey]->publish($event, $data);
  31. }
  32. /**
  33. * 创建游戏
  34. * @return array
  35. */
  36. public function createGame($sync = true) {
  37. $lockKey = 'do_create';
  38. $objLock = new Lock('createGame', 2);
  39. $flag = $objLock->lock($lockKey, false);
  40. if (!$flag) {
  41. return [];
  42. }
  43. $id = $this->_getNewGame();
  44. $cmd = "cleos -u {$GLOBALS['eosUrl']} push action {$GLOBALS['codeBaccarat']} creategame '[{$id}]' -p {$GLOBALS['codeBaccarat']} -j ";
  45. $json = self::execCmd($cmd);
  46. $log = [
  47. 'transaction_type' => 'createGame',
  48. 'from' => $GLOBALS['codeBaccarat'],
  49. 'to' => $GLOBALS['codeBaccarat'],
  50. 'cmd' => $cmd,
  51. 'json' => $json,
  52. ];
  53. // 记录交易,失败重试
  54. $data = self::logTrans($log);
  55. $data['gameid'] = $id;
  56. Eos::log("cmd: {$cmd}");
  57. Eos::log("json: {$json}");
  58. // 同步数据
  59. if ($sync) {
  60. // $this->syncGame();
  61. $objSyncGame = new Sync_Game();
  62. $objSyncGame->pubSubscribe(false);
  63. }
  64. $objLock->unlock($lockKey);
  65. return $data;
  66. }
  67. /**
  68. * 开奖并创建游戏
  69. * @param $game
  70. * @param null $objGame
  71. * @return int
  72. * @throws DB_Exception
  73. */
  74. public function revealCreate($game, $objGame = null) {
  75. !$objGame && $objGame = new Game();
  76. if (!$game['create_block_num']) {
  77. $msg = "game:{$game['id']} is error, create_block_num not found";
  78. Eos::log($msg);
  79. alermErrorMsg($msg);
  80. return null;
  81. }
  82. $newGameId = $this->_getNewGame();
  83. // $commitment = $info['commitment'];
  84. if ($newGameId == $game['id']) {
  85. alermErrorMsg("revealCreate error. newGameId:{$newGameId} == gameid:{$game['id']}");
  86. return null;
  87. }
  88. // 获取开奖数据
  89. if ($game['revealInfo']) {
  90. $revealInfo = json_decode($game['revealInfo'], true);
  91. $win = $revealInfo['win'];
  92. $playerPoker = $revealInfo['player'];
  93. $bankerPoker = $revealInfo['banker'];
  94. $playerPair = $revealInfo['playerPair'];
  95. $bankerPair = $revealInfo['bankerPair'];
  96. $revealBlockNum = $revealInfo['revealBlockNum'];
  97. } else {
  98. try {
  99. list($win, $playerPoker, $bankerPoker, $playerPair, $bankerPair, $revealBlockNum, $revealInfo) = Game::revealGameData($game['create_block_num'], $game['ttl']);
  100. } catch (Exception $e) {
  101. Eos::log($e->getMessage());
  102. return null;
  103. }
  104. }
  105. $playerPoker = sprintf("[%s]", implode(', ', $playerPoker));
  106. $bankerPoker = sprintf("[%s]", implode(', ', $bankerPoker));
  107. $playerPair = $playerPair ? 'true' : 'false';
  108. $bankerPair = $bankerPair ? 'true' : 'false';
  109. $cmd = "cleos -u {$GLOBALS['eosUrl']} push action {$GLOBALS['codeBaccarat']} revealcreate '[ {$game['id']}, {$bankerPoker}, {$playerPoker}, {$bankerPair}, {$playerPair}, {$win}, {$newGameId}]' -p {$GLOBALS['codeBaccarat']} -j ";
  110. $json = self::execCmd($cmd);
  111. $log = [
  112. 'transaction_type' => 'revealcreate',
  113. 'from' => $GLOBALS['codeBaccarat'],
  114. 'to' => $GLOBALS['codeBaccarat'],
  115. 'cmd' => $cmd,
  116. 'json' => $json,
  117. ];
  118. // 记录交易,失败重试
  119. $trans = self::logTrans($log);
  120. $trxId = $trans['transaction_id'];
  121. $objRedis = dwRedis::init(self::REDIS_SERV);
  122. $objRedis->setex('globals:temp_trx:' . $trxId, 300, $trans['block_num']);
  123. if ($trxId) {
  124. $newData = [
  125. 'reveal_trx_id' => $trxId,
  126. ];
  127. if (!$game['revealInfo']) {
  128. $newData['reveal_block_num'] = $revealBlockNum;
  129. $newData['reveal_info'] = json_encode($revealInfo);
  130. }
  131. $objGame->objTable->updateObject($newData, ['id' => $game['id']]);
  132. // 开奖成功转账 balance
  133. $objOffer = new Offer();
  134. $players = $objOffer->getGamePlayer($game['id']);
  135. foreach ($players as $player => $offerTrxId) {
  136. $ret = self::balance($game['id'], $player, $offerTrxId);
  137. $resultTrxId = $ret['transaction_id'];
  138. if ($resultTrxId) {
  139. $objOffer->objTable->updateObject(['result_trx_id' => $resultTrxId], [
  140. 'game_id' => $game['id'],
  141. 'player' => $player,
  142. ]);
  143. }
  144. }
  145. return $newGameId;
  146. } else {
  147. $msg = "开奖超时,gameid:{$game['id']}, json:{$json}";
  148. alermErrorMsg($msg);
  149. return null;
  150. }
  151. }
  152. private function _getNewGame() {
  153. $objGame = new TableHelper('game', 'dw_baccarat');
  154. $keyWord = [
  155. '_sortKey' => 'id DESC',
  156. '_field' => 'id',
  157. 'game_state' => [1, 2]
  158. ];
  159. $row = $objGame->getRow($keyWord);
  160. $id = (int) date("Ymd00010001");
  161. $id2 = (int) $row['id'];
  162. if ($id2 >= $id) {
  163. $id = $id2 + 1;
  164. }
  165. return $id;
  166. }
  167. public static function getTempBlockNum($trxId) {
  168. $objRedis = dwRedis::init(self::REDIS_SERV);
  169. return (int) $objRedis->get('globals:temp_trx:' . $trxId);
  170. }
  171. /**
  172. * 块信息
  173. * @param $blockId
  174. * @return array
  175. */
  176. public static function getBlockInfo($blockId) {
  177. $cmd = "cleos -u https://api.eosnewyork.io get block '{$blockId}'";
  178. $json = self::execCmd($cmd);
  179. return json_decode($json, true);
  180. }
  181. /**
  182. * 获取块id
  183. * @param $block_num
  184. *
  185. * @return array
  186. */
  187. public static function getBlockId($block_num) {
  188. $cmd = "cleos -u {$GLOBALS['eosUrl']} get block {$block_num}";
  189. $json = self::execCmd($cmd);
  190. $json = str_replace("\n", '', $json);
  191. $info = json_decode($json, true);
  192. if ($info && $info['id']) {
  193. $block_id = $info['id'];
  194. } else {
  195. $hex = dechex($block_num);
  196. $hex = sprintf("%08s", $hex);
  197. preg_match("/\"({$hex}[0-9a-fA-F]{56})\"/", $json, $matches);
  198. $block_id = $matches[1];
  199. }
  200. return $block_id;
  201. }
  202. /**
  203. * 提现接口
  204. * @param $player
  205. * @param $eos
  206. * @return array $transaction_id
  207. */
  208. function withdraw($player, $eos) {
  209. $memo = "withdraw";
  210. return $this->transfer($GLOBALS['codeBaccarat'], $player, $eos, $memo, 'withdraw');
  211. }
  212. /**
  213. * 回收gt币接口
  214. * @param $player
  215. * @param $eos
  216. * @return array $transaction_id
  217. */
  218. function buyback($player, $eos) {
  219. $memo = "buyback|1";
  220. return $this->transfer($GLOBALS['codeGtAdmin'], $player, $eos, $memo, 'buyback');
  221. }
  222. /**
  223. * 提现分红接口
  224. * @param $player
  225. * @param $eos
  226. * @return array $transaction_id
  227. */
  228. function withdrawBonus($player, $eos) {
  229. $memo = "withdraw bonus";
  230. return $this->transfer($GLOBALS['codeGtAdmin'], $player, $eos, $memo, 'withdraw bonus');
  231. }
  232. /**
  233. * 游戏分红
  234. * @param $gameid
  235. * @param $bonus
  236. * @param $buyback
  237. *
  238. * @return array
  239. */
  240. function gamebalance($gameid, $bonus, $buyback) {
  241. $memo = "gamebalance|{$gameid}|{$buyback}|{$bonus}";
  242. $eos = sprintf("%.4f EOS", round(($buyback + $bonus) / 10000, 4));
  243. return $this->transfer($GLOBALS['codeBaccarat'], $GLOBALS['codeGtAdmin'], $eos, $memo, 'gamebalance');
  244. }
  245. /**
  246. * 结算转账接口
  247. * @param int $gameid
  248. * @param string $userName
  249. * @return array
  250. */
  251. public static function balance($gameid, $userName, $player_transaction_id) {
  252. $cmd = "cleos -u {$GLOBALS['eosUrl']} push action {$GLOBALS['codeBaccarat']} balance '[ {$gameid}, \"{$userName}\" ]' -j -p {$GLOBALS['codeBaccarat']}";
  253. $json = self::execCmd($cmd);
  254. $log = [
  255. 'player_transaction_id' => $player_transaction_id,
  256. 'transaction_type' => 'balance',
  257. 'from' => $GLOBALS['codeBaccarat'],
  258. 'to' => $userName,
  259. 'cmd' => $cmd,
  260. 'json' => $json,
  261. ];
  262. // 记录交易,失败重试
  263. return self::logTrans($log);
  264. }
  265. /**
  266. * 结算转账接口
  267. * @param string $userName
  268. * @param string $eos
  269. * @param string $gameid
  270. * @param string $betEos
  271. * @param string $player_transaction_id
  272. * @return array
  273. */
  274. function dig($userName, $eos, $gameid, $betEos, $player_transaction_id = '') {
  275. $from = $GLOBALS['codeGtAdmin'];
  276. $memo = "【https://eosget.io】Dig in Box {$gameid}";
  277. $cmd = "cleos -u {$GLOBALS['eosUrl']} push action {$from} dig '[ \"{$userName}\", \"{$eos}\", \"{$memo}\", \"{$betEos}\" ]' -j -p {$from}";
  278. $json = self::execCmd($cmd);
  279. $log = [
  280. 'player_transaction_id' => $player_transaction_id,
  281. 'transaction_type' => 'dig',
  282. 'from' => $from,
  283. 'to' => $userName,
  284. 'cmd' => $cmd,
  285. 'json' => $json,
  286. ];
  287. // 记录交易,失败重试
  288. $trans = self::logTrans($log);
  289. if (!$trans['transaction_id']) {
  290. alermErrorMsg("挖矿异常,player:{$userName}, eos:{$eos}, json:{$json}");
  291. }
  292. return $trans;
  293. }
  294. function _formatDateTime($str) {
  295. if (strpos($str, 'T') > 0) {
  296. return date('Y-m-d H:i:s', strtotime($str) + 8 * 3600);
  297. } else {
  298. return date('Y-m-d H:i:s', strtotime($str));
  299. }
  300. }
  301. public static function getAccountActions($account_name, $limit = 200) {
  302. // $cmd = "cleos -u {$GLOBALS['eosUrl']} get actions {$account_name} -1 -{$limit} -j";
  303. if (ENV == ENV_DEV) {
  304. $url = $GLOBALS['eosUrl'];
  305. } else {
  306. // 正式环境,要查历史数据,只能用 nodes.get-scatter.com
  307. $url = 'https://nodes.get-scatter.com';
  308. }
  309. $cmd = "cleos -u {$url} get actions {$account_name} -1 -{$limit} -j";
  310. $json = self::execCmd($cmd);
  311. return json_decode($json, true);
  312. }
  313. /**
  314. * 获取gt推广奖励
  315. * @param $player
  316. * @param $invite_friend
  317. * @param $amount
  318. * @return array
  319. */
  320. public function getGtReward($player, $invite_friend, $amount) {
  321. $memo = "invite friend:" . $invite_friend;
  322. return $this->transfer($GLOBALS['codeGtAdmin'], $player, $amount, $memo, 'invite friend');
  323. }
  324. /**
  325. * 获取用户gt
  326. * @param $player
  327. */
  328. public static function getAccountGt($player) {
  329. $cmd = "cleos -u {$GLOBALS['eosUrl']} get currency balance eosgetgtoken {$player} gt";
  330. $ret = self::execCmd($cmd);
  331. return $ret;
  332. }
  333. /**
  334. * gt 重新质押
  335. * @param $player
  336. * @param $gt_amount
  337. */
  338. public function restake($player, $gt_amount) {
  339. $cmd = "cleos -u {$GLOBALS['eosUrl']} push action {$GLOBALS['codeGtAdmin']} restake '[\"{$player}\", \"{$gt_amount}\"]' -p {$GLOBALS['codeGtAdmin']} -j";
  340. $ret = self::execCmd($cmd);
  341. return json_decode($ret, true);
  342. }
  343. /**
  344. * gt 解冻用户赎回gt
  345. * @param $player
  346. * @param $gt_amount
  347. * @return array
  348. */
  349. public function unfreeze($player, $gt_amount) {
  350. $cmd = "cleos -u {$GLOBALS['eosUrl']} push action {$GLOBALS['codeGtAdmin']} unfreeze '[\"{$player}\", \"{$gt_amount}\"]' -p {$GLOBALS['codeGtAdmin']} -j";
  351. $ret = self::execCmd($cmd);
  352. return json_decode($ret, true);
  353. }
  354. /*
  355. * gt 分红
  356. * @param $player
  357. * @param $gt_amount
  358. */
  359. public function bonus() {
  360. // cleos -u http://61.160.36.19:7777/ push action eosgetadmin1 bonus '' -p eosgetadmin1
  361. $cmd = "cleos -u {$GLOBALS['eosUrl']} push action {$GLOBALS['codeGtAdmin']} bonus '' -p {$GLOBALS['codeGtAdmin']} -j";
  362. $ret = self::execCmd($cmd);
  363. return json_decode($ret, true);
  364. }
  365. public function unstake($player, $gt_amount) {
  366. $cmd = "cleos -u {$GLOBALS['eosUrl']} push action {$GLOBALS['codeGtAdmin']} unstake '[\"{$player}\", \"{$gt_amount}\"]' -p {$player} -j";
  367. $ret = self::execCmd($cmd);
  368. return json_decode($ret, true);
  369. }
  370. public static function getRoomRewardList($roomId, $gameId) {
  371. return self::PRE_ROOM_REWARD . $roomId . ':' . $gameId;
  372. }
  373. public static function getRoomInfo($roomId) {
  374. return $GLOBALS['roomInfo'][$roomId] ?: [];
  375. }
  376. public static function getRoomIdFromGameId($gameId) {
  377. return intval($gameId / 100000) % 1000;
  378. }
  379. public static function toDisplayFormat($amount, $unit = 'EOS') {
  380. return sprintf("%.4f {$unit}", round($amount / 10000, 4));
  381. }
  382. public static function toDisplayFormat2($amount, $unit = 'EOS', $keep = 4) {
  383. return sprintf("%.{$keep}f {$unit}", round($amount / 10000, 4));
  384. }
  385. public static function toNumber($amount) {
  386. $arr = explode(' ', $amount);
  387. return intval(floatval($arr[0]) * 10000);
  388. }
  389. }