HttpServerSettelement.php 12 KB

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