84 lines
2.6 KiB
PHP
84 lines
2.6 KiB
PHP
<?php
|
||
|
||
namespace App\Services\Settlement\Matchers;
|
||
|
||
use App\Models\TicketCombination;
|
||
use App\Models\TicketItem;
|
||
use App\Services\Settlement\Contracts\SettlementPlayMatcher;
|
||
use App\Services\Settlement\OddsSnapshotReader;
|
||
use App\Services\Settlement\PublishedDrawResultBoard;
|
||
use Illuminate\Support\Collection;
|
||
|
||
/** pos_2abc:后二位命中头/二/三任意一档。 */
|
||
final class Pos2AbcSettlementMatcher implements SettlementPlayMatcher
|
||
{
|
||
public function __construct(
|
||
private readonly OddsSnapshotReader $odds,
|
||
) {}
|
||
|
||
public function match(TicketItem $item, PublishedDrawResultBoard $board, Collection $combinations): array
|
||
{
|
||
$tiers = ['first', 'second', 'third'];
|
||
$suffixByTier = [];
|
||
foreach ($tiers as $t) {
|
||
$s = $board->suffix2ForTier($t, 0);
|
||
if ($s !== '') {
|
||
$suffixByTier[$t] = $s;
|
||
}
|
||
}
|
||
if ($suffixByTier === []) {
|
||
return ['win_amount' => 0, 'matched_prize_tier' => null, 'match_detail' => ['reason' => 'no_suffix']];
|
||
}
|
||
|
||
$snapshot = is_array($item->odds_snapshot_json) ? $item->odds_snapshot_json : null;
|
||
$lines = [];
|
||
$total = 0;
|
||
$bestTier = null;
|
||
$bestRank = 99;
|
||
|
||
foreach ($combinations as $c) {
|
||
/** @var TicketCombination $c */
|
||
$n = (string) $c->number_4d;
|
||
if (strlen($n) < 2) {
|
||
continue;
|
||
}
|
||
$suf = substr($n, -2);
|
||
$hitTier = null;
|
||
$rank = 99;
|
||
foreach ($suffixByTier as $t => $sx) {
|
||
if ($suf !== $sx) {
|
||
continue;
|
||
}
|
||
$r = match ($t) {
|
||
'first' => 0,
|
||
'second' => 1,
|
||
'third' => 2,
|
||
default => 99,
|
||
};
|
||
if ($r < $rank) {
|
||
$rank = $r;
|
||
$hitTier = $t;
|
||
}
|
||
}
|
||
if ($hitTier === null) {
|
||
continue;
|
||
}
|
||
$oddsVal = $this->odds->oddsValueForScope($snapshot, $hitTier);
|
||
$bet = (int) $c->bet_amount;
|
||
$payout = (int) floor($bet * ($oddsVal / 10_000));
|
||
$total += $payout;
|
||
$lines[] = ['number_4d' => $n, 'tier' => $hitTier, 'payout' => $payout];
|
||
if ($rank < $bestRank) {
|
||
$bestRank = $rank;
|
||
$bestTier = $hitTier;
|
||
}
|
||
}
|
||
|
||
return [
|
||
'win_amount' => $total,
|
||
'matched_prize_tier' => $bestTier,
|
||
'match_detail' => ['lines' => $lines],
|
||
];
|
||
}
|
||
}
|