我目前正在研究一种算法,该算法将为随机生成的棋盘创建种子。种子是使用时间戳创建的
我有以下逻辑:
- 如果没有带有时间戳的当前种子,我们在数据库中创建一个新条目,我们说当前玩家正在使用该种子。
- 种子只能由一定数量的用户播放,如果当前种子已满,我们会创建一个新的。现在假设它只能播放 4 次。
- 一个玩家不能玩同一个种子,所以我检查当前玩家是否已经玩过那个种子,如果这是真的,我会创建一个新种子。
我的问题如下:假设种子 A 是由玩家 A 玩的,而这个种子是由以下玩家 A、B 和 C 玩的,所以它还有一个空间。玩家 A 再次播放并生成一个新种子,他将其放入该种子中,然后玩家 D 出现,因此需要将其放入种子 A。我遇到的问题是如何知道要使用哪个种子或如何跟踪的有效种子。我正在使用 GameSparks 平台。
任何包装我的逻辑的帮助将不胜感激,我有点沮丧,因为我不知道如何继续。
这是我的 createMatch 逻辑代码
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());
这是我创建种子的代码
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;
}