Current location: Hot Scripts Forums » Programming Languages » PHP » Energy Recharge-10 min code


Energy Recharge-10 min code

Reply
  #1 (permalink)  
Old 12-02-07, 02:02 PM
Ishmell Ishmell is offline
Newbie Coder
 
Join Date: Jul 2007
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
Energy Recharge-10 min code

I was playing around with some codes but couldnt see to figure it out. Does someone here know how to make the Energy stat on the profile recharge a little and in 10 min it would be fully recharged?
Reply With Quote
  #2 (permalink)  
Old 12-02-07, 04:16 PM
phpdoctor's Avatar
phpdoctor phpdoctor is offline
Code Guru
 
Join Date: Feb 2007
Location: New Zealand
Posts: 767
Thanks: 4
Thanked 2 Times in 2 Posts
Do you have any code already?
Could you explain alittle more, whats the energy?
__________________
01010000 01001000 01010000
Reply With Quote
  #3 (permalink)  
Old 12-02-07, 04:21 PM
Ishmell Ishmell is offline
Newbie Coder
 
Join Date: Jul 2007
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
No I dont have a Energy recharge code yet. Energy is a stat in my browser based game which is a field in the profile section of the database. The user has 100 energy at the moment and lets say the user does a crime which subtracts energy to do the crime. So the energy goes from 100 to 50. Now I want a script that would activate and recharge it back up to 100 again over time.
Reply With Quote
  #4 (permalink)  
Old 12-02-07, 05:34 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Quote:
Originally Posted by Ishmell View Post
No I dont have a Energy recharge code yet.
Oh, that Energy Code, the one that doesn't exist yet and that you didn't bother to explain or provide an example of. Right. Gotcha.

Based on what little you did say, what you probably want is a cron job that runs periodically to reset the "Energy Code value.

Without providing a more coherent explanation (and maybe some code) no one here can give you much more than a generalized answer (i.e. "use a cron job").
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Reply With Quote
  #5 (permalink)  
Old 12-02-07, 06:30 PM
Ishmell Ishmell is offline
Newbie Coder
 
Join Date: Jul 2007
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
yeah I think thats what its called to. Well I really dont have any code for the Energy but the subtract code that takes energy away.
PHP Code:

$_SESSION['Energy'] -= $amount
I can use that code for like after crimes and stuff but I need the regerate energy code which I did hear somewhere on msn that it was called "cron job"
Reply With Quote
  #6 (permalink)  
Old 12-02-07, 07:37 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Do you know any Javascript?
Because that is what you will need to setup a timer.
__________________
Jerry Broughton
Reply With Quote
  #7 (permalink)  
Old 12-02-07, 07:43 PM
Ishmell Ishmell is offline
Newbie Coder
 
Join Date: Jul 2007
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by job0107 View Post
Do you know any Javascript?
Because that is what you will need to setup a timer.
Really? I no none whatsoever but I also herd a while back you could do these things in php as well. I also herd there were do ways of doing this. One was where the energy would rechrage slowly till its full and the other is its recharges completely after a certain number of minutes. Doesnt really matter whichever someone could help me out with. Either one would be perfect.
Reply With Quote
  #8 (permalink)  
Old 12-02-07, 11:00 PM
Patiek Patiek is offline
Wannabe Coder
 
Join Date: Nov 2003
Posts: 165
Thanks: 0
Thanked 0 Times in 0 Posts
This can be done by time stamping a particular event (when the energy for this character last changed) and keeping that time stamp / energy level after the change in a database.

When the user views his character, you take (current time) - (timestamp time) and base the user's energy level off of that (you can update the database while you are at it).

This method doesn't rely on cron and is only updated when the user is visiting the website (the only time the user's energy really needs to be updated). This method requires you to have a new table in your database that holds energy and timestamp for each user.

To recap (in order):
1. User's energy level changes (perhaps the user was hit or something).
2. You place the user's new energy level into a database with a timestamp of the current time.
3. Whenever your game needs to display this user's particular energy level (I am assuming this ONLY happens when the specific user is interacting with the system), you game fetches the time stamp and new energy level from the database.
4. Determine the difference between the current time and the time stamp and multiply it by some value to get the user's energy level. NE = ((current time) - (timestamp))*rate_of_energy_recharge
5. Take (NE) from above and update user's energy in the database (i.e. probably where your user table or attribute table is I am guessing) ~ NOT the timestamp time.
6. When enough time has elapsed or a the user's energy level has changed again (i.e. the user has been hit again), update the user's energy / timestamp in the energy timestamp table.

That would be the basic and most obvious setup here. A good example of #4 equation:

PHP Code:

$timestamp = ... fetched from timestamp table ...

$old_energy = ... fetched from timestamp table ...
$current time();
$rate 5// 5 energy points / second 
$user_energy 3000// the user's energy when fully restored, pulled from your user or attribute table

$new_energy = ($current $timestamp) * $rate;

// if we are finished regenerating
if ($new_energy >= $user_energy)
{
    
// store $new_energy into user's attribute table
    // remove energy timestamp from timestamp table database

This method will work best if your script has ONE function to update a user's energy level. If your script update's the user's energy level from multiple locations, then you will want to include the code you come up with that calculates the user's energy from the timestamp db into the header / global portion of your script (so it is loaded every time to ensure that things such as shops work correctly).
__________________
EP-Dev (http://www.ep-dev.com)
- EP-Dev Whois Script
- EP-Dev Forum News
- EP-Dev Counter
- EP-Dev CMS (coming soon!)
Reply With Quote
  #9 (permalink)  
Old 12-03-07, 10:57 AM
Ishmell Ishmell is offline
Newbie Coder
 
Join Date: Jul 2007
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Patiek View Post
This can be done by time stamping a particular event (when the energy for this character last changed) and keeping that time stamp / energy level after the change in a database.

When the user views his character, you take (current time) - (timestamp time) and base the user's energy level off of that (you can update the database while you are at it).

This method doesn't rely on cron and is only updated when the user is visiting the website (the only time the user's energy really needs to be updated). This method requires you to have a new table in your database that holds energy and timestamp for each user.

To recap (in order):
1. User's energy level changes (perhaps the user was hit or something).
2. You place the user's new energy level into a database with a timestamp of the current time.
3. Whenever your game needs to display this user's particular energy level (I am assuming this ONLY happens when the specific user is interacting with the system), you game fetches the time stamp and new energy level from the database.
4. Determine the difference between the current time and the time stamp and multiply it by some value to get the user's energy level. NE = ((current time) - (timestamp))*rate_of_energy_recharge
5. Take (NE) from above and update user's energy in the database (i.e. probably where your user table or attribute table is I am guessing) ~ NOT the timestamp time.
6. When enough time has elapsed or a the user's energy level has changed again (i.e. the user has been hit again), update the user's energy / timestamp in the energy timestamp table.

That would be the basic and most obvious setup here. A good example of #4 equation:

PHP Code:

$timestamp = ... fetched from timestamp table ...

$old_energy = ... fetched from timestamp table ...
$current time();
$rate 5// 5 energy points / second 
$user_energy 3000// the user's energy when fully restored, pulled from your user or attribute table

$new_energy = ($current $timestamp) * $rate;

// if we are finished regenerating
if ($new_energy >= $user_energy)
{
    
// store $new_energy into user's attribute table
    // remove energy timestamp from timestamp table database

This method will work best if your script has ONE function to update a user's energy level. If your script update's the user's energy level from multiple locations, then you will want to include the code you come up with that calculates the user's energy from the timestamp db into the header / global portion of your script (so it is loaded every time to ensure that things such as shops work correctly).
Okay im pretty sure I get this code. Where it says ... fetched from timestamp table ... is it actually doing something or do I like put something there?
Reply With Quote
  #10 (permalink)  
Old 12-03-07, 11:51 AM
Patiek Patiek is offline
Wannabe Coder
 
Join Date: Nov 2003
Posts: 165
Thanks: 0
Thanked 0 Times in 0 Posts
I just gave you pseudo code... you need to fill in the gaps. You need to come up with the code for "fetched from timestamp table" (some mysql select, etc).

If you wanted to actual code you should have submitted a script request.
__________________
EP-Dev (http://www.ep-dev.com)
- EP-Dev Whois Script
- EP-Dev Forum News
- EP-Dev Counter
- EP-Dev CMS (coming soon!)
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP inside JavaScript? pikaz JavaScript 20 07-10-10 12:55 AM
New to Perl - quick code help jdsmith8 Perl 3 05-05-07 06:09 PM
Display 10 random images from DIR with HTML Code for Image cyclotron Script Requests 2 05-14-06 08:59 AM
Explanation of Code... Please davidk19380 Perl 1 02-26-06 01:50 PM
How to sale php code to customer without giving him code pradeep_soft PHP 4 03-12-04 12:10 PM


All times are GMT -5. The time now is 07:32 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.