SettlementBase.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 结果表数据
  79. * @return array 注单返现额
  80. */
  81. public function stringComputing($dataArray){
  82. /*$dataArray = [
  83. [
  84. ['odds'=>3.02,'winOrLose'=>1],
  85. ['odds'=>2.84,'winOrLose'=>1],
  86. ['odds'=>3.01,'winOrLose'=>1],
  87. ['odds'=>2.94,'winOrLose'=>1]
  88. ], 2
  89. ];*/
  90. $data = $this->combination($dataArray[0],$dataArray[1]);
  91. $dataCount = count($data);
  92. $returnMoney = 0;
  93. foreach ($data as $key => $value) {
  94. $getReturnMoney = $this->winOddsCalculation($value);
  95. $returnMoney += $getReturnMoney['returnMoney'];
  96. }
  97. $returnMoney = $returnMoney / $dataCount;
  98. return $returnMoney;
  99. }
  100. // 组合
  101. function combination($a, $m) {
  102. $r = array();
  103. $n = count($a);
  104. if ($m <= 0 || $m > $n) {
  105. return $r;
  106. }
  107. for ($i=0; $i<$n; $i++) {
  108. $t = array($a[$i]);
  109. if ($m == 1) {
  110. $r[] = $t;
  111. } else {
  112. $b = array_slice($a, $i+1);
  113. $c = $this->combination($b, $m-1);
  114. foreach ($c as $v) {
  115. $r[] = array_merge($t, $v);
  116. }
  117. }
  118. }
  119. return $r;
  120. }
  121. /**
  122. * UUID 生成
  123. */
  124. public static function UUID() {
  125. $prefix = '';
  126. $uuid = '';
  127. $str = md5(uniqid(mt_rand(), true));
  128. $uuid = substr($str, 0, 8) . '-';
  129. $uuid .= substr($str, 8, 4) . '-';
  130. $uuid .= substr($str, 12, 4) . '-';
  131. $uuid .= substr($str, 16, 4) . '-';
  132. $uuid .= substr($str, 20, 12);
  133. return $prefix . $uuid;
  134. }
  135. //获取到某个用户的account模型和detail模型,用于用户的余额计算等使用
  136. public function getUserInfo($account_name){
  137. $accountModel = DB::table('account')->where('account',$account_name)->first();
  138. if (!$accountModel){
  139. throw new \Exception('查无此用户数据account='.$account_name,41002);
  140. }
  141. $detailModel = DB::table('account_detailed')->where('account_identity',$accountModel->identity)->first();
  142. if (!$detailModel){
  143. throw new \Exception('查无此用户数据account_detail='.$accountModel->identity,41002);
  144. }
  145. $ret = [
  146. 'account'=>$detailModel,
  147. 'detail' => $detailModel ,
  148. ];
  149. return $ret;
  150. }
  151. }