def openRegion(self, user, mapCollection):
        serviceJsonpackMap = Service_Map().getDecorateClass(Service_Map.JSONPACK)
        result = serviceJsonpackMap.fromCollectionToList(mapCollection)
        units = Service_Army().decorate(Service_Army.JSONPACK).loadByMapCollectionWithoutUser(user, mapCollection)

        user.getTransfer().forceSend('/map/show', {
            'done': True,
            'result': {
                'data': result
            }
        })

        if (len(units)):
            controller.ArmyController.DeliveryController().updateUnitsOnMap(user, units)
    def load_chunks(self, transfer, data):
        userCollectionChunks = self._getAclMapService().getByVisibleCollection(
            self._getMapUserVisibleService().getByChunks(
                transfer.getUser(),
                data['chunkList']
            )
        )

        userChunksList = Service_Map().getDecorateClass(Service_Map.JSONPACK).fromCollectionToList(userCollectionChunks)
        units = Service_Army().decorate(Service_Army.JSONPACK).loadByMapCollection(userCollectionChunks)

        transfer.send('/map/load_chunks', {
            'done': True,
            'result': {
                'data': userChunksList
            }
        })

        controller.ArmyController.DeliveryController().updateUnitsOnMap(transfer.getUser(), units)
 def _getAclMapService(self):
     return Service_Map().decorate(Service_Map.ACL)
Beispiel #4
0
 def openMapForUser(self, user, coordinate, aclUser=None):
     mapCollection = Service_Map().decorate(
         Service_Map.PARAMS).getRegion(coordinate)
     return Service_MapUserVisible().openRegion(user, mapCollection)
Beispiel #5
0
 def fillChunks(self, chunks, land, landType, user=None):
     return Service_Map().decorate(Service_Map.PARAMS).fillChunks(
         chunks, land, landType)
Beispiel #6
0
 def fillCoordinate(self, coordinate, land, landType, user=None):
     return Service_Map().decorate(Service_Map.PARAMS).fillCoordinate(
         coordinate, land, landType)
Beispiel #7
0
    def checkBattleOnPosition(self, mapCoordinate, general):
        from collection.MapCollection import Map_Collection

        userService = Service_User()

        mapCollection = Map_Collection()
        mapDomain = Service_Map().getByPosition(mapCoordinate)
        mapCollection.append(mapDomain)

        result = Service_Army().loadByMapCollection(mapCollection)

        if len(result) == 1:
            return  # no units for battle

        myArmy = {}
        enemyArmy = {}

        myArmy[general.getUser().getId()] = {
            'units': [general],
            'user': general.getUser()
        }

        for unit in result:
            if unit.getId() == general.getId():
                continue

            unitUser = unit.getUser()

            if userService.getUserState(
                    general.getUser(),
                    unitUser).getState() == UserStateCommon.STATE_WAR:
                if unitUser.getId() not in enemyArmy:
                    enemyArmy[unitUser.getId()] = {
                        'units': [],
                        'user': unitUser
                    }

                enemyArmy[unitUser.getId()]['units'].append(unit)

                break  # Limit for battle at 1 to 1

        if len(myArmy) >= 1 and len(enemyArmy) >= 1:

            def parseArmy(army):
                ask = {}
                for userId in army:
                    ask[str(userId)] = {
                        'user':
                        userId,
                        'accept':
                        False,
                        'units': [{
                            'unit': unit.getId(),
                            'accept': False
                        } for unit in army[userId]['units']]
                    }

                return ask

            attackerAsk = parseArmy(myArmy)
            defenderAsk = parseArmy(enemyArmy)

            battleAskDomain = Service_Battle().addBattleAsk(
                mapCoordinate, attackerAsk, defenderAsk)

            init_celery.waitBattle.apply_async(
                (str(battleAskDomain.getId()), ),
                countdown=int(config.get('battle.wait_for_start_battle')))

            from controller.BattleController import DeliveryController
            DeliveryController().askAboutBattle(mapCoordinate, myArmy,
                                                enemyArmy)
Beispiel #8
0
    def _updatePathMove(self, general):
        moved = self._moveUnitPosition(general)

        path = general.getMovePath()

        if len(path) != 0 and 'code' not in path[0]:
            mapCoordinate = Service_Map().getByPosition(
                helpers.MapCoordinate.MapCoordinate(posId=path[0]['pos_id']))
            armyPosition = helpers.MapCoordinate.MapCoordinate(
                posId=general.getLocation())

            if not (-1 <=
                    (mapCoordinate.getX() - armyPosition.getX()) <= 1 or -1 <=
                    (mapCoordinate.getY() - armyPosition.getY()) <= 1):
                raise Exception()

            waitForMove = int(config.get('army.infantry.base_wait'))
            powerPerSeconds = float(config.get('army.infantry.power_restore'))
            powerMove = Map_Data.MOVE['infantry']['byroad'][
                mapCoordinate.getLand()]
            powerRestoreAtWait = round(powerPerSeconds * int(waitForMove))
            armyPower = general.getPower()

            if general.getMode() == Army_Common.MODE_VERY_FAST:
                if powerMove > (armyPower + powerRestoreAtWait
                                ):  # Restore all needed power to move
                    waitForMove += round(
                        (powerMove -
                         (armyPower + powerRestoreAtWait)) / powerPerSeconds)
                else:  # Do nothing, all info already calculated
                    pass

            elif general.getMode() == Army_Common.MODE_FAST:
                if powerMove >= self.FAST_MOVE_MINIMAL_POWER and powerMove <= armyPower:
                    waitForMove += round(
                        (powerMove - self.FAST_MOVE_MINIMAL_POWER) /
                        powerPerSeconds)
                elif powerMove <= armyPower and powerMove <= self.FAST_MOVE_MINIMAL_POWER:
                    pass  # Do nothing, all info already calculated
                elif powerMove > armyPower:  # Restore all needed power to move
                    waitForMove += round(
                        (powerMove - armyPower) / powerPerSeconds)

            elif general.getMode() == Army_Common.MODE_NORMAL:
                if powerMove >= self.NORMAL_MOVE_MINIMAL_POWER and powerMove <= (
                        armyPower + powerRestoreAtWait):
                    waitForMove += round(
                        (powerMove - self.NORMAL_MOVE_MINIMAL_POWER) /
                        powerPerSeconds)
                elif powerMove <= (
                        armyPower + powerRestoreAtWait
                ) and powerMove <= self.NORMAL_MOVE_MINIMAL_POWER:
                    pass  # Do nothing, all info already calculated
                elif powerMove > (armyPower + powerRestoreAtWait
                                  ):  # Restore all needed power to move
                    waitForMove += round(
                        (powerMove -
                         (armyPower + powerRestoreAtWait)) / powerPerSeconds)

            elif general.getMode() == Army_Common.MODE_SLOW:
                result = round(
                    int(100 - (armyPower + powerRestoreAtWait)) /
                    powerPerSeconds)
                if result > 0:
                    waitForMove += result

            queueMessage = {
                'general': str(general.getId()),
                'power': powerMove,
                'complete_after': waitForMove,
                'start_at': int(time.time())
            }

            path[0]['code'] = str(
                init_celery.army_move.apply_async(
                    (queueMessage, ),
                    countdown=queueMessage['complete_after']))
            path[0]['power'] = queueMessage['power']
            path[0]['start_at'] = queueMessage['start_at']
            path[0]['complete_after'] = queueMessage['complete_after']

            general.setMovePath(path)

        general.getMapper().save(general)

        if moved:
            import controller.ArmyController
            controller.ArmyController.DeliveryController().moveUnit(general)