| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- /**
- * 解析让球方
- */
- const ratioAccept = (ratio) => {
- if (ratio > 0) {
- return 'a';
- }
- return '';
- }
- /**
- * 将比率转换为字符串
- * @param {*} ratio
- * @returns
- */
- const ratioString = (ratio) => {
- ratio = Math.abs(ratio);
- ratio = ratio.toString();
- ratio = ratio.replace(/\./, '');
- return ratio;
- }
- /**
- * 解析胜平负盘口
- * @param {*} moneyline
- * @returns
- */
- const parseMoneyline = (moneyline, maxMoneyline) => {
- // 胜平负
- if (!moneyline) {
- return null;
- }
- const { home, away, draw } = moneyline;
- const max = maxMoneyline;
- return {
- 'ior_mh': { v: home, m: max },
- 'ior_mc': { v: away, m: max },
- 'ior_mn': { v: draw, m: max },
- }
- }
- /**
- * 解析让分盘口
- * @param {*} spreads
- * @param {*} wm
- * @returns
- */
- const parseSpreads = (spreads, wm, maxSpread) => {
- // 让分盘
- if (!spreads?.length) {
- return null;
- }
- const events = {};
- spreads.forEach(spread => {
- const { hdp, home, away, max=maxSpread } = spread;
- // if (!(hdp % 1) || !!(hdp % 0.5)) {
- // // 整数或不能被0.5整除的让分盘不处理
- // return;
- // }
- const ratio_ro = hdp;
- const ratio_r = ratio_ro - wm;
- events[`ior_r${ratioAccept(ratio_r)}h_${ratioString(ratio_r)}`] = {
- v: home,
- r: wm != 0 ? `ior_r${ratioAccept(ratio_ro)}h_${ratioString(ratio_ro)}` : undefined,
- m: max,
- };
- events[`ior_r${ratioAccept(-ratio_r)}c_${ratioString(ratio_r)}`] = {
- v: away,
- r: wm != 0 ? `ior_r${ratioAccept(-ratio_ro)}c_${ratioString(ratio_ro)}` : undefined,
- m: max,
- };
- });
- return events;
- }
- /**
- * 解析大小球盘口
- * @param {*} totals
- * @returns
- */
- const parseTotals = (totals, maxTotal) => {
- // 大小球盘
- if (!totals?.length) {
- return null;
- }
- const events = {};
- totals.forEach(total => {
- const { points, over, under, max=maxTotal } = total;
- events[`ior_ouc_${ratioString(points)}`] = { v: over, m: max };
- events[`ior_ouh_${ratioString(points)}`] = { v: under, m: max };
- });
- return events;
- }
- /**
- * 解析直赛盘口
- * @param {*} straight
- * @param {*} wm
- * @returns
- */
- const parseStraight = (straight, wm) => {
- if (!straight) {
- return null;
- }
- const { cutoff='', status=0, spreads=[], moneyline={}, totals=[], maxSpread, maxMoneyline, maxTotal } = straight;
- const cutoffTime = new Date(cutoff).getTime();
- const nowTime = Date.now();
- if (status != 1 || cutoffTime < nowTime) {
- return null;
- }
- const events = {};
- Object.assign(events, parseSpreads(spreads, wm, maxSpread));
- Object.assign(events, parseMoneyline(moneyline, maxMoneyline));
- Object.assign(events, parseTotals(totals, maxTotal));
- return events;
- }
- /**
- * 解析净胜球盘口
- * @param {*} winningMargin
- * @param {*} home
- * @param {*} away
- * @returns
- */
- const parseWinningMargin = (winningMargin, home, away) => {
- if (!winningMargin) {
- return null;
- }
- const { cutoff='', status='', contestants=[] } = winningMargin;
- const cutoffTime = new Date(cutoff).getTime();
- const nowTime = Date.now();
- if (status != 'O' || cutoffTime < nowTime || !contestants?.length) {
- return null;
- }
- const events = {};
- contestants.forEach(contestant => {
- const { name, price, max } = contestant;
- const nr = name.match(/\d+$/)?.[0];
- if (!nr) {
- return;
- }
- let side;
- if (name.startsWith(home)) {
- side = 'h';
- }
- else if (name.startsWith(away)) {
- side = 'c';
- }
- else {
- return;
- }
- events[`ior_wm${side}_${nr}`] = { v: price, m: max };
- });
- return events;
- }
- /**
- * 解析进球数盘口
- * @param {*} exactTotalGoals
- * @returns
- */
- const parseExactTotalGoals = (exactTotalGoals) => {
- if (!exactTotalGoals) {
- return null;
- }
- const { cutoff='', status='', contestants=[] } = exactTotalGoals;
- const cutoffTime = new Date(cutoff).getTime();
- const nowTime = Date.now();
- if (status != 'O' || cutoffTime < nowTime || !contestants?.length) {
- return null;
- }
- const events = {};
- contestants.forEach(contestant => {
- const { name, price, max } = contestant;
- if (+name >= 1 && +name <= 7) {
- events[`ior_ot_${name}`] = { v: price, m: max };
- }
- });
- return events;
- }
- /**
- * 解析滚球场次状态
- * @param {*} state
- * @returns
- */
- const parseRbState = (state) => {
- let stage = null;
- if (state == 1) {
- stage = '1H';
- }
- else if (state == 2) {
- stage = 'HT';
- }
- else if (state == 3) {
- stage = '2H';
- }
- else if (state >= 4) {
- stage = 'ET';
- }
- return stage;
- }
- /**
- * 解析比赛数据
- * @param {*} game
- * @returns
- */
- const parseGame = (game) => {
- const { id=0, originId=0, periods={}, specials={}, home, away, marketType, state, elapsed, homeScore=0, awayScore=0 } = game;
- const { straight } = periods;
- const { winningMargin={}, exactTotalGoals={} } = specials;
- const wm = homeScore - awayScore;
- const score = `${homeScore}-${awayScore}`;
- const odds = parseStraight(straight, wm) ?? {};
- const stage = parseRbState(state);
- const retime = elapsed ? `${elapsed}'` : '';
- const evtime = Date.now();
- Object.assign(odds, parseWinningMargin(winningMargin, home, away));
- Object.assign(odds, parseExactTotalGoals(exactTotalGoals));
- return { id, originId, odds, evtime, stage, retime, score, wm, marketType }
- }
- export default parseGame;
|