How to: Travian tasks

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
boonika
Forum Newbie
Posts: 18
Joined: Tue May 24, 2005 1:46 pm

How to: Travian tasks

Post by boonika »

Hi,

I don't know if you know travian. Travian (http://www.travian.com) is a browser game (programmed in PHP) where you can build your own village.

I was wondering how they program their building logs.

When a player wants to build something, he pays his recources and then the construction starts. The construction lasts a period of time (depending on the building).

Example.

I build a house: it takes 20 and 10 seconds minutes. 20 min 10 seconds later, I can see the house. Then I can build a threehouse, this takes 7min30sec. How do they program it that exactly after 7min30 sec, the building is made?

I don't think it can be done with a cron job... it would have to be run every second...
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

create table buildings(
   building_id int not null auto_increment primary key,
   building_type_id int not null,
   construction_start timestamp
);
create table building_types (
   building_type_id int not null auto_increment primary key,
   name char(64) not null,
   construction_time int not null unsigned
);
select building_id, (time_to_sec(timediff(now(),construction_start))/construction_time) * 100 as percents_complete 
from buildings
inner join building_types using(building_type_id); -- this query will give you all the buildings with their completion status
The idea is that building status can be calculated once you know the time the construction started and how long it takes to construct a building of this type.
josa
Forum Commoner
Posts: 75
Joined: Mon Jun 24, 2002 4:58 am
Location: Sweden

Post by josa »

The timer is done in client side JavaScript. That combined with some Ajax is what makes it work I think. Backed up with a database of course, like Weirdan said.

/josa
boonika
Forum Newbie
Posts: 18
Joined: Tue May 24, 2005 1:46 pm

Post by boonika »

Well, the datebase thing I understand. It's the timer I would like to know. And is such a timer possible with PHP?
User avatar
The Phoenix
Forum Contributor
Posts: 294
Joined: Fri Oct 06, 2006 8:12 pm

Post by The Phoenix »

boonika wrote:Well, the datebase thing I understand. It's the timer I would like to know. And is such a timer possible with PHP?
It could be that the action of 'create town' goes as follows:

1. Add new db entry for town, with a 'ready at' date in the future
2. Next cron run (every 5 minutes is common for web games), it processes that request, and builds everything that needs to be built, but again sets those items with a "ready at" date
3. View towns only shows items with a ready at date < now. ("SELECT items where ready_at < time()").

That way, cron can run, and you can't see things that are built until the very second - without needing per-second timing from cron.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

It can be any of the above or even a mix of them. The DB way is the first which came to my mind though, as it seems the best to me (even though I didn't mention it).
Post Reply