Exemplo n.º 1
0
    def handleInitialConnection(self, packet):
        def onWorldLoaded(_):
            print ('Pushing the world in the game!')
            self.factory.game.popAllStates()
            self.factory.game.pushState(self.world)

            print ('Sending spawn request.')
            pckt = Packet.construct(Opcodes.CMSG_SPAWN_INGAME)
            self.sendPacket(pckt)

        success = packet.readBool()
        if success:
            playerId = packet.readUint64()
            mapId = packet.readUint32()

            self.world = World(playerId, mapId, self)
            d = threads.deferToThread(self.world.load)

            d.addCallback(onWorldLoaded)
        else:
            print ('Initial connection refused by the server.')
Exemplo n.º 2
0
class WorldSession(protocol.Protocol):

    def __init__(self, factory):
        self.factory = factory

        self.currentPacket = Packet()
        self.lastBytes = bytes()

        self.world = None

    def connectionMade(self):
        print ('Connected to server.')
        self.sendInitialConnection()

    def connectionLost(self, reason):
        pass

    def dataReceived(self, data):
        while len(data) >= Packet.HeaderSize:
            if not self.currentPacket.isHeaderComplete():
                bytesRead = self.currentPacket.consumeHeader(data)
                data = data[bytesRead:]

            if self.currentPacket.isHeaderComplete():
                bytesRead = self.currentPacket.consumeContent(data)
                data = data[bytesRead:]

            if self.currentPacket.isComplete():
                self.handlePacket(self.currentPacket)
                self.currentPacket = Packet()

        self.lastBytes = data


    def handlePacket(self, packet):
        if packet.opcode == Opcodes.MSG_NULL:
            print ('Received a NULL opcode!')

        self.factory.opcodesTable[packet.opcode].handler(self, packet)

    def handleNULL(self, packet):
        # Received either a NULL or unexpected packet.
        pass

    def handleInitialConnection(self, packet):
        def onWorldLoaded(_):
            print ('Pushing the world in the game!')
            self.factory.game.popAllStates()
            self.factory.game.pushState(self.world)

            print ('Sending spawn request.')
            pckt = Packet.construct(Opcodes.CMSG_SPAWN_INGAME)
            self.sendPacket(pckt)

        success = packet.readBool()
        if success:
            playerId = packet.readUint64()
            mapId = packet.readUint32()

            self.world = World(playerId, mapId, self)
            d = threads.deferToThread(self.world.load)

            d.addCallback(onWorldLoaded)
        else:
            print ('Initial connection refused by the server.')

    def handleCastSpell(self, packet):
        pass

    def handleNotification(self, packet):
        message = packet.readString()

    def handleMoveObject(self, packet):
        objectId = packet.readUint64()

        self.world.worldObjects[objectId].moveObject(packet)

    def handleRemoveObject(self, packet):
        objectId = packet.readUint64()
        self.world.removeObject(objectId)

    def handleAddObject(self, packet):
        objectId = packet.readUint64()

        worldObject = WorldObject(objectId, objectId == self.world.me_id)
        worldObject.load(self.world, packet)

        self.world.addObject(worldObject)

    def handleChatMessage(self, packet):
        message = packet.readString()

    def sendInitialConnection(self):
        pckt = Packet.construct(Opcodes.MSG_INITIAL_CONNECTION)
        pckt.writeString("Raito Bezarius", "dg_classm32.gif")
        pckt.writeUint16(32)
        pckt.writeUint32(0, 0)

        self.sendPacket(pckt)

    def sendMovementRequest(self, evtCode):
        pckt = Packet.construct(Opcodes.CMSG_MOVE)
        pckt.writeUint32(evtCode)

        self.world.me.doMovePrediction(pckt._id, evtCode)
        self.sendPacket(pckt)

    def sendPacket(self, pckt):
        self.transport.write(pckt.serialize())