265 lines
11 KiB
PHP
265 lines
11 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\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;
|
|
|
|
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;
|
|
|
|
/**
|
|
* 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('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('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('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 ($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'])) {
|
|
|
|
EventBookmaker::updateOrCreate([
|
|
'event_id' => $event->id,
|
|
'bookmaker_id' => Nuh::$options['bookmaker_id'],
|
|
], ['starttime' => $event->starttime, 'in_feed' => 1, 'bmoid' => $event->bet365_id]);
|
|
|
|
$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['flag'] = 1;
|
|
|
|
/**
|
|
* 1X2
|
|
*/
|
|
if (isset($offer['full_time_result'])) {
|
|
$odds = [];
|
|
$odds['h3w'] = number_format($offer['full_time_result']['odds'][0]['odds'], 2) ?? 1;
|
|
$odds['d3w'] = number_format($offer['full_time_result']['odds'][1]['odds'], 2) ?? 1;
|
|
$odds['a3w'] = number_format($offer['full_time_result']['odds'][2]['odds'], 2) ?? 1;
|
|
$odds['margin'] = (float) Margin::calculate($odds, 1);
|
|
|
|
$odds = array_merge($odds, $base);
|
|
|
|
$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'];
|
|
if ($res->isDirty()) {
|
|
$res->oddchange_at = now();
|
|
}
|
|
$res->checked_at = now();
|
|
$res->margin = $odds['margin'];
|
|
$res->save();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* DC
|
|
*/
|
|
if (isset($offer['double_chance'])) {
|
|
$odds = [];
|
|
$odds['dc1x'] = number_format($offer['double_chance']['odds'][0]['odds'], 2) ?? 1;
|
|
$odds['dc12'] = number_format($offer['double_chance']['odds'][1]['odds'], 2) ?? 1;
|
|
$odds['dcx2'] = number_format($offer['double_chance']['odds'][2]['odds'], 2) ?? 1;
|
|
$odds['margin'] = (float) Margin::calculate($odds, 1);
|
|
|
|
$odds = array_merge($odds, $base);
|
|
|
|
$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'];
|
|
if ($res->isDirty()) {
|
|
$res->oddchange_at = now();
|
|
}
|
|
$res->checked_at = now();
|
|
$res->margin = $odds['margin'];
|
|
$res->save();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* OU
|
|
*/
|
|
if (isset($offer['goals_over_under'])) {
|
|
|
|
$odds = [];
|
|
$odds['over'] = number_format($offer['goals_over_under']['odds'][0]['odds'], 2) ?? 1;
|
|
$odds['under'] = number_format($offer['goals_over_under']['odds'][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);
|
|
|
|
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'];
|
|
if ($res->isDirty()) {
|
|
$res->oddchange_at = now();
|
|
}
|
|
$res->difference = abs(1 / $odds['over'] - 1 / $odds['under']);
|
|
$res->checked_at = now();
|
|
$res->margin = $odds['margin'];
|
|
$res->save();
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* GG NG
|
|
*/
|
|
if (isset($offer['both_teams_to_score'])) {
|
|
$odds = [];
|
|
$odds['gg'] = number_format($offer['both_teams_to_score']['odds'][0]['odds'], 2) ?? 1;
|
|
$odds['ng'] = number_format($offer['both_teams_to_score']['odds'][1]['odds'], 2) ?? 1;
|
|
$odds['margin'] = (float) Margin::calculate($odds, 1);
|
|
|
|
$odds = array_merge($odds, $base);
|
|
|
|
$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'];
|
|
if ($res->isDirty()) {
|
|
$res->oddchange_at = now();
|
|
}
|
|
$res->checked_at = now();
|
|
$res->margin = $odds['margin'];
|
|
$res->save();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* DNB
|
|
*/
|
|
if (isset($offer['draw_no_bet'])) {
|
|
$odds = [];
|
|
$odds['dnb1'] = number_format($offer['draw_no_bet']['odds'][0]['odds'], 2) ?? 1;
|
|
$odds['dnb2'] = number_format($offer['draw_no_bet']['odds'][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);
|
|
|
|
if (! $res) {
|
|
OddsDnb::create($odds);
|
|
} else {
|
|
$res->dnb1 = $odds['dnb1'];
|
|
$res->dnb2 = $odds['dnb2'];
|
|
if ($res->isDirty()) {
|
|
$res->oddchange_at = now();
|
|
}
|
|
$res->checked_at = now();
|
|
$res->margin = $odds['margin'];
|
|
$res->save();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 1X2 HT
|
|
*/
|
|
if (isset($offer['half_time_result'])) {
|
|
$odds = [];
|
|
$odds['h3w'] = number_format($offer['half_time_result']['odds'][0]['odds'], 2) ?? 1;
|
|
$odds['d3w'] = number_format($offer['half_time_result']['odds'][1]['odds'], 2) ?? 1;
|
|
$odds['a3w'] = number_format($offer['half_time_result']['odds'][2]['odds'], 2) ?? 1;
|
|
$odds['margin'] = (float) Margin::calculate($odds, 1);
|
|
|
|
$odds = array_merge($odds, $base);
|
|
|
|
$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->d3w = $odds['a3w'];
|
|
if ($res->isDirty()) {
|
|
$res->oddchange_at = now();
|
|
}
|
|
$res->checked_at = now();
|
|
$res->margin = $odds['margin'];
|
|
$res->save();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|