Chaos Project

Game Development => Sea of Code => Topic started by: G_G on June 22, 2011, 02:37:54 pm

Title: [C#] Switch Statement with RegEx?
Post by: G_G on June 22, 2011, 02:37:54 pm
I'm trying to replicate RMX-OS here. That way I don't have to have a crap ton of lines. Basically trying to do this in C#.
	def check_connection
case @message
when /\ALIN(.+)\t(.+)/ # login request
result = @action.try_login($1, $2)
# if login was successful
if result == RMXOS::RESULT_SUCCESS
# send user data
self.send("UID#{@player.user_id}")
self.send("USR#{@player.username}")
self.send("UGR#{@player.usergroup}")
self.send("BUD#{@player.get_buddies_list}")
# send guild data
if @player.guildname != ''
self.send("GIN#{@player.guildname}\t#{@player.guildleader}\t#{@player.get_guildmembers_list}")
end
end
self.send("LIN#{result}")
return true
when /\AREG(.+)\t(.+)/ # register request
result = @action.try_register($1, $2)
# if registering was successful
if result == RMXOS::RESULT_SUCCESS
# log in as well
@action.try_login($1, $2)
# send user data
self.send("UID#{@player.user_id}")
self.send("USR#{@player.username}")
self.send("UGR#{@player.usergroup}")
self.send("BUD#{@player.get_buddies_list}")
# send guild data
if @player.guildname != ''
self.send("GIN#{@player.guildname}\t#{@player.guildleader}\t#{@player.get_guildmembers_list}")
end
end
self.send("REG#{result}")
return true
when /\ACON(.+)/ # connection request
version = $1.to_f
# get all properly connected clients
clients = $clients.find_all {|client| client.player.user_id != 0}
# version not high enough
if version < RMXOS_VERSION
result = RMXOS::RESULT_FAIL
# server is full
elsif (clients - [self]).size >= MAXIMUM_CONNECTIONS
result = RMXOS::RESULT_DENIED
else
result = RMXOS::RESULT_SUCCESS
end
self.send("CON#{result}\t#{RMXOS_VERSION}")
return true
end
return false
end

Having a little trouble figuring out how thats going to work exactly.
Title: Re: [C#] Switch Statement with RegEx?
Post by: winkio on June 22, 2011, 02:43:41 pm
tbh I've never used regexp in C#, I just use string.split  :^_^':
Title: Re: [C#] Switch Statement with RegEx?
Post by: G_G on June 22, 2011, 02:45:38 pm
lulz thats what I'd usually do. xD I really don't think thats the case here though. =\ Meh I'll keep looking through google.
Title: Re: [C#] Switch Statement with RegEx?
Post by: Ryex on June 22, 2011, 02:47:19 pm
I used Regexp extensively in my rmxos cfg app take a look at the source and look at the part that reads the config file.
Title: Re: [C#] Switch Statement with RegEx?
Post by: G_G on June 22, 2011, 02:52:05 pm
I'm trying to do it without having all of these extra match variables and if branches. It looks like thats what I'm going to have to do though.
Title: Re: [C#] Switch Statement with RegEx?
Post by: Ryex on June 22, 2011, 03:10:01 pm
ya that's the only way it works. kinda sad that it isn't a smooth and easy as ruby. even python doesn't do RegEx as well as ruby.
Title: Re: [C#] Switch Statement with RegEx?
Post by: G_G on June 22, 2011, 03:13:24 pm
This is probably the closest I'll get to keeping minimal code.
            string pattern = "\\AUID(.+)";
           if (Regex.Match(msg, pattern).Success)
               client.user_id = Convert.ToInt32(Regex.Replace(msg, pattern, "$1"));
           pattern = "\\AUSR(.+)";
           if (Regex.Match(msg, pattern).Success)
               client.username = Regex.Replace(msg, pattern, "$1");
           pattern = "\\AUGR(.+)";
           if (Regex.Match(msg, pattern).Success)
               client.usergroup = Convert.ToInt32(Regex.Replace(msg, pattern, "$1"));


EDIT: On a side note, this program is gonna be awesome. A whole different way to boss your server around.
Title: Re: [C#] Switch Statement with RegEx?
Post by: Blizzard on June 22, 2011, 03:51:25 pm
I used regex for text based formats such as a localization file format that we use at Cateia.