Tuesday, 10 September 2013

Stop countdown timer and fire a function

Stop countdown timer and fire a function

Project: I'm made a simple jackpot game, everything is working fine except
for this timer problem(not a frequent timer user).
Problem: This is what i want to do, i'm have a "lastActiveTime" field
which stores the last spin time and 24-hours later, i have to give him
another 10 coins to spin and this continues everyday.
What I did so far
protected void Timer1_Tick(object sender, EventArgs e)
{
var day = 1; //countdown time
var start = player.LastActive; // Use UtcNow instead of Now
var endTime = start.AddDays(day); //endTime is a member, not a local
variable
//timer1.Enabled = true;
TimeSpan remainingTime = endTime - DateTime.UtcNow;
string result = string.Format(@"{0:hh\:mm\:ss}", remainingTime);
Label1.Text = result.ToString();
}
public int AvailableDailyCoins
{
get
{
if ((DateTime.UtcNow.Date - _player.LastActive).Days > 0)
{
_player.Coins = 10;
_db.SaveChanges();
}
return (DateTime.UtcNow.Date - _player.LastActive).Days > 0 ? 10 : 0;
}
}
Question: How do i call the AvailableDailyCoins once the timer has
completed the 24 hour cycle.
Thank you in advance.

No comments:

Post a Comment