From 7a3bf578bc37f8f9ac5e9a3bfc4e2fd3ca2731be Mon Sep 17 00:00:00 2001 From: Joscha Date: Fri, 14 Apr 2017 08:36:32 +0000 Subject: [PATCH] Clean up client code --- client.py | 18 +++--------------- clientchunkpool.py | 11 ++++------- 2 files changed, 7 insertions(+), 22 deletions(-) diff --git a/client.py b/client.py index 7c4216b..fc13ec2 100644 --- a/client.py +++ b/client.py @@ -8,7 +8,7 @@ import websocket from websocket import WebSocketException as WSException from maps import Map, ChunkMap -from chunks import ChunkDiff +from chunks import ChunkDiff, jsonify_changes, dejsonify_changes from utils import Position from clientchunkpool import ClientChunkPool @@ -136,12 +136,7 @@ class Client(): def handle_json(self, message): sys.stderr.write(f"message: {message}\n") if message["type"] == "apply-changes": - changes = [] - for chunk in message["data"]: - pos = Position(chunk[0][0], chunk[0][1]) - change = ChunkDiff.from_dict(chunk[1]) - changes.append((pos, change)) - + changes = dejsonify_changes(message["data"]) sys.stderr.write(f"Changes to apply: {changes}\n") self.map_.apply_changes(changes) @@ -155,14 +150,6 @@ class Client(): #sys.stderr.write(f"requested chunks: {coords}\n") message = {"type": "request-chunks", "data": coords} self._ws.send(json.dumps(message)) - - #def execute(): - #changes = [(pos, ChunkDiff()) for pos in coords] - #with self.pool as pool: - #pool.apply_changes(changes) - - #tx = threading.Timer(1, execute) - #tx.start() def unload_chunks(self, coords): #sys.stderr.write(f"unloading chunks: {coords}\n") @@ -171,6 +158,7 @@ class Client(): def send_changes(self, changes): #sys.stderr.write(f"sending changes: {changes}\n") + changes = jsonify_changes(changes) message = {"type": "save-changes", "data": changes} self._ws.send(json.dumps(message)) diff --git a/clientchunkpool.py b/clientchunkpool.py index 54f12ca..c51055a 100644 --- a/clientchunkpool.py +++ b/clientchunkpool.py @@ -43,13 +43,10 @@ class ClientChunkPool(ChunkPool): def save_changes(self): changes = self.commit_changes() - dchanges = [] - for pos, change in changes: - dchange = change.to_dict() - if dchange: - dchanges.append((pos, dchange)) - if dchanges: - self._client.send_changes(dchanges) + changes = [chunk for chunk in changes if not chunk[1].empty()] + + if changes: + self._client.send_changes(changes) def load(self, pos): raise Exception