Filtering out / altering part of the received text

Posted by Parduz on Mon 30 Jul 2012 02:08 PM — 10 posts, 36,409 views.

Italy #0
The MUD i play have a dedicated client, with some nice features (like visual map, clickable icons at the left of the description, etc).
To implement this, when it connect it sends a special string, and the server then "switches" from normal telnet to "dedicated client".
I'd like to keep this "dedicated" set using MUSH 'cause there's more infos sent from the server, but i need then to "convert" some of data received.
These is a list of the first thing i'd like to convert:

1) Line colors are no more ansi codes, but defined by "&#", where "#" is the initial of one of the standard color (so "&y" means a yellow font color. I learned how to "trap" that lines with a trigger, but how can i remove the "&#" part and replace it with the standard ANSI code?

2) The map is sent using non-ascii charachters. How can i "grab" them and send them to the (still to be made) mapping window?

3) How can i search for a string that begins with, say, - "AbCd - (the " char is part of the string) followed by 5 numbers, comma separated, and show only what follow the last comma in a new line?
(examples:
"AbCd-123,1,2,0,456345,This is the text i need to show
"AbCd-78,1,2,0,1234,This is another text i need to show
)

Thanks
Amended on Mon 30 Jul 2012 02:11 PM by Parduz
USA Global Moderator #1
Quote:
how can i remove the "&#" part and replace it with the standard ANSI code?


The simplest way to do this is

color_ANSI = {
   k = ANSI(0,30),
   r = ANSI(0,31),
   g = ANSI(0,32),
   y = ANSI(0,33),
   b = ANSI(0,34),
   m = ANSI(0,35),
   c = ANSI(0,36),
   w = ANSI(0,37),
   K = ANSI(1,30),
   R = ANSI(1,31),
   G = ANSI(1,32),
   Y = ANSI(1,33),
   B = ANSI(1,34),
   M = ANSI(1,35),
   C = ANSI(1,36),
   W = ANSI(1,37)
}

ANSIstring = string.gsub(str,"&([^&])", color_ANSI)


Quote:
The map is sent using non-ascii charachters. How can i "grab" them and send them to the (still to be made) mapping window?
Plenty of other threads talk about how to make triggers.

Quote:
How can i search for a string that begins with, say, - "AbCd - (the " char is part of the string) followed by 5 numbers, comma separated, and show only what follow the last comma in a new line?
Plenty of other threads talk about how to make triggers.
Amended on Tue 31 Jul 2012 12:43 AM by Fiendish
Italy #2
Fiendish said:

Quote:
how can i remove the "&#" part and replace it with the standard ANSI code?


The simplest way to do this is

color_ANSI = {
   k = ANSI(0,30),
   r = ANSI(0,31),
   g = ANSI(0,32),
   y = ANSI(0,33),
   b = ANSI(0,34),
   m = ANSI(0,35),
   c = ANSI(0,36),
   w = ANSI(0,37),
   K = ANSI(1,30),
   R = ANSI(1,31),
   G = ANSI(1,32),
   Y = ANSI(1,33),
   B = ANSI(1,34),
   M = ANSI(1,35),
   C = ANSI(1,36),
   W = ANSI(1,37)
}

ANSIstring = string.gsub(str,"&([^&])", color_ANSI)

It works, but then i'd like to add some other values to this table.... how can i add a "-" to it (the client receive lines containing the string "&-")?


Fiendish said:
Quote:
The map is sent using non-ascii charachters. How can i "grab" them and send them to the (still to be made) mapping window?
Plenty of other threads talk about how to make triggers.
Quote:
How can i search for a string that begins with, say, - "AbCd - (the " char is part of the string) followed by 5 numbers, comma separated, and show only what follow the last comma in a new line?
Plenty of other threads talk about how to make triggers.

I know. I've read a lot of page. It may be that LUA is new for me, it may be that RegExp are barely understandable, for me; it may be that being English not my first language i'm missing something. It may be that i'm dumb, also.
Anyway, i've still to found a solution.
I tried with a regExp generator (http://osteele.com/tools/rework/#) but the result is not "valid" in the script.

I'll try to ask again, expanding my question.

This are a packet i receive, breaking it in multiple lines to show the various "fields" i need to work with:
1) &d001100
2) &!pi" [»²»2©]2»²])2»2)6»²]\» \;"
3) &!t"Le stalle"
4) &!d"Questa deve essere la stanza dove vengono custoditi i cavalli. Qui il tipico odore di cavalli e ronzini e' molto piu' forte. Non riesci a scorgere il pavimento, tanta e' la paglia che lo ricopre. Su un soppalco sono accumulati grossi fasci di fieno ed avena e alzando lo sguardo noti le grosse travi che sorreggono il soffitto."
5) &!ao"605,0,1,0,70312,Vedi una torcia accesa, posta su un sostegno di ferro infisso alla parete. "
5) &!am"157,0,1,0,632008,Un grasso nano si gratta nervosamente la barba."
6) &o6
7) &!st"22,22,0,0,115,131"

They are, in the order:
1) Unknown data
2) Map data (contains also non ascii chars)
3) Title
4) Description
5) Room contents
6) Weather graphics
7) Status data (health, stamina, mana)

What i'd need, after some thoughs, is to remove the "&!" special code, save the relative lines (and numeric fields) in some global variables, and put in the output everything is between the << " >> chars.
So i put your code in the plugin OnPluginPacketReceived function, and i tried to expand that function.

Trying to follow your example, and using that RegExp generator, i wrote a line to remove the numeric fields from 5)

ANSIstring,n = string.gsub (ANSIstring, '&!ao"([0-9]*,)([0-9]*,)([0-9]*,)([0-9]*,)([0-9]*,)', "\n&g")

Still, i have problems 'cause i'm not able to search for the ([0-9]*,) pattern 5 times, i don't know how to save what i've found in a variable, and how to keep only what's inside the quotes.

Thanks



Amended on Thu 30 Aug 2012 04:02 PM by Parduz
Australia Forum Administrator #3
Quote:

Still, i have problems 'cause i'm not able to search for the ([0-9]*,) pattern 5 times, i don't know how to save what i've found in a variable, and how to keep only what's inside the quotes.


Just to address that point:


(([0-9]+,){5})



Template:regexp
Regular expressions
  • Regular expressions (as used in triggers and aliases) are documented on the Regular expression tips forum page.
  • Also see how Lua string matching patterns work, as documented on the Lua string.find page.
USA Global Moderator #4
Parduz said:
It works, but then i'd like to add some other values to this table.... how can i add a "-" to it (the client receive lines containing the string "&-")?

I don't understand. What color is "&-" supposed to be?

Quote:
Trying to follow your example, and using that RegExp generator, i wrote a line to remove the numeric fields from 5)

ANSIstring,n = string.gsub (ANSIstring, '&!ao"([0-9]*,)([0-9]*,)([0-9]*,)([0-9]*,)([0-9]*,)', "\n&g")

Er, I think maybe you should read about string.gsub at http://lua-users.org/wiki/StringLibraryTutorial
And why are you doing things there instead of as simple triggers?
Amended on Sat 01 Sep 2012 03:33 AM by Fiendish
Italy #5
Fiendish said:

Parduz said:
It works, but then i'd like to add some other values to this table.... how can i add a "-" to it (the client receive lines containing the string "&-")?

I don't understand. What color is "&-" supposed to be?

It's the "command" to reset to default color (white) so that each line that have no "&" key is shown in that color.

Fiendish said:

Quote:
Trying to follow your example, and using that RegExp generator, i wrote a line to remove the numeric fields from 5)

ANSIstring,n = string.gsub (ANSIstring, '&!ao"([0-9]*,)([0-9]*,)([0-9]*,)([0-9]*,)([0-9]*,)', "\n&g")

Er, I think maybe you should read about string.gsub at http://lua-users.org/wiki/StringLibraryTutorial

I read a lot about it, and still feeling lost.

Fiendish said:

And why are you doing things there instead of as simple triggers?

'cause i see this all as the necessary "work" to make the server packet "compatible" with MUSHclient. At "packet level" i'd do all the jobs that are related to what the user "can't see" (so converting the strings in maps, weather and stats images, etc). Without user triggers, just loading the plugin will make MUSHclient similar to the original client behaviour as much as possible.
Then i plan to write triggers to add features that that client/server actually lacks.
Is this a bad/wrong/inefficent approach? If yes, why?
USA Global Moderator #6
Parduz said:

Fiendish said:

Parduz said:
It works, but then i'd like to add some other values to this table.... how can i add a "-" to it (the client receive lines containing the string "&-")?

I don't understand. What color is "&-" supposed to be?

It's the "command" to reset to default color (white) so that each line that have no "&" key is shown in that color.

Ah, interesting. In order to have special characters like - as a table key, you need to do use ["-"] instead of just -.
So change the color_ANSI table to be this:

color_ANSI = {
   k = ANSI(0,30),
   r = ANSI(0,31),
   g = ANSI(0,32),
   y = ANSI(0,33),
   b = ANSI(0,34),
   m = ANSI(0,35),
   c = ANSI(0,36),
   w = ANSI(0,37),
   K = ANSI(1,30),
   R = ANSI(1,31),
   G = ANSI(1,32),
   Y = ANSI(1,33),
   B = ANSI(1,34),
   M = ANSI(1,35),
   C = ANSI(1,36),
   W = ANSI(1,37),
   ["-"] = ANSI(0)
}


Parduz said:

Fiendish said:

And why are you doing things there instead of as simple triggers?

'cause i see this all as the necessary "work" to make the server packet "compatible" with MUSHclient. At "packet level" i'd do all the jobs that are related to what the user "can't see" (so converting the strings in maps, weather and stats images, etc). Without user triggers, just loading the plugin will make MUSHclient similar to the original client behaviour as much as possible.
Then i plan to write triggers to add features that that client/server actually lacks.
Is this a bad/wrong/inefficent approach? If yes, why?
I think it is a bad/inefficient approach. You are doing twice the work in two different places to convert the lines to a different format and then convert them again to another different format. You could just trigger on the original lines and do one conversion in one place to the final output.
Italy #7
Fiendish said:

Ah, interesting. In order to have special characters like - as a table key, you need to do use ["-"] instead of just -.

Thanks!
Fiendish said:

I think it is a bad/inefficient approach. You are doing twice the work in two different places to convert the lines to a different format and then convert them again to another different format. You could just trigger on the original lines and do one conversion in one place to the final output.

Perhaps i've not been clear, sorry for that.
The conversion we're talking on the first lines (the function that use the table) is the last "step" in the received packet event. Before of it i do the other jobs (removing the "&!" lines). So everything happens in the same "event" (the packet received) before making it "available" on the output window.
If this is still inefficient, then pls teach me where should i do it all. Pls note that i need to "grab" that "&!" lines as they contains needed data....

.... and while we're talking about them, how could i store that data while i remove them from the output?
I'm reading LUA tables, and seems to me that i could use them both as "structures" and as arrays.... my C++ heritage makes understanding LUA a bit hard :)
Australia Forum Administrator #8
Just to keep the steam from emerging from my ears, please refer to it as Lua, not LUA. It isn't an acronym.

Yes, tables can be used, in effect, as structures. Since a table can contain other data types, including tables, you can effectively make the top-level table be an "array" so to speak, and then each array item is another table, this being the structure.

http://www.gammon.com.au/forum/?id=4903

http://www.gammon.com.au/forum/?id=6036
USA Global Moderator #9
Parduz said:

I'm reading Lua tables, and seems to me that i could use them both as "structures" and as arrays.... my C++ heritage makes understanding Lua a bit hard :)


In C++ terms, Lua's table is a class that inherits from std::map. It does two things differently than std::map.
1) It overloads insert with a function that automatically assigns a sequential numeric key if no key is provided
2) It allows for other arbitrary dynamic method and operator overrides (via the Lua metatable) as if each C++ member method were actually entries in a map of <method name, function pointer> pairs that happen to point to the default set of functions but can be reassigned or removed or created at any time.