HttpServerSettelement.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/26
  6. * Time: 12:03
  7. */
  8. namespace datainf\logic;
  9. use App\Lib\ModelBase;
  10. use App\Http\Response\Response;
  11. use App\Logic\WinfailLogic;
  12. use datainf\lib\GlobConfigs;
  13. use Illuminate\Database\Capsule\Manager as DB;
  14. use swoole;
  15. use App\Logic\SettelementLogic;
  16. class HttpServerSettelement
  17. {
  18. private $httpserver;
  19. private $config;
  20. private $dbpooleconfig = [];
  21. private $redisonfig = [];
  22. const SQLKEY = 'ALLSQLKEY';
  23. private $TaskSqlQueue;
  24. use SettelementLogic;
  25. public function __construct($config)
  26. {
  27. $this->httpserver = new \swoole\http\server($config['host'], $config['port']);
  28. $this->httpserver->set($config['sets']);
  29. $this->config = $config;
  30. $this->dbpooleconfig = GlobConfigs::getKey('pgsqlpoole');
  31. $this->redisonfig = GlobConfigs::getKey('redis');
  32. $taskWorkingNum = new \swoole\Atomic();
  33. $this->httpserver->taskWorkingNum = $taskWorkingNum;
  34. $this->httpserver->on('request', array($this, 'OnRequest'));
  35. $this->httpserver->on('WorkerStart', array($this, 'onWorkerStart'));
  36. $this->httpserver->on('task', array($this, 'onTask'));
  37. $this->httpserver->on('finish', array($this, 'onFinish'));
  38. }
  39. public function onWorkerStart($serv, $worker_id)
  40. {
  41. $name = $serv->taskworker ? 'Task_' : 'Worker_';
  42. $name = 'Settelement_' . $name . ($serv->worker_id < 10 ? '0' . $serv->worker_id : $serv->worker_id);
  43. swoole_set_process_name($name);
  44. $this->TaskSqlQueue = new \SplQueue();
  45. $GLOBALS['model'] = '';
  46. $GLOBALS['modeltime'] = 0;
  47. if (!$serv->taskworker) {
  48. $this->InitDb();
  49. }
  50. $obj = $this;
  51. Swoole\Timer::tick(1000, function ($id) use ($obj, $serv) {
  52. $obj->TaskSQlToRedis();
  53. });
  54. }
  55. public function OnRequest($request, $response)
  56. {
  57. $response->header('Content-Type', 'text/html; charset=utf-8');
  58. $response->header('Server', 'DataInfaceServer');
  59. $paras = array_merge(['request_time' => date("H:i:s")], !empty($request->get) ? $request->get : [], !empty($request->post) ? $request->post : []);
  60. $request_uri = substr($request->server['request_uri'], 1);
  61. $urls = ['WinFail', 'Settelement'];
  62. echo "现在任务数:" . $this->httpserver->taskWorkingNum->get() . "\n";
  63. if (!in_array($request_uri, $urls)) {
  64. $data = Response::generate('', 0, '', '无效的url');
  65. $response->end($data);
  66. return;
  67. }
  68. if ($this->httpserver->taskWorkingNum->get() > 0) {
  69. $data = Response::generate('', 9, '', '还有未完成任务请稍等...');
  70. $response->end($data);
  71. return;
  72. }
  73. $check_token = true;
  74. if ($check_token) {
  75. $token = isset($paras['token']) ? $paras['token'] : '';
  76. if (empty($token) || empty($this->Tokencheck($token))) {
  77. $data = Response::generate('', 6, '', '安全验证失败!');
  78. $response->end($data);
  79. return;
  80. }
  81. }
  82. echo "计数器值:" . $this->httpserver->taskWorkingNum->get() . "\n";
  83. if ($request_uri == 'WinFail') {
  84. return $this->doWinFailse($request, $response, $paras);
  85. }
  86. if ($request_uri == 'Settelement') {
  87. return $this->doSettelement($request, $response, $paras);
  88. }
  89. return;
  90. }
  91. public function onTask($serv, $task)
  92. {
  93. $this->httpserver->taskWorkingNum->add();
  94. if ($task['type'] == 'doWinFailse') {
  95. $this->Task_winfalse($serv, $task);
  96. }
  97. if ($task['type'] == 'Settelement') {
  98. $this->Task_Settelement($serv, $task);
  99. }
  100. $this->httpserver->taskWorkingNum->sub();
  101. }
  102. private function Tokencheck($token)
  103. {
  104. $tokenvel = DB::table('system_user')->where(['token' => $token])->first();
  105. return $tokenvel;
  106. }
  107. //胜负计算
  108. private function doWinFailse($request, $response, $paras)
  109. {
  110. $logic_obj = new WinfailLogic();
  111. $notice = isset($paras['noticeid']) ? $paras['noticeid'] : 0;
  112. $id = intval($notice);
  113. try {
  114. $noticeModel = $logic_obj->getNoticeDate($id);
  115. $result = $logic_obj->getCompResult($noticeModel->game_code, $noticeModel->match_id);
  116. $moneySimples = $logic_obj->getSimplexData($noticeModel->game_code, $noticeModel->match_id);
  117. $matchs_1 = $logic_obj->getMatchRecords($noticeModel->game_code, $noticeModel->match_id, 1);
  118. $matchs_2 = $logic_obj->getMatchRecords($noticeModel->game_code, $noticeModel->match_id, 2);
  119. $match_firstModel = isset($matchs_1['0']) ? $matchs_1['0'] : (isset($matchs_2['0']) ? $matchs_2['0'] : false);
  120. if (!$match_firstModel) {
  121. throw new \Exception('无数据异常-1!');
  122. return true;
  123. }
  124. $gjModel = $logic_obj->getGjDatas($match_firstModel);
  125. if (count($matchs_1) > 0 && count($moneySimples) <= 0) {
  126. throw new \Exception('数据异常-2!');
  127. }
  128. if (strtolower($match_firstModel->p_code) == 'gj') {
  129. if (empty($gjModel)) {
  130. throw new \Exception("没有冠军数据数据--match_id" . $noticeModel->match_id);
  131. }
  132. $result = [$gjModel];
  133. }
  134. if (!empty($matchs_1)) {
  135. while (true) {
  136. if ($this->httpserver->taskWorkingNum->get() > intval($this->config['sets']['task_worker_num'])) {
  137. co::sleep(50);
  138. } else {
  139. $now = array_shift($matchs_1);
  140. if (empty($now)) {
  141. break;
  142. }
  143. $paras = ['type' => 'doWinFailse', 'stype' => 1, 'matchModel' => $now, 'result' => $result, 'moneySimple' => $moneySimples[$now->batch_id][$now->match_id]];
  144. $this->onTask($this->httpserver, $paras);
  145. }
  146. }
  147. }
  148. if (!empty($matchs_2)) {
  149. while (true) {
  150. if ($this->httpserver->taskWorkingNum->get() > intval($this->config['sets']['task_worker_num'])) {
  151. co::sleep(50);
  152. } else {
  153. $now = array_shift($matchs_2);
  154. if (empty($now)) {
  155. break;
  156. }
  157. $paras = ['type' => 'doWinFailse', 'stype' => 1, 'matchModel' => $now, 'result' => $result, 'moneySimple' => $moneySimples[$now->batch_id][$now->match_id]];
  158. $this->onTask($this->httpserver, $paras);
  159. }
  160. }
  161. }
  162. unset($logic_obj, $noticeModel, $AdapterObj, $result, $moneySimples, $matchs_1, $matchs_2, $RefClass);
  163. /*
  164. $this->httpserver->taskWorkingNum->add();
  165. $this->sqltoRedisqueue($this->TaskSqlArray);
  166. $this->httpserver->taskWorkingNum->sub();
  167. */
  168. $data = Response::generate('', 1, ['cost' => (microtime(true) - $request->server['request_time_float'])], 'succes ');
  169. $response->end($data);
  170. return;
  171. } catch (\Exception $e) {
  172. unset($logic_obj);
  173. $data = Response::generate('', 10, '', $e->getMessage() . '--' . $e->getFile() . '--' . $e->getLine());
  174. $response->end($data);
  175. return;
  176. }
  177. return;
  178. }
  179. /////
  180. private function Task_winfalse($serv, $task)
  181. {
  182. try {
  183. $moneyBuy = $task['moneySimple'];
  184. $matchModel = $task['matchModel'];
  185. $logic = WinfailLogic::getInstance();
  186. $AdapterObj = $logic->getAdapterObj($matchModel->game_code);
  187. $RefClass = new \ReflectionClass($AdapterObj);
  188. //如果手动更改了结果,就用更改后的结果,否则用默认的结果
  189. if ($matchModel->bet_type == 1 && $moneyBuy['result_flag'] == 1) {
  190. $now_result = [json_decode($moneyBuy['single_result'])];
  191. } else {
  192. foreach ($task['result'] as $key => $val) {
  193. $now_result[] = $val;
  194. }
  195. }
  196. $fun1 = $matchModel->odds_code;
  197. $fun2 = $matchModel->p_code;
  198. $winorfalsedef = ['result' => 2, 'matchResult' => 'noRuleOrError'];
  199. try {
  200. if ($RefClass->hasMethod($fun1)) {
  201. $winorfalse = $AdapterObj->$fun1($matchModel, $now_result, []);
  202. } elseif ($RefClass->hasMethod($fun2)) {
  203. $winorfalse = $AdapterObj->$fun2($matchModel, $now_result, []);
  204. }
  205. } catch (\Exception $e) {
  206. }
  207. if (!isset($winorfalse['result']) || !isset($winorfalse['matchResult']) || !in_array($winorfalse['result'], [-1, 1, 2, 3, 4])) {
  208. $winorfalse = $winorfalsedef;
  209. }
  210. $sql_1 = $this->makesql_up_buymatch_winorfalse($matchModel->id, $winorfalse['result'], date("Y-m-d H:i:s"), $winorfalse['matchResult']);
  211. $gs_val = ($winorfalse['result'] == 1) ? 1 : ($winorfalse['result'] == -1 ? 2 : 3);
  212. if ($matchModel->bet_type == 1) {
  213. $sql_2 = "update money_buy_simplex set game_status=$gs_val where batch_id='$matchModel->batch_id' and match_id='$matchModel->match_id' and game_code='$matchModel->game_code' ";
  214. } else {
  215. $sql_2 = "update money_buy_str set wait_match_num=wait_match_num-1 where batch_id='$matchModel->batch_id' ";
  216. }
  217. $this->PushSqlToArray([$sql_1, $sql_2]);
  218. } catch (\Exception $e) {
  219. echo $e->getMessage() . '--' . $e->getFile() . '--' . $e->getLine() . "\n";
  220. }
  221. return true;
  222. }
  223. //生成更新sql;
  224. private function makesql_up_buymatch_winorfalse($id, $result, $utime, $matchResult = '')
  225. {
  226. $sql = "update money_buy_match set result=$result, utime='$utime',matchResult='$matchResult' where id=$id ";
  227. return $sql;
  228. }
  229. private function PushSqlToArray($cmdArray)
  230. {
  231. if (is_string($cmdArray)) {
  232. $this->TaskSqlQueue->enqueue($cmdArray);
  233. } else {
  234. foreach ($cmdArray as $sql) {
  235. $this->TaskSqlQueue->enqueue($sql);
  236. }
  237. }
  238. }
  239. private function TaskSQlToRedis()
  240. {
  241. if ($this->TaskSqlQueue->isEmpty()) {
  242. return;
  243. }
  244. $this->sqltoRedisqueue();
  245. }
  246. private function sqltoRedisqueue()
  247. {
  248. $redisconfig = $this->redisonfig;
  249. $sqlQueue = $this->TaskSqlQueue;
  250. $redis = new \Redis();
  251. $ret = $redis->connect($redisconfig['host'], $redisconfig['port']);
  252. if (!$ret) {
  253. unset($redis, $sqlQueue, $redisconfig, $ret);
  254. return false;
  255. }
  256. $i = 1;
  257. $maxStep = 5000;
  258. $sql = '';
  259. if (!$sqlQueue->isEmpty()) {
  260. $redis->pipeline();
  261. while ($i < $maxStep) {
  262. if (!$sqlQueue->isEmpty()) {
  263. $sql = $sqlQueue->dequeue();
  264. if (empty($sql)) {
  265. break;
  266. }
  267. $redis->lpush(self::SQLKEY, $sql);
  268. }
  269. $i++;
  270. }
  271. $redis->exec();
  272. }
  273. $redis->close();
  274. unset($redis, $sqlQueue, $sql, $i, $maxStep, $redisconfig, $ret);
  275. return;
  276. }
  277. public function onFinish($serv, int $task_id, $data)
  278. {
  279. }
  280. private function InitDb()
  281. {
  282. $over_time = 60 * 5;
  283. $now = microtime(true);
  284. if (!$GLOBALS['modeltime']) {
  285. $GLOBALS['modeltime'] = $now;
  286. $GLOBALS['model'] = $this->httpserver->worker_id;
  287. ModelBase::init();
  288. return;
  289. }
  290. if (($now - $GLOBALS['modeltime']) > $over_time) {
  291. $GLOBALS['modeltime'] = $now;
  292. $GLOBALS['model'] = $this->httpserver->worker_id;
  293. ModelBase::close();
  294. ModelBase::init();
  295. return;
  296. }
  297. }
  298. public function start()
  299. {
  300. $this->httpserver->start();
  301. }
  302. }