Jump to content

Offline calculations..


PhoenixTwo

Recommended Posts

Hi there,

 

I am kind of new to the PHP world. I say new as in I have a bit of experience with it, know basics and stuff.. But I am no PHP-Genius..

 

Anywho, the following is my situation:

 

I am building a random project for myself as a pastime. It is kind of like a little game. Although I hit a brick wall.

 

I have a variable called GOLD, and the amount of gold increases as time goes by. Think of it as resources in most RTS games. If you don't use it, it increases.

 

My question is this:

If I gave the number a maximum value, for instance, 500. And I started off with 10. Increase rate is 5 an hour. Is there a way that if i go offline (I close the page), and come back to it two hours later, it will calculate it to be 20 (start-off is 10, and five per hour for two hours)?? But I kind of need it as real-time. So if I am offline for let's say, two hours and 20 minutes, it uses the buffer in-between also.

 

What I am trying to get at is, it is not calculated hourly, but more by the seconds. So if the increase rate was 60 per hour, the calculations will still be done every second, although will only increase when it hits 60 seconds.

 

And yes, if possible, I would like it to be automatically adjusted into a MySQL database.

 

Sorry if I seem to have gone around in circles, I just wanted to give you guys as much information as possible so you guys know what I am talking about.

 

Thanking you all in advance.

Mark

Link to comment
Share on other sites

Well, if you always wanted the very latest amount of gold, the only way you could do it would be to store a last active time in the database. On each page, you would then check that with the current time and increase gold levels as necessary. You also update the database with the current time as the last active time.

 

This provides the illusion of real time information - all the while the user is offline, the level of gold in the database remains the same but as soon as they log in, it gets updated to the correct value based on when they were last active.

 

The only drawback with this is that it does require a couple of queries on each and every page.

Link to comment
Share on other sites

Yeah, the only issue with cronjobs is that its not really realistic to be running a script every few seconds to make sure the latest possible value of gold is there. If you were willing to compromise on how often the gold got updated, it might make more sense to use cronjobs than my suggestion.

Link to comment
Share on other sites

Wow, I must say I am very impressed with the speed of the replies. Bravo!

 

I have absolutely no idea what a cron is, let alone how to use it.

 

The theory GingerRobot explained seems to be the most logic to use at this very point in time. I have not really had much experience with the time functions on PHP as yet. Are there any specific functions or formulae which enable me to calculate last active time to the new active time? Or would you, or anyone else for that matter, be able to guide me in the right direction on how to get the ball rolling on this matter?

 

Thanking you all very much kindly.

Link to comment
Share on other sites

Hmmm, that is very interesting.

 

I don't know if we're quite on the same page, but here goes...

 

If I made a calculations page, seperate to the normal game page, and using cron, ran the calculations page every x number of seconds, that "COULD" be considered as real-time?

 

Or am I just on a totally different planet altogether? LoL

 

Cheers

Link to comment
Share on other sites

How often is it supposed to go?

 

I did a game like this once. Basically, you need to be checking the database every page load anyway for the user's status, messages, that sort of thing, yeah? So when you do that, you also get out their gold from the DB.

 

Then make a cron job that runs every so often. I did mine every 10-15 minutes. That cron job will have an SQL command (Either a PHP file or just SQL, I did a PHP file) and update the entire database as gold = gold+5 or whatever gold amount.

so "UPDATE users SET gold=(gold+5)"

 

And you're good to go.

Link to comment
Share on other sites

Hi jesirose,

 

What was the game called? And is it even still online? Just curious so I can see how you made it.

 

Either way, within the game, the higher the gold mine, the quicker your gold goes up. That is the route I was thinking of. Not just keeping it completely consistent. So the DB update of (gold+5) every so often kind of gets thrown out the window (no offence). And I was thinking maybe have multiple users, so keeping it one number wouldn't quite work in this case.

 

I was actually thinking (or maybe just asking for a more professional opinion), let's say we have 3 users/players. Maybe some kind of loop? It reads the first players increment number, then reads the total gold of the same player, adjusts the gold accordingly, and moves on to the next player. Is that even possible to query databases within a loop? (Can you tell I'm kind of new to the world of PHP?) :P

Link to comment
Share on other sites

it's online I just never released it because I have about 2% left to do :)

 

The SQL I posted will update every user with 5 more gold, no matter how many users you have.

You can do a lot in your SQL. If you get 5 gold for every level of gold mine, then it would be

 

UPDATE users SET gold = (gold+(5*goldmine))

 

or if it's random, maybe it's from 1-5 time the goldmine.

$num = rand(1,5);

$sql = "UPDATE users SET gold = (gold+($num*goldmine))";

mysql_query($sql);

 

etc. Just figure out how you want it to increment.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.