0

I have a laravel PHP 2 functions. In the first function, I simply create a game and generate a hash for it:

    $token = bin2hex(random_bytes(64));
    Game::query()->create([
        'hash' => $token,
    ]);

and then i create new record from Game model laravel.

In second function, I need to somehow call a nodejs file, where numbers are simply generated, and the resulting numbers are passed to PHP for saving in the database. My simply second function:

 public function setStatus(Request $r)
  {
    $status = $r->get('status');
    $game = $this->getGameInController();

    $game->update([
        'status' => $status
    ]);



    if ($status === 1) {
        $game->update([
            AND HERE I WANT TO RECEIVED INFORMATION FROM NODEJS FILE
        ]);

        return $multiplier;
    }
}

Then i has test.js file, where i want to pass the hash value from the table:

// You need to edit this value
var gameHash = 'HASH FROM LARAVEL GAME MODEL';

/*
Below this line is the formula which we use to calculate the result
*/
var crypto = require('crypto');
const INSTANT_CRASH_PERCENTAGE = 6.66;
var crashPointFromHash = function (serverSeed) {
    var hash = crypto.createHmac('sha256', serverSeed).digest('hex');


    // Use the most significant 52-bit from the hash to calculate the crash point
    var h = parseInt(hash.slice(0, 52 / 4), 16);
    var e = Math.pow(2, 52);
    const result = (100 * e - h) / (e - h);
    // INSTANT_CRASH_PERCENTAGE of 6.66 will result in modifier of 0.934 = 6.66% house edge with a lowest crashpoint of 1.00x
    const houseEdgeModifier = 1 - INSTANT_CRASH_PERCENTAGE / 100;
    const endResult = Math.max(100, result * houseEdgeModifier);

    return Math.floor(endResult);
};

console.log(`Hash: ${gameHash}\n\nGame crashed at ${(crashPointFromHash(gameHash) / 100).toFixed(2)}x`);

And than i want save in database the value, from {(crashPointFromHash(gameHash) / 100).toFixed(2)}

How i can make it? I hope for your help!

yrbet
  • 191
  • 2
  • 10
  • [This answer](https://stackoverflow.com/a/34908775/3323348) might help. – nosurs Apr 09 '21 at 21:43
  • That is not a bad answer but I recommend you 100% to have a microservice for that, because doing `exec` is a security risk. Also if you have to change where that info comes from in a future, if it is a microservice you could still do the same call to the same place, but if you use `exec` you will have to alter that line of code... So using a microservice is simply calling another API and that is universal among devs. – matiaslauriti Apr 09 '21 at 23:28

0 Answers0