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

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

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

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

        self.world = None
    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 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)
Example #5
0
    def say(text):
        self.onChat(text)

        pckt = Packet.construct(MSG_CHAT_MESSAGE)
        pckt.writeUint64(self.objectId)
        pckt.writeString(text)

        self.map.broadcastPacket(pckt)
Example #6
0
    def castSpell(spell, angle):
        pckt = Packet.construct(Opcodes.MSG_CAST_SPELL)
        pckt.writeUint64(self.objectId)
        pckt.writeUint32(spell.effect, spell.displayId)
        pckt.writeFloat(angle)
        pckt.writeUint16(self.map.generateSpellBoxID())

        self.map.broadcastPacket(pckt)
        self.map.addSpell(self, spell, angle)
    def sendPositionUpdateToMap(self, diff, packetId=None, ignoreMyself=False):
        pckt = Packet.construct(Opcodes.SMSG_MOVE_OBJECT)
        pckt.writeUint64(self.objectId)
        pckt.writeVector(self.position, self.velocity)

        self.map.broadcastPlayers(pckt, exclude=[self.objectId])

        if not ignoreMyself:
            if packetId is not None:
                pckt.writeUint64(packetId)

            self.sendPacket(pckt)
    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 handleInitialConnection(self, packet):
        # Received initial parameters from client.
        # Window size, player name, etc...
        playerName = packet.readString()
        tilesetFilename = packet.readString()
        tileSize = packet.readUint16()
        tx, ty = packet.read('!II')
        tilesetPos = Vector2(tx, ty)

        plr = Player(playerName, tileSize, tilesetFilename, tilesetPos)
        plr.bindSession(self)

        self.player = plr

        # Answer by a initial connection with the player ID.
        pckt = Packet.construct(Opcodes.MSG_INITIAL_CONNECTION)
        pckt.writeBool(True)
        pckt.writeUint64(self.player.objectId)
        pckt.writeUint32(1)

        self.sendPacket(pckt)

        print ('Initial connection established with player: {}'.format(playerName))
class WorldSession(protocol.Protocol):

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

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

        self.player = None

    def assertValidPlayer(self):
        if not self.player:
            self.transport.loseConnection()

    def connectionMade(self):
        print ('Player connected.')

    def connectionLost(self, reason):
        print ('Player lost the connection: %s.' % (reason))
        if self.player:
            self.player.cleanup()

    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):
        # Received initial parameters from client.
        # Window size, player name, etc...
        playerName = packet.readString()
        tilesetFilename = packet.readString()
        tileSize = packet.readUint16()
        tx, ty = packet.read('!II')
        tilesetPos = Vector2(tx, ty)

        plr = Player(playerName, tileSize, tilesetFilename, tilesetPos)
        plr.bindSession(self)

        self.player = plr

        # Answer by a initial connection with the player ID.
        pckt = Packet.construct(Opcodes.MSG_INITIAL_CONNECTION)
        pckt.writeBool(True)
        pckt.writeUint64(self.player.objectId)
        pckt.writeUint32(1)

        self.sendPacket(pckt)

        print ('Initial connection established with player: {}'.format(playerName))

    def handleSpawnInGame(self, packet):
        if self.player.isInWorld():
            return

        self.player.registerOnMap(self.factory.world.worldMap)
        print ('Player {} spawned in the world map.'.format(self.player.name))

    def handleCastSpell(self, packet):
        self.assertValidPlayer()
        spellId = packet.readUint32()
        angle = packet.readFloat()


    def handleMove(self, packet):
        # Handle player's movements.
        direction = packet.readUint32()
        pcktId = packet._id

        self.player.move(pcktId, direction)

    def handleChatMessage(self, packet):
        pass

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

    def sendNotification(self, message):
        pckt = Packet.construct(SMSG_NOTIFICATION);
        pckt.writeString(message)

        self.sendPacket(pckt)
    def sendNotification(self, message):
        pckt = Packet.construct(SMSG_NOTIFICATION);
        pckt.writeString(message)

        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)
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())
Example #14
0
 def spellHit(spellBox):
     pckt = Packet.construct(Opcodes.MSG_REMOVE_SPELL)
     pckt.writeUint16(spellBox.boxId)
     self.map.broadcastPacket(pckt)
     self.takeDamage(spellBox.template.value, spellBox.caster)