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

okdewit
Engineer
Engineer
Posts: 54
Joined: 30 Sep 2007 21:55

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

Post by okdewit »

Perfect! :bow:
Image
dihedral
Tycoon
Tycoon
Posts: 1053
Joined: 14 Feb 2007 17:48

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

Post by dihedral »

if you need any further help: read the docs, if that does not provide enough help, feel free to ask ^^
okdewit
Engineer
Engineer
Posts: 54
Joined: 30 Sep 2007 21:55

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

Post by okdewit »

The documentation is pretty clear even for a PHP-newbie like me :)

One question though:
When I get a return value from OpenTTDLib::getDetail(), the [player][$x][vehicles] array is perfectly reporting the amount of trains on my server etc., but the [allclients] and [player][$x][client] arrays remain empty...

Maybe i'm doing something wrong, but there are 3 clients connected, and [clients_on] from getInfo() IS actually reporting the correct number of clients.
I would love to be able to display client names per company and per server though.
Image
dihedral
Tycoon
Tycoon
Posts: 1053
Joined: 14 Feb 2007 17:48

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

Post by dihedral »

that is because the server no longer provides this information via udp packets ;-)
okdewit
Engineer
Engineer
Posts: 54
Joined: 30 Sep 2007 21:55

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

Post by okdewit »

Ah, too bad.

Very useful library anyway, im still trying to combine it with some (of course quite unsafe, but its private servers on a LAN) exec() functions on the dedicated server so i'm able to generate, start and stop new servers including the ability to upload scenery and saves to use as a new dedicated server. Lots of problems still, since I use "openttd -D -f" to fork it to a background process so PHP can read the output from a file, but that prevents me from actually interacting with the running server (rcon etc.)

This library gives quite a few cool stats about already running servers though, only need to find a way to create savegames from the server and send&read chat messages.
Image
dihedral
Tycoon
Tycoon
Posts: 1053
Joined: 14 Feb 2007 17:48

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

Post by dihedral »

i would sugest to make a java webapp instead of using php for that.
perhaps a useful hint would be to use fifo files

Code: Select all

mkfifo in
mkfifo out
mkfifo err
and then use those files when starting openttd:

Code: Select all

openttd -D -f 1>out 2>err <in
then you can access those fifo files (out and err for reading only, in for writing) and still have access to the console - however, for this you do not have to run openttd as an apache child, it would work perfectly find as long as apache has read/write access to the files accordingly.
note though, when writing to the "in" pipe, to send a \n after a command but no EOF, else "in" will not remain usable after.
okdewit
Engineer
Engineer
Posts: 54
Joined: 30 Sep 2007 21:55

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

Post by okdewit »

I was trying to use proc_open() with pipes to launch openttd and it seems to work, but the problem seems to be that PHP runs "/dir/dir/openttd -D" as the user www-data, and www-data is not allowed to start programs that listen on ports, openttd needs to be launched with sudo... But, www-data can not execute sudo since it has no password. :(

Edit: ah, solved by adding www-data with no password option to the sudoers file :D
Of course, stupid thing to do, but as I said the webserver is only available in a trusted LAN anyway :)

Edit2: proc_open is not going to work of course, because when I close the browser and reopen it again PHP has no idea where to look for in/output with the server...
The solution with FIFO files doesnt seem to work either, openttd does not start when appending the "-D -f 1>out 2>err <in"
Image
dihedral
Tycoon
Tycoon
Posts: 1053
Joined: 14 Feb 2007 17:48

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

Post by dihedral »

oh, correct, using fifo's does not allow forking to the background - so, basically: start openttd in a screen session (e.g) and use the fifo's, then use php to connect to the fifo's
okdewit
Engineer
Engineer
Posts: 54
Joined: 30 Sep 2007 21:55

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

Post by okdewit »

Doesn't matter if I execute it with or without -f, with or without -D, with or without sudo...
As soon as I add "1>out 2>err <in" the console just responds/hangs with a newline and no process is started.
Image
dihedral
Tycoon
Tycoon
Posts: 1053
Joined: 14 Feb 2007 17:48

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

Post by dihedral »

yes it would, as all output is directed to those files, and you need to check them in order to see what's going on!
note: you are redirecting the programs output and input!! what would you expect!

if nothing is found in the out or err file, try

Code: Select all

echo -n "\n" >> in
might help. however, generally speaking, all output will be found in "out" and "err"
Eddi
Tycoon
Tycoon
Posts: 8258
Joined: 17 Jan 2007 00:14

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

Post by Eddi »

maybe you should try "tee" to print the output into the file and also the console...
okdewit
Engineer
Engineer
Posts: 54
Joined: 30 Sep 2007 21:55

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

Post by okdewit »

dihedral wrote:yes it would, as all output is directed to those files, and you need to check them in order to see what's going on!
note: you are redirecting the programs output and input!! what would you expect!

if nothing is found in the out or err file, try
echo -n "\n" >> in
might help. however, generally speaking, all output will be found in "out" and "err"
The process won't even start when using either stdin or stderr, only stdout redirection is accepted, but even if I run:

Code: Select all

./openttd -D 1>file.txt
It doesn't produce output into a file, it just creates an empty file and puts the output in the terminal, while "ls -l 1>file.txt" works perfectly on my system.
Are you sure openttd can work with stdin/stdout redirection, and the file descriptor numbers are right?
I don't know much about this stuff, so excuse me if i'm doing something stupid, but I would really love to understand more about how openttd works and if/how it can be integrated/wrapped into other systems :)

Edit:
btw, if I do
"./openttd -D 1>out"
it just puts the cursor on the next line, no process is created and nothing is running, but if I do
"./openttd -D 1>file.txt"
it creates a process, leaves file.txt empty, prints the output in the terminal AND "lsof -p PID" results in:

Code: Select all

openttd PID root    0u   CHR   136,2      0t0      5 /dev/pts/2
openttd PID root    1w   REG     8,6        0 262319 /dir/to/ottd/file.txt
openttd PID root    2u   CHR   136,2      0t0      5 /dev/pts/2
While output is still printed in the terminal, not to the file!
Image
dihedral
Tycoon
Tycoon
Posts: 1053
Joined: 14 Feb 2007 17:48

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

Post by dihedral »

that is because a bunch of output goes to the descriptor 2 and not 1!
you can try with a simple ./openttd 2>&1 >file.txt ;-)
Telk
Engineer
Engineer
Posts: 64
Joined: 05 May 2008 14:04
Location: Seoul, South Korea
Contact:

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

Post by Telk »

It's been a long time after the last replies.
Would you mind if I ask you a question for OpenTTDLib 0.3.2?

My host server is using PHP 5.2.14 (based on Linux),
And no --enable-bcmath option.

But there's an another PHP class instead of bcmath functions which exactly work like bcmaths.
(I will call it as 'Math' class.)

I found all bcadd(), bcmul(), bcpow() and changed into Math::add(), Math::mul(), Math::pow() in OpenttdLibPacket.php file.
But when I open example.php, it says 'sending packet failed'.
(The server what I setted in source file is 'ottd.iruis.net:3979'.)

http://www.korct.com/php/ottdlib0.3.2/example.php


Could you tell me what should I do or what I missed?


Additional: Here's my PHP Configure Command of phpinfo()

Code: Select all

'./configure'
'--with-mysql=/mysql'
'--with-apxs2=/blah/blah/apxs'
'--with-config-file-path=/blah/blah'
'--enable-safe-mode=/blah/blah'
'--enable-ftp'
'--enable-sockets'
'--with-mcrypt'
'--with-gd'
'--with-jpeg-dir=/usr/local/lib/'
'--with-png-dir=/usr/local/lib/'
'--with-freetype-dir=/blah/blah/freetype2/freetype/'
'--with-zlib'
'--with-iconv'
'--enable-exif'
'--enable-mbstring'
'--with-mysqli=/blah/blah/mysql_config'
'--with-openssl'
'--with-libxml-dir=/blah/blah/libxml2/libxml/'
'--enable-soap'
'--with-curl'
'--with-pdo-mysql'
'--with-xmlrpc'
'--enable-zip'
Image
- OpenTTD/OpenRCT2/Parkitect Korean Translator
- TELKLAND site manager, the korean website of OpenRCT2/OpenTTD (https://telk.kr)
dihedral
Tycoon
Tycoon
Posts: 1053
Joined: 14 Feb 2007 17:48

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

Post by dihedral »

Code: Select all

test:
bad packet size
#0 /home/01/korct31/www/php/ottdlib0.3.2/includes/OpenTTDLib.php(1630): OpenTTDLib->RecvData()
#1 /home/01/korct31/www/php/ottdlib0.3.2/includes/OpenTTDLib.php(1679): OpenTTDLib->waitForServerResponse()
#2 /home/01/korct31/www/php/ottdlib0.3.2/example.php(19): OpenTTDLib->queryServer('127.0.0.1', 3979, 1)
#3 {main}You are querying localhost according to the error i see
I only see you sending a query to localhost. But well done on implementing the other class instead of bcmath.
Telk
Engineer
Engineer
Posts: 64
Joined: 05 May 2008 14:04
Location: Seoul, South Korea
Contact:

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

Post by Telk »

Does the charset of my site can be a problem to control packets?
My site uses UTF-8 and now I can use bcmath.
Image
- OpenTTD/OpenRCT2/Parkitect Korean Translator
- TELKLAND site manager, the korean website of OpenRCT2/OpenTTD (https://telk.kr)
dihedral
Tycoon
Tycoon
Posts: 1053
Joined: 14 Feb 2007 17:48

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

Post by dihedral »

Either openttd is not running, there is a firewall, possibly selinux is in the way, or you are accessing the wrong host.
Telk
Engineer
Engineer
Posts: 64
Joined: 05 May 2008 14:04
Location: Seoul, South Korea
Contact:

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

Post by Telk »

dihedral wrote:Either openttd is not running, there is a firewall, possibly selinux is in the way, or you are accessing the wrong host.
When I tested by modifying sendPacket() in OpenttdLib.php,
It didn't return the byte length from fwrite() when querying ottd.iruis.net:3978.
When querying localhost, fwrite returns an integer more than 0 but errors bad packet.

Do you think it comes from the firewall?
Image
- OpenTTD/OpenRCT2/Parkitect Korean Translator
- TELKLAND site manager, the korean website of OpenRCT2/OpenTTD (https://telk.kr)
dihedral
Tycoon
Tycoon
Posts: 1053
Joined: 14 Feb 2007 17:48

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

Post by dihedral »

Just check what i have mentioned!
kamikazefish
Engineer
Engineer
Posts: 2
Joined: 22 Mar 2012 15:18

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

Post by kamikazefish »

I made a simple port to node.js.
It doesn't have all functions the PHP version has.

You can use it with node.js or as a command-line program. Just install it with npm with the --global flag.
It returns a json representation of the data.

nodejs_openttdlib
Post Reply

Return to “General OpenTTD”

Who is online

Users browsing this forum: Google [Bot] and 8 guests