feat(lottery): 扩展传统马来赔率、玩法结算与开注商短码支持
Some checks failed
lotterLaravel CI / test (push) Has been cancelled
lotterLaravel E2E / e2e-api (push) Has been cancelled

This commit is contained in:
2026-07-10 17:04:28 +08:00
parent 2ea3e810a0
commit cbb18b976f
24 changed files with 320 additions and 45 deletions

View File

@@ -76,7 +76,7 @@ final class OddsStreamService
} else {
$currency = Currency::query()->where('is_bettable', true)->where('is_enabled', true)->orderBy('code')->firstOrFail();
foreach (PlayType::query()->orderBy('sort_order')->orderBy('play_code')->get() as $pt) {
foreach (OddsStandardScopes::PRESET_ODDS_BY_SCOPE as $scope => $oddsValue) {
foreach (OddsStandardScopes::presetOddsForPlay((string) $pt->play_code) as $scope => $oddsValue) {
OddsItem::query()->create([
'version_id' => $draft->id,
'provider_code' => 'GLOBAL',

View File

@@ -117,7 +117,7 @@ final class CurrencyActivationService
}
foreach (PlayType::query()->orderBy('sort_order')->orderBy('play_code')->get() as $playType) {
foreach (OddsStandardScopes::PRESET_ODDS_BY_SCOPE as $scope => $oddsValue) {
foreach (OddsStandardScopes::presetOddsForPlay((string) $playType->play_code) as $scope => $oddsValue) {
OddsItem::query()->firstOrCreate(
[
'version_id' => $version->id,

View File

@@ -16,6 +16,8 @@ final class Pos2TierSettlementMatcher implements SettlementPlayMatcher
'pos_2a' => 'first',
'pos_2b' => 'second',
'pos_2c' => 'third',
'pos_2d' => 'starter',
'pos_2e' => 'consolation',
];
public function __construct(

View File

@@ -16,6 +16,8 @@ final class Pos3TierSettlementMatcher implements SettlementPlayMatcher
'pos_3a' => 'first',
'pos_3b' => 'second',
'pos_3c' => 'third',
'pos_3d' => 'starter',
'pos_3e' => 'consolation',
];
public function __construct(

View File

@@ -0,0 +1,45 @@
<?php
namespace App\Services\Settlement\Matchers;
use App\Models\TicketItem;
use App\Models\TicketCombination;
use Illuminate\Support\Collection;
use App\Services\Settlement\OddsSnapshotReader;
use App\Services\Settlement\PublishedDrawResultBoard;
use App\Services\Settlement\Contracts\SettlementPlayMatcher;
/** Settles aggregate 4D / suffix plays against an explicit group of prize scopes. */
final class ScopeFilteredSettlementMatcher implements SettlementPlayMatcher
{
/** @var array<string, list<string>> */
private const SCOPES = [
'four_lower' => ['starter', 'consolation'],
'pos_3lower' => ['starter', 'consolation'],
'pos_2any' => ['first', 'second', 'third', 'starter', 'consolation'],
];
public function __construct(private readonly OddsSnapshotReader $odds) {}
public function match(TicketItem $item, PublishedDrawResultBoard $board, Collection $combinations): array
{
$allowed = self::SCOPES[(string) $item->play_code] ?? [];
$snapshot = is_array($item->odds_snapshot_json) ? $item->odds_snapshot_json : null;
$lines = [];
$total = 0;
$bestTier = null;
foreach ($combinations as $combo) {
/** @var TicketCombination $combo */
$hit = $board->bestTierForNumber((string) $combo->number_4d);
if ($hit === null || ! in_array($hit['tier'], $allowed, true)) continue;
$oddsValue = $this->odds->oddsValueForScope($snapshot, $hit['tier']);
$payout = (int) floor((int) $combo->bet_amount * ($oddsValue / 10_000));
$total += $payout;
$bestTier ??= $hit['tier'];
$lines[] = ['number_4d' => $combo->number_4d, 'tier' => $hit['tier'], 'payout' => $payout];
}
return ['win_amount' => $total, 'matched_prize_tier' => $bestTier, 'match_detail' => ['lines' => $lines]];
}
}

View File

@@ -14,6 +14,7 @@ use App\Services\Settlement\Matchers\Pos4ListTierSettlementMatcher;
use App\Services\Settlement\Matchers\StraightLikeSettlementMatcher;
use App\Services\Settlement\Matchers\Pos4ExactTierSettlementMatcher;
use App\Services\Settlement\Matchers\FirstPrizeComboSettlementMatcher;
use App\Services\Settlement\Matchers\ScopeFilteredSettlementMatcher;
final class SettlementMatcherRegistry
{
@@ -28,6 +29,7 @@ final class SettlementMatcherRegistry
private readonly Pos2TierSettlementMatcher $pos2Tier,
private readonly Pos2AbcSettlementMatcher $pos2Abc,
private readonly FirstPrizeComboSettlementMatcher $firstPrizeCombo,
private readonly ScopeFilteredSettlementMatcher $scopeFiltered,
private readonly NoopSettlementMatcher $noop,
) {}
@@ -36,14 +38,15 @@ final class SettlementMatcherRegistry
// half_boxPRD 一期预留;结算按已落库组合逐条取 23 档最优档,与 big/box 家族一致§5.6.6)。
return match ($playCode) {
'straight', 'roll' => $this->straight,
'big', 'ibox', 'mbox', 'box', 'half_box' => $this->big,
'small' => $this->small,
'big', 'ibox', 'mbox', 'box', 'half_box', 'four_any' => $this->big,
'small', 'four_top' => $this->small,
'pos_4a', 'pos_4b', 'pos_4c' => $this->pos4Exact,
'pos_4d', 'pos_4e' => $this->pos4List,
'pos_3a', 'pos_3b', 'pos_3c' => $this->pos3Tier,
'pos_3a', 'pos_3b', 'pos_3c', 'pos_3d', 'pos_3e' => $this->pos3Tier,
'pos_3abc' => $this->pos3Abc,
'pos_2a', 'pos_2b', 'pos_2c' => $this->pos2Tier,
'pos_2a', 'pos_2b', 'pos_2c', 'pos_2d', 'pos_2e' => $this->pos2Tier,
'pos_2abc' => $this->pos2Abc,
'four_lower', 'pos_3lower', 'pos_2any' => $this->scopeFiltered,
'head', 'tail', 'odd', 'even', 'digit_big', 'digit_small' => $this->firstPrizeCombo,
default => $this->noop,
};

View File

@@ -38,9 +38,11 @@ final class NumberNormalizer
$length = strlen($value);
$expected = match ($playCode) {
'big', 'small', 'pos_4a', 'pos_4b', 'pos_4c', 'pos_4d', 'pos_4e', 'straight', 'box', 'ibox', 'mbox' => 4,
'pos_3a', 'pos_3b', 'pos_3c', 'pos_3abc' => 3,
'pos_2a', 'pos_2b', 'pos_2c', 'pos_2abc' => 2,
'big', 'small', 'pos_4a', 'pos_4b', 'pos_4c', 'pos_4d', 'pos_4e', 'four_any', 'four_top', 'four_lower', 'straight', 'box', 'ibox', 'mbox' => 4,
'pos_3a', 'pos_3b', 'pos_3c', 'pos_3d', 'pos_3e', 'pos_3lower', 'pos_3abc' => 3,
'pos_2a', 'pos_2b', 'pos_2c', 'pos_2d', 'pos_2e', 'pos_2any', 'pos_2abc' => 2,
'five_d' => 5,
'six_d' => 6,
'head', 'tail', 'odd', 'even', 'digit_big', 'digit_small' => match ($dimension) {
'D2' => 1,
'D3' => 1,

View File

@@ -24,9 +24,19 @@ final class PlayRuleEngine
$playCode = (string) $line['play_code'];
$dimension = $line['dimension'] ?? null;
$digitSlot = $line['digit_slot'] ?? null;
$selectionType = (string) ($line['selection_type'] ?? 'straight');
$amount = (int) $line['amount'];
$number = $this->normalizer->normalize($playCode, (string) $line['number'], is_string($dimension) ? $dimension : null);
$allowedSelectionTypes = match ((int) ($playConfig->dimension ?? 4)) {
4 => ['straight', 'reverse', 'full_cover', 'full_play', 'half_play'],
3 => ['straight', 'reverse', 'full_cover', 'full_play'],
default => ['straight'],
};
if (! in_array($selectionType, $allowedSelectionTypes, true)) {
throw new TicketOperationException('selection_type_not_allowed', ErrorCode::BetInvalidPlayInput->value);
}
if ($amount < (int) $playConfig->min_bet_amount || $amount > (int) $playConfig->max_bet_amount) {
throw new TicketOperationException('bet_amount_out_of_range', ErrorCode::WalletAmountExceedsLimit->value);
}
@@ -40,14 +50,17 @@ final class PlayRuleEngine
}
$digitSlotInt = $digitSlot === null ? null : (int) $digitSlot;
$combos = $this->expandToCombinations($playCode, $number, is_string($dimension) ? $dimension : null, $digitSlotInt);
$combos = $this->expandToCombinations($playCode, $number, is_string($dimension) ? $dimension : null, $digitSlotInt, $selectionType);
$combinationCount = count($combos);
if ($combinationCount < 1) {
throw new TicketOperationException('empty_combinations', ErrorCode::BetInvalidPlayInput->value);
}
if ($selectionType === 'full_cover' && $amount % $combinationCount !== 0) {
throw new TicketOperationException('full_cover_amount_not_divisible', ErrorCode::BetInvalidPlayInput->value);
}
$unitBetAmount = $this->resolveUnitBetAmount($playCode, $amount, $combinationCount);
$totalBetAmount = $this->resolveTotalBetAmount($playCode, $amount, $unitBetAmount, $combinationCount);
$unitBetAmount = $this->resolveUnitBetAmount($selectionType === 'full_cover' ? 'mbox' : $playCode, $amount, $combinationCount);
$totalBetAmount = $this->resolveTotalBetAmount($selectionType === 'full_cover' ? 'mbox' : $playCode, $amount, $unitBetAmount, $combinationCount);
$dimensionInt = $this->toDimensionInt(is_string($dimension) ? $dimension : null, $playConfig);
$primaryOdds = $this->pickPrimaryOdds($oddsItems);
$rebateRate = (float) $primaryOdds->rebate_rate;
@@ -64,7 +77,7 @@ final class PlayRuleEngine
'play_code' => $playCode,
'dimension' => $this->toDimensionInt(is_string($dimension) ? $dimension : null, $playConfig),
'digit_slot' => $digitSlotInt,
'bet_mode' => $playConfig->bet_mode,
'bet_mode' => $selectionType,
'unit_bet_amount' => $unitBetAmount,
'total_bet_amount' => $totalBetAmount,
'rebate_rate_snapshot' => number_format($rebateRate, 4, '.', ''),
@@ -83,6 +96,7 @@ final class PlayRuleEngine
'play_code' => $playCode,
'dimension' => $dimension,
'digit_slot' => $digitSlotInt,
'selection_type' => $selectionType,
'combination_count' => $combinationCount,
'rounding_refund_amount' => $playCode === 'mbox'
? max(0, $amount - $totalBetAmount)
@@ -102,14 +116,24 @@ final class PlayRuleEngine
/**
* @return list<string>
*/
private function expandToCombinations(string $playCode, string $number, ?string $dimension, ?int $digitSlot): array
private function expandToCombinations(string $playCode, string $number, ?string $dimension, ?int $digitSlot, string $selectionType = 'straight'): array
{
if (in_array($playCode, ['big', 'small', 'pos_4a', 'pos_4b', 'pos_4c', 'pos_4d', 'pos_4e', 'four_any', 'four_top', 'four_lower'], true)) {
return $this->expandSelection(str_split($number), $selectionType);
}
if (str_starts_with($playCode, 'pos_3')) {
return collect($this->expandSelection(str_split($number), $selectionType))
->flatMap(fn (string $value) => $this->expandSuffix($value, 3))
->unique()->sort()->values()->all();
}
if (str_starts_with($playCode, 'pos_2')) {
return collect($this->expandSuffix($number, 2))->unique()->sort()->values()->all();
}
return match ($playCode) {
'big', 'small', 'pos_4a', 'pos_4b', 'pos_4c', 'pos_4d', 'pos_4e', 'straight' => [$number],
'straight' => [$number],
'ibox', 'mbox', 'box' => $this->uniquePermutations($number),
'roll' => $this->expandRoll($number),
'pos_3a', 'pos_3b', 'pos_3c', 'pos_3abc' => $this->expandSuffix($number, 3),
'pos_2a', 'pos_2b', 'pos_2c', 'pos_2abc' => $this->expandSuffix($number, 2),
'head' => $this->expandHeadTail(true),
'tail' => $this->expandHeadTail(false),
'odd' => $this->expandOddEven($dimension, true),
@@ -120,6 +144,29 @@ final class PlayRuleEngine
};
}
/** @param list<string> $digits @return list<string> */
private function expandSelection(array $digits, string $selectionType): array
{
$original = implode('', $digits);
if ($selectionType === 'straight') return [$original];
if ($selectionType === 'reverse') return array_values(array_unique([$original, strrev($original)]));
$all = $this->uniquePermutations($original);
if ($selectionType === 'full_cover' || $selectionType === 'full_play') return $all;
if ($selectionType === 'half_play') {
if (count($digits) < 2) return $all;
$first = $digits[0];
$second = $digits[1];
if ($first === $second) return $all;
return array_values(array_filter($all, function (string $value) use ($first, $second): bool {
$firstIndex = array_search($first, str_split($value), true);
$secondIndex = array_search($second, str_split($value), true);
return $firstIndex !== false && $secondIndex !== false && $firstIndex < $secondIndex;
}));
}
throw new TicketOperationException('selection_type_invalid', ErrorCode::BetInvalidPlayInput->value);
}
/**
* @return list<string>
*/

View File

@@ -120,9 +120,9 @@ final class TicketPlacementService
}
$configVersions = $this->catalogResolver->lockActiveConfigVersionsForPlacement($expectedVersions);
$providers = $this->betProviderResolver->resolve(
is_array($payload['provider_codes'] ?? null) ? $payload['provider_codes'] : null,
);
$fallbackProviderCodes = is_array($payload['provider_codes'] ?? null)
? $payload['provider_codes']
: null;
$evaluatedLines = [];
$totalBet = 0;
@@ -132,6 +132,9 @@ final class TicketPlacementService
$closedPlayCleanupRows = [];
foreach ((array) $payload['lines'] as $index => $line) {
$providers = $this->betProviderResolver->resolve(
is_array($line['provider_codes'] ?? null) ? $line['provider_codes'] : $fallbackProviderCodes,
);
foreach ($providers as $provider) {
try {
$resolved = $this->catalogResolver->resolve((string) $line['play_code'], $currencyCode, (string) $provider['code']);

View File

@@ -39,9 +39,9 @@ final class TicketPreviewService
$currencyCode = strtoupper((string) $payload['currency_code']);
$this->assertCreditCurrencyMatchesPlayer($player, $currencyCode);
$providers = $this->betProviderResolver->resolve(
is_array($payload['provider_codes'] ?? null) ? $payload['provider_codes'] : null,
);
$fallbackProviderCodes = is_array($payload['provider_codes'] ?? null)
? $payload['provider_codes']
: null;
$lines = [];
$totalBet = 0;
@@ -52,6 +52,9 @@ final class TicketPreviewService
$closedPlayCleanupRows = [];
foreach ((array) $payload['lines'] as $index => $line) {
$providers = $this->betProviderResolver->resolve(
is_array($line['provider_codes'] ?? null) ? $line['provider_codes'] : $fallbackProviderCodes,
);
foreach ($providers as $provider) {
try {
$resolved = $this->catalogResolver->resolve((string) $line['play_code'], $currencyCode, (string) $provider['code']);