-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dba49e4
commit 97620d2
Showing
3 changed files
with
45 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .status import Status |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.""" |