RDR
RDR

Reputation: 1143

Adding a limit timer (Like can only be done/used every 24 hours)

Code for a command for a private server for a game I am "developing".

internal class LotteryCommand : Command
{
    public LotteryCommand()
        : base("lottery", 1)
    {
    }
    //lottery can be used once every 24h, just to attract users making them want to get on at least once a day!
    protected override bool Process(Player player, RealmTime time, string[] args)
    {
        Random rand = new Random();

        string name = player.Name;

        string lottonum = rand.Next(0, 100).ToString();
         //50 50 chance!
        if (int.Parse(lottonum) > 49)
        {
            player.Manager.Database.DoActionAsync(db =>
            {
                player.Credits = db.UpdateCredit(player.Client.Account, +5000);
                player.UpdateCount++;

            });
        }
        foreach (Client i in player.Manager.Clients.Values)
        {
            i.SendPacket(new TextPacket
            {
                BubbleTime = 0,
                Stars = -1,
                Name = "Lottery - " + name,
                Text = "rolled a " + lottonum
            });
        }
        return true;
    }
}

can anyone tell me what to add if i wanted the command only to be used once per 24 hours? for those who cant tell/wanna know, its /lottery, and will say "Lottery - Player has rolled a #" and if the # is 50 or higher, the player who used the command will win 5k gold (credits) and if below, they dont win anything. i just want it to where it can only be used once every 24 hours.

Upvotes: 0

Views: 60

Answers (1)

Joseph Bisaillon
Joseph Bisaillon

Reputation: 173

Whenever a player runs that command you can insert a timestamp into the database. At the beginning of the command method, put a bit of code to check the database for that timestamp and see if it is within the last 24 hours. If it is, the break out of the method or put some functionality to alert the user they have played to often.

You can create another method called CanPlay that returns a bool, and pass it the user and the current timestamp. Use that method to determine if the user can play.

Upvotes: 1

Related Questions