diff --git a/chunks.py b/chunks.py index c9a9a18..2d1aa3d 100644 --- a/chunks.py +++ b/chunks.py @@ -2,8 +2,6 @@ import threading import time from utils import CHUNK_WIDTH, CHUNK_HEIGHT, Position -import sys - class ChunkDiff(): """ Represents differences between two chunks (changes to be made to a chunk). @@ -67,7 +65,24 @@ class ChunkDiff(): def empty(self): return not bool(self._chars) + +def jsonify_changes(changes): + dchanges = [] + for chunk in changes: + pos = chunk[0] + change = chunk[1].to_dict() + dchanges.append((pos, change)) + return dchanges + +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)) + + return changes class Chunk(): """ @@ -116,11 +131,6 @@ class Chunk(): def age(self, now=None): return self.last_modified - (now or time.time()) - #def draw_to(self, x, y, window): - #for line in self._content.combine(self._modifications).lines(): - #window.addstr(y, x, line) - #y += 1 - def lines(self): return self.as_diff().lines() @@ -161,7 +171,6 @@ class ChunkPool(): def apply_changes(self, changes): for change in changes: - #pos = Position(change[0][0], change[0][1]) pos = change[0] diff = change[1] @@ -169,9 +178,7 @@ class ChunkPool(): if not chunk: chunk = self.create(pos) - sys.stderr.write(f"Previous at {pos}: {chunk._content}\n") chunk.commit_diff(diff) - sys.stderr.write(f"Afterwrd at {pos}: {chunk._content}\n") def commit_changes(self): changes = [] diff --git a/server.py b/server.py index 17199ec..d1268c8 100644 --- a/server.py +++ b/server.py @@ -4,7 +4,7 @@ import threading from SimpleWebSocketServer import SimpleWebSocketServer, WebSocket from utils import Position -from chunks import ChunkDiff +from chunks import ChunkDiff, jsonify_changes, dejsonify_changes from dbchunkpool import DBChunkPool pool = DBChunkPool() @@ -17,12 +17,12 @@ class WotServer(WebSocket): for coor in coords: pos = Position(coor[0], coor[1]) change = pool.get(pos) or pool.create(pos) - dchange = change.as_diff().to_dict() - changes.append((pos, dchange)) + changes.append((pos, change.as_diff())) self.loaded_chunks.add(pos) - message = {"type": "apply-changes", "data": changes} + dchanges = jsonify_changes(changes) + message = {"type": "apply-changes", "data": dchanges} self.sendMessage(json.dumps(message)) def handle_unload_chunks(self, coords): @@ -32,12 +32,7 @@ class WotServer(WebSocket): self.loaded_chunks.remove(pos) def handle_save_changes(self, dchanges): - changes = [] - for chunk in dchanges: - #print("CHUNK!", chunk) - pos = Position(chunk[0][0], chunk[0][1]) - change = ChunkDiff.from_dict(chunk[1]) - changes.append((pos, change)) + changes = dejsonify_changes(dchanges) with pool: pool.apply_changes(changes) @@ -47,12 +42,8 @@ class WotServer(WebSocket): client.send_changes(changes) def send_changes(self, changes): - dchanges = [] - for chunk in changes: - pos = chunk[0] - change = chunk[1] - if pos in self.loaded_chunks: - dchanges.append((pos, change.to_dict())) + changes = [chunk for chunk in changes if chunk[0] in self.loaded_chunks] + dchanges = jsonify_changes(changes) if dchanges: message = {"type": "apply-changes", "data": dchanges} @@ -84,7 +75,8 @@ class WotServer(WebSocket): i = clients.index(self) graphstr = "".join(["┷" if j == i else ("│" if v else " ") for j, v in enumerate(clients)]) - print(f"{graphstr} {self.address[0]}") + print(graphstr) + #print(f"{graphstr} {self.address[0]}") clients[i] = None while clients and not clients[-1]: