GameIO
Core of the game-sdk. Used to communicate with other controllers and the gamecontrol.
__init__(self, mqtt_conf)
special
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mqtt_conf |
dict |
Information needed to connect controller to the mqtt broker |
required |
Source code in game_sdk/game_io.py
def __init__(self, mqtt_conf: dict):
"""
Arguments:
mqtt_conf: Information needed to connect controller to the mqtt broker
"""
self._ip = mqtt_conf['broker_ip']
self._port = mqtt_conf['broker_port'] if 'broker_port' in mqtt_conf else 1883
self._uname = mqtt_conf['username'] if 'username' in mqtt_conf else None
self._passwd = mqtt_conf['password'] if 'password' in mqtt_conf else None
self.client = Client(self._ip, self._port,
username=self._uname, password=self._passwd)
connect(self)
async
Connect to MQTT client
Source code in game_sdk/game_io.py
async def connect(self):
"""
Connect to MQTT client
"""
await self.client.connect()
publish(self, topic, payload)
async
Publish any message to any desired topic
Source code in game_sdk/game_io.py
async def publish(self, topic: str, payload: dict):
"""
Publish any message to any desired topic
"""
try:
logging.debug("Publish to: %s with %s", topic, payload)
await self.client.publish(topic, json.dumps(payload))
except TypeError:
logging.error("MQTT Payload is not JSON serializable")
ready(self, seat)
async
Publish ready state
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seat |
int |
Controller seat |
required |
Source code in game_sdk/game_io.py
async def ready(self, seat: int):
"""
Publish ready state
Arguments:
seat: Controller seat
"""
payload = dict(seat=seat, ready=True)
await self.publish('status/ready', payload)
score(self, score, seat)
async
Publish score
Parameters:
Name | Type | Description | Default |
---|---|---|---|
score |
int |
Score |
required |
seat |
int |
Controller seat |
required |
Source code in game_sdk/game_io.py
async def score(self, score: int, seat: int):
"""
Publish score
Arguments:
score: Score
seat: Controller seat
"""
payload = dict(seat=seat, score=score)
await self.publish('score', payload)
set_game_state(self, state)
async
Publish new game_state
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state |
GameState |
State to publish |
required |
Source code in game_sdk/game_io.py
async def set_game_state(self, state: GameState):
"""
Publish new `game_state`
Arguments:
state: State to publish
"""
payload = dict(mode=state.value)
await self.publish("status/game", payload)
subscribe(self, topics)
Subscribe to the different MQTT topics published by the gamecontrol
Parameters:
Name | Type | Description | Default |
---|---|---|---|
topics |
list |
List of topics to subscribe to. Each topic is a tuple consisting of the actual topic and QOS e.g. ("some/topic", 0) |
required |
Source code in game_sdk/game_io.py
async def subscribe(self, topics: list):
"""
Subscribe to the different MQTT topics published by the gamecontrol
Arguments:
topics: List of topics to subscribe to. Each topic is a tuple consisting of the actual topic and QOS e.g. ("some/topic", 0)
"""
async with self.client.unfiltered_messages() as messages:
await self.client.subscribe(topics)
async for msg in messages:
msg: MQTTMessage = msg
try:
data = json.loads(msg.payload)
yield (msg.topic, data)
except JSONDecodeError:
logging.warning("Got MQTT message with wrong format")
subscribe_to_status(self)
Subscribe to the gamestatus published by the gamecontrol
Source code in game_sdk/game_io.py
async def subscribe_to_status(self):
"""
Subscribe to the gamestatus published by the gamecontrol
"""
topics = [("status/game", 0)]
async for msg in self.subscribe(topics):
data = msg[1]
mode = data['mode'] if 'mode' in data else 'idle'
game_state = GameState.IDLE
if mode == 'start':
game_state = GameState.START
elif mode == 'run':
game_state = GameState.RUN
elif mode == 'end':
game_state = GameState.END
yield game_state