Forum

> > CS2D > Scripts > Parsing player message
Forums overviewCS2D overview Scripts overviewLog in to reply

English Parsing player message

6 replies
To the start Previous 1 Next To the start

old Parsing player message

Kisses
User Off Offline

Quote
I was thinking what is easiest and the most efficient way to parse player messages. Lua's patterns are pretty limited and they don't support OR-statements like regexp does. So I came up with doing it hard way.

This is how far I got:

> input
1
!kick hacker "reason: calling me \"noob\""
> output
1
2
3
4
5
{
	[1] = !kick
	[2] = hacker
	[3] = reason: calling me "noob"
}

source code >

If you have any ideas how I could improve this, please share them!
edited 4×, last 11.06.12 07:34:51 pm

old Re: Parsing player message

Apache uwu
User Off Offline

Quote
I believe kick even works with the player's name instead of the id.

Try

1
2
3
4
5
kiss.say = function(id, message)
	local cmd = kiss.parseCommand(message)
	parse("kick "..cmd[2])
	msg(string.char(169).."255255255"..player(id,"name").." has kicked "..cmd[2])
end

However you should add additional checks such as the player's existance as well as if the target is another administrator or not.

old Re: Parsing player message

Kisses
User Off Offline

Quote
I'm not trying to create admin mod. Kicking somebody was only an example of how the script parses strings.

old Re: Parsing player message

Apache uwu
User Off Offline

Quote
Does this work for a message like...

!kick_____________________hacker_______________________________calling me noob

_ = space

old Re: Parsing player message

Kisses
User Off Offline

Quote
user Apache uwu has written
Does this work for a message like...

!kick_____________________hacker_______________________________calling me noob

_ = space


Thank you for pointing that out. It's now fixed

old Re: Parsing player message

omg
User Off Offline

Quote
this is copy and pasted, but its a good piece of code for strings
1
2
3
4
5
6
7
8
function totable(t,match)
	local cmd = {}
	if not match then match = "[^%s]+" end
	for word in string.gmatch(t, match) do
		table.insert(cmd, word)
	end 
	return cmd 
end
it chunks up each word (no spaces) in the string into the table in order. used like this:
1
2
totable(string)--returns table of strings
totable(string)[1]--first word of string, provided it exists
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview