我目前正致力于为随机生成的电路板创建种子的算法。使用时间戳
创建种子我有以下逻辑:
我的问题如下:让我们说种子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;
}
答案 0 :(得分:0)
很简单,您可以按照白板的方式跟踪有效种子。白板有四列:一个用于种子,三个用于使用该种子的玩家。让我们考虑这样一个董事会的州:
seed p1 p2 p3
1111 A B C
现在,A
想再玩一次。 A上的每个种子都由A播放。创建一个新种子并将其分配给A:
seed p1 p2 p3
1111 A B C
2222 A
接下来,C
想玩。第一个可用的种子是2222
:
seed p1 p2 p3
1111 A B C
2222 A C
现在,D
想玩。第一个可用的种子是1111
。然而,那已经有三个先前的球员。给D
游戏1111的链接并退出种子:
seed p1 p2 p3
2222 A C
看看这是如何工作的?如果需要,还要保留一份退役种子清单。
只要您指定第一个可用的种子,该列表就会非常小,仅在1-3个玩家非常活跃而其他玩家闲置的情况下延伸。