From d68d32ff2f802a64405f7111d886f5270947dbca Mon Sep 17 00:00:00 2001 From: Joscha Date: Fri, 14 Apr 2017 11:09:50 +0000 Subject: [PATCH] Rename "changes" to "diffs" in most places --- chunks.py | 50 +++++++++++++++++++-------------- client.py | 12 ++++---- clientchunkpool.py | 13 +++++---- dbchunkpool.py | 4 ++- maps.py | 4 +-- server.py | 70 +++++++++++++++++++++++----------------------- 6 files changed, 82 insertions(+), 71 deletions(-) diff --git a/chunks.py b/chunks.py index 934397e..e47a18b 100644 --- a/chunks.py +++ b/chunks.py @@ -81,23 +81,23 @@ class ChunkDiff(): return ChunkDiff.from_dict(diffs) -def jsonify_changes(changes): - dchanges = [] - for chunk in changes: - pos = chunk[0] - change = chunk[1].to_dict() - dchanges.append((pos, change)) +def jsonify_diffs(diffs): + ddiffs = [] + for dchunk in diffs: + pos = dchunk[0] + ddiff = dchunk[1].to_dict() + ddiffs.append((pos, ddiff)) - return dchanges + return ddiffs -def dejsonify_changes(dchanges): - changes = [] - for chunk in dchanges: - pos = Position(chunk[0][0], chunk[0][1]) - change = ChunkDiff.from_dict(chunk[1]) - changes.append((pos, change)) +def dejsonify_diffs(ddiffs): + diffs = [] + for dchunk in ddiffs: + pos = Position(dchunk[0][0], dchunk[0][1]) + diff = ChunkDiff.from_dict(dchunk[1]) + diffs.append((pos, diff)) - return changes + return diffs class Chunk(): """ @@ -126,6 +126,9 @@ class Chunk(): self.commit_diff(self._modifications) self._modifications = ChunkDiff() + def apply_diff(self, diff): + self._modifications.apply(diff) + def commit_diff(self, diff): self._content.apply(diff) self._content.clear_deletions() @@ -184,15 +187,20 @@ class ChunkPool(): self.set(pos, chunk) return chunk - def apply_changes(self, changes): - for change in changes: - pos = change[0] - diff = change[1] + def apply_diffs(self, diffs): + for dchunk in diffs: + pos = dchunk[0] + diff = dchunk[1] - chunk = self.get(pos) - if not chunk: - chunk = self.create(pos) + chunk = self.get(pos) or self.create(pos) + chunk.apply_diff(diff) + + def commit_diffs(self, diffs): + for dchunk in diffs: + pos = dchunk[0] + diff = dchunk[1] + chunk = self.get(pos) or self.create(pos) chunk.commit_diff(diff) def commit_changes(self): diff --git a/client.py b/client.py index 4216acb..683a9ff 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, jsonify_changes, dejsonify_changes +from chunks import ChunkDiff, jsonify_diffs, dejsonify_diffs from utils import Position from clientchunkpool import ClientChunkPool @@ -131,8 +131,8 @@ class Client(): def handle_json(self, message): if message["type"] == "apply-changes": - changes = dejsonify_changes(message["data"]) - self.map_.apply_changes(changes) + diffs = dejsonify_diffs(message["data"]) + self.map_.commit_diffs(diffs) def stop(self): self.stopping = True @@ -147,9 +147,9 @@ class Client(): message = {"type": "unload-chunks", "data": coords} self._ws.send(json.dumps(message)) - def send_changes(self, changes): - changes = jsonify_changes(changes) - message = {"type": "save-changes", "data": changes} + def send_changes(self, diffs): + diffs = jsonify_diffs(diffs) + message = {"type": "save-changes", "data": diffs} self._ws.send(json.dumps(message)) def main(argv): diff --git a/clientchunkpool.py b/clientchunkpool.py index 4465a67..6e32683 100644 --- a/clientchunkpool.py +++ b/clientchunkpool.py @@ -15,8 +15,8 @@ class ClientChunkPool(ChunkPool): def set(self, pos, chunk): super().set(pos, chunk) - def apply_changes(self, changes): - super().apply_changes(changes) + def commit_diffs(self, diffs): + super().commit_diffs(diffs) self._client.redraw() @@ -29,11 +29,12 @@ class ClientChunkPool(ChunkPool): self._save_thread.start() def save_changes(self): - changes = self.commit_changes() - changes = [chunk for chunk in changes if not chunk[1].empty()] + diffs = self.commit_changes() + # filter out empty changes/chunks + diffs = [dchunk for dchunk in diffs if not dchunk[1].empty()] - if changes: - self._client.send_changes(changes) + if diffs: + self._client.send_changes(diffs) def load(self, pos): raise Exception diff --git a/dbchunkpool.py b/dbchunkpool.py index e318f2a..0d9b6a0 100644 --- a/dbchunkpool.py +++ b/dbchunkpool.py @@ -12,4 +12,6 @@ class DBChunkPool(ChunkPool): A ChunkPool that can load/save chunks from/to a database. """ - pass + #def __init__(self, filename): + #super().init() + #self._chunkdb = ChunkDB(filename) diff --git a/maps.py b/maps.py index 69f1f86..30ba597 100644 --- a/maps.py +++ b/maps.py @@ -210,9 +210,9 @@ class Map(): #self.load_visible() - def apply_changes(self, changes): + def commit_diffs(self, diffs): with self.chunkpool as pool: - pool.apply_changes(changes) + pool.commit_diffs(diffs) ChunkStyle = namedtuple("ChunkStyle", "string color") diff --git a/server.py b/server.py index 574196a..68c99c9 100644 --- a/server.py +++ b/server.py @@ -5,7 +5,7 @@ import threading from SimpleWebSocketServer import SimpleWebSocketServer, WebSocket from utils import Position -from chunks import ChunkDiff, jsonify_changes, dejsonify_changes +from chunks import ChunkDiff, jsonify_diffs, dejsonify_diffs from dbchunkpool import DBChunkPool pool = DBChunkPool() @@ -13,17 +13,17 @@ clients = [] class WotServer(WebSocket): def handle_request_chunks(self, coords): - changes = [] + diffs = [] with pool: for coor in coords: pos = Position(coor[0], coor[1]) - change = pool.get(pos) or pool.create(pos) - changes.append((pos, change.as_diff())) + chunk = pool.get(pos) or pool.create(pos) + diffs.append((pos, chunk.as_diff())) self.loaded_chunks.add(pos) - dchanges = jsonify_changes(changes) - message = {"type": "apply-changes", "data": dchanges} + ddiffs = jsonify_diffs(diffs) + message = {"type": "apply-changes", "data": ddiffs} self.sendMessage(json.dumps(message)) def handle_unload_chunks(self, coords): @@ -32,51 +32,51 @@ class WotServer(WebSocket): if pos in self.loaded_chunks: self.loaded_chunks.remove(pos) - def handle_save_changes(self, dchanges): - changes = dejsonify_changes(dchanges) + def handle_save_changes(self, ddiffs): + diffs = dejsonify_diffs(ddiffs) # check whether changes are correct (exclude certain characters) # if not correct, send corrections them back to sender - legitimate_changes = [] - illegitimate_changes = [] - for chunk in changes: - if chunk[1].legitimate(): - legitimate_changes.append(chunk) + legitimate_diffs = [] + illegitimate_diffs = [] + for dchunk in diffs: + if dchunk[1].legitimate(): + legitimate_diffs.append(dchunk) else: - illegitimate_changes.append(chunk) + illegitimate_diffs.append(dchunk) - if legitimate_changes: + if legitimate_diffs: with pool: - pool.apply_changes(legitimate_changes) + pool.apply_diffs(legitimate_diffs) for client in clients: if client: - client.send_changes(legitimate_changes) + client.send_changes(legitimate_diffs) - if illegitimate_changes: - reverse_changes = self.reverse_changes(changes) - reverse_dchanges = jsonify_changes(reverse_changes) - message = {"type": "apply-changes", "data": reverse_dchanges} + if illegitimate_diffs: + reverse_diffs = self.reverse_diffs(illegitimate_diffs) + reverse_ddiffs = jsonify_diffs(reverse_diffs) + message = {"type": "apply-changes", "data": reverse_ddiffs} self.sendMessage(json.dumps(message)) - def reverse_changes(self, changes): + def reverse_diffs(self, diffs): with pool: - reverse_changes = [] - for chunk in changes: - pos = chunk[0] - change = chunk[1] - real_chunk = pool.get(pos) or pool.create(pos) - reverse_change = change.diff(real_chunk.as_diff()) - reverse_changes.append((pos, reverse_change)) + reverse_diffs = [] + for dchunk in diffs: + pos = dchunk[0] + diff = dchunk[1] + chunk = pool.get(pos) or pool.create(pos) + reverse_diff = diff.diff(chunk.as_diff()) + reverse_diffs.append((pos, reverse_diff)) - return reverse_changes + return reverse_diffs - def send_changes(self, changes): - changes = [chunk for chunk in changes if chunk[0] in self.loaded_chunks] - dchanges = jsonify_changes(changes) + def send_changes(self, diffs): + diffs = [dchunk for dchunk in diffs if dchunk[0] in self.loaded_chunks] + ddiffs = jsonify_diffs(diffs) - if dchanges: - message = {"type": "apply-changes", "data": dchanges} + if ddiffs: + message = {"type": "apply-changes", "data": ddiffs} self.sendMessage(json.dumps(message)) def handleMessage(self):