The Admin Network has no native packet for listing individual vehicles, so list_vehicles() sends a "list_vehicles" command over the existing GameScript JSON channel and relies on a companion server-side script to reply with vehicle data via ServerGamescript. Requires subscribing to Gamescript updates (documented in docs/PROTOCOL.md) to receive the reply. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
66 lines
2.1 KiB
Python
66 lines
2.1 KiB
Python
import asyncio
|
|
import logging
|
|
import sys
|
|
import os
|
|
|
|
# Add the lib directory to sys.path so we can import the openttd package
|
|
sys.path.append(os.path.join(os.path.dirname(__file__), 'lib'))
|
|
|
|
from openttd import OpenTTDAdminClient
|
|
from openttd.protocol import AdminUpdateType, AdminUpdateFrequency
|
|
|
|
# Configuration
|
|
SERVER_HOST = "127.0.0.1"
|
|
ADMIN_PORT = 3977
|
|
ADMIN_PASSWORD = "asd"
|
|
|
|
async def run_admin():
|
|
admin = OpenTTDAdminClient(host=SERVER_HOST, port=ADMIN_PORT, admin_name="GeminiAdmin")
|
|
|
|
# Setup callbacks
|
|
def chat_logger(**kwargs):
|
|
print(f">>> [ADMIN CHAT] <{kwargs.get('client_id')}> {kwargs.get('message')}")
|
|
|
|
def console_logger(**kwargs):
|
|
print(f">>> [CONSOLE] [{kwargs.get('origin')}] {kwargs.get('text')}")
|
|
|
|
def gamescript_logger(data):
|
|
print(f">>> [GAMESCRIPT] {data}")
|
|
|
|
admin.on_chat = chat_logger
|
|
admin.on_console = console_logger
|
|
admin.on_gamescript = gamescript_logger
|
|
|
|
try:
|
|
await admin.connect(admin_password=ADMIN_PASSWORD, secure=True)
|
|
await admin.joined.wait()
|
|
print("--- Admin joined ---")
|
|
|
|
# Initial poll
|
|
print("--- Initial Poll ---")
|
|
await admin.poll_companies()
|
|
await admin.poll_clients()
|
|
|
|
# Subscribe to everything
|
|
await admin.update_frequency(AdminUpdateType.ClientInfo, AdminUpdateFrequency.Automatic)
|
|
await admin.update_frequency(AdminUpdateType.CompanyInfo, AdminUpdateFrequency.Automatic)
|
|
await admin.update_frequency(AdminUpdateType.Chat, AdminUpdateFrequency.Automatic)
|
|
await admin.update_frequency(AdminUpdateType.Gamescript, AdminUpdateFrequency.Automatic)
|
|
|
|
print("--- Requesting vehicle info via GameScript ---")
|
|
await admin.list_vehicles()
|
|
|
|
await asyncio.sleep(5)
|
|
print("--- Quitting ---")
|
|
await admin.quit()
|
|
|
|
except Exception as e:
|
|
print(f"!!! Error: {e}")
|
|
|
|
if __name__ == "__main__":
|
|
logging.basicConfig(level=logging.INFO, format='%(levelname)s:%(name)s:%(message)s')
|
|
try:
|
|
asyncio.run(run_admin())
|
|
except KeyboardInterrupt:
|
|
pass
|