I'm currently working on an algorithm that will create a seed for a random generated board. The seed is created using a timestamp
I have the following logic:
- If theres no current seed with the timestamp we create a new entry in the DB and we say that the current player is using that seed.
- The seed can only be played by a certain amount of users, if the current seed is full we create a new one. For now lets say it can only be played 4 times.
- A player cannot play the same seed, so I check if the current player has already played that seed if this is true I create a new seed.
My problem is the following: lets say that Seed A was played by Player A and also this seed was played by the following players A,B and C so it has room for one more. Player A plays once again and generates a new seed he is put in this one and then comes Player D so it needs to be put inside Seed A. The issue I'm having is how to know which Seed to use or how to keep track of the valid seeds. I'm using the GameSparks platform.
Any help for wrapping my logic would be highly appreciated, I'm kinda frustrated because I dont know how to continue.
This is my code for the createMatch logic
require('SeedGenerator');
// Selects a randomly generated seed for the match
function selectSeed(){
// Get the seed info from the module
var weekSeed = getSeed();
// Check if the seed is already created
var seedCursor = Spark.runtimeCollection("seedHistory").findOne({"seedId":weekSeed},{"_id":0});
if(seedCursor){
// Check for if the player has played the seed
var playerId = findPlayerId(seedCursor.playerList);
}
// Did not found a seed
if(!seedCursor){
var randomBot = selectBotPlayer();
var toInsert = {
"seedId": weekSeed,
"playerList":[{
"playerId": Spark.getPlayer().getPlayerId(),
"highestScore": 0,
"adversaryId": randomBot.userId
}]
};
Spark.runtimeCollection("seedHistory").insert(toInsert);
return toInsert;
// Check if the current seed is full or the player has already played the current seed
} else if(seedCursor.playerList.length === 4 || playerId){
var randomBot = selectBotPlayer();
var toInsert = {
"seedId": weekSeed + 1,
"playerList": [{
"playerId": Spark.getPlayer().getPlayerId(),
"highestScore": 0,
"adversaryId": randomBot.userId
}]
};
Spark.runtimeCollection("seedHistory").insert(toInsert);
return toInsert;
} else{
// Insert that new player to the current seed
Spark.runtimeCollection("seedHistory").update({
"seedId": weekSeed
},{
$push: {
"playerList": {
"playerId": Spark.getPlayer().getPlayerId(),
"highestScore":0,
"adversaryId": "-2"
}
}
});
}
return weekSeed;
}
// Function that checks if the player has already played certain seed
function findPlayerId(cursor){
for(var i=0; i<cursor.length; i++){
// Check if the player has played this seed
if((cursor[i].playerId || cursor[i].adversaryId) === Spark.getPlayer().getPlayerId()){
return true;
}else{
return false;
}
}
}
//TODO: get the players from MMR ranges
// If there are no comparative scores to use (players with same seed), we will use a collection
// of randomly generated ‘bot’ users with estimated scores that will likely be lower than the players
function selectBotPlayer(){
var players = [
{
"userId":"-1",
"name": "Pepe"
},
{
"userId":"-2",
"name": "Pablo"
},
{
"userId":"-3",
"name":"Pedro"
},
{
"userId":"-4",
"name":"Juan"
}
];
// Choose a random bot
return players[Math.floor(Math.random()*players.length)];
}
Spark.setScriptData("Created Seed", selectSeed());
And this is my code for creating the seed
function getSeed(){
var d = new Date();
var day = d.getDay();
var diff = d.getDate() - day + (day == 0 ? -6:1); // adjust when day is sunday
var result = d.setDate(diff);
var resultDate = new Date(result);
var utcDate = Date.UTC(resultDate.getUTCFullYear(), resultDate.getUTCMonth(), resultDate.getUTCDate(), 0, 0, 0);
return utcDate;
}