January 22nd, 2005, 02:10 PM
I need some help determining what, in the data my socket is receiving, is a username and what is a message. For example, my bot may receive something like, 1002 JOIN TyRaNiZe 0000 [SEXP]. 1002 is just the 4 numbers that represents JOIN, JOIN is obviously just there to remind you that the user is joining, TyRaNiZe is the username, 0000 is pointless to show, and [SEXP] is the client. Now for when a user talks, it will show something such as, 1005 TALK CleanSlateBot 000 "Hello."
This is the same as the JOIN, CleanSlateBot would be the username and the message would be what's in the quotes. Can anyone show me some code that pulls out what is a user and what is a message?
Update: I figured it out, I used find().
January 22nd, 2005, 08:12 PM
You used find()? So you know the username ahead of time?
I was going to suggest username = in_string.split()
if they're always in that order, and separated by a space....
January 22nd, 2005, 09:35 PM
I used find() to know which part othe string was the username/message.
January 23rd, 2005, 02:57 PM
I'd probably start by splitting the data at the first n white spaces. This will make it easier to check which elements are what. This way you can check if part 1 is... JOIN.
I don't really know what data means what to battle.net of course but in theory: If part 1 is JOIN then part 2 has to be the userName. Or thats the general idea.
Hope this helps,
January 23rd, 2005, 03:21 PM
Well here's what I did. If the data contained TALK, here was my code, d is the data.
This worked just fine, so far I've gotten absolutely no errors.
elif d[5:9] == 'TALK':
username = d[10:d.find('"')-6]
user = username
message = d[d.find('"')+1:-1]
msg = message