SettlementBase.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/4/25
  6. * Time: 14:10
  7. */
  8. namespace App\Lib\Settlement;
  9. set_time_limit(600);
  10. ini_set('memory_limit', '256M');
  11. use Illuminate\Support\Facades\DB;
  12. class SettlementBase
  13. {
  14. const USER_WIN = 1 ; //用户赢钱
  15. const USER_LOSE = -1; //用户输钱
  16. const USER_FLAT = 2 ; //持平,用户钱原路返回
  17. const USER_HAFWIN = 3 ; //半平半赢
  18. const USER_HALFLOSE = 4 ; //半平半输
  19. private $RefClass = [] ; //适配器缓存
  20. //输赢判断
  21. public function winOrfalseInfo($gameType,$buyMatchModel,$resultModel,$resultRecordsArray){
  22. if (!isset($this->RefClass[$gameType])){
  23. $gameType = ucfirst($gameType);
  24. $pathBase = dirname(__FILE__).DIRECTORY_SEPARATOR.'Adapter'.DIRECTORY_SEPARATOR ;
  25. $file = $pathBase.$gameType.'Rule.php';
  26. if (!file_exists($file)){ throw new \Exception('无胜负判断规则文件');}
  27. include_once($file);
  28. $tmpapater = gameType.'Rule' ;
  29. $this->RefClass[$gameType] = new $tmpapater();
  30. }
  31. $fun = $buyMatchModel->odds_code ;
  32. $fun2 = $buyMatchModel->p_code ;
  33. if ( $this->RefClass[$gameType]->hasMethod($fun) ){
  34. $winorfalse = $this->RefClass[$gameType]->$fun($buyMatchModel,$resultModel,$resultRecordsArray);
  35. }elseif ( $this->RefClass[$gameType]->hasMethod($fun2) ){
  36. $winorfalse = $this->RefClass[$gameType]->$fun2($buyMatchModel,$resultModel,$resultRecordsArray);
  37. }else{
  38. throw new \Exception('没有找到玩法输赢判断规则',40010);
  39. }
  40. if (!in_array($winorfalse,[-1,1,2,3,4])){
  41. throw new \Exception('胜负判断结果异常',40011);
  42. }
  43. return $winorfalse ;
  44. }
  45. /**
  46. * 注单返现计算
  47. * @param mixed $dataArray 结果表数据 [['odds'=>xx,'winOrLose'=>1],[...]];
  48. * @return array 注单返现额
  49. */
  50. public function winOddsCalculation($dataArray){
  51. $returnMoney = 1;
  52. // 循环计算每注返现赔率
  53. foreach ($dataArray as $value) {
  54. $odds = floatval($value['odds']);
  55. // 因结果不同更改赔率
  56. switch (intval($value['winOrLose'])) {
  57. case 1:
  58. $oddsDiscount = $odds;
  59. break;
  60. case 2:
  61. $oddsDiscount = 1;
  62. break;
  63. case 3:
  64. $oddsDiscount = $odds * 0.5;
  65. break;
  66. default:
  67. $oddsDiscount = 0.5;
  68. }
  69. // 计算每注返现
  70. $returnMoney = $returnMoney * $oddsDiscount;
  71. }
  72. // 计算赚钱
  73. $earnMoney = $returnMoney - 1;
  74. return ['earnMoney' => $earnMoney, 'returnMoney' => $returnMoney];
  75. }
  76. /**
  77. * 注单返现计算
  78. * @param mixed $dataArray 结果表数据 [['odds'=>xx,'winOrLose'=>1],[...]];
  79. * @return array 注单返现额
  80. */
  81. public function stringOdds($dataArray){
  82. $returnMoney = 1;
  83. // 循环计算每注返现赔率
  84. foreach ($dataArray as $value) {
  85. $odds = floatval($value['odds']);
  86. // 因结果不同更改赔率
  87. switch (intval($value['winOrLose'])) {
  88. case 1:
  89. $oddsDiscount = 1 + $odds;
  90. break;
  91. case 2:
  92. $oddsDiscount = 1;
  93. break;
  94. case 3:
  95. $oddsDiscount = 1 + $odds * 0.5;
  96. break;
  97. default:
  98. $oddsDiscount = 0.5;
  99. }
  100. // 计算每注返现
  101. $returnMoney = $returnMoney * $oddsDiscount;
  102. }
  103. // 计算赚钱
  104. $earnMoney = $returnMoney - 1;
  105. return ['earnMoney' => $earnMoney, 'returnMoney' => $returnMoney];
  106. }
  107. /**
  108. * 注单返现计算
  109. * @param mixed $dataArray 结果表数据
  110. * @return array 注单返现额
  111. */
  112. public function stringComputing($dataArray){
  113. /*$dataArray = [
  114. [
  115. ['odds'=>3.02,'winOrLose'=>1],
  116. ['odds'=>2.84,'winOrLose'=>1],
  117. ['odds'=>3.01,'winOrLose'=>1],
  118. ['odds'=>2.94,'winOrLose'=>1]
  119. ], 2
  120. ];*/
  121. $data = $this -> combination($dataArray[0],$dataArray[1]);
  122. $dataCount = count($data);
  123. $returnMoney = 0;
  124. foreach ($data as $key => $value) {
  125. $getReturnMoney = $this -> stringOdds($value);
  126. $returnMoney += $getReturnMoney['returnMoney'];
  127. }
  128. $returnMoney = $returnMoney / $dataCount;
  129. return $returnMoney;
  130. }
  131. // 组合
  132. function combination($a, $m) {
  133. $r = array();
  134. $n = count($a);
  135. if ($m <= 0 || $m > $n) {
  136. return $r;
  137. }
  138. for ($i=0; $i<$n; $i++) {
  139. $t = array($a[$i]);
  140. if ($m == 1) {
  141. $r[] = $t;
  142. } else {
  143. $b = array_slice($a, $i+1);
  144. $c = $this->combination($b, $m-1);
  145. foreach ($c as $v) {
  146. $r[] = array_merge($t, $v);
  147. }
  148. }
  149. }
  150. return $r;
  151. }
  152. /**
  153. * UUID 生成
  154. */
  155. public static function UUID() {
  156. $prefix = '';
  157. $uuid = '';
  158. $str = md5(uniqid(mt_rand(), true));
  159. $uuid = substr($str, 0, 8) . '-';
  160. $uuid .= substr($str, 8, 4) . '-';
  161. $uuid .= substr($str, 12, 4) . '-';
  162. $uuid .= substr($str, 16, 4) . '-';
  163. $uuid .= substr($str, 20, 12);
  164. return $prefix . $uuid;
  165. }
  166. //获取到某个用户的account模型和detail模型,用于用户的余额计算等使用
  167. public function getUserInfo($account_name){
  168. $accountModel = DB::table('account')->where('account',$account_name)->first();
  169. if (!$accountModel){
  170. throw new \Exception('查无此用户数据account='.$account_name,41002);
  171. }
  172. $detailModel = DB::table('account_detailed')->where('account_identity',$accountModel->identity)->first();
  173. if (!$detailModel){
  174. throw new \Exception('查无此用户数据account_detail='.$accountModel->identity,41002);
  175. }
  176. $ret = [
  177. 'account'=>$detailModel,
  178. 'detail' => $detailModel ,
  179. ];
  180. return $ret;
  181. }
  182. }