feat(emulator): docstrings

This commit is contained in:
Laureηt 2021-11-02 19:32:54 +01:00
parent 0473db2a28
commit eadc64b036
No known key found for this signature in database
GPG key ID: D88C6B294FD40994
2 changed files with 18 additions and 12 deletions

View file

@ -39,7 +39,12 @@ mgba.log.silence()
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
async def parse_message(message: dict[str, str]): def parse_message(message: dict[str, str]):
"""Parse the server's reponse.
Args:
message (dict[str, str]): the data received (through the websocket).
"""
if "action" in message: if "action" in message:
data = message["action"] data = message["action"]
if data in KEYMAP: if data in KEYMAP:
@ -69,6 +74,7 @@ async def parse_message(message: dict[str, str]):
async def main(): async def main():
"""Start the emulator."""
with pyvirtualcam.Camera(width=WIDTH, height=HEIGHT, fps=FPS) as cam: with pyvirtualcam.Camera(width=WIDTH, height=HEIGHT, fps=FPS) as cam:
logging.debug(f"Using virtual camera: {cam.device}") logging.debug(f"Using virtual camera: {cam.device}")
async with websockets.connect(URI) as websocket: async with websockets.connect(URI) as websocket:

View file

@ -16,15 +16,15 @@ VOTES: Votes = Votes()
USERS: Users = Users() USERS: Users = Users()
async def parse_message(user: User, msg: dict[str, str]): async def parse_message(user: User, message: dict[str, str]):
"""Parse the `user`'s `msg`. """Parse the user's message.
Args: Args:
user (User): the sender of the message. user (User): the sender of the message.
msg (dict[str, str]): the data received (through the websocket). message (dict[str, str]): the data received (through the websocket).
""" """
if "auth" in msg: if "auth" in message:
data = msg["auth"] data = message["auth"]
if USERS.emulator is None and data == PASSWORD_EMU: if USERS.emulator is None and data == PASSWORD_EMU:
USERS.emulator = user USERS.emulator = user
logging.debug(f"emulator authenticated: {user}") logging.debug(f"emulator authenticated: {user}")
@ -32,8 +32,8 @@ async def parse_message(user: User, msg: dict[str, str]):
USERS.admin = user USERS.admin = user
logging.debug(f"admin authenticated: {user}") logging.debug(f"admin authenticated: {user}")
if "action" in msg: if "action" in message:
data = msg["action"] data = message["action"]
if data in VOTES: if data in VOTES:
VOTES[data] += 1 VOTES[data] += 1
user.last_message = time.time() user.last_message = time.time()
@ -41,8 +41,8 @@ async def parse_message(user: User, msg: dict[str, str]):
else: else:
logging.error(f"unsupported action: {data}") logging.error(f"unsupported action: {data}")
if "admin" in msg: if "admin" in message:
data = msg["admin"] data = message["admin"]
if USERS.emulator is not None and user == USERS.admin: if USERS.emulator is not None and user == USERS.admin:
if data == "save": if data == "save":
await USERS.emulator.send('{"admin":"save"}') await USERS.emulator.send('{"admin":"save"}')
@ -53,8 +53,8 @@ async def parse_message(user: User, msg: dict[str, str]):
else: else:
logging.error(f"user is not admin: {user}") logging.error(f"user is not admin: {user}")
if "emu" in msg: if "emu" in message:
data = msg["emu"] data = message["emu"]
if user == USERS.emulator: if user == USERS.emulator:
if data == "get": if data == "get":
await USERS.emulator.send(f'{{"action":"{VOTES.next_vote()}"}}') await USERS.emulator.send(f'{{"action":"{VOTES.next_vote()}"}}')