Add admin port support with other major refactorations
This commit is contained in:
65
main_admin.py
Normal file
65
main_admin.py
Normal file
@@ -0,0 +1,65 @@
|
||||
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.send_gamescript({"command": "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
|
||||
Reference in New Issue
Block a user