예제 #1
0
    def __init__(self, sid=0):
        super(MissileEntityService, self).__init__(sid)

        self.register(conf.MISSILE_ENTITY_SHOOT_CMD, self.missileEntityShoot)
        self.register(conf.MISSILE_ENTITY_HIT_CMD, self.missileEntityHit)

        self.client_msg_handler = ClientMessage()
예제 #2
0
    def __init__(self, sid=0):
        super(MonsterEntityService, self).__init__(sid)

        self.register(conf.MONSTER_ENTITY_ATTACK_CMD, self.monsterEntityAttack)
        self.register(conf.PUMPKIN_HIT_CMD, self.pumpkinHit)

        self.client_msg_handler = ClientMessage()
예제 #3
0
    def __init__(self, sid=0):
        super(TrapEntityService, self).__init__(sid)

        self.register(conf.TRAP_ENTITY_CREATE_CMD, self.trapEntityCreate)
        self.register(conf.MONSTER_ENTER_ICE_TRAP_CMD, self.monsterEnterIceTrap)
        self.register(conf.MONSTER_OUTER_ICE_TRAP_CMD, self.monsterOutIceTrap)
        self.register(conf.NEEDLE_TRAP_HURT_MONSTER_CMD, self.needleTrapHurtMonster)

        self.client_msg_handler = ClientMessage()
예제 #4
0
    def __init__(self, sid=0):
        super(PlayerEntityService, self).__init__(sid)

        self.register(conf.PLAYER_ENTITY_REGISTER_CMD,
                      self.playerEntityRegister)
        self.register(conf.PLAYER_ENTITY_SYNC_CMD, self.playerEntitySync)
        self.register(conf.PLAYER_ENTITY_ATTACK_CMD, self.playerEntityAttack)
        self.register(conf.PLAYER_SKILL_LEVEL_UP_CMD, self.playerSkillUp)

        self.client_msg_handler = ClientMessage()
예제 #5
0
class MissileEntityService(Service):
    def __init__(self, sid=0):
        super(MissileEntityService, self).__init__(sid)

        self.register(conf.MISSILE_ENTITY_SHOOT_CMD, self.missileEntityShoot)
        self.register(conf.MISSILE_ENTITY_HIT_CMD, self.missileEntityHit)

        self.client_msg_handler = ClientMessage()

    def missileEntityHit(self, server, msg, client_id):
        missile_hit_msg = self.client_msg_handler.unmarshal(msg.data)
        missile_hit_msg_dict = json.loads(missile_hit_msg.msg_data)

        missile_entity_id = missile_hit_msg_dict[constants.MISSILE_ID]
        missile_entity = server.entities[
            constants.MISSILE_ENTITY_TYPE][missile_entity_id]

        player_id = missile_hit_msg_dict[constants.PLAYER_ID]

        if player_id > 0:
            player_entity = server.getEntityByID(constants.PLAYER_ENTITY_TYPE,
                                                 player_id)
        else:
            player_entity = None

        # send missile explosion
        server.addToBroadcastMsgQueue(missile_entity.genMissileExplosionMsg())

        self.hurtMonster(server, missile_entity.location, player_entity,
                         client_id)
        self.hurtPlayer(server, missile_entity, player_entity)

        # delete missile in server
        server.deleteEntity(constants.MISSILE_ENTITY_TYPE, missile_entity_id)

    @staticmethod
    def hurtPlayer(server, missile_entity, shoot_player_entity):

        for player_id, player_entity in server.entities[
                constants.PLAYER_ENTITY_TYPE].iteritems():

            if player_entity.location.getDistance(
                    missile_entity.location
            ) <= constants.MISSILE_EXPLOSION_DISTANCE:

                if player_entity.health <= 0:
                    continue

                missile_level = 1
                if player_entity is not None:
                    missile_level = shoot_player_entity.missile_level
                player_entity.health -= constants.MISSILE_DAMAGE[
                    missile_level] / 2

                print "player {0} hit by missile {1} with damage {2}".\
                    format(player_id, missile_entity.entity_id, constants.MISSILE_DAMAGE[missile_level] / 2)

                server.addToSingleMsgQueue(player_entity.client_id,
                                           player_entity.genSyncPlayerMsg())

                if player_entity.health <= 0:

                    # if not kill by self, update win player money
                    if player_entity.entity_id != missile_entity.player_id:
                        shoot_player_entity = server.getEntityByID(
                            constants.PLAYER_ENTITY_TYPE,
                            missile_entity.player_id)
                        shoot_player_entity.money += player_entity.money
                        server.addToSingleMsgQueue(
                            shoot_player_entity.client_id,
                            shoot_player_entity.genSyncPlayerMsg())

                    print "player {0} die! killed by missile {1}".format(
                        player_entity.entity_id, missile_entity.entity_id)
                    server.addToBroadcastMsgQueue(
                        player_entity.genPlayerDieMsg())
                    # when player die, delete the player
                    server.addToReadyDeleteEntities(
                        constants.PLAYER_ENTITY_TYPE, player_entity.entity_id)

    # missile hurt aoe monster
    @staticmethod
    def hurtMonster(server, missile_loc, player_entity, client_id):

        need_send_msg = False
        # if enemy in missile damage range
        for monster_entity_id, monster_entity in server.entities[
                constants.MONSTER_ENTITY_TYPE].iteritems():

            if monster_entity.location.getDistance(
                    missile_loc) <= constants.MISSILE_EXPLOSION_DISTANCE:

                # if monster is already die, but hasn't been deleted
                if monster_entity.health <= 0:
                    continue
                missile_level = 1
                if player_entity is not None:
                    missile_level = player_entity.missile_level
                monster_entity.health -= constants.MISSILE_DAMAGE[
                    missile_level]

                print "monster {0} hit by missile with damage {1}". \
                    format(monster_entity_id, constants.MISSILE_DAMAGE[missile_level])

                # when monster die, tell all clients monster is die, and delete monster in server
                if monster_entity.health <= 0:
                    if player_entity is not None:
                        player_entity.killMonsterGetScore(
                            monster_entity.monster_type)

                        print "player {0} score: {1} money: {2}".format(
                            player_entity.entity_id, player_entity.score,
                            player_entity.money)

                        server.addToBroadcastMsgQueue(
                            player_entity.genPlayerKillMsg(monster_entity_id))
                    # server.deleteEntity(constants.MONSTER_ENTITY_TYPE, monster_entity_id)
                    server.addToReadyDeleteEntities(
                        constants.MONSTER_ENTITY_TYPE, monster_entity_id)
                    need_send_msg = True

        # sync layer score and money msg
        if need_send_msg and player_entity is not None:
            server.addToSingleMsgQueue(client_id,
                                       player_entity.genSyncPlayerMsg())

    def missileEntityShoot(self, server, msg, client_id):
        missile_shoot_msg = self.client_msg_handler.unmarshal(msg.data)

        missile_shoot_msg_dict = json.loads(missile_shoot_msg.msg_data)

        missile_entity = MissileEntity(server=server)

        missile_entity.location.setFromDict(
            missile_shoot_msg_dict[constants.LOCATION])
        missile_entity.rotation.setFromDict(
            missile_shoot_msg_dict[constants.ROTATION])
        missile_entity.init_location.setFromDict(
            missile_shoot_msg_dict[constants.LOCATION])
        missile_entity.player_id = missile_shoot_msg_dict[constants.PLAYER_ID]

        toward_dict = missile_shoot_msg_dict[constants.TOWARD_VECTOR]
        missile_entity.toward_vector = [
            toward_dict['x'], toward_dict['y'], toward_dict['z']
        ]
        # print missile_entity.toward_vector

        server.registerEntity(constants.MISSILE_ENTITY_TYPE, missile_entity)

        print "player {0} shoot missile {1}".format(missile_entity.player_id,
                                                    missile_entity.entity_id)

        server.addToBroadcastMsgQueue(missile_entity.genMissileCreateMsg())
예제 #6
0
class MonsterEntityService(Service):
    def __init__(self, sid=0):
        super(MonsterEntityService, self).__init__(sid)

        self.register(conf.MONSTER_ENTITY_ATTACK_CMD, self.monsterEntityAttack)
        self.register(conf.PUMPKIN_HIT_CMD, self.pumpkinHit)

        self.client_msg_handler = ClientMessage()

    def monsterEntityAttack(self, server, msg, client_id):
        # print "Monster Attack"
        monster_attack_msg = self.client_msg_handler.unmarshal(msg.data)

        monster_attack_msg_dict = json.loads(monster_attack_msg.msg_data)

        player_entity = server.getEntityByID(
            constants.PLAYER_ENTITY_TYPE,
            monster_attack_msg_dict[constants.PLAYER_ID])

        if player_entity is None:
            return

        print "monster {0} attack player {1}".\
            format(monster_attack_msg_dict[constants.MONSTER_ID], player_entity.entity_id)

        if player_entity.health > 0:
            player_entity.health -= constants.MONSTER_DAMAGE_VALUE
            print "player {0} health {1}".format(player_entity.entity_id,
                                                 player_entity.health)

            server.addToSingleMsgQueue(client_id,
                                       player_entity.genSyncPlayerMsg())

            if player_entity is not None and player_entity.health <= 0:
                print "player {0} die! killed by monster {1}".format(
                    player_entity.entity_id,
                    monster_attack_msg_dict[constants.MONSTER_ID])
                server.addToBroadcastMsgQueue(player_entity.genPlayerDieMsg())
                # when player die, delete the player
                server.deleteEntity(constants.PLAYER_ENTITY_TYPE,
                                    player_entity.entity_id)

    def pumpkinHit(self, server, msg, client_id):

        pumpkin_hit_msg = self.client_msg_handler.unmarshal(msg.data)

        pumpkin_hit_dict = json.loads(pumpkin_hit_msg.msg_data)

        player_id = pumpkin_hit_dict[constants.PLAYER_ID]
        monster_id = pumpkin_hit_dict[constants.MONSTER_ID]

        player_entity = server.getEntityByID(constants.PLAYER_ENTITY_TYPE,
                                             player_id)

        print "monster {0} use pumpkin attack player {1}".format(
            monster_id, player_id)

        if player_entity is not None and player_entity.health > 0:

            player_entity.health -= constants.PUMPKIN_DAMAGE_VALUE
            print "player {0} health {1}".format(player_entity.entity_id,
                                                 player_entity.health)
            server.addToSingleMsgQueue(client_id,
                                       player_entity.genSyncPlayerMsg())

            if player_entity.health <= 0:
                print "player {0} die! killed by monster {1}".format(
                    player_id, monster_id)
                server.addToBroadcastMsgQueue(player_entity.genPlayerDieMsg())
                # when player die, delete the player
                server.deleteEntity(constants.PLAYER_ENTITY_TYPE,
                                    player_entity.entity_id)
예제 #7
0
class PlayerEntityService(Service):
    def __init__(self, sid=0):
        super(PlayerEntityService, self).__init__(sid)

        self.register(conf.PLAYER_ENTITY_REGISTER_CMD,
                      self.playerEntityRegister)
        self.register(conf.PLAYER_ENTITY_SYNC_CMD, self.playerEntitySync)
        self.register(conf.PLAYER_ENTITY_ATTACK_CMD, self.playerEntityAttack)
        self.register(conf.PLAYER_SKILL_LEVEL_UP_CMD, self.playerSkillUp)

        self.client_msg_handler = ClientMessage()

    def playerSkillUp(self, server, msg, client_id):

        player_skill_up_msg = self.client_msg_handler.unmarshal(msg.data)

        player_skill_up_dict = json.loads(player_skill_up_msg.msg_data)

        player_id = player_skill_up_dict[constants.PLAYER_ID]
        skill_type = player_skill_up_dict[constants.SKILL_TYPE]

        player_entity = server.getEntityByID(constants.PLAYER_ENTITY_TYPE,
                                             player_id)

        if player_entity is not None:

            if skill_type == constants.MISSILE_LEVEL_UP:
                player_entity.missile_level += 1
            elif skill_type == constants.ICE_TRAP_LEVEL_UP:
                player_entity.ice_trap_level += 1
            elif skill_type == constants.NEEDLE_TRAP_LEVEL_UP:
                player_entity.needle_trap_level += 1

            server.addToSingleMsgQueue(client_id,
                                       player_entity.genSyncPlayerMsg())

    # handle player shooting attack
    def playerEntityAttack(self, server, msg, client_id):
        player_attack_msg = self.client_msg_handler.unmarshal(msg.data)

        player_attack_msg_dict = json.loads(player_attack_msg.msg_data)

        player_entity = server.getEntityByClientID(
            constants.PLAYER_ENTITY_TYPE, client_id)

        if player_entity is None:
            return

        # send other clients player attack
        server.addToExceptMsgQueue(
            client_id,
            player_entity.genOtherPlayerShootMsg(
                player_attack_msg_dict[constants.SHOOT_POINT]))

        if player_attack_msg_dict[constants.MONSTER_ID] > 0:

            monster_id = player_attack_msg_dict[constants.MONSTER_ID]

            monster_entity = server.getEntityByID(
                constants.MONSTER_ENTITY_TYPE, monster_id)

            # if monster is already die, but hasn't been deleted
            if monster_entity is None or monster_entity.health <= 0:
                return

            monster_entity.health -= player_attack_msg_dict[
                constants.PLAYER_DAMAGE]

            # when monster die, tell all clients monster is die, and delete monster in server
            if monster_entity.health <= 0:
                player_entity.killMonsterGetScore(monster_entity.monster_type)

                print "player {0} score: {1} money: {2}".format(
                    player_entity.entity_id, player_entity.score,
                    player_entity.money)

                server.addToBroadcastMsgQueue(
                    player_entity.genPlayerKillMsg(monster_id))
                server.addToSingleMsgQueue(client_id,
                                           player_entity.genSyncPlayerMsg())
                server.deleteEntity(constants.MONSTER_ENTITY_TYPE, monster_id)

    # handle player entity register
    def playerEntityRegister(self, server, msg, client_id):

        player_entity_msg = self.client_msg_handler.unmarshal(msg.data)

        player_entity_dict = json.loads(player_entity_msg.msg_data)

        player_entity = PlayerEntity()

        player_entity.setFromRegisterMsg(player_entity_dict)
        player_entity.client_id = client_id

        server_all_entities_msg = server.genAllServerEntitiesMsg()

        server.registerEntity(constants.PLAYER_ENTITY_TYPE, player_entity)

        print "[create player entity]  --> client: {0} with player_id: {1} player name: {2}".\
            format(client_id, player_entity.entity_id, player_entity.name)

        # update player msg from database
        player_data_dict = server.game_db.player_entity_table.find_name(
            player_entity.name)
        if player_data_dict is not None:
            player_entity.history_score = player_data_dict[
                constants.HISTORY_SCORE]
            player_entity.user_level = player_data_dict[constants.USER_LEVEL]
            player_entity.ice_trap_level = player_data_dict[
                constants.ICE_TRAP_LEVEL]
            player_entity.needle_trap_level = player_data_dict[
                constants.NEEDLE_TRAP_LEVEL]
            player_entity.missile_level = player_data_dict[
                constants.MISSILE_LEVEL]
            player_entity.exp = player_data_dict[constants.EXP]
            print player_data_dict

        # a user has already registered, game is starting
        server._game_start = True

        # sync entity id to client
        server.addToSingleMsgQueue(client_id, player_entity.genSyncPlayerMsg())
        # get all game data
        server.addToSingleMsgQueue(client_id, server_all_entities_msg)
        # add login successfully client
        server.addToLoginSuccessfulClient(client_id)
        # sync new player to other clients
        server.addToExceptMsgQueue(client_id,
                                   player_entity.genOtherPlayerCreateMsg())

    # handle player entity transform sync
    def playerEntitySync(self, server, msg, client_id):

        player_sync_msg = self.client_msg_handler.unmarshal(msg.data)
        player_sync_dict = json.loads(player_sync_msg.msg_data)

        player_entity = server.getEntityByClientID(
            constants.PLAYER_ENTITY_TYPE, client_id)

        if player_entity is not None:
            # just change location and rotation from client msg
            player_entity.location.setFromDict(
                player_sync_dict[constants.LOCATION])
            player_entity.rotation.setFromDict(
                player_sync_dict[constants.ROTATION])

            #  send player sync msg to other clients
            server.addToExceptMsgQueue(client_id,
                                       player_entity.genOtherPlayerSyncMsg())
예제 #8
0
class TrapEntityService(Service):

    def __init__(self, sid=0):
        super(TrapEntityService, self).__init__(sid)

        self.register(conf.TRAP_ENTITY_CREATE_CMD, self.trapEntityCreate)
        self.register(conf.MONSTER_ENTER_ICE_TRAP_CMD, self.monsterEnterIceTrap)
        self.register(conf.MONSTER_OUTER_ICE_TRAP_CMD, self.monsterOutIceTrap)
        self.register(conf.NEEDLE_TRAP_HURT_MONSTER_CMD, self.needleTrapHurtMonster)

        self.client_msg_handler = ClientMessage()

    def trapEntityCreate(self, server, msg, client_id):

        trap_create_msg = self.client_msg_handler.unmarshal(msg.data)
        trap_create_dict = json.loads(trap_create_msg.msg_data)

        trap_entity = TrapEntity()
        trap_entity.player_id = trap_create_dict[constants.PLAYER_ID]
        trap_entity.trap_type = trap_create_dict[constants.TRAP_TYPE]
        trap_entity.location.setFromDict(trap_create_dict[constants.LOCATION])

        server.registerEntity(constants.TRAP_ENTITY_TYPE, trap_entity)

        player_entity = server.getEntityByID(constants.PLAYER_ENTITY_TYPE, trap_entity.player_id)

        if player_entity is not None:
            player_entity.money -= constants.TRAP_MONEY[trap_entity.trap_type]
            server.addToSingleMsgQueue(client_id, player_entity.genSyncPlayerMsg())

            if trap_entity.trap_type == constants.NEEDLE_TRAP_TYPE:
                trap_entity.trap_level = player_entity.needle_trap_level
            elif trap_entity.trap_type == constants.ICE_TRAP_TYPE:
                trap_entity.trap_level = player_entity.ice_trap_level

        print "player {0} put trap {1} with level {2}".\
            format(trap_entity.player_id, trap_entity.entity_id, trap_entity.trap_level)

        # send trap create msg to all the clients
        server.addToBroadcastMsgQueue(trap_entity.genTrapCreateMsg())

    def monsterEnterIceTrap(self, server, msg, client_id):

        monster_enter_trap_msg = self.client_msg_handler.unmarshal(msg.data)
        monster_enter_trap_dict = json.loads(monster_enter_trap_msg.msg_data)

        monster_id = monster_enter_trap_dict[constants.MONSTER_ID]
        trap_id = monster_enter_trap_dict[constants.TRAP_ID]

        monster_entity = server.getEntityByID(constants.MONSTER_ENTITY_TYPE, monster_id)

        trap_entity = server.getEntityByID(constants.TRAP_ENTITY_TYPE, trap_id)
        if monster_entity is not None and monster_entity is not None:
            monster_entity.speed_factor = constants.ICE_TRAP_IMPACT_SPEED_FACTOR[trap_entity.trap_level]

            print "monster {0} enter ice trap {1} with speed_factor {2}".\
                format(monster_id, trap_id, monster_entity.speed_factor)

    def monsterOutIceTrap(self, server, msg, client_id):

        monster_out_trap_msg = self.client_msg_handler.unmarshal(msg.data)
        monster_out_trap_dict = json.loads(monster_out_trap_msg.msg_data)

        monster_id = monster_out_trap_dict[constants.MONSTER_ID]
        trap_id = monster_out_trap_dict[constants.TRAP_ID]

        monster_entity = server.getEntityByID(constants.MONSTER_ENTITY_TYPE, monster_id)

        # when monster die, will also trigger the exit trigger function
        if monster_entity is not None:
            monster_entity.speed_factor = 1

        print "monster {0} out ice trap {1}".format(monster_id, trap_id)

    def needleTrapHurtMonster(self, server, msg, client_id):

        needle_trap_hurt_msg = self.client_msg_handler.unmarshal(msg.data)
        needle_trap_hurt_dict = json.loads(needle_trap_hurt_msg.msg_data)

        monster_id = needle_trap_hurt_dict[constants.MONSTER_ID]
        trap_id = needle_trap_hurt_dict[constants.TRAP_ID]
        player_id = needle_trap_hurt_dict[constants.PLAYER_ID]

        trap_entity = server.getEntityByID(constants.TRAP_ENTITY_TYPE, trap_id)
        if trap_entity is not None:
            monster_entity = server.getEntityByID(constants.MONSTER_ENTITY_TYPE, monster_id)
            player_entity = server.getEntityByID(constants.PLAYER_ENTITY_TYPE, player_id)

            hurt_damage = constants.NEED_TRAP_HURT_VALUE[trap_entity.trap_level]

            if monster_entity is None or monster_entity.health <= 0:
                return

            monster_entity.health -= hurt_damage

            print "needle {0} hurt monster {1} with damage {2}".\
                format(trap_entity.entity_id, monster_entity.entity_id, hurt_damage)

            # when monster die, tell all clients monster is dead, and delete monster entity in server
            if monster_entity.health <= 0:
                if player_entity is not None:
                    player_entity.killMonsterGetScore(monster_entity.monster_type)

                    print "player {0} score: {1} money: {2}".format(player_entity.entity_id,
                                                                    player_entity.score, player_entity.money)

                    server.addToSingleMsgQueue(client_id, player_entity.genSyncPlayerMsg())

                server.addToBroadcastMsgQueue(monster_entity.genServerKillMsg())
                server.deleteEntity(constants.MONSTER_ENTITY_TYPE, monster_id)