PhoenixTwo Posted September 20, 2007 Share Posted September 20, 2007 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 Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted September 20, 2007 Share Posted September 20, 2007 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. Quote Link to comment Share on other sites More sharing options...
effigy Posted September 20, 2007 Share Posted September 20, 2007 There's cron on Unix, and I believe they're called services on Windows. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted September 20, 2007 Share Posted September 20, 2007 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. Quote Link to comment Share on other sites More sharing options...
redking Posted September 20, 2007 Share Posted September 20, 2007 no, don't use a cron use the time thing find out how much time has passed and update accordingly. You could create a function that, anythime gold is accessed it would be updated. Quote Link to comment Share on other sites More sharing options...
PhoenixTwo Posted September 20, 2007 Author Share Posted September 20, 2007 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. Quote Link to comment Share on other sites More sharing options...
MmmVomit Posted September 20, 2007 Share Posted September 20, 2007 cron is a program on unix systems that lets you schedule programs to be run at specific times during the day. Quote Link to comment Share on other sites More sharing options...
PhoenixTwo Posted September 20, 2007 Author Share Posted September 20, 2007 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 Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 20, 2007 Share Posted September 20, 2007 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. Quote Link to comment Share on other sites More sharing options...
PhoenixTwo Posted September 20, 2007 Author Share Posted September 20, 2007 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?) Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 20, 2007 Share Posted September 20, 2007 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. Quote Link to comment Share on other sites More sharing options...
PhoenixTwo Posted September 20, 2007 Author Share Posted September 20, 2007 Is there still some sort of example we/I can look at? It would be of great assistance to me in getting my head around how to code the damn thing. Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 20, 2007 Share Posted September 20, 2007 I posted several examples of code that will do what you want. What are you asking for? If you don't even know how you want to determine how much their gold goes up, you need to figure that out before you code it. Quote Link to comment Share on other sites More sharing options...
PhoenixTwo Posted September 20, 2007 Author Share Posted September 20, 2007 I really do apologise. I think I was more curious of your game/design and started blabbering on about other stuff which were unnecessary. Either way, thank you very much for your help. It was definitely very useful in the least Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 20, 2007 Share Posted September 20, 2007 Once the game is ready to be released I'll try to remember to PM you Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.