Initialized openttd-client repo
This commit is contained in:
63
main.py
Normal file
63
main.py
Normal file
@@ -0,0 +1,63 @@
|
||||
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 OpenTTDClient
|
||||
|
||||
# Configuration
|
||||
SERVER_HOST = "127.0.0.1"
|
||||
SERVER_PORT = 3979
|
||||
SERVER_PASSWORD = "asd"
|
||||
|
||||
# Company configuration
|
||||
COMPANY_ID = 0 # "Én transport"
|
||||
COMPANY_PASSWORD = "asd123"
|
||||
|
||||
async def run_client():
|
||||
# 1. Initialize high-level client
|
||||
username = sys.argv[1] if len(sys.argv) > 1 else "Modular_Joiner"
|
||||
client = OpenTTDClient(host=SERVER_HOST, port=SERVER_PORT, username=username)
|
||||
|
||||
# 2. Setup chat callback (optional)
|
||||
def chat_logger(cid, msg):
|
||||
print(f">>> [CHAT] <{cid}> {msg}")
|
||||
client.on_chat = chat_logger
|
||||
|
||||
try:
|
||||
# 3. Connect and initiate handshake
|
||||
# The client will handle PAKE auth and encryption automatically
|
||||
await client.connect(server_password=SERVER_PASSWORD)
|
||||
|
||||
# 4. Configure company join
|
||||
# This will happen automatically once the handshake is done
|
||||
await client.join_company(company_id=COMPANY_ID, company_password=COMPANY_PASSWORD)
|
||||
|
||||
# 5. Wait for the client to be fully synced (map downloaded, states progressed)
|
||||
print(f"--- Joining as {username}... ---")
|
||||
await client.joined.wait()
|
||||
print(f"--- Successfully joined! Client ID: {client.client_id} ---")
|
||||
|
||||
# 6. Lifecycle management
|
||||
# We wait for either a manual shutdown signal or a 10s timeout
|
||||
try:
|
||||
await asyncio.wait_for(client.shutdown_event.wait(), timeout=10.0)
|
||||
except asyncio.TimeoutError:
|
||||
print("--- Finished 10s stay, exiting gracefully ---")
|
||||
await client.quit()
|
||||
|
||||
except Exception as e:
|
||||
print(f"!!! Error: {e}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Setup global logging
|
||||
logging.basicConfig(level=logging.INFO, format='%(levelname)s:%(name)s:%(message)s')
|
||||
|
||||
# Run the async loop
|
||||
try:
|
||||
asyncio.run(run_client())
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
Reference in New Issue
Block a user