Exemplo n.º 1
0
    def addBuildingToQueue(self, queue, bname):
        if not self.checkLogin(): return

        if queue not in ['build', 'level']:
            self.stderr('Unknown action: ' + queue)
        # The building type has to exist in order to be built, obviously
        if not Building.buildings.has_key(bname):
            self.stderr('Unknown Building Type')
        else:
            # Update the user's resouces & clear the building queue if
            # it has finished
            self.user.updateValues()
            level = 1

            if queue == 'level':
                level = int(getattr(self.user, bname + 'Lvl')) + 1

            cost = Building.levelUpCost(bname, level)

            # You need to pay for what you build!
            if         self.user.gold  < cost['cost']['gold']    \
                    or self.user.wood  < cost['cost']['wood']    \
                    or self.user.stone < cost['cost']['stone']   \
                    or self.user.food  < cost['cost']['food']:
                self.stderr('Not enough resources!')
            # All checks successful, take the resources from the user
            # and put this into the building queue
            else:
                self.user.gold  -= cost['cost']['gold']
                self.user.wood  -= cost['cost']['wood']
                self.user.stone -= cost['cost']['stone']

                # Special case
                # There's a chance mines will infact be Gold Mines :)
                if queue == 'build' and bname == 'mine':
                    chance = Building.buildings['mine']['goldMineChance']
                    if random.randrange(0, 99) < chance:
                        bname = 'goldMine'

                queueO           = Queue()
                queueO.queueType = queue
                queueO.name      = bname
                self.user.addQueue(queueO, cost['time'])
                queueO.put()

                self.json['status'] = 'Started'
                self.json['queue']  = queueO.toDict()

            self.user.put()