diff options
| author | troido <troido@hotmail.com> | 2017-10-29 23:31:46 +0100 |
|---|---|---|
| committer | troido <troido@hotmail.com> | 2017-10-29 23:31:46 +0100 |
| commit | d1f84633cff02d9d43c321814364b2c30c4fa3a9 (patch) | |
| tree | cf7fc926e0bc6c8dad5774d79cdb03f841fb675b | |
| parent | a8fd6fc77a3c5af06727a68684e61c63553679a8 (diff) | |
merged recent changes into package branch again
| -rw-r--r-- | asciifarm/client/display/__init__.py | 11 | ||||
| -rw-r--r-- | asciifarm/client/display/messagepad.py | 42 | ||||
| -rw-r--r-- | asciifarm/client/gameclient.py | 19 |
3 files changed, 68 insertions, 4 deletions
diff --git a/asciifarm/client/display/__init__.py b/asciifarm/client/display/__init__.py index dc553a8..870571e 100644 --- a/asciifarm/client/display/__init__.py +++ b/asciifarm/client/display/__init__.py @@ -7,6 +7,7 @@ from .healthpad import HealthPad from .inventorypad import InventoryPad from .screen import Screen from .colours import Colours +from .messagepad import MessagePad SIDEWIDTH = 20 @@ -33,6 +34,7 @@ class Display: self.groundPad = InventoryPad("Ground", 8) self.lastinfostring = None self.changed = False + self.messagePad = messagepad.MessagePad(5) def resizeField(self, size): @@ -65,6 +67,10 @@ class Display: def setGround(self, items): self.groundPad.setInventory(items) self.change() + + def addMessage(self, message): + self.messagePad.addMessage(message) + self.change() def getChar(self, sprite): char = self.characters.get(sprite, self.defaultChar) @@ -78,14 +84,17 @@ class Display: def update(self): if self.changed: fieldRight = min(self.fieldPad.getWidth(), self.screen.getWidth()-SIDEWIDTH-1) + fieldBottom = min(self.fieldPad.getHeight(), self.screen.getHeight()-self.messagePad.getHeight()) healthBottom = self.healthPad.getHeight() inventoryBottom = healthBottom + self.inventoryPad.getHeight() groundBottom = inventoryBottom + self.groundPad.getHeight() - self.fieldPad.update(self, 0,0,fieldRight, min(self.fieldPad.getHeight(), self.screen.getHeight())) + self.fieldPad.update(self, 0,0,fieldRight, fieldBottom) + self.messagePad.update(self, 0,fieldBottom, fieldRight, min(self.screen.getHeight(), fieldBottom+self.messagePad.getHeight())) self.healthPad.update(self, fieldRight+1,0, self.screen.getWidth(), healthBottom) self.inventoryPad.update(self, fieldRight+1, healthBottom, self.screen.getWidth(), min(self.screen.getHeight(), inventoryBottom)) self.groundPad.update(self, fieldRight+1, inventoryBottom, self.screen.getWidth(), min(self.screen.getHeight(), groundBottom)) self.infoPad.update(self, fieldRight+1,groundBottom+1, self.screen.getWidth(), self.screen.getHeight()) + curses.doupdate() self.changed = False diff --git a/asciifarm/client/display/messagepad.py b/asciifarm/client/display/messagepad.py new file mode 100644 index 0000000..50d34f5 --- /dev/null +++ b/asciifarm/client/display/messagepad.py @@ -0,0 +1,42 @@ + +import curses +import textwrap + +class MessagePad: + + def __init__(self, maxLines=10): + self.maxLines = maxLines + self.pad = curses.newpad(maxLines+2, 200) + self.changed = False + self.lastView = None + self.messages = [] + + def addMessage(self, message): + self.messages.append(message) + self.changed = True + + def getHeight(self): + return min(len(self.messages), self.maxLines) + + def update(self, screen, x, y, xmax, ymax): + if not self.changed and (x, y, xmax, ymax) == self.lastView : + return + width = xmax - x + height = ymax - y # should equal self.getHeight() + if height < 1: + return + lines = [] + for message in self.messages: + lines += textwrap.wrap(message, width) + if len(lines) > height: + lines = lines[len(lines)-height:] + self.pad.erase() + self.pad.addstr(0,0,'\n'.join(lines)) + self.changed = False + self.pad.noutrefresh( + 0, + 0, + y, + x, + ymax-1, + xmax-1) diff --git a/asciifarm/client/gameclient.py b/asciifarm/client/gameclient.py index d6e2595..6038fec 100644 --- a/asciifarm/client/gameclient.py +++ b/asciifarm/client/gameclient.py @@ -5,7 +5,6 @@ import sys import curses import threading -#import logging import json import getpass import argparse @@ -13,16 +12,16 @@ from .display.screen import Screen import string from .display import Display -#logging.basicConfig(filename="client.log", filemode='w', level=logging.DEBUG) class Client: - def __init__(self, stdscr, display, name, connection, keybindings): + def __init__(self, stdscr, display, name, connection, keybindings, logFile=None): self.stdscr = stdscr self.display = display self.name = name self.keepalive = True self.connection = connection + self.logFile = logFile self.commands = {} for key, commands in keybindings["input"].items(): @@ -89,14 +88,28 @@ class Client: health = msg[1] if health: self.display.setHealth(*health) + else: + self.log("You have died. Restart the client to respawn") if msgType == "inventory": self.display.setInventory(msg[1]) if msgType == "ground": self.display.setGround(msg[1]) + if msgType == "message": + self.log(msg[1]) + #self.display.addMessage(msg[1]) + #if self.logFile: + #with(open(self.logFile, 'a')) as f: + #f.write(msg[1]+'\n') self.display.update() + def log(self, text): + self.display.addMessage(text) + if self.logFile: + with(open(self.logFile, 'a')) as f: + f.write(text+'\n') + def command_loop(self): while self.keepalive: key = self.stdscr.getch() |
