Save changes to logfile if supplied with logfile name
This commit is contained in:
parent
4822cab0fc
commit
cc31ed6be9
1 changed files with 34 additions and 8 deletions
42
client.py
42
client.py
|
|
@ -4,6 +4,7 @@ import os
|
||||||
import string
|
import string
|
||||||
import sys
|
import sys
|
||||||
import threading
|
import threading
|
||||||
|
import time
|
||||||
import websocket
|
import websocket
|
||||||
from websocket import WebSocketException as WSException
|
from websocket import WebSocketException as WSException
|
||||||
|
|
||||||
|
|
@ -13,17 +14,19 @@ from utils import Position
|
||||||
from clientchunkpool import ClientChunkPool
|
from clientchunkpool import ClientChunkPool
|
||||||
|
|
||||||
class Client():
|
class Client():
|
||||||
def __init__(self, address):
|
def __init__(self, address, logfile=None):
|
||||||
self.stopping = False
|
self.stopping = False
|
||||||
|
|
||||||
|
self.map_ = None
|
||||||
|
self.chunkmap = None
|
||||||
self.chunkmap_active = False
|
self.chunkmap_active = False
|
||||||
|
|
||||||
self.address = f"ws://{address}/"
|
self.address = f"ws://{address}/"
|
||||||
self._drawevent = threading.Event()
|
self._drawevent = threading.Event()
|
||||||
self.pool = ClientChunkPool(self)
|
self.pool = ClientChunkPool(self)
|
||||||
#self.map_ = Map(sizex, sizey, self.pool)
|
|
||||||
#self.chunkmap = Chunkmap(sizex, sizey, self.pool) # size changeable by +/-?
|
|
||||||
|
|
||||||
#self.sock = socket.Socket(...)
|
self.logfile = logfile
|
||||||
|
self.log_messages = []
|
||||||
|
|
||||||
def launch(self, stdscr):
|
def launch(self, stdscr):
|
||||||
# connect to server
|
# connect to server
|
||||||
|
|
@ -61,7 +64,7 @@ class Client():
|
||||||
while not self.stopping:
|
while not self.stopping:
|
||||||
self._drawevent.wait()
|
self._drawevent.wait()
|
||||||
self._drawevent.clear()
|
self._drawevent.clear()
|
||||||
stdscr.noutrefresh()
|
|
||||||
with self.map_ as m:
|
with self.map_ as m:
|
||||||
m.draw()
|
m.draw()
|
||||||
|
|
||||||
|
|
@ -76,9 +79,22 @@ class Client():
|
||||||
|
|
||||||
curses.doupdate()
|
curses.doupdate()
|
||||||
|
|
||||||
|
if self.logfile:
|
||||||
|
self.save_log(self.logfile)
|
||||||
|
|
||||||
def redraw(self):
|
def redraw(self):
|
||||||
self._drawevent.set()
|
self._drawevent.set()
|
||||||
|
|
||||||
|
def log(self, message):
|
||||||
|
if self.logfile:
|
||||||
|
self.log_messages.append(message)
|
||||||
|
|
||||||
|
def save_log(self, filename):
|
||||||
|
with open(filename, "w") as f:
|
||||||
|
f.write(f"[[[ {int(time.time())} ]]]\n")
|
||||||
|
for msg in self.log_messages:
|
||||||
|
f.write(msg + "\n")
|
||||||
|
|
||||||
def input_thread(self, scr):
|
def input_thread(self, scr):
|
||||||
while True:
|
while True:
|
||||||
i = scr.get_wch()
|
i = scr.get_wch()
|
||||||
|
|
@ -90,6 +106,11 @@ class Client():
|
||||||
elif i == 267: # F3
|
elif i == 267: # F3
|
||||||
self.map_.alternating_colors = not self.map_.alternating_colors
|
self.map_.alternating_colors = not self.map_.alternating_colors
|
||||||
self.redraw()
|
self.redraw()
|
||||||
|
elif i == 268:
|
||||||
|
self.stdscr_visible = not self.stdscr_visible
|
||||||
|
if self.stdscr_visible: self.stdscr.redrawwin()
|
||||||
|
else: self.map_.redraw()
|
||||||
|
self.redraw()
|
||||||
elif i == 269: # F5
|
elif i == 269: # F5
|
||||||
self.map_.redraw()
|
self.map_.redraw()
|
||||||
# scrolling the map (10 vertical, 20 horizontal)
|
# scrolling the map (10 vertical, 20 horizontal)
|
||||||
|
|
@ -119,6 +140,8 @@ class Client():
|
||||||
elif isinstance(i, str) and len(i) == 1 and ord(i) > 31 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)
|
||||||
|
|
||||||
|
self.log(f"K: {i!r}")
|
||||||
|
|
||||||
def connection_thread(self):
|
def connection_thread(self):
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
|
|
@ -153,14 +176,17 @@ class Client():
|
||||||
self._ws.send(json.dumps(message))
|
self._ws.send(json.dumps(message))
|
||||||
|
|
||||||
def main(argv):
|
def main(argv):
|
||||||
if len(argv) != 2:
|
if len(argv) == 2:
|
||||||
|
client = Client(argv[1])
|
||||||
|
elif len(argv) == 3:
|
||||||
|
client = Client(argv[1], argv[2])
|
||||||
|
else:
|
||||||
print("Usage:")
|
print("Usage:")
|
||||||
print(f" {argv[0]} address")
|
print(f" {argv[0]} address [logfile]")
|
||||||
return
|
return
|
||||||
|
|
||||||
os.environ.setdefault('ESCDELAY', '25') # only a 25 millisecond delay
|
os.environ.setdefault('ESCDELAY', '25') # only a 25 millisecond delay
|
||||||
|
|
||||||
client = Client(argv[1])
|
|
||||||
curses.wrapper(client.launch)
|
curses.wrapper(client.launch)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue