Clean up client code

This commit is contained in:
Joscha 2017-04-14 08:36:32 +00:00
parent 4d81dea41c
commit 7a3bf578bc
2 changed files with 7 additions and 22 deletions

View file

@ -8,7 +8,7 @@ import websocket
from websocket import WebSocketException as WSException from websocket import WebSocketException as WSException
from maps import Map, ChunkMap from maps import Map, ChunkMap
from chunks import ChunkDiff from chunks import ChunkDiff, jsonify_changes, dejsonify_changes
from utils import Position from utils import Position
from clientchunkpool import ClientChunkPool from clientchunkpool import ClientChunkPool
@ -136,12 +136,7 @@ class Client():
def handle_json(self, message): def handle_json(self, message):
sys.stderr.write(f"message: {message}\n") sys.stderr.write(f"message: {message}\n")
if message["type"] == "apply-changes": if message["type"] == "apply-changes":
changes = [] changes = dejsonify_changes(message["data"])
for chunk in message["data"]:
pos = Position(chunk[0][0], chunk[0][1])
change = ChunkDiff.from_dict(chunk[1])
changes.append((pos, change))
sys.stderr.write(f"Changes to apply: {changes}\n") sys.stderr.write(f"Changes to apply: {changes}\n")
self.map_.apply_changes(changes) self.map_.apply_changes(changes)
@ -155,14 +150,6 @@ class Client():
#sys.stderr.write(f"requested chunks: {coords}\n") #sys.stderr.write(f"requested chunks: {coords}\n")
message = {"type": "request-chunks", "data": coords} message = {"type": "request-chunks", "data": coords}
self._ws.send(json.dumps(message)) 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): def unload_chunks(self, coords):
#sys.stderr.write(f"unloading chunks: {coords}\n") #sys.stderr.write(f"unloading chunks: {coords}\n")
@ -171,6 +158,7 @@ class Client():
def send_changes(self, changes): def send_changes(self, changes):
#sys.stderr.write(f"sending changes: {changes}\n") #sys.stderr.write(f"sending changes: {changes}\n")
changes = jsonify_changes(changes)
message = {"type": "save-changes", "data": changes} message = {"type": "save-changes", "data": changes}
self._ws.send(json.dumps(message)) self._ws.send(json.dumps(message))

View file

@ -43,13 +43,10 @@ class ClientChunkPool(ChunkPool):
def save_changes(self): def save_changes(self):
changes = self.commit_changes() changes = self.commit_changes()
dchanges = [] changes = [chunk for chunk in changes if not chunk[1].empty()]
for pos, change in changes:
dchange = change.to_dict() if changes:
if dchange: self._client.send_changes(changes)
dchanges.append((pos, dchange))
if dchanges:
self._client.send_changes(dchanges)
def load(self, pos): def load(self, pos):
raise Exception raise Exception