<?php
//Case Battle Fairness | Round Validation
$serverSeed = '0fbcbf286e15c3916e56c75c37c48370e66e390ff315d7922ac2f396c0b3b47a'; // Server seed
$eosBlockSeed = '16203c4944240111289076b00dfa748f7056c67776262835e7ccc265535498f9'; // EOS Block Seed
/*
* This is the round number, where 1 round is one case opened. If the battle has 10 cases,
* this means there are 10 rounds - 1-10.
* IMPORTANT: If it's the tiebreaker round, it will always be totalRounds + 1
* because the tiebreaker is considered as one additional round. If you have 2 cases
* which results in 2 rounds, the tiebreaker roundNumber is 3 because it's total (2) + 1.
*/
$roundNumber = 2;
/*
* Player position that we want to validate the roll for.
* For example, if you want to validate your own roll and you were holding
* the 3rd position from left to right, then playerPosition will be 3.
*/
$playerPosition = 3; // The Player Position (1 to 4)
// Update this to true if you are validating a tiebreaker result.
$isTiebreaker = false;
/*
* Case Ticket's Quantity, by default it's 100,000
* We only change this if there's a case that has different ticket distribution.
* Right now, all cases use 100,000 by default.
* IMPORTANT: If it's the tiebreaker round, this will be number of players participating on the tiebreaker.
* For example, if two players are participating on a tiebreaker the ticketQuantity is 2.
*/
$ticketQuantity = 100000;
/* ------------------ */
if ($serverSeed == '' || $eosBlockSeed == '' || $roundNumber < 0 || $playerPosition < 1 || $playerPosition > 4 || $ticketQuantity <= 0) {
echo "Fill in details";
return;
}
define('MAX_HEX_SEGMENTS', 6);
define('HEX_SEGMENT_SIZE', 2);
define('BASE_FOR_HEX_CONVERSION', 256);
define('HASH_TYPE', 'sha256');
function calculateDecimalValue(string $preResult): float
{
$decimalValue = 0;
for ($i = 0; $i < MAX_HEX_SEGMENTS; $i++) {
$hexValue = substr($preResult, HEX_SEGMENT_SIZE * $i, HEX_SEGMENT_SIZE);
$decimalValue += hexdec($hexValue) / pow(BASE_FOR_HEX_CONVERSION, $i + 1);
}
return $decimalValue;
}
function getProvablyFairResult(string $init, string $serverSeed, int $qty): array
{
$preResult = hash_hmac(HASH_TYPE, $init, $serverSeed);
$decimalValue = calculateDecimalValue($preResult);
$result = (int) ($decimalValue * $qty) + 1;
return [
'preResult' => $preResult,
'result' => $result,
];
}
$serverSeed = preg_replace("/\r|\n/", "", $serverSeed);
$eosBlockSeed = preg_replace("/\r|\n/", "", $eosBlockSeed);
if ($isTiebreaker) {
$stringToHash = "$eosBlockSeed-$roundNumber";
} else {
$stringToHash = "$eosBlockSeed-$roundNumber-$playerPosition";
}
$result = getProvablyFairResult($stringToHash, $serverSeed, $ticketQuantity);
echo "Result: {$result['result']}";
?>