Skip to content

Commit

Permalink
Begin conversion to asyncio communication
Browse files Browse the repository at this point in the history
  • Loading branch information
danmackey committed Aug 31, 2023
1 parent c847eab commit 83126b6
Show file tree
Hide file tree
Showing 16 changed files with 3,069 additions and 1 deletion.
86 changes: 85 additions & 1 deletion ror_server_bot/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,85 @@
__version__ = '0.0.0'
import logging
import sys
from logging import handlers
from pathlib import Path

from devtools import PrettyFormat
from rich.logging import RichHandler

__version__ = '0.0.0'

RORNET_VERSION = 'RoRnet_2.44'

PROJECT_DIRECTORY = Path(__file__).parent

TRUCK_TO_NAME_FILE = Path('truck_to_name.json')


class PFormat(PrettyFormat):
def _format_str_bytes(
self,
value: str | bytes,
value_repr: str,
indent_current: int,
indent_new: int
) -> None:
if isinstance(value, bytes):
value = value.replace(b'\x00', b'')
return super()._format_str_bytes(
value,
value_repr,
indent_current,
indent_new
)


pformat = PFormat(indent_step=2)

stream_handler = logging.StreamHandler(sys.stdout)
file_handler = handlers.RotatingFileHandler(
filename='ror_server_bot.log',
mode='at',
maxBytes=1024*1024*16,
backupCount=5,
encoding='utf-8'
)
rich_handler = RichHandler(
omit_repeated_times=False,
keywords=[
'[CHAT]',
'[EMIT]'
'[GAME_CMD]',
'[HEAD]',
'[PCKT]',
'[PRIV]',
'[RECV]',
'[SEND]',
]
)

stream_fmt = logging.Formatter(
fmt='{asctime} | {levelname} | {filename}:{lineno} | {message}',
style='{'
)
file_fmt = logging.Formatter(
fmt='{asctime} | {levelname} | {filename}:{lineno} | {message}',
style='{'
)
rich_fmt = logging.Formatter(fmt='{message}', style='{')

msec_fmt = '%s.%04d'
stream_fmt.default_msec_format = msec_fmt
file_fmt.default_msec_format = msec_fmt

stream_handler.setFormatter(stream_fmt)
stream_handler.setLevel(logging.INFO)
file_handler.setFormatter(file_fmt)
file_handler.setLevel(logging.DEBUG)
rich_handler.setFormatter(rich_fmt)
rich_handler.setLevel(logging.DEBUG)


logging.basicConfig(
level=logging.DEBUG,
handlers=[file_handler, rich_handler]
)
46 changes: 46 additions & 0 deletions ror_server_bot/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import asyncio
import logging

import discord

from .ror_bot import RoRClient, RoRClientConfig

logger = logging.getLogger(__name__)

if __name__ == '__main__':
def start():
class DiscordClient:
pass

intents = discord.Intents.default()
intents.message_content = True

client = DiscordClient(intents=intents)
logger.warning(
'Expect a slowdown when requesting guild information from Discord!'
)
client.run(client.config.discord_bot_token)

config = RoRClientConfig(
id='1',
enabled=True,
server=RoRClientConfig.ServerConfig(
host='10.90.1.64',
port=12000,
password=''
),
user=RoRClientConfig.UserConfig(
token=''
),
discord_channel_id=-1,
announcements=RoRClientConfig.Announcements(),
reconnection_interval=1,
reconnection_tries=3,
)

async def main():
async with RoRClient(config):
while True:
await asyncio.sleep(0.1)

asyncio.run(main())
7 changes: 7 additions & 0 deletions ror_server_bot/ror_bot/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from .models import RoRClientConfig
from .ror_client import RoRClient

__all__ = [
'RoRClientConfig',
'RoRClient',
]
226 changes: 226 additions & 0 deletions ror_server_bot/ror_bot/enums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
from enum import auto, Enum, IntEnum


# names from https://www.color-name.com/
class PlayerColor(Enum):
"""The color assigned to each player."""

#! DO NOT REORDER
GREEN = "#00CC00"
BLUE = "#0066B3"
ORANGE = "#FF8000"
YELLOW = "#FFCC00"
LIME = "#CCFF00"
RED = "#FF0000"
GRAY = "#808080"
DARK_GREEN = "#008F00"
WINDSOR_TAN = "#B35A00"
LIGHT_GOLD = "#B38F00"
APPLE_GREEN = "#8FB300"
UE_RED = "#B30000"
DARK_GRAY = "#BEBEBE"
LIGHT_GREEN = "#80FF80"
LIGHT_SKY_BLUE = "#80C9FF"
MAC_AND_CHEESE = "#FFC080"
YELLOW_CRAYOLA = "#FFE680"
LAVENDER_FLORAL = "#AA80FF"
ELECTRIC_PINK = "#EE00CC"
CONGO_PINK = "#FF8080"
BRONZE_YELLOW = "#666600"
BRILLIANT_LAVENDER = "#FFBFFF"
SEA_GREEN = "#00FFCC"
WILD_ORCHID = "#CC6699"
DARK_YELLOW = "#999900"


class Color(Enum):
BLACK = "#000000"
GREY = "#999999"
RED = "#FF0000"
YELLOW = "#FFFF00"
WHITE = "#FFFFFF"
CYAN = "#00FFFF"
BLUE = "#0000FF"
GREEN = "#00FF00"
MAGENTA = "#FF00FF"
COMMAND = "#941E8D"
WHISPER = "#967417"
SCRIPT = "#32436F"


class ActorType(Enum):
TRUCK = 'truck'
CAR = 'car'
LOAD = 'load'
AIRPLANE = 'airplane'
BOAT = 'boat'
TRAILER = 'trailer'
TRAIN = 'train'
FIXED = 'fixed'


class MessageType(IntEnum):
HELLO = 1025
"""Client sends its version as the first message."""

# Hello Responses
SERVER_FULL = auto()
"""Server is full."""
WRONG_PASSWORD = auto()
"""Wrong password."""
WRONG_VERSION = auto()
"""Wrong version."""
BANNED = auto()
"""Client not allowed to join (banned)."""
WELCOME = auto()
"""Client accepted."""

# Technical
SERVER_VERSION = auto()
"""Server sends its version."""
SERVER_SETTINGS = auto()
"""Server sends client the terrain name."""
USER_INFO = auto()
"""User data that is sent from the server to clients."""
MASTER_SERVER_INFO = auto()
"""Server sends master server info."""
NET_QUALITY = auto()
"""Server sends network quality information."""

# Gameplay
GAME_CMD = auto()
"""Script message. Can be sent in both directions."""
USER_JOIN = auto()
"""New user joined."""
USER_LEAVE = auto()
"""User leaves."""
CHAT = auto()
"""Chat line in UTF8 encoding."""
PRIVATE_CHAT = auto()
"""Private chat line in UTF8 encoding."""

# Stream Functions
STREAM_REGISTER = auto()
"""Create new stream."""
STREAM_REGISTER_RESULT = auto()
"""Result of a stream creation."""
STREAM_UNREGISTER = auto()
"""Remove stream."""
STREAM_DATA = auto()
"""Stream data."""
STREAM_DATA_DISCARDABLE = auto()
"""Stream data that is allowed to be discarded."""

# Legacy
USER_INFO_LEGACY = 1003
"""Wrong version."""


class StreamType(IntEnum):
ACTOR = 0
CHARACTER = 1
AI = 2
CHAT = 3


class AuthLevels(IntEnum):
NONE = 0
"""no authentication"""
ADMIN = 1
"""admin on the server"""
RANKED = 2
"""ranked status"""
MOD = 4
"""moderator status"""
BOT = 8
"""bot status"""
BANNED = 16
"""banned"""

@classmethod
def get_auth_str(cls, auth: 'AuthLevels') -> str:
auth_str = ''
if auth is AuthLevels.NONE:
auth_str = ''
if auth is AuthLevels.ADMIN:
auth_str = 'A'
if auth is AuthLevels.MOD:
auth_str = 'M'
if auth is AuthLevels.RANKED:
auth_str = 'R'
if auth is AuthLevels.BOT:
auth_str = 'B'
if auth is AuthLevels.BANNED:
auth_str = 'X'
return auth_str

@property
def auth_str(self) -> str:
return self.get_auth_str(self)


class NetMask(IntEnum):
HORN = 1
"""Horn is in use."""
POLICE_AUDIO = auto()
"""Police siren is on."""
PARTICLE = auto()
"""Custom particles are on."""
PARKING_BRAKE = auto()
"""Parking brake is on."""
TRACTION_CONTROL_ACTIVE = auto()
"""Traction control is on."""
ANTI_LOCK_BRAKES_ACTIVE = auto()
"""Anti-lock brakes are on."""
ENGINE_CONTACT = auto()
"""Ignition is on."""
ENGINE_RUN = auto()
"""Engine is running."""
ENGINE_MODE_AUTOMATIC = auto()
"""Using automatic transmission."""
ENGINE_MODE_SEMIAUTO = auto()
"""Using semi-automatic transmission."""
ENGINE_MODE_MANUAL = auto()
"""Using manual transmission."""
ENGINE_MODE_MANUAL_STICK = auto()
"""Using manual transmission with stick."""
ENGINE_MODE_MANUAL_RANGES = auto()
"""Using manual transmission with ranges."""


class LightMask(IntEnum):
CUSTOM_LIGHT_1 = 1
CUSTOM_LIGHT_2 = auto()
CUSTOM_LIGHT_3 = auto()
CUSTOM_LIGHT_4 = auto()
CUSTOM_LIGHT_5 = auto()
CUSTOM_LIGHT_6 = auto()
CUSTOM_LIGHT_7 = auto()
CUSTOM_LIGHT_8 = auto()
CUSTOM_LIGHT_9 = auto()
CUSTOM_LIGHT_10 = auto()
HEADLIGHT = auto()
HIGHBEAMS = auto()
FOGLIGHTS = auto()
SIDELIGHTS = auto()
BRAKES = auto()
REVERSE = auto()
BEACONS = auto()
BLINK_LEFT = auto()
BLINK_RIGHT = auto()
BLINK_WARN = auto()

class CharacterCommand(IntEnum):
INVALID = 0
POSITION = auto()
ATTACH = auto()
DETACH = auto()

class CharacterAnimation(Enum):
IDLE_SWAY = "Idle_sway"
SPOT_SWIM = "Spot_swim"
WALK = "Walk"
RUN = "Run"
SWIM_LOOP = "Swim_loop"
TURN = "Turn"
DRIVING = "Driving"
Loading

0 comments on commit 83126b6

Please sign in to comment.