Skip to content

Commit

Permalink
Update Satisfactory Protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
BattlefieldDuck committed Jan 23, 2024
1 parent dba49e4 commit 97620d2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 16 deletions.
43 changes: 27 additions & 16 deletions opengsq/protocols/satisfactory.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,60 @@
import struct

from opengsq.responses.satisfactory import Status
from opengsq.binary_reader import BinaryReader
from opengsq.exceptions import InvalidPacketException
from opengsq.protocol_base import ProtocolBase
from opengsq.protocol_socket import UdpClient


class Satisfactory(ProtocolBase):
"""Satisfactory Protocol"""
full_name = 'Satisfactory Protocol'
"""
This class represents the Satisfactory Protocol. It provides methods to interact with the Satisfactory API.
"""

async def get_status(self) -> dict:
full_name = "Satisfactory Protocol"

async def get_status(self) -> Status:
"""
Retrieves information about the server including state, version, and beacon port
Server state: 1 - Idle (no game loaded), 2 - currently loading or creating a game, 3 - currently in game
Asynchronously retrieves the status of the game server. The status includes the server state, version, and beacon port.
The server state can be one of the following:
1 - Idle (no game loaded)
2 - Currently loading or creating a game
3 - Currently in game
:return: A Status object containing the status of the game server.
"""
# Credit: https://github.com/dopeghoti/SF-Tools/blob/main/Protocol.md

# Send message id, protocol version
request = struct.pack('2b', 0, 0) + 'opengsq'.encode()
request = struct.pack("2b", 0, 0) + "opengsq".encode()
response = await UdpClient.communicate(self, request)
br = BinaryReader(response)
header = br.read_byte()

if header != 1:
raise InvalidPacketException('Packet header mismatch. Received: {}. Expected: {}.'.format(chr(header), chr(1)))
raise InvalidPacketException(
"Packet header mismatch. Received: {}. Expected: {}.".format(
chr(header), chr(1)
)
)

br.read_byte() # Protocol version
br.read_bytes(8) # Request data

result = {}
result['State'] = br.read_byte()
result['Version'] = br.read_long()
result['BeaconPort'] = br.read_short()

return result
return Status(
state=br.read_byte(), version=br.read_long(), beacon_port=br.read_short()
)


if __name__ == '__main__':
if __name__ == "__main__":
import asyncio
import json
from dataclasses import asdict

async def main_async():
satisfactory = Satisfactory(host='79.136.0.124', port=15777, timeout=5.0)
satisfactory = Satisfactory(host="79.136.0.124", port=15777, timeout=5.0)
status = await satisfactory.get_status()
print(json.dumps(status, indent=None) + '\n')
print(json.dumps(asdict(status), indent=None) + "\n")

asyncio.run(main_async())
1 change: 1 addition & 0 deletions opengsq/responses/satisfactory/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .status import Status
17 changes: 17 additions & 0 deletions opengsq/responses/satisfactory/status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from dataclasses import dataclass


@dataclass
class Status:
"""
Represents the status response.
"""

state: int
"""The state."""

version: int
"""The version."""

beacon_port: int
"""The beacon port."""

0 comments on commit 97620d2

Please sign in to comment.