Need help with real-time calculations in my browser-based game

So I’m fairly new to PHP, I’ve been working on a real-time browser-based game for the last couple of weeks. Everything with the graphics have been going great and I’m now ready to start working on the real-time aspect of the game. Here’s a picture of what I have so far, this is the main section of the game showing the map navigation page:

Since I’m attempting this project for the first time I need a bit of help getting started. In my game there are 4 types of resources - wood, stone, iron, and gold. These resources are generated by constructing resource buildings on the map. For example, when a player constructs a lumber mill on the map, he/she starts generating wood at a rate of 60 units/hour. What’s important here is that resources are generated over time, and it’s this which I need help with right now.

There are other browser-based games out there which use the same resource generation over time, I recommend you guys check them out since I’m trying to simulate the exact same concept in my website. Here’s some links for a couple of those games:

http://www.tribalwars.net/
http://cb1.castle-battle.com/

If you’ve noticed, these websites update the resources a player has dynamically, meaning that if a player is generating 60 wood/hour, the amount of wood the player has goes up by 1 every minute, without having to refresh the page. This is what I need to simulate in my game. Since the resource counter needs to update without the page having to refresh, I’m assuming I would need to use some form of javascript? I’m really not sure haha. It would be great if anyone experienced in this sort of thing can give me an idea about how to go about this, it’s been done in the other websites I’ve just showed you guys, so there must be some way of doing it.

So any and all input is welcome, thanks in advance.

A simple way is to use ajax and update a div every second holding the amount of reso, and every second it calls some backend script to update that users amount (with a time measure to make sure no cheating goes on)
while this is quick and easy, it could put a HUGE strain on the server with so many calls coming in.

A better method is to have the script time it and then use javascript to keep counting up the reso and like every either call to any page (can be a include file anywhere) or if they haven’t changed any pages the javascript will call it every 10secs so the database gets updated.
this way is less heavy on the server but is more hard to maintain and create.

cool, I took one java course a year ago but I haven’t actually learnt any ajax or javascript specifically. Say Gears, would you still be interested in joining my development team? It’s nice learning PHP and dynamic html as I go but the lack of experience can be frustrating sometimes. It would be great to have someone with more experience on the team, for the sake of the site haha. What do you think?

I would love to join the dev team!
I been waiting for you to ask hehe :stuck_out_tongue:

Sponsor our Newsletter | Privacy Policy | Terms of Service