Reverse illegitimate changes on server
This commit is contained in:
parent
54e09d18cc
commit
3cb21c02bc
3 changed files with 50 additions and 6 deletions
15
chunks.py
15
chunks.py
|
|
@ -1,3 +1,4 @@
|
||||||
|
import string
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
from utils import CHUNK_WIDTH, CHUNK_HEIGHT, Position
|
from utils import CHUNK_WIDTH, CHUNK_HEIGHT, Position
|
||||||
|
|
@ -65,6 +66,20 @@ class ChunkDiff():
|
||||||
|
|
||||||
def empty(self):
|
def empty(self):
|
||||||
return not bool(self._chars)
|
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):
|
def jsonify_changes(changes):
|
||||||
dchanges = []
|
dchanges = []
|
||||||
|
|
|
||||||
|
|
@ -116,7 +116,7 @@ class Client():
|
||||||
elif i == "\n": self.map_.newline()
|
elif i == "\n": self.map_.newline()
|
||||||
#elif i in string.digits + string.ascii_letters + string.punctuation + " ":
|
#elif i in string.digits + string.ascii_letters + string.punctuation + " ":
|
||||||
#self.map_.write(i)
|
#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)
|
self.map_.write(i)
|
||||||
|
|
||||||
def connection_thread(self):
|
def connection_thread(self):
|
||||||
|
|
|
||||||
39
server.py
39
server.py
|
|
@ -35,12 +35,41 @@ class WotServer(WebSocket):
|
||||||
def handle_save_changes(self, dchanges):
|
def handle_save_changes(self, dchanges):
|
||||||
changes = dejsonify_changes(dchanges)
|
changes = dejsonify_changes(dchanges)
|
||||||
|
|
||||||
with pool:
|
# check whether changes are correct (exclude certain characters)
|
||||||
pool.apply_changes(changes)
|
# 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)
|
||||||
|
|
||||||
for client in clients:
|
if legitimate_changes:
|
||||||
if client:
|
with pool:
|
||||||
client.send_changes(changes)
|
pool.apply_changes(legitimate_changes)
|
||||||
|
|
||||||
|
for client in clients:
|
||||||
|
if client:
|
||||||
|
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):
|
def send_changes(self, changes):
|
||||||
changes = [chunk for chunk in changes if chunk[0] in self.loaded_chunks]
|
changes = [chunk for chunk in changes if chunk[0] in self.loaded_chunks]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue