/    Sign up×
Community /Pin to ProfileBookmark

Update a script based on time elapsed?

Hi there im writing a text based game along the lines of mafia wars (facebook) and want a users stamina to increase every 5 minutes and am wondering how i would go about doing tht and what would be the best way to implement such a system?

this is the one thing i’m really struggling with, the stamina will be saved in the users table along side their user id and i want the stamina to continue to increase whether they are in a current session or not.

Any ideas? Thanks in advance ?

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@MindzaiMay 18.2009 — If the stamina is to increase constantly, you should just need to save the last time the stamina was updated in your table. If you know the current stamina for a user, the time that stamina was set, and the amount of stamina per 5 mins it's a pretty easy formula.

I would reccomend storing the time as a unix timestamp for easy calculations.

[code=php]
$stamina_per_fivemins = 10;
$current_stamina = 100; // this would come from a SELECT query
$stamina_updated = 1242663179; // also from the database
$seconds_elapsed = time() - stamina_updated;

$new_stamina = round(($seconds_elapsed / 300) * $stamina_per_fivemins);
[/code]


Obviously in reality you would probably make this into a function and compact down some of the code:

[code=php]
function calculate_stamina($current, $last_updated) {
return round(((time() - $last_updated) / 300) * 10);
}
[/code]
Copy linkTweet thisAlerts:
@ToffeeCrispauthorMay 18.2009 — thanks for your reply Mindzai I will definitely do that and see how it goes, cheers for the help!
×

Success!

Help @ToffeeCrisp spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...