PHP code example of magadanuhak / laravel-provably-fair

1. Go to this page and download the library: Download magadanuhak/laravel-provably-fair library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

magadanuhak / laravel-provably-fair example snippets

 
    $provablyFairService = new ProvablyFair(); //Initialization of provably fair
    $clientSeed = $request->client_seed; // Client seed is a string that you should get from frontend
    $resultedData = $provablyFairService->getRandomNumber($clientSeed); // This method will return an object ProvablyFairResultData

class ProvablyFairResultData
{
    public function __construct(
        public readonly string $clientSeed,
        public readonly string $serverSeed,
        public readonly int    $nonce,
        public readonly float  $resultedNumber,
        public readonly float  $minimalValue,
        public readonly float  $maximalValue,
    )
    {
    }
}

class GetRandomWeapon {

    public function chances(): Collection
    {
        return collect([
            "Ak-47" => 45,
            "Mp-40" => 50,
            "AWM" => 5,
        ]);
    } 

    public function getItem(): string //Returns won Item Name 
    {
        $sum = 0;

        $provablyFairService = new ProvablyFair(); //Initialization of provably fair
        $clientSeed = $request->client_seed; // Client seed is a string that you should get from frontend
        $resultedData = $provablyFairService->getRandomNumber($clientSeed);  // This method will return an object ProvablyFairResultData
//      $resultedData = $provablyFairService->getRandomNumber($clientSeed, $serverSeed, $nonce);  // $serverSeed, $nonce are optionally This method will return an object ProvablyFairResultData

        $choice = $resultedData->resultedNumber;

        return app(
            $this->chances()
                ->map(function ($value, $item) use (&$sum) {
                    $sum += $value;
                    return $sum;
                })
                ->reduce(function ($result, $value, $key) use ($choice) {
                    if (is_string($result)) {
                        return $result;
                    }

                    if ($choice <= $value) {
                        return $key;
                    }

                    return $result + $value;
                }, 0)
        );
    }
}

class GetRandomItemByChance {
    
    public function __construct(
        ProvablyFairContract $provablyFair
    )
    {}

    public function getProvablyFairResult(string $clientSeed): ProvablyFairResultData
    {
        return $this->provablyFair->getRandomNumber($clientSeed);
    }
}