[gnome-mud] Connection IDs and isFocus() patch



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

* Adds the field 'connection_id' to the the Connection object which contains a 
unique connection ID. Connection IDs are assigned sequentially each time 
gnome-mud connects to a server.

* Adds the method isFocus() to the Connection object which returns 1 (true) if 
the connection object is the current focus connection (tab the user is 
viewing) and false if it isn't.

Both of these are features that I encountered a need for when trying to 
implement support for multiple simultaneous muds in my HPInfo plugin and 
would likely be needed by other plugin authors who attempt the same.

There is a bit of 'debris' in this patch, which is the commented out attempt 
at an equals() method for the Connection class. It's intended to serve a 
similar purpose as the 'connection_id' field but there are some cases where 
in coding where it would simply be a neater solution (although the 
connection_id field has it's own advantages, such as possibility of combining 
it with dictionaries :).

Updated version of HPInfo which takes advantage of the new features is 
included. Also included support for multiple regexes per mud.

- -- 
<Fooz> In a perfect world... spammers would get caught, go to jail, and share 
a cell with many men who have enlarged their penisses, taken Viagra and are 
looking for a new relationship.

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)

iD8DBQFAEQRK94zI/zB2CYURAqk4AJ9WIlXZ2hTY0jXQeiHNuEZpbdkI/wCdHsJe
hI67tlqM+7Z8HX8LONeykwc=
=etTv
-----END PGP SIGNATURE-----
diff -ur ../gnome-mud-0.10.4a/config.log ./config.log
--- ../gnome-mud-0.10.4a/config.log	2004-01-17 15:47:28.000000000 +0930
+++ ./config.log	2004-01-22 01:24:02.000000000 +0930
@@ -1573,3 +1573,21 @@
 #define VERSION "0.10.4a"
 
 configure: exit 0
+
+## ---------------------- ##
+## Running config.status. ##
+## ---------------------- ##
+
+This file was extended by gnome-mud config.status 0.10.4a, which was
+generated by GNU Autoconf 2.58.  Invocation command line was
+
+  CONFIG_FILES    = 
+  CONFIG_HEADERS  = 
+  CONFIG_LINKS    = 
+  CONFIG_COMMANDS = 
+  $ ./config.status src/Makefile depfiles
+
+on ironclad
+
+config.status:804: creating src/Makefile
+config.status:1252: executing depfiles commands
Only in ./: gnome-mud-0.10.4a-cid.patch
Binary files ../gnome-mud-0.10.4a/src/gnome-mud and ./src/gnome-mud differ
diff -ur ../gnome-mud-0.10.4a/src/gnome-mud.h ./src/gnome-mud.h
--- ../gnome-mud-0.10.4a/src/gnome-mud.h	2004-01-17 14:33:03.000000000 +0930
+++ ./src/gnome-mud.h	2004-01-23 08:59:49.000000000 +0930
@@ -101,6 +101,7 @@
 	GtkWidget		*vscrollbar;
   	gint             telnet_state;
   	gint             telnet_subneg;
+    gint             conn_id;  
 };
 
 struct system_data {
diff -ur ../gnome-mud-0.10.4a/src/net.c ./src/net.c
--- ../gnome-mud-0.10.4a/src/net.c	2004-01-17 14:33:03.000000000 +0930
+++ ./src/net.c	2004-01-23 17:38:33.000000000 +0930
@@ -143,7 +143,8 @@
 	PROFILE_DATA	*pd;
 	GtkWidget		*label;
 	GtkWidget		*box;
-
+  static gint c_id = 0;
+  
 	if (main_connection->connected)
 	{
 		box = gtk_hbox_new(FALSE, 0);
@@ -166,6 +167,7 @@
 	g_free(c->host); g_free(c->port);
 	c->host = g_strdup(hoster);
 	c->port = g_strdup(porter);
+  c->conn_id = c_id++;
 
 	if ((pd = profiledata_find(profile)) == NULL)
 	{
diff -ur ../gnome-mud-0.10.4a/src/python.c ./src/python.c
--- ../gnome-mud-0.10.4a/src/python.c	2004-01-17 14:33:19.000000000 +0930
+++ ./src/python.c	2004-01-23 20:38:11.000000000 +0930
@@ -126,18 +126,48 @@
 	return Py_None;
 }
 
+/* FIXME: Doesn't work /
+static int *pyConnection_equals(pyConnection_ConnectionObject *self, PyObject *args)
+{
+  PyObject *conn2;
+  gint      cid2;
+  
+  if (!PyArg_ParseTuple(args, "O", &conn2))
+    return 0; /* False /
+    
+  if (conn2->ob_type == &pyConnection_ConnectionType) {
+    //conn2->m->connection->conn_id = 0;
+  } else
+    return 0; /* False /  
+}*/
+
+static PyObject *pyConnection_isFocus(pyConnection_ConnectionObject *self) {
+  gint i;
+ 
+  i = gtk_notebook_get_current_page (GTK_NOTEBOOK (main_notebook));
+  
+  if (self->connection->conn_id == connections[i]->conn_id) {
+    return PyInt_FromLong(1); /* True */
+  } else {
+    return PyInt_FromLong(0); /* False */
+  }
+}
+
 static PyMethodDef pyConnection_methods[] =
 {
-    { "send",  (PyCFunction) pyConnection_send,  METH_VARARGS },
-    { "write", (PyCFunction) pyConnection_print, METH_VARARGS },
+    { "send",     (PyCFunction) pyConnection_send,     METH_VARARGS },
+    { "write",    (PyCFunction) pyConnection_print,    METH_VARARGS },
+    /*{ "equals",   (PyCFunction) pyConnection_equals,   METH_VARARGS },*/
+    { "isFocus",  (PyCFunction) pyConnection_isFocus,  METH_NOARGS },
     {NULL, NULL}
 };
 
 static struct memberlist pyConnection_memberlist[] = 
 {
-    { "connected", T_INT,    offsetof(struct connection_data,connected), RO },
-    { "host",      T_STRING, offsetof(struct connection_data,host),      RO },
-    { "port",      T_STRING, offsetof(struct connection_data,port),      RO },
+    { "connected",           T_INT,    offsetof(struct connection_data,connected), RO },
+    { "host",                T_STRING, offsetof(struct connection_data,host),      RO },
+    { "port",                T_STRING, offsetof(struct connection_data,port),      RO },
+    { "connection_id",       T_INT,    offsetof(struct connection_data,conn_id),   RO },
     {NULL}
 };
 
#!/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

HPI_VER = "1.1"

#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  or host 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]*)\|? +"],
             "mud.merentha.com:10000"      : ["([0-9]*) hps*\|? *([0-9]*) sp*\|? *([0-9]*) mp*\|? *",
                                              "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]
  elif MUD_REGEX.has_key(host): #Do some muds provide multiple ports to connect to?
    return MUD_REGEX[host]
  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("#2222AA"))
    self.mpbar.modify_base(gtk.STATE_ACTIVE,      gtk.gdk.color_parse("#4444CC"))
    self.mpbar.modify_base(gtk.STATE_PRELIGHT,    gtk.gdk.color_parse("#4444CC"))
    self.mpbar.modify_base(gtk.STATE_SELECTED,    gtk.gdk.color_parse("#4444CC"))
    self.mpbar.modify_base(gtk.STATE_INSENSITIVE, gtk.gdk.color_parse("#222288"))
    
    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
    hpinfo.render(1.0,1.0,1.0,2.0,2.0,2.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
      
  def renderHPInfo(self):
    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)
      
      for regex in getRegex(c.host,c.port):
        m = re.search(regex,noansi)
        if m:
          self.updateHPInfo(m)
    if c.isFocus():
      self.renderHPInfo()
     
def inputText(c,s):
  if c.cid not in players:
    players[c.connection_id] = PlayerState()
  players[c.connection_id].inputText(c,s)
   
if __name__ == "__main__":
  hpinfo = HPInfo()
  players = {}
  #player = PlayerState()
     
  GnomeMud.add_user_widget(hpinfo.frame,1,1,5)
  GnomeMud.register_input_handler(inputText)
  
  c = GnomeMud.connection()
  c.write("\n[*]-----------------------------------------------------[*]\n")
  c.write("HP-Info plugin " + HPI_VER +" 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]