Minase Posted October 23, 2009 Share Posted October 23, 2009 hy i was wondering what's the best method in order to make some automatic updates in a php game let's say that the user should gain 1gold each minute but the user is not logged in and the process is happening automatically . what will be the best method? if it's cron jobs then what you will use if you need constant update (real time) because cron jobs every second is resource killing thank you Quote Link to comment https://forums.phpfreaks.com/topic/178682-php-game/ Share on other sites More sharing options...
drisate Posted October 23, 2009 Share Posted October 23, 2009 you should use cron jobs and for the userinterface javascript or ajax ;-) good luck Quote Link to comment https://forums.phpfreaks.com/topic/178682-php-game/#findComment-942523 Share on other sites More sharing options...
keldorn Posted October 23, 2009 Share Posted October 23, 2009 I think i would do something like this. Consider this logic. Create a table in your mysql database for a time, say "last activity", set a time in session also $_SESSION['last_activity'] and evalute it on each page load, if it goes over 10 minutes from last activity time, then reupdate the session time, and update their coins by 10 coins. (1 coin x 10 minutes). Also reupdate their database field saying there last time.. when thye log in again when they dont have their session. Then evaluate the time in the database of their activity and compare is too the current time and then evaluate how many coins. Good luck. Quote Link to comment https://forums.phpfreaks.com/topic/178682-php-game/#findComment-942528 Share on other sites More sharing options...
Minase Posted October 23, 2009 Author Share Posted October 23, 2009 but what if the user is not logged in (the coins will not update) and if other user attacks him then no those coins will not be calculated.. so it's unfair Quote Link to comment https://forums.phpfreaks.com/topic/178682-php-game/#findComment-942533 Share on other sites More sharing options...
Andy-H Posted October 23, 2009 Share Posted October 23, 2009 make a field of integer type to hold a unix timestamp in the users table and store the users username in a PHP session. then use something along these lines. <?php session_start(); // connect to db or require db connection file here. $timer = 60; $goldToAdd = 1; $query = "SELECT gold, time FROM users WHERE username = '" . mysql_real_escape_string($_SESSION['username']) . "' LIMIT 1"; $result = mysql_query($query); while (list($gold, $time) = mysql_fetch_row($result)) { $time -= time(); while ($time >= $timer) { $gold += $goldToAdd; $time -= $timer; } $time += time(); $query = "UPDATE users SET gold = " . $gold . ", time = " . $time . " WHERE username = '" . mysql_real_escape_string($_SESSION['username']) . "' LIMIT 1"; $result = mysql_query($query)or trigger_error('Error on line: ' . __LINE__ . ' : Gold could not be updated.'); } ?> but what if the user is not logged in (the coins will not update) and if other user attacks him then no those coins will not be calculated.. so it's unfair As for that you could update a users gold before they are attacked or w.e. as the attacker will input their username. Quote Link to comment https://forums.phpfreaks.com/topic/178682-php-game/#findComment-942534 Share on other sites More sharing options...
shane18 Posted October 23, 2009 Share Posted October 23, 2009 My Ideas 1. for that.... i would make a refresh page... that refreshes every minute and you have it on your computer and have leave it up 24/7 and make sure that it can't be accessed by others. 2. Make it so any time someone on the site refreshes a page or goes to a new one it updates all the gold based on the last time stamp... For example you go from one page to a other.. it will update everyones gold based off there last gold update timestamp Quote Link to comment https://forums.phpfreaks.com/topic/178682-php-game/#findComment-942569 Share on other sites More sharing options...
mikesta707 Posted October 23, 2009 Share Posted October 23, 2009 I would just do somethink like Andy posted. Probably just use ajax to update their gold when they are logged on, then just update their gold when they log in Quote Link to comment https://forums.phpfreaks.com/topic/178682-php-game/#findComment-942571 Share on other sites More sharing options...
Gayner Posted October 23, 2009 Share Posted October 23, 2009 I would just do somethink like Andy posted. Probably just use ajax to update their gold when they are logged on, then just update their gold when they log in ? javascript updates php ? Quote Link to comment https://forums.phpfreaks.com/topic/178682-php-game/#findComment-942573 Share on other sites More sharing options...
Andy-H Posted October 23, 2009 Share Posted October 23, 2009 I would just do somethink like Andy posted. Probably just use ajax to update their gold when they are logged on, then just update their gold when they log in ? javascript updates php ? No, AJAX uses Javascript to sent HTTP messages to PHP scripts that send back content so that javascript can update content without refreshing the whole page, theres a whole board dedicated to AJAX on PHPFreaks if you wish to learn more. Quote Link to comment https://forums.phpfreaks.com/topic/178682-php-game/#findComment-942593 Share on other sites More sharing options...
Gayner Posted October 23, 2009 Share Posted October 23, 2009 I would just do somethink like Andy posted. Probably just use ajax to update their gold when they are logged on, then just update their gold when they log in ? javascript updates php ? No, AJAX uses Javascript to sent HTTP messages to PHP scripts that send back content so that javascript can update content without refreshing the whole page, theres a whole board dedicated to AJAX on PHPFreaks if you wish to learn more. do u Think I wasn't being starcastic? Quote Link to comment https://forums.phpfreaks.com/topic/178682-php-game/#findComment-942595 Share on other sites More sharing options...
joel24 Posted October 23, 2009 Share Posted October 23, 2009 andy is on the right path, you could have someones gold stores what is in the database plus the amount automatically given to them since their account creation i.e. have a unix timestamp of their creation, then each time someone interacts with that user they work out the amount of gold that user has which would be (current unix timestamp - account creation timestamp) / 60 (for minutes). then add that to the amount in the database, which is not inclusive of any automatically given gold... and could be a negative or positive number... so when someone stole gold or wanted to see how much gold a person had $sql = @mysql_query("SELECT accountCreation, gold FROM users WHERE userid = xx"); $results = mysql_fetch_row($sql); //amount of gold in users account $gold = (time() - $results['accountCreation']) / 60 + $results['gold']; either that or you can have it so each time a user logs on or a user interacts with another user, a script runs which updates the amount of gold in their account... Quote Link to comment https://forums.phpfreaks.com/topic/178682-php-game/#findComment-942599 Share on other sites More sharing options...
Andy-H Posted October 23, 2009 Share Posted October 23, 2009 I would just do somethink like Andy posted. Probably just use ajax to update their gold when they are logged on, then just update their gold when they log in ? javascript updates php ? No, AJAX uses Javascript to sent HTTP messages to PHP scripts that send back content so that javascript can update content without refreshing the whole page, theres a whole board dedicated to AJAX on PHPFreaks if you wish to learn more. do u Think I wasn't being starcastic? Ahh lmao, I only gave it a quick scan... Sorry Quote Link to comment https://forums.phpfreaks.com/topic/178682-php-game/#findComment-942605 Share on other sites More sharing options...
Andy-H Posted October 23, 2009 Share Posted October 23, 2009 andy is on the right path, you could have someones gold stores what is in the database plus the amount automatically given to them since their account creation i.e. have a unix timestamp of their creation, then each time someone interacts with that user they work out the amount of gold that user has which would be (current unix timestamp - account creation timestamp) / 60 (for minutes). then add that to the amount in the database, which is not inclusive of any automatically given gold... and could be a negative or positive number... so when someone stole gold or wanted to see how much gold a person had $sql = @mysql_query("SELECT accountCreation, gold FROM users WHERE userid = xx"); $results = mysql_fetch_row($sql); //amount of gold in users account $gold = (time() - $results['accountCreation']) / 60 + $results['gold']; either that or you can have it so each time a user logs on or a user interacts with another user, a script runs which updates the amount of gold in their account... You would also have to track the amount of times their gold has been updated with that one, otherwise it would add one gold per minute since account cration each time the code piece was run. Say 4 mins after account creation, they would have 10 gold rather than 4 ie, 1 + 2 + 3 + 4 rather than 1 + 1 + 1 + 1 Well, assuming the code runs once per min. Quote Link to comment https://forums.phpfreaks.com/topic/178682-php-game/#findComment-942606 Share on other sites More sharing options...
joel24 Posted October 23, 2009 Share Posted October 23, 2009 noo i meant so the automatically added gold was never stored in the database... instead only acquired gold was stored in there... and then each time the user interacted with someone or someone viewed their gold it would add the amount in teh database + (current unix timestamp - account creation timestamp) / 60 i.e. if they might have -400 gold in the database however since they've been a member so long they actually have 10000 gold. i guess then there's issues if you want to select the person with the most gold from the database etc etc... other way is to write a script which runs each time the user is interacted with... and after it puts in gold it adds the current unix timestamp to the database, so next time someone interacts with the user it adds the amount of gold accumulated since the previous interaction...? Quote Link to comment https://forums.phpfreaks.com/topic/178682-php-game/#findComment-942610 Share on other sites More sharing options...
Andy-H Posted October 23, 2009 Share Posted October 23, 2009 noo i meant so the automatically added gold was never stored in the database... instead only acquired gold was stored in there... and then each time the user interacted with someone or someone viewed their gold it would add the amount in teh database + (current unix timestamp - account creation timestamp) / 60 i.e. if they might have -400 gold in the database however since they've been a member so long they actually have 10000 gold. i guess then there's issues if you want to select the person with the most gold from the database etc etc... other way is to write a script which runs each time the user is interacted with... and after it puts in gold it adds the current unix timestamp to the database, so next time someone interacts with the user it adds the amount of gold accumulated since the previous interaction...? Also, issues with amount of gold spent, issues with checking if a user can afford an item.... As for your solution, thats exactly what the code I supplied does... //edit Although I should have written it to a function/class method which takes a username and updates that users gold by the required amount, easily done though. Quote Link to comment https://forums.phpfreaks.com/topic/178682-php-game/#findComment-942612 Share on other sites More sharing options...
Gayner Posted October 23, 2009 Share Posted October 23, 2009 I would just do somethink like Andy posted. Probably just use ajax to update their gold when they are logged on, then just update their gold when they log in ? javascript updates php ? No, AJAX uses Javascript to sent HTTP messages to PHP scripts that send back content so that javascript can update content without refreshing the whole page, theres a whole board dedicated to AJAX on PHPFreaks if you wish to learn more. do u Think I wasn't being starcastic? Ahh lmao, I only gave it a quick scan... Sorry I was just havin fun sorry about that lol Quote Link to comment https://forums.phpfreaks.com/topic/178682-php-game/#findComment-942638 Share on other sites More sharing options...
Minase Posted October 23, 2009 Author Share Posted October 23, 2009 thank you for all your answers i will let this thread a little bit open to see others opinion . thank you Quote Link to comment https://forums.phpfreaks.com/topic/178682-php-game/#findComment-942743 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.