Reverse illegitimate changes on server

This commit is contained in:
Joscha 2017-04-14 09:41:02 +00:00
parent 54e09d18cc
commit 3cb21c02bc
3 changed files with 50 additions and 6 deletions

View file

@ -1,3 +1,4 @@
import string
import threading
import time
from utils import CHUNK_WIDTH, CHUNK_HEIGHT, Position
@ -66,6 +67,20 @@ class ChunkDiff():
def empty(self):
return not bool(self._chars)
def legitimate(self):
for i, char in self._chars.items():
if not (isinstance(char, str) and len(char) == 1 and ord(char) > 31 and (char not in string.whitespace or char == " ")):
return False
else:
return True
def diff(self, chunk):
diffs = {}
for pos, char in self._chars.items():
diffs[pos] = chunk._chars.get(pos, " ")
return ChunkDiff.from_dict(diffs)
def jsonify_changes(changes):
dchanges = []
for chunk in changes:

View file

@ -116,7 +116,7 @@ class Client():
elif i == "\n": self.map_.newline()
#elif i in string.digits + string.ascii_letters + string.punctuation + " ":
#self.map_.write(i)
elif isinstance(i, str) and len(i) == 1 and (i not in string.whitespace or i == " "):
elif isinstance(i, str) and len(i) == 1 and ord(i) > 31 and (i not in string.whitespace or i == " "):
self.map_.write(i)
def connection_thread(self):

View file

@ -35,12 +35,41 @@ class WotServer(WebSocket):
def handle_save_changes(self, dchanges):
changes = dejsonify_changes(dchanges)
# 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)
else:
illegitimate_changes.append(chunk)
if legitimate_changes:
with pool:
pool.apply_changes(changes)
pool.apply_changes(legitimate_changes)
for client in clients:
if client:
client.send_changes(changes)
client.send_changes(legitimate_changes)
if illegitimate_changes:
reverse_changes = self.reverse_changes(changes)
reverse_dchanges = jsonify_changes(reverse_changes)
message = {"type": "apply-changes", "data": reverse_dchanges}
self.sendMessage(json.dumps(message))
def reverse_changes(self, changes):
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))
return reverse_changes
def send_changes(self, changes):
changes = [chunk for chunk in changes if chunk[0] in self.loaded_chunks]