pythonverse

Python-based client for OpenVerse with extra features
git clone https://code.literati.org/pythonverse.git
Log | Files | Refs | README | LICENSE

commit f991a8c9a89053e0a8ce2e186b773948949ae0c8
parent 028597def35a20a62255be5710f6931f29491753
Author: renze <renze>
Date:   Fri,  1 Feb 2002 00:10:48 +0000

Rewrote the Console render() method to draw forwards for colors, added some simple colorization, and some bug fixes

Diffstat:
MOpenVerse.py | 9++++++++-
Mpvui_pygame.py | 108+++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------
2 files changed, 78 insertions(+), 39 deletions(-)

diff --git a/OpenVerse.py b/OpenVerse.py @@ -179,6 +179,7 @@ class ServerConnection(transutil.Connection): '(\S+) (\d+) (\d+) (\S+) (\d+) (\S+) (\d+) (\d+)', (str, int, int, str, int, str, int, int)), ('DCCSENDAV', self.cmd_DCCSENDAV, '(\d+) (\S+)', (int, str)), + ('SUB', self.cmd_SUB, '(\S+) (\S+) (.*)', (str, str, str)), ('WHOIS', self.cmd_WHOIS, '(\S+) (.*)', (str, str))))) self.host = host self.port = port @@ -257,6 +258,9 @@ class ServerConnection(transutil.Connection): def getnick(self): return self.nick + def gethostport(self): + return (self.host, self.port) + def new_connect(self, host, port): self.close() self.host = host @@ -408,7 +412,7 @@ class ServerConnection(transutil.Connection): self.client.del_avatar(nick) def cmd_EXIT_OBJ(self, name, x1, y1, x2, y2, duration, host, port): - self.client.exit_obj(name, duration, host, port) + self.client.exit_obj(name, host, port) def cmd_DCCGET(self, port, filename, size): DCCGet(self.host, port, filename, size, @@ -433,6 +437,9 @@ class ServerConnection(transutil.Connection): if image2 is None: image2 = self.client.newimage() self.client.mouseover(name, (x, y), image1, image2) + def cmd_SUB(self, nick, command, cmdargs): + print 'TODO: Implement SUB' + def cmd_WHOIS(self, nick, text): self.client.chat(nick, '*%s* is %s' % (nick, text)) diff --git a/pvui_pygame.py b/pvui_pygame.py @@ -24,7 +24,7 @@ import pygame, pygame.font, pygame.image, pygame.time, pygame.draw from math import * from pygame.locals import * from types import * -import transutil +import transutil, color def wrap_lines(lines, width, font): r = [] @@ -32,7 +32,8 @@ def wrap_lines(lines, width, font): r.extend(wrap(text, width, font)) for t in range(len(r)): - if r[t] == "": del r[t] + if t < len(r): # in case any lines have been removed + if r[t] == "": del r[t] return r @@ -315,9 +316,24 @@ class Avatar(Sprite): if self.label is not None: print self.nick, 'on' self.label.die() + # Strip color codes from the nick for display - nick = self.nick - # TODO + nicklist = list(self.nick) + nick = '' + avoid = list(string.digits) + avoid.extend([',', '\x03']) + found = 0 + for n in nicklist: + if n == '\x03': + found = 1 + if found == 1: + try: + test = avoid.index(n) + except: + found = 0 + if found == 0: + nick = nick + n + sx, sy = _font.size(nick) s = _font.render(nick, 0, (255, 255, 255)) image = pygame.Surface((sx+2, sy+2)) @@ -805,39 +821,54 @@ class Console(Sprite): image.fill(bg) Sprite.__init__(self, rect, image) self.scrollback = scrollback - # Use a circular buffer for efficiency self.buffer = [''] * scrollback self.font = font self.bg = bg - self.index = 0 self.linesize = linesize(font) + self.numlines = int(self.rect.height / self.linesize) + 1 + self.currentcolor = (255, 255, 255) def add_lines(self, paragraphs): for paragraph in paragraphs: - self.buffer[self.index] = paragraph - self.index = self.index + 1 - if self.index >= self.scrollback: self.index = 0 - + del self.buffer[0] + self.buffer.append(paragraph) + return self.render() def render(self): """Update my image to match the current buffer""" self.image.fill(self.bg) - y = self.rect.height - 1 - i = self.index - while i and y > 0-linesize(_font): - i = i - 1 - lines = wrap(self.buffer[i], self.rect.width - 2, self.font) - j = len(lines) - while j and y > 0: - j = j - 1 - y = y - self.linesize - line = lines[j] + lines = self.buffer[(self.scrollback - self.numlines):] + newlines = [] + for line in lines: + if line != '': + newlines.extend(wrap(line, self.rect.width - 2, self.font)) + else: + newlines.append('') + lines = newlines[len(newlines) - self.numlines:] + x = 1 + y = self.rect.height - ((self.numlines * self.linesize) + 1) + for line in lines: + if line != '': + if line[0:1] == '<': + self.currentcolor = (255, 255, 255) + if line[0:1] == '*': + self.currentcolor = (0, 255, 255) + if line[0:1] == '!': + if line[1:14] == 'Whispering to': + self.currentcolor = (255, 191, 127) + else: + self.currentcolor = (255, 255, 0) + if line[0:1] == '[': + self.currentcolor = (0, 255, 0) + if line[0:2] == '->': + self.currentcolor = (0, 0, 255) ss = self.font.render(line, 0, (0,0,0)) for xx in range(3): for yy in range(3): - self.image.blit(ss, (xx, y + (yy - 1))) - self.image.blit(self.font.render(line, 1, (255,255,255)), (1, y)) + self.image.blit(ss, (x + (xx - 1), y + (yy - 1))) + self.image.blit(self.font.render(line, 1, self.currentcolor), (x, y)) + y = y + self.linesize return [self.rect] @@ -849,6 +880,7 @@ class Client: self.width, self.height = 640, 480 self.background = progress(0, (self.width, self.height)) self.server = None + self.sobjects = Group() self.sprites = Group() self.balloons = Group() self.text = Group() @@ -889,14 +921,16 @@ class Client: if self is not _active: return if rects is None: _display.blit(self.background, (0, 0)) + self.sobjects.draw(_display) self.sprites.draw(_display) - self.balloons.draw(_display) self.text.draw(_display) + self.balloons.draw(_display) pygame.display.update() else: if not type(rects) is ListType: rects = [rects] for rect in rects: _display.blit(self.background, rect, rect) + self.sobjects.draw(_display, rect) self.sprites.draw(_display, rect) self.text.draw(_display, rect) self.balloons.draw(_display, rect) @@ -988,7 +1022,7 @@ class Client: dirtyrects.append(urlmo.rect) del self.avatars[url] try: del self.exits['{' + url + '}_link'] - except: print url, 'is not an exit.' + except: print >> sys.stderr, url, 'is not an exit.' self.sprites.remove(urlmo) if dirtyrects: self.redraw(dirtyrects) @@ -1038,13 +1072,14 @@ class Client: else: if self.whichmouseover != 'ov_tram_exit': self.whichmouseover = '{' + self.whichmouseover + '}_link' - try: remtime, host, port = self.exits[self.whichmouseover] + try: host, port = self.exits[self.whichmouseover] except: self.server.move(event.pos) else: if port == -1: self.server.openurl(host) if port == -2: - print 'TODO: file transfers' + realhost, realport, filename, filesize = host + # TODO: file transfers if port > 0: self.cmd_connect(host, port) @@ -1110,13 +1145,9 @@ class Client: self.sprites.remove(avatar) self.redraw(dirtyrects) - def exit_obj(self, name, duration, host, port): + def exit_obj(self, name, host, port): if host != 'dummyhost': - if duration > 0: - remtime = pygame.time.get_ticks() + (duration * 1000) - else: - remtime = 0 - self.exits[name] = (remtime, host, port) + self.exits[name] = (host, port) else: del self.exits[name] @@ -1185,8 +1216,8 @@ class Client: offset = url.find('://') + 3 offset2 = url.find(':', offset) host = url[offset:offset2] - port = int(url[offset2 + 1:]) - self.exit_obj('{' + url + '}_link', 0, host, port) + port = int(url[offset2+1:]) + self.exit_obj('{' + url + '}_link', host, port) text = 'OV:' tagcolor = (255, 255, 0) if head == 'fil': @@ -1195,16 +1226,17 @@ class Client: offset3 = url.find('/', offset2) offset4 = url.find(':', offset3) host = url[offset:offset2] - port = url[offset2 + 1:offset3] + port = url[offset2+1:offset3] filename = url[offset3+1:offset4] filesize = url[offset4:] text = 'File' tagcolor = (0, 255, 255) - # have to implement DCC first + self.exit_obj('{' + url + '}_link' , + (host, port, filename, filesize), -2) if head == 'htt' or head == 'ftp' or head == 'mai': text = 'URL' tagcolor = (0, 255, 0) - self.exit_obj('{' + url + '}_link', 0, url, -1) + self.exit_obj('{' + url + '}_link', url, -1) # generate images for URL links image1 = pygame.Surface((40, 16)) image2 = pygame.Surface((40, 16)) @@ -1224,7 +1256,7 @@ class Client: image2.blit(_font.render(text, 1, (255, 255, 255)), (2, 0)) image1.blit(image2, (0, 0)) image1.set_alpha(127) - pos = (int((random.random()*600)+20), int((random.random()*458)+8)) + pos = (int((random.random()*600)+20), int((random.random()*445)+10)) urlmo = Mouseover(self.server, url, pos, image1, image2) self.sprites.add(urlmo) self.avatars[url] = urlmo