Beispiel #1
0
def do_it(s, hours):
    ids, cities = getIdsOfCities(s)
    while True:
        # getIdsOfCities is called on a loop because the amount of cities may change
        ids, cities_new = getIdsOfCities(s)
        if len(cities_new) != len(cities):
            cities = cities_new

        for cityId in cities:
            if 'reported' not in cities[cityId]:
                cities[cityId]['reported'] = False

        for cityId in cities:
            html = s.get(urlCiudad + cityId)
            city = getCity(html)

            # if the city doesn't even have a tavern built, ignore it
            if 'tavern' not in [
                    building['building'] for building in city['position']
            ]:
                continue

            consumption_per_hour = city['consumo']

            # is a wine city
            if cities[cityId]['tradegood'] == '1':
                wine_production = getProduccionPerSecond(s, cityId)[1]
                wine_production = wine_production * 60 * 60
                if consumption_per_hour > wine_production:
                    consumption_per_hour -= wine_production
                else:
                    continue

            consumption_per_seg = Decimal(consumption_per_hour) / Decimal(3600)
            wine_available = city['recursos'][1]

            if consumption_per_seg == 0:
                if cities[cityId]['reported'] is False:
                    msg = _('The city {} is not consuming wine!').format(
                        city['name'])
                    sendToBot(s, msg)
                    cities[cityId]['reported'] = True
                continue

            seconds_left = Decimal(wine_available) / Decimal(
                consumption_per_seg)
            if seconds_left < hours * 60 * 60:
                if cities[cityId]['reported'] is False:
                    time_left = daysHoursMinutes(seconds_left)
                    msg = _('In {}, the wine will run out in {}').format(
                        time_left, city['name'])
                    sendToBot(s, msg)
                    cities[cityId]['reported'] = True
            else:
                cities[cityId]['reported'] = False

        time.sleep(20 * 60)
Beispiel #2
0
def do_it(s, minutes):
	# this thread lets the user react to an attack once the alert is sent
	t = threading.Thread(target=respondToAttack, args=(s,))
	t.start()

	knownAttacks = []
	while True:
		# get the militaryMovements
		html = s.get()
		idCiudad = re.search(r'currentCityId:\s(\d+),', html).group(1)
		url = 'view=militaryAdvisor&oldView=city&oldBackgroundView=city&backgroundView=city&currentCityId={}&actionRequest=REQUESTID&ajax=1'.format(idCiudad)
		posted = s.post(url)
		postdata = json.loads(posted, strict=False)
		militaryMovements = postdata[1][1][2]['viewScriptParams']['militaryAndFleetMovements']
		timeNow = int(postdata[0][1]['time'])

		currentAttacks = []
		for militaryMovement in [ mov for mov in militaryMovements if mov['isHostile'] ]:
			id = militaryMovement['event']['id']
			currentAttacks.append(id)
			# if we already alerted this, do nothing
			if id not in knownAttacks:
				knownAttacks.append(id)

				# get information about the attack
				missionText = militaryMovement['event']['missionText']
				origin = militaryMovement['origin']
				target = militaryMovement['target']
				amountTroops = militaryMovement['army']['amount']
				amountFleets = militaryMovement['fleet']['amount']
				timeLeft = int(militaryMovement['eventTime']) - timeNow

				# send alert
				msg  = _('-- ALERT --\n')
				msg += missionText + '\n'
				msg += _('from the city {} of {}\n').format(origin['name'], origin['avatarName'])
				msg += _('a {}\n').format(target['name'])
				msg += _('{} units\n').format(amountTroops)
				msg += _('{} fleet\n').format(amountFleets)
				msg += _('arrival in: {}\n').format(daysHoursMinutes(timeLeft))
				msg += _('If you want to put the account in vacation mode send:\n')
				msg += _('{:d}:1').format(os.getpid())
				sendToBot(s, msg)

		# remove old attacks from knownAttacks
		for id in list(knownAttacks):
			if id not in currentAttacks:
				knownAttacks.remove(id)

		time.sleep(minutes * 60)
Beispiel #3
0
def do_it(session, hours):
    """
    Parameters
    ----------
    session : ikabot.web.session.Session
    hours : int
    """

    was_alerted = {}
    while True:
        # getIdsOfCities is called on a loop because the amount of cities may change
        ids, cities = getIdsOfCities(session)

        for cityId in cities:
            if cityId not in was_alerted:
                was_alerted[cityId] = False

        for cityId in cities:
            html = session.get(city_url + cityId)
            city = getCity(html)

            # if the city doesn't even have a tavern built, ignore it
            if 'tavern' not in [
                    building['building'] for building in city['position']
            ]:
                continue

            consumption_per_hour = city['consumo']

            # is a wine city
            if cities[cityId]['tradegood'] == '1':
                wine_production = getProductionPerSecond(session, cityId)[1]
                wine_production = wine_production * 60 * 60
                if consumption_per_hour > wine_production:
                    consumption_per_hour -= wine_production
                else:
                    was_alerted[cityId] = False
                    continue

            consumption_per_seg = Decimal(consumption_per_hour) / Decimal(3600)
            wine_available = city['recursos'][1]

            if consumption_per_seg == 0:
                if was_alerted[cityId] is False:
                    msg = _('The city {} is not consuming wine!').format(
                        city['name'])
                    sendToBot(session, msg)
                    was_alerted[cityId] = True
                continue

            seconds_left = Decimal(wine_available) / Decimal(
                consumption_per_seg)
            if seconds_left < hours * 60 * 60:
                if was_alerted[cityId] is False:
                    time_left = daysHoursMinutes(seconds_left)
                    msg = _('In {}, the wine will run out in {}').format(
                        time_left, city['name'])
                    sendToBot(session, msg)
                    was_alerted[cityId] = True
            else:
                was_alerted[cityId] = False

        time.sleep(20 * 60)