trangleCalc.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. const crypto = require('crypto');
  2. const Logs = require('../libs/logs');
  3. const IOR_KEYS_MAP = require('./iorKeys');
  4. const SETTING = {
  5. innerDefaultAmount: 10000,
  6. minProfitAmount: 0,
  7. innerRebateRatio: 0,
  8. obRebateRatio: 0,
  9. hgRebateRatio: 0,
  10. }
  11. /**
  12. * 筛选最优赔率
  13. */
  14. function getOptimalSelections(odds, rules) {
  15. const results = [];
  16. const { obRebateRatio, hgRebateRatio } = SETTING;
  17. const obRebate = 1 + obRebateRatio / 100;
  18. const hgRebate = 1 + hgRebateRatio / 100;
  19. rules.forEach((rule, index) => {
  20. let validOptions = [];
  21. for (let i = 0; i < 3; i++) {
  22. const innerIndex = i;
  23. const selection = [];
  24. let isValid = true;
  25. for (let j = 0; j < 3; j++) {
  26. const key = rule[j];
  27. const item = odds[key];
  28. if (!item) {
  29. isValid = false;
  30. break;
  31. }
  32. if (j === innerIndex) {
  33. if (!('ps' in item)) {
  34. isValid = false;
  35. break;
  36. }
  37. selection.push({
  38. k: key,
  39. p: 'ps',
  40. v: item.ps,
  41. o: item,
  42. });
  43. }
  44. else {
  45. const candidates = ['ob', 'hg'].filter((k) => k in item);
  46. if (candidates.length === 0) {
  47. isValid = false;
  48. break;
  49. }
  50. const best = candidates.reduce((a, b) => item[a]*obRebate > item[b]*hgRebate ? a : b);
  51. selection.push({
  52. k: key,
  53. p: best,
  54. v: item[best],
  55. o: item,
  56. });
  57. }
  58. }
  59. if (isValid) {
  60. validOptions.push(selection);
  61. }
  62. }
  63. if (validOptions.length > 0) {
  64. const iors = validOptions.reduce((a, b) => {
  65. const sumA = a.reduce((sum, x) => sum + x.v, 0);
  66. const sumB = b.reduce((sum, x) => sum + x.v, 0);
  67. return sumA > sumB ? a : b;
  68. });
  69. results.push({ rule, iors, index });
  70. }
  71. });
  72. return results;
  73. }
  74. /**
  75. * 精确浮点数字
  76. * @param {number} number
  77. * @param {number} x
  78. * @returns {number}
  79. */
  80. const fixFloat = (number, x=2) => {
  81. return parseFloat(number.toFixed(x));
  82. }
  83. /**
  84. * 计算盈利
  85. */
  86. const triangleProfitCalc = (goldsInfo, oddsOption) => {
  87. const { innerRebateRatio } = SETTING;
  88. const {
  89. gold_side_a: x,
  90. gold_side_b: y,
  91. gold_side_c: z,
  92. odds_side_a: a,
  93. odds_side_b: b,
  94. odds_side_c: c
  95. } = goldsInfo;
  96. const { crossType, innerIndex, rebateA: A = 0, rebateB: B = 0, rebateC: C = 0 } = oddsOption;
  97. /**
  98. * crossType:
  99. * la: 全输
  100. * wa: 全赢
  101. * lh: 半输
  102. * wh: 半赢
  103. * dr: 和局
  104. * la_wh_wa, la_dr_wa, la_lh_wa, lh_dr_wa, lh_lh_wa, la_la_wa
  105. */
  106. let inner_rebate = 0;
  107. if (innerIndex == 0) {
  108. inner_rebate = x * innerRebateRatio;
  109. }
  110. else if (innerIndex == 1) {
  111. inner_rebate = y * innerRebateRatio;
  112. }
  113. else if (innerIndex == 2) {
  114. inner_rebate = z * innerRebateRatio;
  115. }
  116. let win_side_a = 0, win_side_b = 0, win_side_c = 0;
  117. win_side_a = a*x - y - z + a*A*x + B*y + C*z;
  118. win_side_b = b*y - x - z + b*B*y + A*x + C*z;
  119. switch (crossType) {
  120. case 'la_wh_wa': // 全输 半赢 全赢
  121. win_side_c = c*z - x + b*y/2 + c*C*z + A*x + b*B*y/2;
  122. break;
  123. case 'la_dr_wa': // 全输 和局 全赢
  124. win_side_c = c*z - x + c*C*z + A*x;
  125. break;
  126. case 'la_lh_wa': // 全输 半输 全赢
  127. win_side_c = c*z - x - y/2 + c*C*z + A*x + B*y/2;
  128. break;
  129. case 'lh_dr_wa': // 半输 和局 全赢
  130. win_side_c = c*z - x/2 + c*C*z + A*x/2;
  131. break;
  132. case 'lh_lh_wa': // 半输 半输 全赢
  133. win_side_c = c*z - x/2 - y/2 + c*C*z + A*x/2 + B*y/2;
  134. break;
  135. case 'la_la_wa': // 全输 全输 全赢
  136. win_side_c = c*z - x - y + c*C*z + A*x + B*y;
  137. break;
  138. }
  139. win_side_a = fixFloat(win_side_a + inner_rebate);
  140. win_side_b = fixFloat(win_side_b + inner_rebate);
  141. win_side_c = fixFloat(win_side_c + inner_rebate);
  142. const win_average = fixFloat((win_side_a + win_side_b + win_side_c) / 3);
  143. return { win_side_a, win_side_b, win_side_c, win_average }
  144. }
  145. const triangleGoldCalc = (oddsInfo, oddsOption) => {
  146. const { innerDefaultAmount } = SETTING;
  147. const { odds_side_a: a, odds_side_b: b, odds_side_c: c } = oddsInfo;
  148. if (!a || !b || !c) {
  149. return;
  150. }
  151. const { crossType, innerIndex, rebateA: A = 0, rebateB: B = 0, rebateC: C = 0 } = oddsOption;
  152. let x = innerDefaultAmount;
  153. let y = (a + a*A + 1 - A) * x / (b + b*B + 1 - B);
  154. let z;
  155. switch (crossType) {
  156. case 'la_wh_wa': // 全输 半赢 全赢
  157. z = (b + b*B) * y / 2 / (c + c*C + 1 - C);
  158. break;
  159. case 'la_dr_wa': // 全输 和局 全赢
  160. z = (b + b*B) * y / (c + c*C + 1 - C);
  161. break;
  162. case 'la_lh_wa': // 全输 半输 全赢
  163. z = (2*b + 2*b*B + 1 - B) * y / 2 / (c + c*C + 1 - C);
  164. break;
  165. case 'lh_dr_wa': // 半输 和局 全赢
  166. z = ((b + b*B) * y - (1 - A) * x / 2) / (c + c*C + 1 - C);
  167. break;
  168. case 'lh_lh_wa': // 半输 半输 全赢
  169. z = ((2*b + 2*b*B + 1 - B) * y / 2 + (1 - A) * x / 2) / (c + c*C + 1 - C);
  170. break;
  171. case 'la_la_wa': // 全输 全输 全赢
  172. z = (b + b*B + 1 - B) * y / (c + c*C + 1 - C);
  173. break;
  174. default:
  175. z = 0;
  176. }
  177. if (innerIndex == 1) {
  178. const scale = innerDefaultAmount / y;
  179. x = x * scale;
  180. y = innerDefaultAmount;
  181. z = z * scale;
  182. }
  183. else if (innerIndex == 2) {
  184. const scale = innerDefaultAmount / z;
  185. x = x * scale;
  186. y = y * scale;
  187. z = innerDefaultAmount;
  188. }
  189. return {
  190. gold_side_a: x,
  191. gold_side_b: fixFloat(y),
  192. gold_side_c: fixFloat(z),
  193. odds_side_a: a,
  194. odds_side_b: b,
  195. odds_side_c: c,
  196. };
  197. }
  198. const eventSolutions = (oddsInfo, oddsOption) => {
  199. const { innerDefaultAmount } = SETTING;
  200. const goldsInfo = triangleGoldCalc(oddsInfo, oddsOption);
  201. if (!goldsInfo) {
  202. return;
  203. }
  204. const profitInfo = triangleProfitCalc(goldsInfo, oddsOption);
  205. const { odds_side_a, odds_side_b, odds_side_c } = goldsInfo;
  206. const { win_average } = profitInfo;
  207. return {
  208. odds_side_a,
  209. odds_side_b,
  210. odds_side_c,
  211. win_average,
  212. rebate_side_a: oddsOption.rebateA,
  213. rebate_side_b: oddsOption.rebateB,
  214. rebate_side_c: oddsOption.rebateC,
  215. cross_type: oddsOption.crossType,
  216. inner_index: oddsOption.innerIndex,
  217. inner_base: innerDefaultAmount,
  218. }
  219. }
  220. /**
  221. * 盘口排序
  222. */
  223. const priority = { ps: 1, ob: 2, hg: 3 };
  224. const sortCpr = (cpr) => {
  225. const temp = [...cpr];
  226. temp.sort((a, b) => priority[a.p] - priority[b.p]);
  227. return temp;
  228. }
  229. /**
  230. * 添加返佣
  231. */
  232. const attachRebate = (ior) => {
  233. const { innerRebateRatio, obRebateRatio, hgRebateRatio } = SETTING;
  234. const { p } = ior;
  235. let rebate = 0;
  236. if (p == 'ps') {
  237. rebate = innerRebateRatio;
  238. }
  239. else if (p == 'ob') {
  240. rebate = obRebateRatio;
  241. }
  242. else if (p == 'hg') {
  243. rebate = hgRebateRatio;
  244. }
  245. return { ...ior, r: rebate };
  246. }
  247. const eventsCombination = (passableEvents, setting) => {
  248. Object.keys(setting).forEach(key => {
  249. if (key in SETTING && SETTING[key] !== setting[key]) {
  250. SETTING[key] = setting[key];
  251. Logs.out(`setting ${key} changed to ${setting[key]}`);
  252. }
  253. });
  254. const solutions = [];
  255. passableEvents.forEach(events => {
  256. const { odds, info } = events;
  257. Object.keys(IOR_KEYS_MAP).forEach(iorGroup => {
  258. const rules = IOR_KEYS_MAP[iorGroup];
  259. const optimalSelections = getOptimalSelections(odds, rules);
  260. optimalSelections.forEach(selection => {
  261. const { rule, iors, index } = selection;
  262. const [, , , crossType] = rule;
  263. const oddsSideA = attachRebate(iors[0]);
  264. const oddsSideB = attachRebate(iors[1]);
  265. const oddsSideC = attachRebate(iors[2]);
  266. const innerIndex = iors.findIndex(item => item.p == 'ps');
  267. if (!oddsSideA || !oddsSideB || !oddsSideC) {
  268. return;
  269. }
  270. const cpr = [ oddsSideA, oddsSideB, oddsSideC ];
  271. const oddsInfo = {
  272. odds_side_a: fixFloat(oddsSideA.v - 1),
  273. odds_side_b: fixFloat(oddsSideB.v - 1),
  274. odds_side_c: fixFloat(oddsSideC.v - 1),
  275. };
  276. const oddsOption = {
  277. crossType, innerIndex,
  278. rebateA: parseFloat((oddsSideA.r / 100).toFixed(4)),
  279. rebateB: parseFloat((oddsSideB.r / 100).toFixed(4)),
  280. rebateC: parseFloat((oddsSideC.r / 100).toFixed(4)),
  281. };
  282. const sol = eventSolutions(oddsInfo, oddsOption);
  283. if (sol?.win_average > SETTING.minProfitAmount) {
  284. const id = info.id;
  285. const sortedCpr = sortCpr(cpr);
  286. const keys = sortedCpr.map(item => `${item.k}`).join('_');
  287. const sid = crypto.createHash('sha1').update(`${id}_${keys}`).digest('hex');
  288. const crpGroup = `${id}_${sortedCpr[0].k}`;
  289. const timestamp = Date.now();
  290. solutions.push({sid, sol, cpr, info, group: crpGroup, rule: `${iorGroup}:${index}`, timestamp});
  291. }
  292. });
  293. });
  294. });
  295. return solutions.sort((a, b) => {
  296. return b.sol.win_average - a.sol.win_average;
  297. });
  298. // return Object.values(solutions).map(item => {
  299. // return item.sort((a, b) => {
  300. // return b.sol.win_average - a.sol.win_average;
  301. // })
  302. // .slice(0, 2);
  303. // })
  304. // .sort((a, b) => {
  305. // return b[0].sol.win_average - a[0].sol.win_average;
  306. // });
  307. }
  308. module.exports = { eventsCombination };