60 lines
2.2 KiB
PHP
60 lines
2.2 KiB
PHP
<?php
|
|
namespace App\Core;
|
|
|
|
class DiceAlgorithm implements GameAlgorithmInterface {
|
|
|
|
public static function generateResult(): array {
|
|
return [rand(1, 6), rand(1, 6), rand(1, 6)];
|
|
}
|
|
|
|
public static function generateControlledResult(array $bets, array $waterConfig, int $attempts = 100): array {
|
|
if (empty($bets) || empty($waterConfig)) return self::generateResult();
|
|
$bestResult = null;
|
|
$bestProfit = PHP_INT_MIN;
|
|
for ($i = 0; $i < $attempts; $i++) {
|
|
$result = self::generateResult();
|
|
$profit = self::calculatePlatformProfit($result, $bets);
|
|
if ($profit > $bestProfit) { $bestProfit = $profit; $bestResult = $result; }
|
|
}
|
|
return $bestResult;
|
|
}
|
|
|
|
public static function checkWin(array $result, string $betType, string $betTarget): bool {
|
|
$total = array_sum($result);
|
|
switch ($betType) {
|
|
case 'big_small':
|
|
return ($betTarget === 'big' && $total >= 11) || ($betTarget === 'small' && $total <= 10);
|
|
case 'odd_even':
|
|
return ($betTarget === 'odd' && $total % 2 === 1) || ($betTarget === 'even' && $total % 2 === 0);
|
|
case 'sum':
|
|
return (int)$betTarget === $total;
|
|
default: return false;
|
|
}
|
|
}
|
|
|
|
public static function calculatePlatformProfit(array $result, array $bets): float {
|
|
$totalBet = 0; $totalPayout = 0;
|
|
foreach ($bets as $bet) {
|
|
$amount = (float)$bet['amount'];
|
|
$totalBet += $amount;
|
|
if (self::checkWin($result, $bet['bet_type'], $bet['bet_target'] ?? $bet['bet_value'] ?? '')) {
|
|
$totalPayout += $amount + $amount * (float)$bet['odds'];
|
|
}
|
|
}
|
|
return $totalBet - $totalPayout;
|
|
}
|
|
|
|
public static function getResultTable(): ?string { return null; }
|
|
|
|
public static function formatResultForStorage(array $result, int $periodId): array {
|
|
return [
|
|
'dice1' => $result[0], 'dice2' => $result[1], 'dice3' => $result[2],
|
|
'total' => array_sum($result),
|
|
];
|
|
}
|
|
|
|
public static function parseResultFromDb(array $row): array {
|
|
return [(int)($row['dice1'] ?? 0), (int)($row['dice2'] ?? 0), (int)($row['dice3'] ?? 0)];
|
|
}
|
|
}
|