Page 1 of 1

How to: Travian tasks

Posted: Mon Sep 03, 2007 2:07 pm
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...

Posted: Mon Sep 03, 2007 2:14 pm
by Oren

Posted: Mon Sep 03, 2007 2:38 pm
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.

Posted: Mon Sep 03, 2007 2:52 pm
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

Posted: Mon Sep 03, 2007 3:16 pm
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?

Posted: Mon Sep 03, 2007 3:22 pm
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.

Posted: Mon Sep 03, 2007 4:23 pm
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).