389 lines
14 KiB
PHP
389 lines
14 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands\Grapper\Betsapi;
|
|
|
|
use App\Console\Commands\Curl;
|
|
use App\Console\Commands\Nuh;
|
|
use App\Models\Event;
|
|
use App\Models\EventBookmaker;
|
|
use App\Models\Odds2way;
|
|
use App\Models\Odds3way;
|
|
use App\Models\Odds3wayHt;
|
|
use App\Models\OddsDc;
|
|
use App\Models\OddsDnb;
|
|
use App\Models\OddsGgng;
|
|
use App\Models\OddsTotal;
|
|
use App\Utilities\Margin;
|
|
use App\Utilities\Notice;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class Bet365
|
|
{
|
|
public static function grap()
|
|
{
|
|
|
|
$debug = Nuh::$options['d'] ?? 0;
|
|
|
|
$today = Nuh::$options['t'] ?? 0;
|
|
|
|
$week = Nuh::$options['w'] ?? 0;
|
|
|
|
$single = Nuh::$options['id'] ?? 0;
|
|
|
|
switch (Nuh::$options['sport']) {
|
|
case 'Soccer':
|
|
$sport_id = 1;
|
|
break;
|
|
case 'Ice Hockey':
|
|
$sport_id = 2;
|
|
break;
|
|
case 'Basketball':
|
|
$sport_id = 3;
|
|
break;
|
|
case 'Tennis':
|
|
$sport_id = 5;
|
|
break;
|
|
default:
|
|
Notice::note('SPORT NOT IMPLEMENTED', 'r', 1);
|
|
break;
|
|
}
|
|
|
|
$events_collector = [];
|
|
|
|
$has_markets_collector = [];
|
|
|
|
/**
|
|
* Bet365
|
|
*/
|
|
$bookmaker_id = 126;
|
|
|
|
if ($single) {
|
|
$events = Event::where('id', Nuh::$options['id'])->where('bet365_id', '>', 0)->select('id', 'sport_id', 'bet365_id', 'starttime')->get();
|
|
} elseif ($today) {
|
|
$events = Event::where('sport_id', $sport_id)->where('bet365_id', '>', 0)->where('starttime', '>', Carbon::now()->toDateTimeString())->where('starttime', '<', Carbon::now()->addDay()->toDateTimeString())->select('id', 'sport_id', 'bet365_id', 'starttime')->get();
|
|
} elseif ($week) {
|
|
$events = Event::where('sport_id', $sport_id)->where('bet365_id', '>', 0)->where('starttime', '>', Carbon::now()->toDateTimeString())->where('starttime', '<', Carbon::now()->addDays(7)->toDateTimeString())->select('id', 'sport_id', 'bet365_id', 'starttime')->get();
|
|
} else {
|
|
// all
|
|
$events = Event::where('sport_id', $sport_id)->where('bet365_id', '>', 0)->where('starttime', '>', Carbon::now()->toDateTimeString())->select('id', 'sport_id', 'bet365_id', 'starttime')->get();
|
|
}
|
|
if ($events ?? null && $debug) {
|
|
Notice::note('No events', 'r');
|
|
}
|
|
foreach ($events as $key => $event) {
|
|
|
|
if (! $event->has_markets) {
|
|
$has_markets_collector[] = $event_id;
|
|
}
|
|
|
|
if ($debug) {
|
|
Notice::note([
|
|
'id' => $event->id,
|
|
'bet365_id' => $event->bet365_id,
|
|
'starttime' => $event->starttime,
|
|
'count' => count($events).'/'.$key + 1,
|
|
], 'b');
|
|
}
|
|
/**
|
|
* BET365 ODDS (per event)
|
|
*/
|
|
$url = 'https://api.b365api.com/v4/bet365/prematch?token='.config('env.betsapi_token').'&FI='.$event->bet365_id;
|
|
|
|
$data = Curl::load($url);
|
|
|
|
if (isset($data['results'][0]['main'])) {
|
|
|
|
DB::transaction(function () use ($event, $bookmaker_id) {
|
|
EventBookmaker::updateOrCreate([
|
|
'event_id' => $event->id,
|
|
'sport_id' => $event->sport_id,
|
|
'bookmaker_id' => $bookmaker_id,
|
|
], ['starttime' => $event->starttime, 'in_feed' => 1, 'bmoid' => $event->bet365_id]);
|
|
}, 5);
|
|
|
|
$offer = $data['results'][0]['main']['sp'];
|
|
|
|
$base = [];
|
|
$base['event_id'] = $event->id;
|
|
$base['bookmaker_id'] = $bookmaker_id;
|
|
$base['sport_id'] = $event->sport_id;
|
|
$base['provider_id'] = Nuh::$options['provider_id'];
|
|
$base['bmoid'] = $event->bet365_id;
|
|
$base['flags'] = 1;
|
|
|
|
/**
|
|
* ML
|
|
*/
|
|
if (isset($offer['to_win_match'])) {
|
|
|
|
$data = $offer['to_win_match']['odds'] ?? [];
|
|
|
|
if ($data) {
|
|
|
|
$odds = [];
|
|
$odds['h2w'] = number_format($data[0]['odds'], 2) ?? 1;
|
|
$odds['a2w'] = number_format($data[1]['odds'], 2) ?? 1;
|
|
$odds['margin'] = (float) Margin::calculate($odds, 1);
|
|
|
|
$odds = array_merge($odds, $base);
|
|
|
|
$events_collector['odds_2ways'][] = $event->id;
|
|
|
|
$res = Odds2way::where('event_id', $event->id)->where('bookmaker_id', $bookmaker_id)->first();
|
|
|
|
if (! $res) {
|
|
Odds2way::create($odds);
|
|
} else {
|
|
$res->h3w = $odds['h2w'];
|
|
$res->a3w = $odds['a2w'];
|
|
$res->flags = 1;
|
|
$res->margin = $odds['margin'];
|
|
DB::transaction(function () use ($res) {
|
|
$res->save();
|
|
}, 5);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 1X2
|
|
*/
|
|
if (isset($offer['full_time_result'])) {
|
|
|
|
$data = $offer['full_time_result']['odds'] ?? [];
|
|
|
|
if ($data) {
|
|
|
|
$odds = [];
|
|
$odds['h3w'] = number_format($data[0]['odds'], 2) ?? 1;
|
|
$odds['d3w'] = number_format($data[1]['odds'], 2) ?? 1;
|
|
$odds['a3w'] = number_format($data[2]['odds'], 2) ?? 1;
|
|
$odds['margin'] = (float) Margin::calculate($odds, 1);
|
|
|
|
$odds = array_merge($odds, $base);
|
|
|
|
$events_collector['odds_3ways'][] = $event->id;
|
|
|
|
$res = Odds3way::where('event_id', $event->id)->where('bookmaker_id', $bookmaker_id)->first();
|
|
|
|
if (! $res) {
|
|
Odds3way::create($odds);
|
|
} else {
|
|
$res->h3w = $odds['h3w'];
|
|
$res->a3w = $odds['d3w'];
|
|
$res->d3w = $odds['a3w'];
|
|
$res->flags = 1;
|
|
$res->margin = $odds['margin'];
|
|
DB::transaction(function () use ($res) {
|
|
$res->save();
|
|
}, 5);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* DC
|
|
*/
|
|
if (isset($offer['double_chance'])) {
|
|
|
|
$data = $offer['double_chance']['odds'] ?? [];
|
|
|
|
if ($data) {
|
|
|
|
$odds = [];
|
|
$odds['dc1x'] = number_format($data[0]['odds'], 2) ?? 1;
|
|
$odds['dc12'] = number_format($data[1]['odds'], 2) ?? 1;
|
|
$odds['dcx2'] = number_format($data[2]['odds'], 2) ?? 1;
|
|
$odds['margin'] = (float) Margin::calculate($odds, 1);
|
|
|
|
$odds = array_merge($odds, $base);
|
|
|
|
$events_collector['odds_dc'][] = $event->id;
|
|
|
|
$res = OddsDc::where('event_id', $event->id)->where('bookmaker_id', $bookmaker_id)->first();
|
|
|
|
if (! $res) {
|
|
OddsDc::create($odds);
|
|
} else {
|
|
$res->dc1x = $odds['dc1x'];
|
|
$res->dc12 = $odds['dc12'];
|
|
$res->dcx2 = $odds['dcx2'];
|
|
$res->flags = 1;
|
|
$res->margin = $odds['margin'];
|
|
DB::transaction(function () use ($res) {
|
|
$res->save();
|
|
}, 5);
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* OU
|
|
*/
|
|
if (isset($offer['goals_over_under'])) {
|
|
|
|
$data = $offer['goals_over_under']['odds'] ?? [];
|
|
|
|
if ($data) {
|
|
|
|
$odds = [];
|
|
$odds['over'] = number_format($data[0]['odds'], 2) ?? 1;
|
|
$odds['under'] = number_format($data[1]['odds'], 2) ?? 1;
|
|
$odds['margin'] = (float) Margin::calculate($odds, 1);
|
|
if ($base['sport_id'] == 1) {
|
|
$odds['active_spread'] = 1;
|
|
}
|
|
|
|
$odds = array_merge($odds, $base);
|
|
|
|
$events_collector['odds_totals'][] = $event->id;
|
|
|
|
if ($offer['goals_over_under']['odds'][1]['name'] == '2.5') {
|
|
$odds['total'] = 2.5;
|
|
$res = OddsTotal::where('event_id', $event->id)
|
|
->where('bookmaker_id', $bookmaker_id)
|
|
->where('total', $odds['total'])
|
|
->first();
|
|
|
|
if (! $res) {
|
|
OddsTotal::create($odds);
|
|
} else {
|
|
$res->over = $odds['over'];
|
|
$res->under = $odds['under'];
|
|
$res->difference = abs(1 / $odds['over'] - 1 / $odds['under']);
|
|
$res->flags = 1;
|
|
$res->margin = $odds['margin'];
|
|
DB::transaction(function () use ($res) {
|
|
$res->save();
|
|
}, 5);
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* GG NG
|
|
*/
|
|
if (isset($offer['both_teams_to_score'])) {
|
|
|
|
$data = $offer['both_teams_to_score']['odds'] ?? [];
|
|
|
|
if ($data) {
|
|
|
|
$odds = [];
|
|
$odds['gg'] = number_format($data[0]['odds'], 2) ?? 1;
|
|
$odds['ng'] = number_format($data[1]['odds'], 2) ?? 1;
|
|
$odds['margin'] = (float) Margin::calculate($odds, 1);
|
|
|
|
$odds = array_merge($odds, $base);
|
|
|
|
$events_collector['odds_ggng'][] = $event->id;
|
|
|
|
$res = OddsGgng::where('event_id', $event->id)->where('bookmaker_id', $bookmaker_id)->first();
|
|
|
|
if (! $res) {
|
|
OddsGgng::create($odds);
|
|
} else {
|
|
$res->gg = $odds['gg'];
|
|
$res->ng = $odds['ng'];
|
|
$res->flags = 1;
|
|
$res->margin = $odds['margin'];
|
|
DB::transaction(function () use ($res) {
|
|
$res->save();
|
|
}, 5);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* DNB
|
|
*/
|
|
if (isset($offer['draw_no_bet'])) {
|
|
|
|
$data = $offer['draw_no_bet']['odds'] ?? [];
|
|
|
|
if ($data) {
|
|
|
|
$odds = [];
|
|
$odds['dnb1'] = number_format($data[0]['odds'], 2) ?? 1;
|
|
$odds['dnb2'] = number_format($data[1]['odds'], 2) ?? 1;
|
|
$odds['margin'] = (float) Margin::calculate($odds, 1);
|
|
|
|
$res = OddsDnb::where('event_id', $event->id)->where('bookmaker_id', $bookmaker_id)->first();
|
|
|
|
$odds = array_merge($odds, $base);
|
|
|
|
$events_collector['odds_dnb'][] = $event->id;
|
|
|
|
if (! $res) {
|
|
OddsDnb::create($odds);
|
|
} else {
|
|
$res->dnb1 = $odds['dnb1'];
|
|
$res->dnb2 = $odds['dnb2'];
|
|
$res->flags = 1;
|
|
$res->margin = $odds['margin'];
|
|
DB::transaction(function () use ($res) {
|
|
$res->save();
|
|
}, 5);
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 1X2 HT
|
|
*/
|
|
if (isset($offer['half_time_result'])) {
|
|
|
|
$data = $offer['half_time_result']['odds'] ?? [];
|
|
|
|
if ($data) {
|
|
|
|
$odds = [];
|
|
$odds['h3w'] = number_format($data[0]['odds'], 2) ?? 1;
|
|
$odds['d3w'] = number_format($data[1]['odds'], 2) ?? 1;
|
|
$odds['a3w'] = number_format($data[2]['odds'], 2) ?? 1;
|
|
$odds['margin'] = (float) Margin::calculate($odds, 1);
|
|
|
|
$odds = array_merge($odds, $base);
|
|
|
|
$events_collector['odds_3ways_ht'][] = $event->id;
|
|
|
|
$res = Odds3wayHt::where('event_id', $event->id)->where('bookmaker_id', $bookmaker_id)->first();
|
|
|
|
if (! $res) {
|
|
Odds3wayHt::create($odds);
|
|
} else {
|
|
$res->h3w = $odds['h3w'];
|
|
$res->d3w = $odds['d3w'];
|
|
$res->a3w = $odds['a3w'];
|
|
$res->flags = 1;
|
|
$res->margin = $odds['margin'];
|
|
DB::transaction(function () use ($res) {
|
|
$res->save();
|
|
}, 5);
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$id_collector = [];
|
|
foreach ($events_collector as $table => $event_ids) {
|
|
$id_collector[] = $event_ids;
|
|
DB::table($table)->where('bookmaker_id', $bookmaker_id)->whereNotIn('event_id', $event_ids)->update(['flags' => 0]);
|
|
}
|
|
if ($id_collector) {
|
|
foreach ($id_collector as $event_id) {
|
|
if (in_array($event_id, $has_markets_collector)) {
|
|
Event::where('id', $event_id)->update(['has_markets' => 1]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|