Files
pk10/App/Core/XocDiaAlgorithm.php
T

71 lines
2.7 KiB
PHP

<?php
namespace App\Core;
class XocDiaAlgorithm implements GameAlgorithmInterface {
public static function generateResult(): array {
$coins = [];
for ($i = 0; $i < 4; $i++) $coins[] = (rand(0, 1) === 0) ? 'red' : 'white';
return $coins;
}
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 {
$redCount = count(array_filter($result, fn($c) => $c === 'red'));
switch ($betTarget) {
case 'even': return ($redCount % 2 === 0);
case 'odd': return ($redCount % 2 === 1);
case '4red': return ($redCount === 4);
case '4white': return ($redCount === 0);
case '3red1white': return ($redCount === 3);
case '1red3white': return ($redCount === 1);
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 {
$redCount = count(array_filter($result, fn($c) => $c === 'red'));
return [
'dice1' => ($result[0] === 'red') ? 1 : 0,
'dice2' => ($result[1] === 'red') ? 1 : 0,
'dice3' => ($result[2] === 'red') ? 1 : 0,
'total' => $redCount,
];
}
public static function parseResultFromDb(array $row): array {
return [
((int)($row['dice1'] ?? 0)) ? 'red' : 'white',
((int)($row['dice2'] ?? 0)) ? 'red' : 'white',
((int)($row['dice3'] ?? 0)) ? 'red' : 'white',
// 第4个硬币从 result JSON 恢复,或根据 total 推断
'white', // fallback
];
}
}