Eos.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. if ($trxId) {
  122. $newData = [
  123. 'reveal_trx_id' => $trxId,
  124. ];
  125. if (!$game['revealInfo']) {
  126. $newData['reveal_block_num'] = $revealBlockNum;
  127. $newData['reveal_info'] = json_encode($revealInfo);
  128. }
  129. $objGame->objTable->updateObject($newData, ['id' => $game['id']]);
  130. // 开奖成功转账 balance
  131. $objOffer = new Offer();
  132. $players = $objOffer->getGamePlayer($game['id']);
  133. foreach ($players as $player => $offerTrxId) {
  134. self::balance($game['id'], $player, $offerTrxId);
  135. }
  136. return $newGameId;
  137. } else {
  138. $msg = "开奖超时,gameid:{$game['id']}, json:{$json}";
  139. alermErrorMsg($msg);
  140. return null;
  141. }
  142. }
  143. private function _getNewGame() {
  144. $objGame = new TableHelper('game', 'dw_baccarat');
  145. $keyWord = [
  146. '_sortKey' => 'id DESC',
  147. '_field' => 'id',
  148. 'game_state' => [1, 2]
  149. ];
  150. $row = $objGame->getRow($keyWord);
  151. $id = (int) date("Ymd00010001");
  152. $id2 = (int) $row['id'];
  153. if ($id2 >= $id) {
  154. $id = $id2 + 1;
  155. }
  156. return $id;
  157. }
  158. /**
  159. * 块信息
  160. * @param $blockId
  161. * @return array
  162. */
  163. public static function getBlockInfo($blockId) {
  164. $cmd = "cleos -u https://api.eosnewyork.io get block '{$blockId}'";
  165. $json = self::execCmd($cmd);
  166. return json_decode($json, true);
  167. }
  168. /**
  169. * 获取块id
  170. * @param $block_num
  171. *
  172. * @return array
  173. */
  174. public static function getBlockId($block_num) {
  175. $cmd = "cleos -u {$GLOBALS['eosUrl']} get block {$block_num}";
  176. $json = self::execCmd($cmd);
  177. $json = str_replace("\n", '', $json);
  178. $info = json_decode($json, true);
  179. if ($info && $info['id']) {
  180. $block_id = $info['id'];
  181. } else {
  182. $hex = dechex($block_num);
  183. $hex = sprintf("%08s", $hex);
  184. preg_match("/\"({$hex}[0-9a-fA-F]{56})\"/", $json, $matches);
  185. $block_id = $matches[1];
  186. }
  187. return $block_id;
  188. }
  189. /**
  190. * 提现接口
  191. * @param $player
  192. * @param $eos
  193. * @return array $transaction_id
  194. */
  195. function withdraw($player, $eos) {
  196. $memo = "withdraw";
  197. return $this->transfer($GLOBALS['codeBaccarat'], $player, $eos, $memo, 'withdraw');
  198. }
  199. /**
  200. * 回收gt币接口
  201. * @param $player
  202. * @param $eos
  203. * @return array $transaction_id
  204. */
  205. function buyback($player, $eos) {
  206. $memo = "buyback|1";
  207. return $this->transfer($GLOBALS['codeGtAdmin'], $player, $eos, $memo, 'buyback');
  208. }
  209. /**
  210. * 提现分红接口
  211. * @param $player
  212. * @param $eos
  213. * @return array $transaction_id
  214. */
  215. function withdrawBonus($player, $eos) {
  216. $memo = "withdraw bonus";
  217. return $this->transfer($GLOBALS['codeGtAdmin'], $player, $eos, $memo, 'withdraw bonus');
  218. }
  219. /**
  220. * 游戏分红
  221. * @param $gameid
  222. * @param $bonus
  223. * @param $buyback
  224. *
  225. * @return array
  226. */
  227. function gamebalance($gameid, $bonus, $buyback) {
  228. $memo = "gamebalance|{$gameid}|{$buyback}|{$bonus}";
  229. $eos = sprintf("%.4f EOS", round(($buyback + $bonus) / 10000, 4));
  230. return $this->transfer($GLOBALS['codeBaccarat'], $GLOBALS['codeGtAdmin'], $eos, $memo, 'gamebalance');
  231. }
  232. /**
  233. * 结算转账接口
  234. * @param int $gameid
  235. * @param string $userName
  236. * @return string
  237. */
  238. public static function balance($gameid, $userName, $player_transaction_id) {
  239. $cmd = "cleos -u {$GLOBALS['eosUrl']} push action {$GLOBALS['codeBaccarat']} balance '[ {$gameid}, \"{$userName}\" ]' -j -p {$GLOBALS['codeBaccarat']}";
  240. $json = self::execCmd($cmd);
  241. $log = [
  242. 'player_transaction_id' => $player_transaction_id,
  243. 'transaction_type' => 'balance',
  244. 'from' => $GLOBALS['codeBaccarat'],
  245. 'to' => $userName,
  246. 'cmd' => $cmd,
  247. 'json' => $json,
  248. ];
  249. // 记录交易,失败重试
  250. self::logTrans($log);
  251. return $json;
  252. }
  253. /**
  254. * 结算转账接口
  255. * @param string $userName
  256. * @param string $eos
  257. * @param string $gameid
  258. * @param string $betEos
  259. * @param string $player_transaction_id
  260. * @return array
  261. */
  262. function dig($userName, $eos, $gameid, $betEos, $player_transaction_id = '') {
  263. $from = $GLOBALS['codeGtAdmin'];
  264. $memo = "【https://eosget.io】Dig in Box {$gameid}";
  265. $cmd = "cleos -u {$GLOBALS['eosUrl']} push action {$from} dig '[ \"{$userName}\", \"{$eos}\", \"{$memo}\", \"{$betEos}\" ]' -j -p {$from}";
  266. $json = self::execCmd($cmd);
  267. $log = [
  268. 'player_transaction_id' => $player_transaction_id,
  269. 'transaction_type' => 'dig',
  270. 'from' => $from,
  271. 'to' => $userName,
  272. 'cmd' => $cmd,
  273. 'json' => $json,
  274. ];
  275. // 记录交易,失败重试
  276. $trans = self::logTrans($log);
  277. if (!$trans['transaction_id']) {
  278. alermErrorMsg("挖矿异常,player:{$userName}, eos:{$eos}, json:{$json}");
  279. }
  280. return $trans;
  281. }
  282. function _formatDateTime($str) {
  283. if (strpos($str, 'T') > 0) {
  284. return date('Y-m-d H:i:s', strtotime($str) + 8 * 3600);
  285. } else {
  286. return date('Y-m-d H:i:s', strtotime($str));
  287. }
  288. }
  289. public static function getAccountActions($account_name, $limit = 200) {
  290. // $cmd = "cleos -u {$GLOBALS['eosUrl']} get actions {$account_name} -1 -{$limit} -j";
  291. if (ENV == ENV_DEV) {
  292. $url = $GLOBALS['eosUrl'];
  293. } else {
  294. // 正式环境,要查历史数据,只能用 nodes.get-scatter.com
  295. $url = 'https://nodes.get-scatter.com';
  296. }
  297. $cmd = "cleos -u {$url} get actions {$account_name} -1 -{$limit} -j";
  298. $json = self::execCmd($cmd);
  299. return json_decode($json, true);
  300. }
  301. /**
  302. * 获取gt推广奖励
  303. * @param $player
  304. * @param $invite_friend
  305. * @param $amount
  306. * @return array
  307. */
  308. public function getGtReward($player, $invite_friend, $amount) {
  309. $memo = "invite friend:" . $invite_friend;
  310. return $this->transfer($GLOBALS['codeGtAdmin'], $player, $amount, $memo, 'invite friend');
  311. }
  312. /**
  313. * 获取用户gt
  314. * @param $player
  315. */
  316. public static function getAccountGt($player) {
  317. $cmd = "cleos -u {$GLOBALS['eosUrl']} get currency balance eosgetgtoken {$player} gt";
  318. $ret = self::execCmd($cmd);
  319. return $ret;
  320. }
  321. /**
  322. * gt 重新质押
  323. * @param $player
  324. * @param $gt_amount
  325. */
  326. public function restake($player, $gt_amount) {
  327. $cmd = "cleos -u {$GLOBALS['eosUrl']} push action {$GLOBALS['codeGtAdmin']} restake '[\"{$player}\", \"{$gt_amount}\"]' -p {$GLOBALS['codeGtAdmin']} -j";
  328. $ret = self::execCmd($cmd);
  329. return json_decode($ret, true);
  330. }
  331. /**
  332. * gt 解冻用户赎回gt
  333. * @param $player
  334. * @param $gt_amount
  335. * @return array
  336. */
  337. public function unfreeze($player, $gt_amount) {
  338. $cmd = "cleos -u {$GLOBALS['eosUrl']} push action {$GLOBALS['codeGtAdmin']} unfreeze '[\"{$player}\", \"{$gt_amount}\"]' -p {$GLOBALS['codeGtAdmin']} -j";
  339. $ret = self::execCmd($cmd);
  340. return json_decode($ret, true);
  341. }
  342. /*
  343. * gt 分红
  344. * @param $player
  345. * @param $gt_amount
  346. */
  347. public function bonus() {
  348. // cleos -u http://61.160.36.19:7777/ push action eosgetadmin1 bonus '' -p eosgetadmin1
  349. $cmd = "cleos -u {$GLOBALS['eosUrl']} push action {$GLOBALS['codeGtAdmin']} bonus '' -p {$GLOBALS['codeGtAdmin']} -j";
  350. $ret = self::execCmd($cmd);
  351. return json_decode($ret, true);
  352. }
  353. public function unstake($player, $gt_amount) {
  354. $cmd = "cleos -u {$GLOBALS['eosUrl']} push action {$GLOBALS['codeGtAdmin']} unstake '[\"{$player}\", \"{$gt_amount}\"]' -p {$player} -j";
  355. $ret = self::execCmd($cmd);
  356. return json_decode($ret, true);
  357. }
  358. public static function getRoomRewardList($roomId, $gameId) {
  359. return self::PRE_ROOM_REWARD . $roomId . ':' . $gameId;
  360. }
  361. public static function getRoomInfo($roomId) {
  362. return $GLOBALS['roomInfo'][$roomId] ?: [];
  363. }
  364. public static function getRoomIdFromGameId($gameId) {
  365. return intval($gameId / 100000) % 1000;
  366. }
  367. public static function toDisplayFormat($amount, $unit = 'EOS') {
  368. return sprintf("%.4f {$unit}", round($amount / 10000, 4));
  369. }
  370. public static function toDisplayFormat2($amount, $unit = 'EOS', $keep = 4) {
  371. return sprintf("%.{$keep}f {$unit}", round($amount / 10000, 4));
  372. }
  373. public static function toNumber($amount) {
  374. $arr = explode(' ', $amount);
  375. return intval(floatval($arr[0]) * 10000);
  376. }
  377. }