Skip to main content

Python library to communicate with OpenTTD Admin port

Project description

pyOpenTTDAdmin

pyOpenTTDAdmin is a Python library designed to facilitate communication with OpenTTD's Admin port. OpenTTD (Open Transport Tycoon Deluxe) is an open-source simulation game where players manage a transport company.

This library enables developers to interact with an OpenTTD server programmatically, allowing them to perform various administrative tasks and receive real-time updates from the game server.

Features

  • Authentication: Authenticate with an OpenTTD server using the provided password.
  • Packet Handling: Receive and parse packets from the server, allowing developers to react to different events and messages.
  • Sending Commands: Send commands to the OpenTTD server, such as chat messages, remote console commands, and more.
  • Subscription to Updates: Subscribe to various types of updates from the server, including chat messages, client information, company data, and more.
  • Flexible Usage: The library provides a flexible framework for building custom applications that interact with OpenTTD servers.

Usage

The basic usage of pyOpenTTDAdmin involves creating an instance of the Admin class, subscribing to updates from the server, and handling the received packets.

Example code for an echo bot can be found here:

from pyopenttdadmin import Admin, AdminUpdateType, openttdpacket

# Set the IP address and port number for connection
ip_address = "127.0.0.1"
port_number = 3977

# Instantiate the Admin class and establish connection to the server
admin = Admin(ip = ip_address, port = port_number)
admin.login("pyOpenTTDAdmin", password = "toor") # assuming the password is "toor"

# Subscribe to receive chat updates
admin.subscribe(AdminUpdateType.CHAT)

# Print chat packets
@admin.add_handler(openttdpacket.ChatPacket)
def chat_packet(admin: Admin, packet: openttdpacket.ChatPacket):
    print(f'ID: {packet.id} Message: {packet.message}')

# Echo chat
@admin.add_handler(openttdpacket.ChatPacket)
def echo_chat(admin: Admin, packet: openttdpacket.ChatPacket):
    admin.send_global(packet.message)

# Run admin
admin.run()

pyOpenTTDAdmin also support async/await syntax:

import asyncio
from aiopyopenttdadmin import Admin, AdminUpdateType, openttdpacket

# Set the IP address and port number for connection
ip_address = "127.0.0.1"
port_number = 3977

async def main():
    # Instantiate the Admin class and establish connection to the server
    admin = Admin(ip = ip_address, port = port_number)
    await admin.login("pyOpenTTDAdmin", password = "toor") # assuming the password is "toor"

    # Subscribe to receive chat updates
    await admin.subscribe(AdminUpdateType.CHAT)

    # Print chat packets
    @admin.add_handler(openttdpacket.ChatPacket)
    async def chat_packet(admin: Admin, packet: openttdpacket.ChatPacket):
        print(f'ID: {packet.id} Message: {packet.message}')

    # Echo chat
    @admin.add_handler(openttdpacket.ChatPacket)
    async def echo_chat(admin: Admin, packet: openttdpacket.ChatPacket):
        await admin.send_global(packet.message)

    # Run admin
    print("running")
    await admin.run()


if __name__ == "__main__":
    asyncio.run(main())

Available Subscribe Types and Packet Types

The following are the available subscribe types that can be used with the library:

  • DATE: Updates about the date of the game.
  • CLIENT_INFO: Updates about the information of clients.
  • COMPANY_INFO: Updates about the generic information of companies.
  • COMPANY_ECONOMY: Updates about the economy of companies.
  • COMPANY_STATS: Updates about the statistics of companies.
  • CHAT: The admin would like to have chat messages.
  • CONSOLE: The admin would like to have console messages.
  • CMD_NAMES: The admin would like a list of all DoCommand names.
  • CMD_LOGGING: The admin would like to have DoCommand information.
  • GAMESCRIPT: The admin would like to have gamescript messages.

Here you find a list of all the packets that can be received from the server:

  • Errorpacket: The server tells the admin an error has occurred.
  • AdminJoinPacket: The admin announces and authenticates itself to the server.
  • ProtocolPacket: The server tells the admin its protocol version.
  • WelcomePacket: The server welcomes the admin to a game.
  • NewGamePacket: The server tells the admin its going to start a new game.
  • ShutdownPacket: The server tells the admin its shutting down.
  • DatePacket: The server tells the admin what the current game date is.
  • ClientJoinPacket: The server tells the admin that a client has joined.
  • ClientInfoPacket: The server gives the admin information about a client.
  • ClientUpdatePacket: The server gives the admin an information update on a client.
  • ClientQuitPacket: The server tells the admin that a client quit.
  • ClientErrorPacket: The server tells the admin that a client caused an error.
  • CompanyNewPacket: The server tells the admin that a new company has started.
  • CompanyInfoPacket: The server gives the admin information about a company.
  • CompanyUpdatePacket: The server gives the admin an information update on a company.
  • CompanyRemovePacket: The server tells the admin that a company has been removed.
  • CompanyEconomyPacket: The server gives the admin some economy related company information.
  • CompanyStatsPacket: The server gives the admin some statistics about a company.
  • ChatPacket: The server received a chat message and relays it.
  • RconEndPacket: The server indicates that the remote console command has completed.
  • RconPacket: The server's reply to a remote console command.
  • ConsolePacket: The server gives the admin the data that got printed to its console.
  • GamescriptPacket: The server gives the admin information from the GameScript in JSON.
  • CmdNamesPacket: The server sends out the names of the DoCommands to the admins.
  • CmdLoggingPacket: The server gives the admin copies of incoming command packets.
  • AdminRconPacket: The admin sends a remote console command.
  • AdminChatPacket: The admin sends a chat message to be distributed.
  • AdminSubscribePacket: The admin tells the server the update frequency of a particular piece of information.

Contributing

Contributions to pyOpenTTDAdmin are welcome! If you find any issues or have ideas for improvements, feel free to open an issue or submit a pull request on GitHub.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyopenttdadmin-1.0.3.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyopenttdadmin-1.0.3-py3-none-any.whl (15.2 kB view details)

Uploaded Python 3

File details

Details for the file pyopenttdadmin-1.0.3.tar.gz.

File metadata

  • Download URL: pyopenttdadmin-1.0.3.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.9

File hashes

Hashes for pyopenttdadmin-1.0.3.tar.gz
Algorithm Hash digest
SHA256 be84f57f715414a57a13f4f7d442bcbc18689051899eec09130da50425a24634
MD5 a2854765d6389a0452eb2144dad8ab2f
BLAKE2b-256 9174313613dd1937d37bbca9f9cedb4f1cda780ea0dc0f27721a04e3481eb4fc

See more details on using hashes here.

File details

Details for the file pyopenttdadmin-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: pyopenttdadmin-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 15.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.9

File hashes

Hashes for pyopenttdadmin-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 b1d1e8b57e20865f0c248acdbba6c09732e35850d2100d2af8c300465ea9f573
MD5 9ded8e8c02d4a09619476879aad4237b
BLAKE2b-256 a14b205d5bcf384df9c3551fe7829c913434b319ca609bee338cbd3ea3a4a6da

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page