コード例 #1
0
def do_it(session, city, offers, amount_to_buy):
    """
	Parameters
	----------
	session : ikabot.web.session.Session
	city : dict
	offers : list[dict]
	amount_to_buy : int
	"""
    while True:
        for offer in offers:
            if amount_to_buy == 0:
                return
            if offer['amountAvailable'] == 0:
                continue

            ships_available = waitForArrival(session)
            storageCapacity = ships_available * 500
            buy_amount = min(amount_to_buy, storageCapacity,
                             offer['amountAvailable'])

            amount_to_buy -= buy_amount
            offer['amountAvailable'] -= buy_amount
            buy(session, city, offer, buy_amount)
            # start from the beginning again, so that we always buy from the cheapest offers fisrt
            break
コード例 #2
0
ファイル: attackBarbarians.py プロジェクト: physics-sp/ikabot
def do_it(session, island, city, babarians_info, plan):

    units_data = {}

    battle_start = None

    for attack_round in plan:

        # this round is supposed to get the resources
        if attack_round['loot']:
            break

        attack_data = {
            'action': 'transportOperations',
            'function': 'attackBarbarianVillage',
            'actionRequest': actionRequest,
            'islandId': island['id'],
            'destinationCityId': 0,
            'cargo_army_304_upkeep': 3,
            'cargo_army_304': 0,
            'cargo_army_315_upkeep': 1,
            'cargo_army_315': 0,
            'cargo_army_302_upkeep': 4,
            'cargo_army_302': 0,
            'cargo_army_303_upkeep': 3,
            'cargo_army_303': 0,
            'cargo_army_312_upkeep': 15,
            'cargo_army_312': 0,
            'cargo_army_309_upkeep': 45,
            'cargo_army_309': 0,
            'cargo_army_307_upkeep': 15,
            'cargo_army_307': 0,
            'cargo_army_306_upkeep': 25,
            'cargo_army_306': 0,
            'cargo_army_305_upkeep': 30,
            'cargo_army_305': 0,
            'cargo_army_311_upkeep': 20,
            'cargo_army_311': 0,
            'cargo_army_310_upkeep': 10,
            'cargo_army_310': 0,
            'transporter': 0,
            'barbarianVillage': 1,
            'backgroundView': 'island',
            'currentIslandId': island['id'],
            'templateView': 'plunder',
            'ajax': 1
        }

        attack_data, ships_needed, travel_time = load_troops(
            session, city, island, attack_round, units_data, attack_data)

        try:
            wait_for_round(session, city, island, travel_time, battle_start,
                           attack_round['round'])
        except AssertionError:
            # battle ended before expected
            break

        ships_available = waitForArrival(session)
        while ships_available < ships_needed:
            ships_available = waitForArrival(session)
        ships_available -= ships_needed

        # if the number of available troops changed, the POST request might not work as intended

        attack_data['transporter'] = min(babarians_info['ships'],
                                         attack_round['ships'],
                                         ships_available)

        # send new round
        session.post(payloadPost=attack_data)

        if attack_round['round'] == 1:
            battle_start = time.time() + travel_time

    wait_until_attack_is_over(session, city, island)

    last_round = plan[-1]
    if last_round['loot']:
        loot(session, city, island, units_data, last_round)
コード例 #3
0
def do_it1(session, left_to_sell, offers, resource_type, city_to_buy_from):
    """
    Parameters
    ----------
    session : ikabot.web.session.Session
    left_to_sell : int
    offers : list[dict]
    resource_type : int
    city_to_buy_from : dict
    """
    for offer in offers:
        cityname, username, amount, precio, dist, destination_city_id = offer
        cityname = cityname.strip()
        amount_to_buy = amount.replace(',', '').replace('.', '')
        amount_to_buy = int(amount_to_buy)
        while True:
            amount_to_sell = min(amount_to_buy, left_to_sell)
            ships_available = waitForArrival(session)
            ships_needed = math.ceil((Decimal(amount_to_sell) / Decimal(500)))
            ships_used = min(ships_available, ships_needed)
            if ships_needed > ships_used:
                amount_to_sell = ships_used * 500
            left_to_sell -= amount_to_sell
            amount_to_buy -= amount_to_sell

            data = {
                'action': 'transportOperations',
                'function': 'sellGoodsAtAnotherBranchOffice',
                'cityId': city_to_buy_from['id'],
                'destinationCityId': destination_city_id,
                'oldView': 'branchOffice',
                'position': city_to_buy_from['pos'],
                'avatar2Name': username,
                'city2Name': cityname,
                'type': '333',
                'activeTab': 'bargain',
                'transportDisplayPrice': '0',
                'premiumTransporter': '0',
                'capacity': '5',
                'max_capacity': '5',
                'jetPropulsion': '0',
                'transporters': str(ships_used),
                'backgroundView': 'city',
                'currentCityId': city_to_buy_from['id'],
                'templateView': 'takeOffer',
                'currentTab': 'bargain',
                'actionRequest': actionRequest,
                'ajax': '1'
            }
            if resource_type == 0:
                data['cargo_resource'] = amount_to_sell
                data['resourcePrice'] = precio
            else:
                data['tradegood{:d}Price'.format(resource_type)] = precio
                data['cargo_tradegood{:d}'.format(
                    resource_type)] = amount_to_sell

            session.get(city_url + city_to_buy_from['id'], noIndex=True)
            session.post(payloadPost=data)

            if left_to_sell == 0:
                return
            if amount_to_buy == 0:
                break
コード例 #4
0
ファイル: attackBarbarians.py プロジェクト: physics-sp/ikabot
def loot(session, island, city, units_data, loot_round):
    while True:

        attack_data = {
            'action': 'transportOperations',
            'function': 'attackBarbarianVillage',
            'actionRequest': actionRequest,
            'islandId': island['id'],
            'destinationCityId': 0,
            'cargo_army_304_upkeep': 3,
            'cargo_army_304': 0,
            'cargo_army_315_upkeep': 1,
            'cargo_army_315': 0,
            'cargo_army_302_upkeep': 4,
            'cargo_army_302': 0,
            'cargo_army_303_upkeep': 3,
            'cargo_army_303': 0,
            'cargo_army_312_upkeep': 15,
            'cargo_army_312': 0,
            'cargo_army_309_upkeep': 45,
            'cargo_army_309': 0,
            'cargo_army_307_upkeep': 15,
            'cargo_army_307': 0,
            'cargo_army_306_upkeep': 25,
            'cargo_army_306': 0,
            'cargo_army_305_upkeep': 30,
            'cargo_army_305': 0,
            'cargo_army_311_upkeep': 20,
            'cargo_army_311': 0,
            'cargo_army_310_upkeep': 10,
            'cargo_army_310': 0,
            'transporter': 0,
            'barbarianVillage': 1,
            'backgroundView': 'island',
            'currentIslandId': island['id'],
            'templateView': 'plunder',
            'ajax': 1
        }

        # make sure we have ships on the port
        ships_available = waitForArrival(session)

        # if the barbarians are active again or all the resources were stolen, return
        html = session.get(island_url + island['id'])
        island = getIsland(html)
        destroyed = island['barbarians']['destroyed'] == 1
        resources = get_barbarians_lv(session, island)['resources']
        if destroyed is False or sum(resources) == 0:
            return

        # if we already sent an attack and we still have ships on the port, it was the last one
        attacks = get_current_attacks(session, city['id'], island['id'])
        attacks = filter_loading(attacks) + filter_traveling(attacks)
        if len(attacks) > 0:
            return

        attack_data, ships_needed, travel_time = load_troops(
            session, city, island, loot_round, units_data, attack_data,
            sum(resources))
        attack_data['transporter'] = min(ships_available, ships_needed)

        # make sure we have time to send the attack
        time_left = None
        resp = get_barbarians_info(session, island['id'])
        if 'barbarianCityCooldownTimer' in resp[2][1]:
            time_left = resp[2][1]['barbarianCityCooldownTimer']['countdown'][
                'enddate']
            time_left -= time.time()
        if time_left is not None and travel_time > time_left:
            return

        # send attack
        session.post(payloadPost=attack_data)