OpenTTDLib - fetch live game data (UPDATE - VERSION 0.3.2)

OpenTTD is a fully open-sourced reimplementation of TTD, written in C++, boasting improved gameplay and many new features.

Moderator: OpenTTD Developers

maartena
Route Supervisor
Route Supervisor
Posts: 470
Joined: 11 Jul 2003 04:53
Location: California, U.S.A.

Post by maartena »

rav wrote:just remove any die() statements and/or replace them with print()?
That's "just" something you "just" need to know ;) I ain't no php coder :D
Guess what! I got a fever! And the only prescription...... is MORE COWBELL!
dihedral
Tycoon
Tycoon
Posts: 1053
Joined: 14 Feb 2007 17:48

Post by dihedral »

not such a good idea rev!

it will just output way more error messages if you do that ;-)

if( !$openttd->queryServer( $server['host'], $server['port'] ) ){
print( $openttd->getError() );
}

$info = $openttd->getInfo();
$detail = $openttd->getDetail();

as there is no 'else' section to this ifstatement that wrappes everything following the if statement, a print would show the error message, but then the script would contintue trying to assing data to $info and $detail.
getInfo() and getDetail() should at anytime return an array() to satisfy the foreach loops. Idealy if an error occures (i.e. queryServer() returns false)
the script should skip to the next index in $servers, if there is one.

As i just noticed, the script does not handle this part correctly anyway, so i shall fix that, so the script just skips to the next $server index.

thank you for your idea rev.

EDIT: you cann replace the die() with print() followed by a continue, ie:
print( $openttd->getError() );
continue;

@maartena: well done on the edit :-D
User avatar
rav
Traffic Manager
Traffic Manager
Posts: 253
Joined: 17 May 2007 13:36
Location: Netherlands
Contact:

Post by rav »

and if it still doesn't work you can just wrap the whole thing in a try{} catch{} :)
dihedral
Tycoon
Tycoon
Posts: 1053
Joined: 14 Feb 2007 17:48

Post by dihedral »

if the php errors were catchable, yes. usually this is stated along with the error message php outputs, i.e
catchable error foo in file blah on line ##

but it is a nice idea to throw exeptions if something goes wrong, rather than just setting a class internal flag.

i shall try to add that, thank you rev for that idea.
maartena
Route Supervisor
Route Supervisor
Posts: 470
Joined: 11 Jul 2003 04:53
Location: California, U.S.A.

Post by maartena »

the continue; addition works perfectly. Now when I take down a server or it crashes, it simply doesn't display it's information but the information for other servers is still displayed. Perfect solution for me :)

EDIT: Another suggestion I have:

Can you make the Company #'s on the output HTML correspondent with the actual company/player number? For instance, I have a server with 8 companies and 6 players, nicely listed as 1 through 8. I then decide to delete company 3 and 7 because the players have left and the companies weren't even used. Instead of then displaying 1, 2, 4, 5, 6, and 8 as the companies, it now displays company 1 through 6.

For management purposes, and to see who belongs to what company without having to log on to the server itself, it would be great if the company #'s would match up. :)
Guess what! I got a fever! And the only prescription...... is MORE COWBELL!
User avatar
Bilbo
Tycoon
Tycoon
Posts: 1710
Joined: 06 Jun 2007 21:07
Location: Czech Republic

Post by Bilbo »

Just wonder .. will there be querying the master server for server list supported in some future versions, or should I do this myself?
If you need something, do it yourself or it will be never done.

My patches: Extra large maps (1048576 high, 1048576 wide) (FS#1059), Vehicle + Town + Industry console commands (FS#1060), few minor patches (FS#2820, FS#1521, FS#2837, FS#2843), AI debugging facility

Other: Very large ships NewGRF, Bilbo's multiplayer patch pack v5 (for OpenTTD 0.7.3)
t2t2
Engineer
Engineer
Posts: 46
Joined: 08 Jul 2004 13:22
Location: .ee

Post by t2t2 »

Bilbo wrote:Just wonder .. will there be querying the master server for server list supported in some future versions, or should I do this myself?

Code: Select all

<?
$serverpage = file_get_contents('http://www.openttd.org/servers.php');
$serverpage = explode('<table cellspacing="0" cellpadding="4" border="0" class="fullwidth">', $serverpage);
$serverpage = explode('</table>', $serverpage[1]);
$serverpage = $serverpage[0];
print '<table cellspacing="0" cellpadding="4" border="0" class="fullwidth">'. $serverpage .'</table>';
?>
1. Gets HTML code of http://www.openttd.org/servers.php
2. Narrows it down to table of servers
3. Prints output with table starting tags.
Kanibal wrote::twisted: :twisted: Why is illegal stuff always fun :twisted: :twisted:
Refresh page and my avatar colors will change!
dihedral
Tycoon
Tycoon
Posts: 1053
Joined: 14 Feb 2007 17:48

Post by dihedral »

afaik the master server could be queried with udp packets, fetching a list of ip's and port numbers for games, though i dont think there should ever be a reason anybody needs to fetch data from servers that are not their own :-)
User avatar
Bilbo
Tycoon
Tycoon
Posts: 1710
Joined: 06 Jun 2007 21:07
Location: Czech Republic

Post by Bilbo »

t2t2 wrote:
Bilbo wrote:Just wonder .. will there be querying the master server for server list supported in some future versions, or should I do this myself?
...
1. Gets HTML code of http://www.openttd.org/servers.php
2. Narrows it down to table of servers
3. Prints output with table starting tags.
I thought of that too. But then I realized one small glitch. The list contain only server addresses and no ports (and ports often differ). And fetching one page from the server for each server just to get port ... that would be silly and waste of server resources, bandwidth, etc ...
If you need something, do it yourself or it will be never done.

My patches: Extra large maps (1048576 high, 1048576 wide) (FS#1059), Vehicle + Town + Industry console commands (FS#1060), few minor patches (FS#2820, FS#1521, FS#2837, FS#2843), AI debugging facility

Other: Very large ships NewGRF, Bilbo's multiplayer patch pack v5 (for OpenTTD 0.7.3)
User avatar
Bilbo
Tycoon
Tycoon
Posts: 1710
Joined: 06 Jun 2007 21:07
Location: Czech Republic

Post by Bilbo »

dihedral wrote:afaik the master server could be queried with udp packets, fetching a list of ip's and port numbers for games, though i dont think there should ever be a reason anybody needs to fetch data from servers that are not their own :-)
Well, there are reason, like:

I have nicks of my friends. List servers on which they are playing.

Or to filter the list based on some more advanced criteria. The in-game server browser have no filtering options and the window is pretty small ... maybe best will be to fix it in the code, but making some extra PHP thingy is much easier for me :)
If you need something, do it yourself or it will be never done.

My patches: Extra large maps (1048576 high, 1048576 wide) (FS#1059), Vehicle + Town + Industry console commands (FS#1060), few minor patches (FS#2820, FS#1521, FS#2837, FS#2843), AI debugging facility

Other: Very large ships NewGRF, Bilbo's multiplayer patch pack v5 (for OpenTTD 0.7.3)
dihedral
Tycoon
Tycoon
Posts: 1053
Joined: 14 Feb 2007 17:48

Post by dihedral »

it would be better if the master server provided such features, rather than OpenTTDLib fetching details from the master server, then fetching details from each of those servers. this would take way too long to process as well.

so rather than everybody having their own copy of OpenTTDLib that does this kind of trick, it would be a much cleaner job if the master server did this.

i might add the query to the master server, but only after discussing it with TrueLight.

again: personally i do not think that everybody needs to query other peoples servers with OpenTTDLib, just to find a nick name of a player... ignoring the fact for now that a nick name can appear more than once.

the todo list currently includes following:
- query server for new grf data
- send methods for OpenTTDLibPacket class (required for fetching new grf data )
- moving all packet related methods to OpenTTDLibPacket, and making it more like src/network/core/packet.cpp

- xml webservice (though may never become public; primary use for sandra-bullock.co.uk games)

Edit: just so people read this: there is a php bug under windows, currently tested on a 64bit system, with IIS6. problem lies in the usage of the php function stream_select()
VPN-User
Traffic Manager
Traffic Manager
Posts: 136
Joined: 07 Jun 2002 08:43
Location: Radolfzell at Lake of Constance in Germany
Contact:

Post by VPN-User »

There is another bug: Quotations are being escaped which results in company names like 'Foo \"bar\" and stupid' etc.
dihedral
Tycoon
Tycoon
Posts: 1053
Joined: 14 Feb 2007 17:48

Post by dihedral »

thanks - yes, it's a just a simple function call in openttd.class.php
it will be removed for the next release
VPN-User
Traffic Manager
Traffic Manager
Posts: 136
Joined: 07 Jun 2002 08:43
Location: Radolfzell at Lake of Constance in Germany
Contact:

Post by VPN-User »

I also have a feature request: What about a possibility to get the "restart_date" of the server?
dihedral
Tycoon
Tycoon
Posts: 1053
Joined: 14 Feb 2007 17:48

Post by dihedral »

currently this is not included in the info packet the server returns. it the devs should decide to upgrade this info packet (which i am hoping for) they might also add the restart date, though at the moment there is no way for me to fetch these details.

the only currently available data that OpenTTDLib does not fetch at the moment is details on loaded new grf's.

i am working on getting this included
VPN-User
Traffic Manager
Traffic Manager
Posts: 136
Joined: 07 Jun 2002 08:43
Location: Radolfzell at Lake of Constance in Germany
Contact:

Post by VPN-User »

Anyway, if anyone is interested in how I used OpenTTDLib: http://tot.kik-server.de/ :wink:
dihedral
Tycoon
Tycoon
Posts: 1053
Joined: 14 Feb 2007 17:48

Post by dihedral »

thanks - i like seeing it in use elsewhere :-)
dihedral
Tycoon
Tycoon
Posts: 1053
Joined: 14 Feb 2007 17:48

v 0.2 released

Post by dihedral »

just finished work on v 0.2

this release is mainly getting me a step closser to adding new GRF support
though a few things have changed:

- OpenTTDLib now throws an exception (OpenTTDLibException) rather than returning false
- added images for station and vehicle types (see http://openttdlib.dihedral.de/wiki/LiveDemo)
- removed escaping from company and player names (the disturbing backslash '\')
- more dynamic option setting (e.g. timeout, conversion of netlang, mapset, etc)
...

when upgrading from v. 0.1.1 please pay close attention to the docs and example.php and example2.php

NOTE: there is no backwards compatibility!

downloads available at http://openttdlib.dihedral.de/wiki/Download
or from the first post of this thread:
http://www.tt-forums.net/viewtopic.php? ... 40#p599640
dihedral
Tycoon
Tycoon
Posts: 1053
Joined: 14 Feb 2007 17:48

Re: OpenTTDLib - fetch live game data (UPDATE - v 0.2)

Post by dihedral »

release of version 0.3

changelog:

Code: Select all

change	$info $detail and $newgrf are now handled as objects
		 can be retreived as arrays if need be (see documentation)
added   caching, currently only newgrf data is cached, minimizing the
		 amount of newgrf requests sent to the server
added   support for fetching newgrf data, packet types:
		 UDP_CLIENT_GET_NEWGRFS and UDP_SERVER_NEWGRFS
fixed   conversion of vehicle and station types was brocken
download at the first post of this topic or at http://openttdlib.dihedral.de
maartena
Route Supervisor
Route Supervisor
Posts: 470
Joined: 11 Jul 2003 04:53
Location: California, U.S.A.

Re: OpenTTDLib - fetch live game data (UPDATE - VERSION 0.3)

Post by maartena »

Hey :)

I just happen to have reinstalled my linux box. I installed OpenSUSE 10.3, got apache running, installed php5, installed PEAR and used the pear command from a terminal to install the sigma.php in the right spot.... but when you click on the link in my sig it doesn't work. My guess is that I need to put an include somewhere for php to know PEAR is there or something, bu i haven't been able to figure it out yet. Any ideas?
Guess what! I got a fever! And the only prescription...... is MORE COWBELL!
Post Reply

Return to “General OpenTTD”

Who is online

Users browsing this forum: Amazon [Bot], Bing [Bot] and 14 guests