Valhalla Legends Forums Archive | Java Programming | [Python]DataArrival

AuthorMessageTime
Yegg
I can't seem to get the correct code for my bot's data arrival. I tried converting it from VB6 to Python, but I couldn't figure out the correct code.
Note: My bot is using chat client
Here is the VB6 code, and below that is what I've come up with.
[code]Private Sub Winsock_DataArrival(ByVal bytesTotal As Long)
    Dim strTmp As String
    Winsock.GetData strTmp, vbString
    ParseData strTmp
End Sub[/code]
[code]def dataArrival(data):
    tmpStr = s.recv(data)
    s.recv(tmpStr)
    parse_data(tmpStr)[/code]
Thanks.
January 15, 2005, 8:33 PM
j0k3r
This has nothing to do with java, why is it in the java forum.
January 15, 2005, 10:40 PM
St0rm.iD
ew...
[code]
BUFSIZE=1024
s = some socket
def readNextData():
    parse_data(s.recv(1024))
[/code]

Of course, it looks to me like you might want to restructure your code a bit...
January 16, 2005, 3:03 PM
Yegg
Ok, I'm getting a problem with parse_data(s.recv(1024)). The bot connects to battle.net fine but it still can't receive any message, etc. I did a try and except to find out what was wrong and it had a problem with parse_data(s.recv(1024)). Right now I have parse_data = lambda d: map(handle_event, d.split("\r\n")). So I have no clue what's wrong. It seems like everything else is correct. So here's my code, I can't find anything wrong with it.

[code]def event_user(args, string_arg):
    pass
def event_join(args, string_arg):
    pass
def event_leave(args, string_arg):
    pass
def event_recvwhisper(args, string_args):
    pass
def event_talk(args, string_args):
    pass
def event_broadcast(args, string_args):
    pass
def event_channel(args, string_args):
    print "You have joined a channel."
def event_flags(args, string_args):
    pass
def event_sendwhisper(args, string_args):
    pass
def event_channelfull(args, string_args):
    print "That channel is full."
def event_channelnotexist(args, string_args):
    print "That channel does not exist."
def event_channelrestricted(args, string_args):
    print "That channel is restricted."
def event_info(args, string_args):
    pass
def event_error(args, string_args):
    pass
def event_emote(args, string_args):
    pass
def event_name(args, string_args):
    pass
def event_unknown(args, string_args):
    print "An unknown error has occured."
event_handlers = {
    1001: event_user,
    1002: event_join,
    1003: event_leave,
    1004: event_recvwhisper,
    1005: event_talk,
    1006: event_broadcast,
    1007: event_channel,
    1009: event_flags,
    1010: event_sendwhisper,
    1013: event_channelfull,
    1014: event_channelnotexist,
    1015: event_channelrestricted,
    1016: event_info,
    1018: event_info,
    1019: event_error,
    1023: event_emote,
    2010: event_name,
    3000: event_info,
    }
def handle_event(d):
    try:
      if d.find('"') > -1:
          string_arg = d[d.find('"')+1:d.rfind('"')]
      else:
          string_arg = ""
      args = d.split(" ")
      event_handlers[int(args[0])](args, string_arg)
    except:
      event_unknown(d)
parse_data = lambda d: map(handle_event, d.split("\r\n"))[/code]

And after I connect I have parse_data(s.recv(1024)), so it looks like this,
[code]s.connect((server, port))
Login(bnetuser, bnetpass)
parse_data(s.recv(1024))[/code]
Note: Login() just sends bnet the username and password, it's code is fine. Can you find anything wrong with this?
January 17, 2005, 1:06 AM
St0rm.iD
Put a bunch of print statements in there to see what it's doing.
January 17, 2005, 1:10 AM
Yegg
Ok, I used a bunch of try statements on all the places where an error could occur, here's my code and heres wut happens in the Interpreter.

[code]def event_user(args, string_arg):
    pass
def event_join(args, string_arg):
    pass
def event_leave(args, string_arg):
    pass
def event_recvwhisper(args, string_args):
    pass
def event_talk(args, string_args):
    pass
def event_broadcast(args, string_args):
    pass
def event_channel(args, string_args):
    print "You have joined a channel."
def event_flags(args, string_args):
    pass
def event_sendwhisper(args, string_args):
    pass
def event_channelfull(args, string_args):
    print "That channel is full."
def event_channelnotexist(args, string_args):
    print "That channel does not exist."
def event_channelrestricted(args, string_args):
    print "That channel is restricted."
def event_info(args, string_args):
    pass
def event_error(args, string_args):
    pass
def event_emote(args, string_args):
    pass
def event_name(args, string_args):
    pass
def event_unknown(args, string_args):
    print "An unknown error has occured."
try:
    event_handlers = {
        1001: event_user,
        1002: event_join,
        1003: event_leave,
        1004: event_recvwhisper,
        1005: event_talk,
        1006: event_broadcast,
        1007: event_channel,
        1009: event_flags,
        1010: event_sendwhisper,
        1013: event_channelfull,
        1014: event_channelnotexist,
        1015: event_channelrestricted,
        1016: event_info,
        1018: event_info,
        1019: event_error,
        1023: event_emote,
        2010: event_name,
        3000: event_info,
        }
except:
    print "Problem: event_handlers"
def handle_event(d):
    try:
      if d.find('"') > -1:
          string_arg = d[d.find('"')+1:d.rfind('"')]
      else:
          string_arg = ""
      args = d.split(" ")
      event_handlers[int(args[0])](args, string_arg)
    except:
      event_unknown(d)
try:
    parse_data = lambda d: map(handle_event, d.split("\r\n"))
except:
    print "Problem: lambda"
def Login(username, password):
    s.send(chr(3) + chr(4) + username + "\r\n" + password + "\r\n")
print "["+mytime+"]Connecting to "+server+" "+"6112"
Log("["+mytime+"]Connecting to "+server+" "+"6112")
try:
    try:
        s.connect((server, port))
    except:
        print "Problem: s.connect((server, port))"
    try:
        Login(bnetuser, bnetpass)
    except:
        print "Problem: Login(bnetuser, bnetpass)"
    try:
        parse_data(s.recv(1024))
    except:
        print "Problem: parse_data(s.recv(1024))"
    print "["+mytime+"]Connect time: "+mytime
    Log("["+mytime+"]Connect time: "+mytime)
    print "["+mytime+"]Logged on as: "+bnetuser
    Log("["+mytime+"]Logged on as: "+bnetuser)
    print "["+mytime+"]Logon server: "+server+" 6112"
    Log("["+mytime+"]Logon server: "+server+" 6112")
except:
    print "["+mytime+"]Error: A connection error has occured, please check BG3."
    print "["+mytime+"]Error: You could also be ipbanned from battle.net."[/code]

Now heres wut coems up in the interpreter:
[code][4:08:43PM]Connecting to 127.0.0.1 6112
Problem: parse_data(s.recv(1024))
[4:08:43PM]Connect time: 4:08:43PM
[4:08:43PM]Logged on as: Yegg
[4:08:43PM]Logon server: 127.0.0.1 6112[/code]
January 17, 2005, 9:11 PM
St0rm.iD
Instead of printing "Problem: parse_data...", do this:
[code]
import traceback
traceback.print_exc()
[/code]

Also, see
http://docs.python.org/lib/module-logging.html
January 18, 2005, 3:03 AM
Yegg
:D. You own banana fanna fo fanna. Once I used that traceback thing I found 3 small errors and now my bot works great, it can see whos in the channel, who's joining/leaving, etc.
PS. I gave credit to you and 1 other person on my bot.

Update: I only have one problem. The program will not continue to parse data, it only does this once it connects, it shows the users in the channel, the info, the channel I connected to and the username I logged on with. Should i continuously send parse_data(s.recv(1024))?
January 18, 2005, 10:18 PM
St0rm.iD
You should infinitely loop it:

[code]
while True:
    parse_data(s.recv(1024))
[/code]
January 19, 2005, 2:04 AM
Yegg
:D, thanks again, it works perfect.
January 19, 2005, 8:02 PM
St0rm.iD
any web page for your bot?
January 20, 2005, 8:35 PM
Yegg
Lol, actually, it started out as a private bot, but now that I realized that battle.net (my theory) is too slow to comprehend a floodbot spamming messages and someone else trying to ban/ignore it, because of this my bot wont ban floodbots, so I decided to just use it for logging on like 3 of my main accounts through the one bot until I can find a good use for it.
January 20, 2005, 11:01 PM

Search