[C#] Switch Statement with RegEx?

Started by G_G, June 22, 2011, 02:37:54 pm

Previous topic - Next topic

G_G

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.

winkio

tbh I've never used regexp in C#, I just use string.split  :^_^':

G_G

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.

Ryex

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.
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

G_G

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.

Ryex

June 22, 2011, 03:10:01 pm #5 Last Edit: June 22, 2011, 03:11:07 pm by Ryex
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.
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

G_G

June 22, 2011, 03:13:24 pm #6 Last Edit: June 22, 2011, 03:37:55 pm by game_guy
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.

Blizzard

I used regex for text based formats such as a localization file format that we use at Cateia.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.