diff --git a/README.md b/README.md index cfdbd22..0cc3a12 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,44 @@ ## Client-Server-Kommunikation -- der Client verbindet sich mit dem Server per websocket -- zu Beginn sendet der client seine Platform und version als json zum server: - ```json - {"platform":"python", "version":999} - ``` -- sobald der client in 0 Lage ist ein pixel zu setzen schickt dieser ein `request_pixel` an den server - - der Server antwortet dann mit dem zu setzenden pixel als json, e.g.: +- Der Client verbindet sich mit dem Server per websocket +- Zu Beginn sendet der client seine Platform und version als json zum server: ```json { - "operation":"pixel", - "data":{ + "operation": "handshake", + "data": { + "platform": "python", + "version": 999 + } +} +``` + +- Sollte der Server feststellen, dass der Client eine alte Version verwendet, sendet er diesem eine Update aufforderung zurück: +```json +{ + "operation": "notify-update" +} +``` + +- sobald der client in 0 Lage ist ein pixel zu setzen schickt dieser ein `request-pixel` an den server +```json +{ + "operation": "request-pixel", + "user": "" +} + ``` + +- der Server antwortet dann mit dem zu setzenden pixel als json, e.g.: +```json +{ + "operation": "place-pixel", + "data": { "x": 0, "y": 857, "color": 4, "priority": 1 - } + }, + "user": "" } ``` - - wenn kein Pixel existiert, wird `null` zurückgesendet. + +- wenn kein Pixel existiert, wird `{}` zurückgesendet. diff --git a/application/connections/websocket_server.py b/application/connections/websocket_server.py index cf3b774..1bf8d16 100644 --- a/application/connections/websocket_server.py +++ b/application/connections/websocket_server.py @@ -20,16 +20,18 @@ class Server: """ Websocket server, dieser managed die Verbindung zu den client Bots und teilt denen auf Anfrage neue Pixel zu. """ - __slots__ = ("config", "port", "__server_loop", "__server", "host", "provider", "__client_count") + __slots__ = ("config", "versions", "port", "__server_loop", "__server", "host", "provider", "__client_count") __client_count: int config: Dict[str, Any] + versions: {} port: int host: str provider: Canvas - def __init__(self, provider: Canvas, config: Dict[str, Any]): + def __init__(self, provider: Canvas, versions, config: Dict[str, Any]): self.provider = provider self.config = config + self.versions = versions self.port = config.get("port", DEFAULT_PORT) self.host = config.get("host", DEFAULT_HOST) self.__server_loop = None @@ -49,8 +51,6 @@ async def __handler(self, socket: WebSocketServerProtocol): bot_count = 0 try: - # TODO: check for update availability. - async for msg in socket: req = json.loads(msg) @@ -69,6 +69,11 @@ async def __handler(self, socket: WebSocketServerProtocol): await socket.send("{}") elif req.get("operation") == "handshake": + version = req["data"].get("version", 0) + target_version = self.versions.get(req["data"]["platform"], 0) + if version.isdigit() and version != 0 and target_version != 0 and version < target_version: + await socket.send(json.dumps({"operation": "notify-update"})) + bot_count = abs(req["data"].get("useraccounts", 1)) self.__client_count += bot_count print(f"{bot_count} New Client(s) connected! New bot count: {self.__client_count}") diff --git a/application/target_configuration/target_configuration.py b/application/target_configuration/target_configuration.py index 3cbd37e..c0cbddc 100644 --- a/application/target_configuration/target_configuration.py +++ b/application/target_configuration/target_configuration.py @@ -2,6 +2,7 @@ import random import time import asyncio +from typing import Dict import aiohttp import requests @@ -18,6 +19,7 @@ class TargetConfiguration: """ def __init__(self): + self.versions = {} self.last_update = 0 self.config = {} self.pixels = [] @@ -31,6 +33,7 @@ async def get_config(self, ignore_time: bool = False): self.last_update = time.time() lst = [] + self.versions = self.config["versions"] priorities = self.config["priorities"] for s in self.config["structures"].values(): prio = (priorities.get(str(s.get("priority"))) or 0) * random.randint(0, 100) / 100 diff --git a/main.py b/main.py index b22d750..85a8c24 100644 --- a/main.py +++ b/main.py @@ -18,7 +18,7 @@ # manage r/place canvas monalisa = canvas.Canvas(target_configuration) # server for remote bot connections -server = websocket_server.Server(monalisa, {"host": "0.0.0.0", "port": 8080}) +server = websocket_server.Server(monalisa, target_configuration.versions, {"host": "0.0.0.0", "port": 8080}) async def main_loop(): while True: