feat: better settings.py

This commit is contained in:
Laureηt 2021-11-10 23:33:51 +01:00
parent 8246ab44d8
commit 3382b69bc3
No known key found for this signature in database
GPG key ID: D88C6B294FD40994
3 changed files with 79 additions and 32 deletions

View file

@ -7,17 +7,33 @@ import mgba.image
import mgba.log import mgba.log
import redis import redis
from settings import FPS, HEIGHT, KEYS, MGBA_KEYS, POLLING_RATE, REDIS_INIT, SPF, WIDTH from settings import (
EMULATOR_FPS,
EMULATOR_HEIGHT,
EMULATOR_POLLING_RATE,
EMULATOR_ROM_PATH,
EMULATOR_SPF,
EMULATOR_WIDTH,
FFMPEG_BITRATE,
FFMPEG_FPS,
FFMPEG_HEIGHT,
FFMPEG_WIDTH,
KEYS_ID,
KEYS_MGBA,
KEYS_RESET,
REDIS_HOST,
REDIS_PORT,
RTMP_STREAM_URI,
)
core = mgba.core.load_path("roms/pokemon.gba") core = mgba.core.load_path(EMULATOR_ROM_PATH)
# core = mgba.core.load_path("roms/BtnTest.gba") screen = mgba.image.Image(EMULATOR_WIDTH, EMULATOR_HEIGHT)
screen = mgba.image.Image(WIDTH, HEIGHT)
core.set_video_buffer(screen) core.set_video_buffer(screen)
core.reset() core.reset()
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
mgba.log.silence() mgba.log.silence()
r = redis.Redis(host="redis", port=6379, db=0) r = redis.Redis(host=REDIS_HOST, port=REDIS_PORT, db=0)
def next_action(): def next_action():
@ -26,9 +42,9 @@ def next_action():
Returns: Returns:
int: key used by mgba int: key used by mgba
""" """
votes = list(map(int, r.mget(KEYS))) votes = list(map(int, r.mget(KEYS_ID)))
if any(votes): if any(votes):
r.mset(REDIS_INIT) r.mset(KEYS_RESET)
return votes.index(max(votes)) return votes.index(max(votes))
else: else:
return -1 return -1
@ -43,26 +59,26 @@ stream = Popen(
"-vcodec", "-vcodec",
"png", "png",
"-r", "-r",
f"{FPS}", f"{EMULATOR_FPS}",
"-s", "-s",
f"{WIDTH}x{HEIGHT}", f"{EMULATOR_WIDTH}x{EMULATOR_HEIGHT}",
"-i", "-i",
"-", "-",
"-f", "-f",
"flv", "flv",
"-s", "-s",
f"{WIDTH}x{HEIGHT}", f"{FFMPEG_WIDTH}x{FFMPEG_HEIGHT}",
"-r", "-r",
"30", f"{FFMPEG_FPS}",
"-b:v", "-b:v",
"2M", FFMPEG_BITRATE,
"-fflags", "-fflags",
"nobuffer", "nobuffer",
"-flags", "-flags",
"low_delay", "low_delay",
"-strict", "-strict",
"experimental", "experimental",
"rtmp://rtmp:1935/live/test", RTMP_STREAM_URI,
], ],
stdin=PIPE, stdin=PIPE,
) )
@ -71,8 +87,8 @@ while True:
last_frame_t = time.time() last_frame_t = time.time()
if not (core.frame_counter % POLLING_RATE): if not (core.frame_counter % EMULATOR_POLLING_RATE):
core.clear_keys(*MGBA_KEYS) core.clear_keys(*KEYS_MGBA)
next_key = next_action() next_key = next_action()
if next_key != -1: if next_key != -1:
core.set_keys(next_key) core.set_keys(next_key)
@ -82,6 +98,6 @@ while True:
image = screen.to_pil().convert("RGB") image = screen.to_pil().convert("RGB")
image.save(stream.stdin, "PNG") image.save(stream.stdin, "PNG")
sleep_t = last_frame_t - time.time() + SPF sleep_t = last_frame_t - time.time() + EMULATOR_SPF
if sleep_t > 0: if sleep_t > 0:
time.sleep(sleep_t) time.sleep(sleep_t)

View file

@ -6,13 +6,22 @@ import time
import redis import redis
import websockets import websockets
from settings import KEYS, PASSWORD_ADMIN, REDIS_INIT, USER_TIMEOUT from settings import (
KEYS_ID,
KEYS_RESET,
PASSWORD_ADMIN,
REDIS_HOST,
REDIS_PORT,
USER_TIMEOUT,
WEBSOCKET_LISTEN,
WEBSOCKET_PORT,
)
from utils import User, Users from utils import User, Users
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
r = redis.Redis(host="redis", port=6379, db=0) r = redis.Redis(host=REDIS_HOST, port=REDIS_PORT, db=0)
r.mset(REDIS_INIT) r.mset(KEYS_RESET)
USERS: Users = Users() USERS: Users = Users()
@ -37,7 +46,7 @@ async def parse_message(user: User, message: dict[str, str]) -> None:
if user.last_message + USER_TIMEOUT > time.time(): if user.last_message + USER_TIMEOUT > time.time():
logging.debug(f"dropping action: {data}") logging.debug(f"dropping action: {data}")
return None return None
elif data in KEYS: elif data in KEYS_ID:
r.incr(data) r.incr(data)
user.last_message = time.time() user.last_message = time.time()
user.has_voted = True user.has_voted = True
@ -67,7 +76,7 @@ async def handler(websocket, path: str):
async def main(): async def main():
"""Start the websocket server.""" """Start the websocket server."""
async with websockets.serve(handler, "0.0.0.0", 6789): # nosec async with websockets.serve(handler, WEBSOCKET_LISTEN, WEBSOCKET_PORT): # nosec
await asyncio.Future() # run forever await asyncio.Future() # run forever

View file

@ -1,13 +1,35 @@
WIDTH: int = 240 from os import getenv
HEIGHT: int = 160
URI: str = "ws://127.0.0.1:6789/" WEBSOCKET_HOST: str = getenv("WEBSOCKET_HOST", "localhost")
PASSWORD_ADMIN: str = "password_admin" WEBSOCKET_PORT: int = int(getenv("WEBSOCKET_PORT", 6789))
WEBSOCKET_URI: str = f"ws://{WEBSOCKET_HOST}:{WEBSOCKET_PORT}/"
WEBSOCKET_LISTEN: str = getenv("WEBSOCKET_LISTEN", "localhost")
RTMP_HOST: str = getenv("WEBSOCKET_HOST", "localhost")
RTMP_PORT: int = int(getenv("WEBSOCKET_PORT", 1935))
RTMP_URI: str = f"rtmp://{RTMP_HOST}:{RTMP_PORT}/"
RTMP_STREAM_PATH: str = getenv("RTMP_STREAM_PATH", "live")
RTMP_STREAM_KEY: str = getenv("RTMP_STREAM_KEY", "test")
RTMP_STREAM_URI: str = RTMP_URI + f"{RTMP_STREAM_PATH}/{RTMP_STREAM_KEY}"
REDIS_HOST: str = getenv("REDIS_HOST", "localhost")
REDIS_PORT: int = int(getenv("REDIS_PORT", 6379))
EMULATOR_WIDTH: int = int(getenv("EMULATOR_WIDTH", 240))
EMULATOR_HEIGHT: int = int(getenv("EMULATOR_HEIGHT", 160))
EMULATOR_FPS: int = int(getenv("EMULATOR_FPS", 60))
EMULATOR_SPF: float = 1.0 / EMULATOR_FPS
EMULATOR_INPUT_HZ: int = int(getenv("EMULATOR_INPUT_HZ", 10))
EMULATOR_POLLING_RATE: int = EMULATOR_FPS // EMULATOR_INPUT_HZ
EMULATOR_ROM_PATH: str = getenv("EMULATOR_ROM_PATH", "roms/pokemon.gba")
FFMPEG_WIDTH: int = int(getenv("FFMPEG_WIDTH", EMULATOR_WIDTH))
FFMPEG_HEIGHT: int = int(getenv("FFMPEG_HEIGHT", EMULATOR_HEIGHT))
FFMPEG_FPS: int = int(getenv("FFMPEG_FPS", 30))
FFMPEG_BITRATE: str = getenv("FFMPEG_BIRATE", "2M")
PASSWORD_ADMIN: str = getenv("PASSWORD_ADMIN", "password_admin")
FPS: int = 60
SPF: float = 1.0 / FPS
HZ: int = 10
POLLING_RATE: int = FPS // HZ
USER_TIMEOUT: float = 0.5 USER_TIMEOUT: float = 0.5
KEYMAP: dict[str, int] = { KEYMAP: dict[str, int] = {
@ -22,6 +44,6 @@ KEYMAP: dict[str, int] = {
"r": 8, "r": 8,
"l": 9, "l": 9,
} }
KEYS: tuple = tuple(KEYMAP.keys()) KEYS_ID: tuple = tuple(KEYMAP.keys())
MGBA_KEYS: tuple = tuple(KEYMAP.values()) KEYS_MGBA: tuple = tuple(KEYMAP.values())
REDIS_INIT: dict = dict([(x, 0) for x in KEYS]) KEYS_RESET: dict = dict([(x, 0) for x in KEYS_ID])