Let Clients pause and unpause the game

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

Moderator: OpenTTD Developers

Post Reply
raycluster
Engineer
Engineer
Posts: 5
Joined: 18 May 2015 20:06

Let Clients pause and unpause the game

Post by raycluster »

Hey folks :mrgreen:

I'm setting up a small private server for my friends and me. We're not playing competitively, so I want everyone to be able to pause and unpause the game. How can I do that? Awesome would be, if everyone could just type "!pause" into chat, and the game pauses. Or, even better, the pause button is not greyed out. rcon is a really terrible way of managing the pause state ?(

The game is running as a dedicated server on a small rented virtual machine, with Ubuntu 14.10 installed on it. I have full root rights.
Eddi
Tycoon
Tycoon
Posts: 8289
Joined: 17 Jan 2007 00:14

Re: Let Clients pause and unpause the game

Post by Eddi »

you need something that listens on the admin port, that can listen to the chat and issue pause/unpause commands

like joan or libottdadmin

or you make a patch that un-greys the pause button, and sends the appropriate network commands to the server. preferably with a setting to allow/disallow clients to (un)pause
raycluster
Engineer
Engineer
Posts: 5
Joined: 18 May 2015 20:06

Re: Let Clients pause and unpause the game

Post by raycluster »

Ok, I thought so. Unfortunately I'm not too experienced with python, and I haven't found a good, newb friendly manual for libottdadmin2. Could you maybe give me a short code snippet that illustrates the basic the use of the library? Doesn't have to be a solution for my whole problem, but maybe a snippet on how to connect to the admin interface, and receive and send data?

I tried

Code: Select all

from libottdadmin2 import *
from libottdadmin2.adminconnection import *

A = AdminConnection();
A.port = 3977
A.host = '127.0.0.1'
A.password = 'blabla'
A.connect()
but the last line only gives me back the value 'false' ;( It doesn't even say that it tries to connect, just 'false'
Eddi
Tycoon
Tycoon
Posts: 8289
Joined: 17 Jan 2007 00:14

Re: Let Clients pause and unpause the game

Post by Eddi »

i'm not experienced with this, but my guess is that connection failed because
  • openttd is not running
  • the admin port was not enabled in openttd.cfg (this is separate from rcon)
  • the firewall is blocking access
  • ...
raycluster
Engineer
Engineer
Posts: 5
Joined: 18 May 2015 20:06

Re: Let Clients pause and unpause the game

Post by raycluster »

Ooops, didn't pay attention to the openttd.cfg file. When restarting the server, it wrote it current settings to it, overwriting my admin password. :oops:

I can connect now to the interface, but now I'm really lost. How can I receive chat and send pause commands? The source code tells me nothing anymore. I tried

Code: Select all

from libottdadmin2 import *
from libottdadmin2.adminconnection import *
from libottdadmin2.client import *

A = AdminConnection();
A.port = 3977
A.host = '127.0.0.1'
A.password = 'blabla'
A.connect()

C = AdminClient(A)

but this gives me the error

Code: Select all

Traceback (most recent call last):
  File "bot.py", line 12, in <module> 
    C = AdminClient(A) 
TypeError: __init__() takes exactly 1 argument (2 given)
When I do not pass the Client the connection, no exception is thrown, but it doesn't make sense to me that the connection is not needed. After all, the poll command doesn't work either way.
Also, I don't know how to go from there, I've never used event handlers.

I really could use an example :(
raycluster
Engineer
Engineer
Posts: 5
Joined: 18 May 2015 20:06

Re: Let Clients pause and unpause the game

Post by raycluster »

Ok, I mashed together a code that works:

Code: Select all

from libottdadmin2 import *
from libottdadmin2.adminconnection import *
from libottdadmin2.client import *
from libottdadmin2.enums import *
import time

A = AdminConnection();
A.port = 3977
A.name = 'myBOT'
A.host = '127.0.0.1'
A.password = 'PW'
A.connect();

A.send_packet(AdminUpdateFrequency, updateType = UpdateType.CHAT,updateFreq= UpdateFrequency.AUTOMATIC)
while(True):
	time.sleep(0.1)
        P = A.recv_packet()
	try:
		M =  P[1]["message"]
		print M
		if(M=='up' or M=='unpause'):
			A.send_packet(AdminRcon, command = "unpause")
		if(M=='p' or M=='pause'):
			A.send_packet(AdminRcon, command = "pause")
	except:
		True
It's not pretty, but it does the job. :mrgreen:
Any suggestions on how to improve on it?
Eddi
Tycoon
Tycoon
Posts: 8289
Joined: 17 Jan 2007 00:14

Re: Let Clients pause and unpause the game

Post by Eddi »

raycluster wrote:

Code: Select all

	except:
		True
*shudder*
first of all, a catch-all-except is a horrible nightmare if you actually need to debug stuff. and secondly, the command you are looking for here is "pass", not "True"
raycluster
Engineer
Engineer
Posts: 5
Joined: 18 May 2015 20:06

Re: Let Clients pause and unpause the game

Post by raycluster »

Yeah, I know that one shouldn't catch all exceptions, and I certainly wouldn't do that in a real project. It was just much more convenient here, since there are lots of different packet type coming in. Didn't feel like parsing it correctly :oops:
Post Reply

Return to “General OpenTTD”

Who is online

Users browsing this forum: No registered users and 49 guests