variable comparisons

Posted by ErockMahan on Thu 23 Oct 2008 10:13 PM — 7 posts, 32,624 views.

#0
I'm trying to figure out a simple way to automatically log and track characters based on the ip address. What I would like to do, in theory, is to have the ip address saved as a variable and then to be able to pull up all instances of that variable occurring.

I always think seeing it makes more sense than explaining it:

Bob has logged in from 12.34.56.78
(trigger will save both "bob" and the port number)

Mike has logged in from 12.34.56.78
(trigger will save both "mike" and the port number)


I then want to run an alias that will call all instances of 12.34.56.78, thus showing me both "mike" and "bob"


Is this even possible?
Australia Forum Administrator #1
Anything is possible. ;0

First, this would be easier in Lua, because you have tables which have key/value combinations. However if you want to use VBscript, try using the array functions in MUSHclient:

http://www.gammon.com.au/scripts/doc.php?general=Arrays

In Lua I would have used a table of IP addresses, and for each IP address, another table of names which have used that address. Thus, for an address, I can get a list of all the names. However a similar thing could be done in other languages.
USA #2
In VB, you do have access to the Scripting.Dictionary object, which has the exact same functionality that Lua's tables have... although MC ships with the serialize library for Lua code, which makes persisting them easier. :)
#3
Here is the trigger:

<triggers>
<trigger
enabled="y"
keep_evaluating="y"
match="[ *[*] has connected.*"
send_to="12"
sequence="100"
>
<send>ArraySet "port", "%1", "%2"</send>
</trigger>
</triggers>

It triggers on this:
[ Bob[12.345.6.789] has connected. [22] ]



Here are the aliases I've tried using to see if it is working:

<aliases>
<alias
match="arrayget *"
enabled="y"
send_to="12"
sequence="100"
>
<send>Note ArrayGet ("port", "%1")</send>
</alias>
<alias
match="arraylist"
enabled="y"
send_to="12"
sequence="100"
>
<send>dim arrList

arrList = world.ArrayListAll

If Not IsEmpty (arrList) Then

For Each a In arrList
world.note a
Next

End If</send>
</alias>
</aliases>


Nothing happens when I use those aliases (or rather, a blank line comes through). What am I doing wrong?
Australia Forum Administrator #4
It helps to check the return code if nothing seems to be working. In your case I tried your trigger, modified a bit like this:


Note ArraySet ("port", "%1", "%2")


That returned 30056 (That array does not exist).

The array documentation notes you have to create an array before using it. One way is just to modify your trigger like this:


ArrayCreate "port"
ArraySet "port", "%1", "%2"


Only the first ArrayCreate will succeed (after that it will return an error "already exists") but as you will ignore the error that is no big deal.

After doing that, it worked and your alias to test the array did what you would expect.

#5
After getting it to "work", my alias will now show me the most recent who logged from what ip, and what ip applies to who. While this is great, it doesn't seem to work for multiple instances of the event. For example:

[ Bob[12.345.678.90] has connected. [19] ]
[ Bob has disconnected. [19] ]
[ Bobby[12.345.678.90] has connected. [19] ]

My alias will only pull up the most recent instance of the ip address (Bobby) instead of what I was hoping, which is that it would show both Bob and Bobby. Am I doing something wrong, or have I been going about this the wrong way?
Australia Forum Administrator #6
You really need a table within a table (or an array within an array).

Effectively you have: <array of names>

Now for each name you need <array of IP addresses>

(Or, for each IP address: <array of names>).

One way of going about that would be to use a comma-delimited string for each IP address. Thus the value (per IP address) might be:

Key: 12.345.678.90
Value: Bob,Bobby,Bruce

Now to add to that you first need to get Bob,Bobby,Bruce into another array. If you want to use the Array functions in MUSHclient, you need key/value pairs, but in this case you don't care about the values (the presence of the key is enough).

Thus it would really be:


Key: 12.345.678.90
Value: Bob,,Bobby,,Bruce,

You could import that into an array like this:


names = "Bob,,Bobby,,Bruce,"   ' original names

' make an array to hold the current names

ArrayCreate "names"
ArrayImport "names", names, ","

' our new name

newname = "Nick"

' add to array

ArraySet "names", newname, ""

' export back out

names = ArrayExport ("names", ",")

' see result 
Note (names)   ' --> Bob,,Bobby,,Bruce,,Nick,


Now this new list of names would be stored in the other array as the value for IP address 12.345.678.90.