Atomic, Persistent and Pseudo Random number generator in GameSparks



This will allow you to share a pseudo random number generator between several users. To use it during  a Real time match you just have to send the request from your RT event.

It needs a runtime collection to store the seed related to the current session, it's called "MatchSeed", that's completely managed from the following module, MODULE_ATOMIC_RANDOM. The module has functions to retrieve, update and remove the seeds from the collection. Just create a Cloud Code's Module and copy the following code:

/////////////////////////////////////////////////////////
// File MODULE_ATOMIC_RANDOM
//////////////////////////////////////////////////////////
function GetMatchSeed(sessionId)
{
    var matchSeedCollection = Spark.runtimeCollection("MatchSeed");
    var result = undefined;
    var matchSeed = matchSeedCollection.findOne({"sessionId":sessionId});
  
    if (matchSeed != null && matchSeed != undefined)
    {
        result = matchSeed["seed"];
    }
  
    return result;
}

function SetMatchSeed(sessionId, seed)
{
    var matchSeedCollection = Spark.runtimeCollection("MatchSeed");
    var query = {
        "sessionId": sessionId
    };

    var newObject = {
        "sessionId": sessionId,
        "seed": seed
    };

    var success = matchSeedCollection.update(query, newObject, true, false ); //upsert: true, multi: false
    return success;
}

function RemoveMatchSeed(sessionId)
{
    var matchSeedCollection = Spark.runtimeCollection("MatchSeed");
    var query = {
        "sessionId": sessionId
    };
  
    var success = matchSeedCollection.remove(query);
    return success;
}

And here you have the event's code you need to get your Atomic,Persistent, Pseudo Random-Generator

//////////////////////////////////////////////////////////
//AtomicPseudoRandom.js:
//////////////////////////////////////////////////////////
require("MODULE_ATOMIC_RANDOM");

/* We pass an object because we want the changes on the seed to be an output parameter
   That way we can store the seed in a runtime collection and use it again when needed,
   keeping the sequence of random numbers from one event execution to another event execution.*/
var PseudoRandomSeed = function(obj) {
    return function() {
        obj.seed = Math.sin(obj.seed) * 10000;
        return obj.seed - Math.floor(obj.seed);
    };
};

var sessionId   = Spark.getData().sessionId;
var lastSeed = undefined;

//Mutex: We don't want other user to get a random while this is running
Spark.lockKey("AtomicRandom", 100);

var seedFromDB = GetMatchSeed(sessionId);
   
//If there is no seed get the session ID as the prime seed
if (seedFromDB == null || seedFromDB == undefined)
{
    lastSeed = parseInt(sessionId, 16); //I'm supossing the sessionID is an hexadecimal number
}
else //Otherwise, we used the stored seed
{
    lastSeed = seedFromDB;
}
  
var myObj = {
    seed: lastSeed
};
  
var PseudoRandom = PseudoRandomSeed(myObj);
  
//Generate randomNumber and store it in the scriptData
var randomNumber = PseudoRandom();

Spark.setScriptData("randomNumber", randomNumber)

var setMatchSeedSuccess = SetMatchSeed(sessionId, myObj.seed);

Spark.unlockKey("AtomicRandom");


Comentarios

Entradas populares