2022-05-22 22:47:33 +01:00
|
|
|
import logging
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import pathlib
|
|
|
|
|
2022-05-23 03:16:06 +01:00
|
|
|
# import asyncio
|
2022-05-22 22:47:33 +01:00
|
|
|
from aiohttp import web
|
2022-05-23 03:16:06 +01:00
|
|
|
import aiohttp
|
2022-05-22 22:47:33 +01:00
|
|
|
|
2022-06-11 01:02:05 +01:00
|
|
|
HOME_DIR = "/home/deck"
|
2022-05-22 22:47:33 +01:00
|
|
|
SETTINGS_DIR = HOME_DIR + "/.config/powertools"
|
|
|
|
|
|
|
|
if not os.path.exists(SETTINGS_DIR):
|
|
|
|
os.mkdir(SETTINGS_DIR)
|
|
|
|
|
|
|
|
http_runner = None
|
|
|
|
http_server = None
|
2022-05-23 03:16:06 +01:00
|
|
|
http_site = None
|
2022-05-22 22:47:33 +01:00
|
|
|
|
|
|
|
class GameInfo:
|
|
|
|
def __init__(self, gameid: int, game_info: dict):
|
|
|
|
self.gameid = gameid
|
|
|
|
self.game_info = game_info
|
|
|
|
|
|
|
|
def appid(self):
|
|
|
|
return self.game_info["appid"]
|
|
|
|
|
|
|
|
def name(self):
|
|
|
|
return self.game_info["display_name"]
|
|
|
|
|
|
|
|
def settings_path(self) -> str:
|
2022-05-23 03:16:06 +01:00
|
|
|
return SETTINGS_DIR + os.path.sep + str(self.appid()) + ".json"
|
2022-05-22 22:47:33 +01:00
|
|
|
|
|
|
|
def load_settings(self) -> dict:
|
|
|
|
settings_path = self.settings_path()
|
2022-05-23 03:16:06 +01:00
|
|
|
if os.path.exists(settings_path):
|
2022-05-22 22:47:33 +01:00
|
|
|
with open(settings_path, mode="r") as f:
|
|
|
|
return json.load(f)
|
|
|
|
return None
|
|
|
|
|
|
|
|
def has_settings(self) -> bool:
|
2022-05-23 03:16:06 +01:00
|
|
|
return os.path.exists(self.settings_path())
|
2022-05-22 22:47:33 +01:00
|
|
|
|
|
|
|
|
2022-07-05 23:14:50 +01:00
|
|
|
class Server:
|
2022-05-22 22:47:33 +01:00
|
|
|
|
|
|
|
def __init__(self, version):
|
|
|
|
super().__init__()
|
|
|
|
self.version = version
|
|
|
|
self.current_game = None
|
|
|
|
logging.debug("Server init complete")
|
|
|
|
|
|
|
|
def game(self) -> GameInfo:
|
|
|
|
return self.current_game
|
|
|
|
|
2022-06-11 01:02:05 +01:00
|
|
|
def set_game(self, game_id, data):
|
|
|
|
self.current_game = GameInfo(game_id, data)
|
|
|
|
|
|
|
|
def unset_game(self, game_id):
|
|
|
|
if self.current_game is None:
|
|
|
|
return
|
|
|
|
if game_id is None or self.current_game.gameid == game_id:
|
|
|
|
self.current_game = None
|
|
|
|
|
2022-05-22 22:47:33 +01:00
|
|
|
async def start(version):
|
2022-07-05 23:14:50 +01:00
|
|
|
global http_server
|
2022-05-22 22:47:33 +01:00
|
|
|
http_server = Server(version)
|
|
|
|
|
|
|
|
async def shutdown(): # never really called
|
2022-07-05 23:14:50 +01:00
|
|
|
global http_server
|
2022-05-22 22:47:33 +01:00
|
|
|
http_server = None
|