Re: [gnome-mud] PyGTK demo plugin



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Sunday 18 January 2004 13:33, Adam Luchjenbroers wrote:
> This plugin will display the users HP/SP/MP values as 3 bars at the top of
> the screen. It's designed for the mud "Primal Darkness" but should be
> adaptable to any mud that provides numeric values for these stats.
>
> If you need to change how its read, you'll want to modify the regex on line
> 133.
> m = re.search("hp: ([0-9]*)\|? *sp: ([0-9]*)\|? *mp: ([0-9]*)\|? *",noansi)

Updated plugin, added comments at the top of the code that say I'm the author 
of the plugin and that place the plugin under GPL. Added text to the loader 
message also saying that the plugin is under GPL.

The regex that extracts the HP/SP/MP tables is now read using a lookup table 
at the top of the script. This should allow players of multiple muds to more 
easily make it work with all of their chosen muds. An example entry is 
provided to show them how to do it.

I was working on code to keep a seperate player-state object for each 
connection. But apparently every time the connection is handled to python, 
it's a different object even if it represents the same connection. I suggest 
we look into some form of connection-unique ID number or a 
connection.equals(connection2) method.

For scripts to handle multiple connections at once, this may well be a 
necessity, host + port alone are not unique enough (multiple connections to 
the same MUD are possible). I'll look into this myself but any advice on 
where to start would be appreciated.

I also suggest moving the methods under 'Useful shiznat' into the GnomeMud 
module, and there are two other methods I have written that are also worth 
adding (ttoi and makeAnsi). These provide methods for things many scripters 
are likely to want to do. I'll look into how we might do that aswell.

- -- 
The world is a cat toy.

Please avoid sending me Word or PowerPoint attachments.
See http://www.fsf.org/philosophy/no-word-attachments.html

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFADhbZ94zI/zB2CYURAohjAJ9hivTy5WYXIUfGGkakJMSF5Pg0GgCgmTDb
ViEJpemHANrPY/HF6sHY3Eg=
=CISu
-----END PGP SIGNATURE-----
#!/usr/bin/python

# This plugin is copyright 2003 Adam Luchjenbroers.
# Use and distribution of this plugin is permitted under the terms of the GNU General Public License.
# http://www.gnu.org/licenses/gpl.html

import gtk
import re
import GnomeMud

#The regex used for parsing prompt strings and status lines is determined using this list
#default is used if no entry corresponding to the host:port combination that has been connected to.
MUD_REGEX = {"default"                     : "hp: ([0-9]*)\|? *sp: ([0-9]*)\|? *mp: ([0-9]*)\|? *", 
             "mud.primaldarkness.com:5000" : "hp: ([0-9]*)\|? *sp: ([0-9]*)\|? *mp: ([0-9]*)\|? *"}

def getRegex(host,port):
  addr = host + ":" + port
  if MUD_REGEX.has_key(addr):
    return MUD_REGEX[addr]
  else:
    return MUD_REGEX["default"]             
             
# ===========================================================
# Useful shiznat
# ---------
# Not a class that handles a UI component, just a bunch of 
# useful methods
# ===========================================================

#Strip ANSI colour codes from string s 
def stripAnsi(s):
  return re.sub("\[[\d;]*m","",s)

def atoi(s):
   num = 0
   for i in range(len(s)):
    c = s[i]
    if c.isdigit():
      num *= 10
      num += (ord(c) - ord('0'))
    
   return num

# ===========================================================
# HPInfo
# ---------
# Displays HP/MP/SP information as a row of vertical bars
# 
# Constructor values, h,s,m correspond to the maximums for
# hp, sp and mp respectively
# ===========================================================        
class HPInfo:
  
  def __init__(self):
    
    self.frame = gtk.VBox()
     
    self.hpbar = gtk.ProgressBar()
    self.spbar = gtk.ProgressBar()
    self.mpbar = gtk.ProgressBar() 
    
    self.hpbar.modify_base(gtk.STATE_NORMAL,      gtk.gdk.color_parse("#880000"))
    self.hpbar.modify_base(gtk.STATE_ACTIVE,      gtk.gdk.color_parse("#AA0000"))
    self.hpbar.modify_base(gtk.STATE_PRELIGHT,    gtk.gdk.color_parse("#AA0000"))
    self.hpbar.modify_base(gtk.STATE_SELECTED,    gtk.gdk.color_parse("#AA0000"))
    self.hpbar.modify_base(gtk.STATE_INSENSITIVE, gtk.gdk.color_parse("#660000"))
    
    self.spbar.modify_base(gtk.STATE_NORMAL,      gtk.gdk.color_parse("#008800"))
    self.spbar.modify_base(gtk.STATE_ACTIVE,      gtk.gdk.color_parse("#00AA00"))
    self.spbar.modify_base(gtk.STATE_PRELIGHT,    gtk.gdk.color_parse("#00AA00"))
    self.spbar.modify_base(gtk.STATE_SELECTED,    gtk.gdk.color_parse("#00AA00"))
    self.spbar.modify_base(gtk.STATE_INSENSITIVE, gtk.gdk.color_parse("#006600"))
    
    self.mpbar.modify_base(gtk.STATE_NORMAL,      gtk.gdk.color_parse("#000088"))
    self.mpbar.modify_base(gtk.STATE_ACTIVE,      gtk.gdk.color_parse("#0000AA"))
    self.mpbar.modify_base(gtk.STATE_PRELIGHT,    gtk.gdk.color_parse("#0000AA"))
    self.mpbar.modify_base(gtk.STATE_SELECTED,    gtk.gdk.color_parse("#0000AA"))
    self.mpbar.modify_base(gtk.STATE_INSENSITIVE, gtk.gdk.color_parse("#000066"))
    
    self.hpbar.show()
    self.spbar.show()
    self.mpbar.show()   
    
    self.frame.add(self.hpbar)
    self.frame.add(self.spbar)
    self.frame.add(self.mpbar)
    
    self.frame.show()
    
  def render(self,hp=1.0, sp=1.0, mp=1.0, hpmax=2.0, spmax=2.0, mpmax=2.0):
    
    if hp > 0:
      self.hpbar.set_fraction(hp/hpmax)
    else:
      self.hpbar.set_fraction(0)
      
    if sp > 0:
      self.spbar.set_fraction(sp/spmax)
    else:
      self.spbar.set_fraction(0)
      
    if mp > 0:
      self.mpbar.set_fraction(mp/mpmax)
    else:
      self.mpbar.set_fraction(0)

# ===========================================================
# PlayerState
# ---------
# Tracks player information, vitals, and whatever else we 
# have code to track.
# ===========================================================        
class PlayerState:
    
  def __init__(self):
    #Initial values for playerstate
    self.hp = self.hpmax = 1.0
    self.sp = self.spmax = 1.0
    self.mp = self.mpmax = 1.0
   
  def updateHPInfo(self,m):
    self.hp = atoi(m.group(1)) * 1.0
    self.sp = atoi(m.group(2)) * 1.0
    self.mp = atoi(m.group(3)) * 1.0
    if self.hp > self.hpmax:
      self.hpmax = self.hp
    if self.sp > self.spmax:
      self.spmax = self.sp
    if self.mp > self.mpmax:
      self.mpmax = self.mp
    hpinfo.render(self.hp,self.sp,self.mp,self.hpmax,self.spmax,self.mpmax)
  
  def inputText(self,c,s):
   
    lines = s.splitlines()
 
    for line in lines:
      noansi = stripAnsi(line)
      m = re.search(getRegex(c.host,c.port),noansi)
      if m:
        self.updateHPInfo(m)
   
if __name__ == "__main__":
  hpinfo = HPInfo()
  player = PlayerState()
     
  GnomeMud.add_user_widget(hpinfo.frame,1,1,5)
  GnomeMud.register_input_handler(player.inputText)
  
  c = GnomeMud.connection()
  c.write("\n[*]-----------------------------------------------------[*]\n")
  c.write("HP-Info plugin 1.0 loaded\n")
  c.write("Distribution and use of this plugin is covered under the \nterms of the GPL\n")
  c.write("[*]-----------------------------------------------------[*]\n")



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]