Sign In Register

How can we help you today?

Start a new topic
Answered

Limiting An Action Within An Event Based On Daily Cap

I have an event that can be called which credits currency to the player.  I want to allow the player to call the event as many times as they want but I want to limit the number of times it actually credits the player on a daily basis.  If anyone has any good approaches to this sort of system I would love to hear it (or read it in this case).


Best Answer

Hi George


You can use a counter in a mongo collection for this. If you perform a findAndModify with an $inc you can track how many credits a player has had a day.


I created a cloud code script that you can use to get started.


Hope this is useful !


Gabriel


   

var AMOUNT_TO_CREDIT = 10;
var MAX_CREDITS_PER_DAY = 5;

var today = new Date();
today.setMilliseconds(0);
today.setSeconds(0);
today.setMinutes(0);
today.setHours(0);

var playerDayRecord = Spark.runtimeCollection("credit_log").findAndModify(
	{"_id" : Spark.getPlayer().getPlayerId(), "day" : today}, 
	null, 
	null, 
	false, 
	{"$inc" : {"count" : 1}}, 
	true, 
	true
);

var numberOfTimesCalledToday = playerDayRecord.count;

if(numberOfTimesCalledToday < MAX_CREDITS_PER_DAY){
    Spark.getPlayer().credit1(AMOUNT_TO_CREDIT)
    Spark.setScriptData("credited", true);
} else {
    Spark.setScriptData("credited", false);
}

   


Answer

Hi George


You can use a counter in a mongo collection for this. If you perform a findAndModify with an $inc you can track how many credits a player has had a day.


I created a cloud code script that you can use to get started.


Hope this is useful !


Gabriel


   

var AMOUNT_TO_CREDIT = 10;
var MAX_CREDITS_PER_DAY = 5;

var today = new Date();
today.setMilliseconds(0);
today.setSeconds(0);
today.setMinutes(0);
today.setHours(0);

var playerDayRecord = Spark.runtimeCollection("credit_log").findAndModify(
	{"_id" : Spark.getPlayer().getPlayerId(), "day" : today}, 
	null, 
	null, 
	false, 
	{"$inc" : {"count" : 1}}, 
	true, 
	true
);

var numberOfTimesCalledToday = playerDayRecord.count;

if(numberOfTimesCalledToday < MAX_CREDITS_PER_DAY){
    Spark.getPlayer().credit1(AMOUNT_TO_CREDIT)
    Spark.setScriptData("credited", true);
} else {
    Spark.setScriptData("credited", false);
}

   

Thanks for this!


This was a great starting point for me as well as a good nudge into MongoDB.

Login to post a comment