verbyte.py:
#topaz
#docs:
#http://bnetdocs.valhallalegends.com/content.php?Section=m&Code=180
#http://www.valhallalegends.com/yoni/BNLSProtocolSpec.txt
import struct
import socket
BNLS_REQUESTVERBYTE = 0x10
productID = {'STAR':0x01, 'SEXP':0x02, 'W2BN':0x03, 'D2DV':0x04,
'D2XP':0x05, 'JSTR':0x06, 'WAR3':0x07, 'W3XP':0x08}
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
class packetbuffer:
def __init__(self):
self.buffer = []
def insertData(self, data):
self.buffer.append(data)
def insertDWORD(self, data):
data = self.makeDWORD(data)
self.buffer.append(data)
def makeDWORD(self, data):
return struct.pack('I', data)
def makeWORD(self, data):
return struct.pack('H', data)
def getDWORD(self, data):
return struct.unpack('I', data)
def sendPacket(self, packetID):
tmp = ''
for i in self.buffer: tmp += i
packetlen = self.makeWORD(len(tmp) + 3)
sock.send(packetlen + chr(packetID) + tmp)
self.clear()
def clear(self):
self.buffer = list()
def getVerbyte(data):
verbyte = pbuffer.getDWORD(data[7:11])
verbyte = hex(verbyte[0])
print 'verbyte: %s' %(verbyte)
sock.connect(('bnls.valhallalegends.com', 9367))
pbuffer = packetbuffer()
pbuffer.insertDWORD(productID[productID])
pbuffer.sendPacket(0x10)
getVerbyte(sock.recv(1024))
Tested and known to work.
You seem to be doing well with Python, nice job on the dictionary usage. Just a reminder incase you don't already know, the following line
print 'verbyte: %s' %(verbyte)
can also be written as
print 'verbyte: %s' % verbyte
When you are only inputting a single variable when formatting text, you don't need to enclose that variable with parenthesis, you only need to enclose it when there are 2 or more. IMO, no parenthesis when one variable is used looks nicer, you may think otherwise.