Our Scripts Vault contains many game scripts that you can use to create your own game!
I can send you pics of my cocks if you want reference.
In here I posted a timestamp comparing I did plan to use in my next game, but the project is put on hold at the moment. Feel free to use it as how it can be done. There is other ways, but I did find that the easiest.
Players increasing* experience per hour* money per hour* lost energy per hour* lost health per hourWould this be better to do with Cron jobs, or would there be a better way to dothis with a simple code?Personally I've always used Cron jobs, but friends tell me that its a bad way to do itand that theres an easier way.
Well the four points blackfang mentioned I would say should be updated for all accounts in the game. So if you want to avoid updating for inactive accounts you will have to setup a way to avoid that no matter which way you choose.But I can give two reasons why to use cron jobs:1. You are certain the updates will take place when you want them. If you let a player trigger the update there will always be a risk of time difference. For example, you want some kind of update to happen exactly at midnight. What if no player happens to load the page in question exactly at midnight?2. With a bigger page load rate the risk of the page being loaded twice at the same time will happen. So you will have to implement some kind of locking mechanism if you want to be totaly sure you don't trigger it twice by accident.Having said that, I use both ways. Depending on what I want to update, and how important it is that it's updated on an exact moment. For example, increasing everyones money is something I want to happen to all accounts (except on a few occasions) and I want to be sure it's done at certain times. Thus I have it in a cron job. Reviving dead people depends on how long they have been dead. So I don't want it to update all accounts, nor on any certain times. So I use a timestamp-based technique that's triggered when players load certain pages.
Well what I ment about the inactive accounts would of course only be valid when you want to update something for all accounts in the game, not the single account loading the page. For example you have an economy system where all players get money every hour, even when they are offline. Whether or not you use a timestamp based update or a cron-job, you would want the update to take place on all the players in the game.Accidental double load is not from the same player, but when two different players does it at the same time. The database transactions takes time, even tho a very small amount of time. But if two players loads a page at more or less the exact same millisecond, they may both make a successful check against the timestamp in the database. It won't happen very often or maybe never, but it can happen. And if it can happen, you need to deal with it.This of course once again is only valid when you are updating something that should update more than just the single account.But balancing server load.. let's say you have 5 tasks you want your cron-job to perform on the hour, so you solve it by performing 5 calls to the database. In the grand scheme of things 5 extra calls to the database shouldn't unbalance your server load. If your cron-job does unbalance the server load, then you probably have bigger issues to look at.
The end result is that all accounts will be update in due time.
It is perfectly acceptable to only update the player's account when he next logs in.
The OP wants to be able to update gold/hour, and other hourly type of gains. We are advising him to do the following:
Two different players loading the same check at the same time is not going to have the issues you are talking about because EVERY player has a separate time stamp. Every player has their own last_update stamp and so there cannot be a conflict between two players.
And back to the server load question, you are suggesting that the OP uses a cron job to update a record for every single user in his database.Let's say this is done once per hour, and the OP has 10,000 accounts. You're telling him to update 10,000 accounts with one call, so every hour there will be a period in which this huge database call takes place. Alternatively, if he used a time lapse method, the record updates would only be occurring when the user logs in or a call is made to the update check. The result is you will have 10,000 updates run over the course of the hour (if all 10,000 logged in), instead of 10,000 updates run at the same time. This is the essence of load balancing: performing an action over a period of time, rather than at an absolute time. By default, if you are running a cron job to accomplish this, you ARE unbalancing your sever load. If you looked at a line graph of the load, you would essentially have a line graph that has one huge spike at a particular time, instead of a constant, and consistent stream of updates.
Quote from: Brandon on February 11, 2009, 07:53:02 AMIt is perfectly acceptable to only update the player's account when he next logs in.Well I would say that depends on how the game should work and what you want to update. Lets say that you can steal money from a player, 10% of what he has. Now, if the player's money isn't increased in a timely manner when he's offline, the active players doesn't get the gain in money they should have.Same goes with health. Maybe you want players to heal even if they are offline, so the active players can attack them.
Just wanted to query something here, wouldn't it be possible to simply run the resource update script at this point, so say player1 attacks player2, who hasn't logged in for a week, before the update they have 50p in there account, after the update they have £1500.99 which then 10% is stolen, that way inactive players still lose a fair amount of resources, without the monster updates required by the cronned script.
Quote from: lolninja on February 11, 2009, 10:36:12 AMJust wanted to query something here, wouldn't it be possible to simply run the resource update script at this point, so say player1 attacks player2, who hasn't logged in for a week, before the update they have 50p in there account, after the update they have £1500.99 which then 10% is stolen, that way inactive players still lose a fair amount of resources, without the monster updates required by the cronned script.Yes of course it may be possible. And if it's possible, then you should. But then of course you have to keep in mind any risks of race conditions.But I can think of cases where your game design may require another setup.For example, maybe you have rankings being updated in real-time where your money is part of the ranking. If your money isn't updated for a week then your ranking will be scewed. Maybe you have a team-based game, where part of your updated money goes to the team. It depends. I clearly see the need to have controlled updates by using cron-jobs. But that doesn't mean it should be used all the time.
If a cron job is run on 10,000 accounts, 1 time per hour, then you are looking at 240,000 updates in a day.
Quote from: Brandon on February 11, 2009, 09:10:56 AMIf a cron job is run on 10,000 accounts, 1 time per hour, then you are looking at 240,000 updates in a day. It sounds bad but it is not. You can put 50,000 accounts with 1 hour updates on a cheap $2/month host (tested it, worked without problems ).What you said makes sense but... to my experience the performance issues were always caused by active players, not by hourly updates, even if cron was very unoptimized. Also bear in mind that some updates can be done in bulk ("UPDATE players SET gold=gold+$constant" is extremelly fast, even for hundreds of thousands players). I never got any real scalability problem. Let's face it, the problem is to get players, not to make an application that could handle them I wish I had scalability problems...
The OP asked which is better. I'm not going to give him the resource-ineffective way of accomplishing this task. Might as well practice good programming techniques now. It pays dividends later.
"Overly complex" and "tiny bit of performance" are not exactly phrases I would coin in regards to this, but again, that all depends on the size of project.
If you are dead set on a ranking system that relies on them, then minimize the taxing effects, such as only calculating rankings once per day.
Quote from: Brandon on February 11, 2009, 07:53:02 AMThe OP wants to be able to update gold/hour, and other hourly type of gains. We are advising him to do the following:Well the OP wrote "Players increasing X per hour"I wouldn't interpret that as the OP wants the players to have hourely gain updated when they login. But I guess I interpret depending on how I'm used to deal with it, for example some things needs to updated on a timely manner even if the player is offline and other things can be updated when the player is online.
Quote from: Brandon on February 11, 2009, 07:53:02 AMTwo different players loading the same check at the same time is not going to have the issues you are talking about because EVERY player has a separate time stamp. Every player has their own last_update stamp and so there cannot be a conflict between two players.You will have this issue as soon as more than a single player can trigger something that may update more accounts than just his own. As I wrote, this is only valid when two players are triggering the same update i.e. they are both checking against the same timetamp.But if what you want to achieve is to update a single players who is online, then you're are very true that you can solve it all by using timestamps.
//query old timestamp etc.$old_timestamp=0$time_now=1860 //seconds$time_passed=$time_now-$old_timestamp//$amount_multiplier=100/3600 //100/hour, 0.027 per second.$amount=round($amount_multiplier*$time_passed) //50.22 woods, but we can't produce partials so we round it down to 50.$new_time=$old_timestamp+round($amount*$amount_multiplier) //1800 is the new timestamp that gets stored, even when 1860 seconds has passed since the last update.