This is a first try at connecting multiple clients using a server. The commit includes a lot of debugging messages. I will hopefully clean up the server and some of the client code.
74 lines
1.7 KiB
Python
74 lines
1.7 KiB
Python
import threading
|
|
from chunks import ChunkPool
|
|
|
|
import sys
|
|
|
|
class ClientChunkPool(ChunkPool):
|
|
"""
|
|
A ChunkPool that requests/loads chunks from a client.
|
|
"""
|
|
|
|
def __init__(self, client):
|
|
super().__init__()
|
|
|
|
self._client = client
|
|
self._save_thread = None
|
|
|
|
def set(self, pos, chunk):
|
|
super().set(pos, chunk)
|
|
|
|
#def commit_changes(self):
|
|
#changes = []
|
|
|
|
#for pos, chunk in self._chunks.items():
|
|
#changes.append((pos, chunk.get_changes()))
|
|
#chunk.commit_changes()
|
|
|
|
#return changes
|
|
|
|
def apply_changes(self, changes):
|
|
super().apply_changes(changes)
|
|
|
|
self._client.redraw()
|
|
|
|
def save_changes_delayed(self):
|
|
sys.stderr.write("Pre-HEHEHE\n")
|
|
if not self._save_thread:
|
|
def threadf():
|
|
sys.stderr.write("HEHEHE\n")
|
|
self.save_changes()
|
|
self._save_thread = None
|
|
self._save_thread = threading.Timer(.25, threadf)
|
|
self._save_thread.start()
|
|
|
|
def save_changes(self):
|
|
changes = self.commit_changes()
|
|
dchanges = []
|
|
for pos, change in changes:
|
|
dchange = change.to_dict()
|
|
if dchange:
|
|
dchanges.append((pos, dchange))
|
|
if dchanges:
|
|
self._client.send_changes(dchanges)
|
|
|
|
def load(self, pos):
|
|
raise Exception
|
|
|
|
def load_list(self, coords):
|
|
coords = [pos for pos in coords if pos not in self._chunks]
|
|
if coords:
|
|
self._client.request_chunks(coords)
|
|
|
|
#def unload(self, pos):
|
|
#raise Exception
|
|
|
|
def unload_list(self, coords):
|
|
if coords:
|
|
#self.save_changes()
|
|
self._client.unload_chunks(coords)
|
|
super().unload_list(coords)
|
|
|
|
# What needs to happen differently from the default implementation:
|
|
# loading -> only ask server when necessary
|
|
# unloading -> send message to server
|
|
# unloading -> commit changes when anything is actually unloaded
|