Skip to content
This repository has been archived by the owner on Nov 30, 2023. It is now read-only.

Commit

Permalink
feat(farmer): exchange resources on TRADING POST > CARAVAN (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
hldh214 committed Mar 9, 2022
1 parent 09656be commit ba577f3
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 10 deletions.
3 changes: 2 additions & 1 deletion lokbot/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ def main(token, captcha_solver_config=None):

schedule.every(120).to(240).minutes.do(farmer.alliance_helper)
schedule.every(120).to(240).minutes.do(farmer.alliance_farmer)
schedule.every(120).to(240).minutes.do(farmer.use_resource_in_item_list)
schedule.every(200).to(300).minutes.do(farmer.vip_chest_claim)
schedule.every(120).to(240).minutes.do(farmer.caravan_farmer)
schedule.every(120).to(240).minutes.do(farmer.use_resource_in_item_list)
schedule.every(60).to(120).minutes.do(farmer.harvester)

schedule.run_all()
Expand Down
11 changes: 11 additions & 0 deletions lokbot/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,17 @@ def kingdom_world_change(self, world_id):
"""
return self.post('kingdom/world/change', {'worldId': world_id})

def kingdom_caravan_list(self):
return self.post('kingdom/caravan/list')

@tenacity.retry(
wait=tenacity.wait_fixed(1),
retry=tenacity.retry_if_exception_type(ratelimit.RateLimitException),
)
@ratelimit.limits(calls=1, period=4)
def kingdom_caravan_buy(self, caravan_item_id):
return self.post('kingdom/caravan/buy', {'caravanItemId': caravan_item_id})

def alliance_help_all(self):
"""
帮助全部
Expand Down
28 changes: 28 additions & 0 deletions lokbot/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,21 @@
BUILDING_CODE_MAP['gold_mine']
]

ITEM_CODE_FOOD = 10100001
ITEM_CODE_LUMBER = 10100002
ITEM_CODE_STONE = 10100003
ITEM_CODE_GOLD = 10100004
ITEM_CODE_CRYSTAL = 10100005

ITEM_CODE_FOOD_1K = 10101013
ITEM_CODE_FOOD_5K = 10101014
ITEM_CODE_FOOD_10K = 10101015
ITEM_CODE_FOOD_50K = 10101016
ITEM_CODE_FOOD_100K = 10101017
ITEM_CODE_FOOD_500K = 10101018
ITEM_CODE_FOOD_1M = 10101019
ITEM_CODE_FOOD_5M = 10101020
ITEM_CODE_FOOD_10M = 10101021

ITEM_CODE_LUMBER_1K = 10101022
ITEM_CODE_LUMBER_5K = 10101023
Expand All @@ -105,6 +113,8 @@
ITEM_CODE_LUMBER_100K = 10101026
ITEM_CODE_LUMBER_500K = 10101027
ITEM_CODE_LUMBER_1M = 10101028
ITEM_CODE_LUMBER_5M = 10101029
ITEM_CODE_LUMBER_10M = 10101030

ITEM_CODE_STONE_1K = 10101031
ITEM_CODE_STONE_5K = 10101032
Expand All @@ -113,6 +123,8 @@
ITEM_CODE_STONE_100K = 10101035
ITEM_CODE_STONE_500K = 10101036
ITEM_CODE_STONE_1M = 10101037
ITEM_CODE_STONE_5M = 10101038
ITEM_CODE_STONE_10M = 10101039

ITEM_CODE_GOLD_1K = 10101040
ITEM_CODE_GOLD_5K = 10101041
Expand All @@ -121,6 +133,8 @@
ITEM_CODE_GOLD_100K = 10101044
ITEM_CODE_GOLD_500K = 10101045
ITEM_CODE_GOLD_1M = 10101046
ITEM_CODE_GOLD_5M = 10101047
ITEM_CODE_GOLD_10M = 10101048

ITEM_CODE_FOOD_BOOST_8H = 10102001
ITEM_CODE_FOOD_BOOST_1D = 10102002
Expand All @@ -143,6 +157,20 @@
ITEM_CODE_GOLD_1K, ITEM_CODE_GOLD_5K, ITEM_CODE_GOLD_10K, ITEM_CODE_GOLD_50K, ITEM_CODE_GOLD_100K,
)

BUYABLE_CARAVAN_ITEM_CODE_LIST = (
ITEM_CODE_FOOD, ITEM_CODE_FOOD_1K, ITEM_CODE_FOOD_5K, ITEM_CODE_FOOD_10K, ITEM_CODE_FOOD_50K,
ITEM_CODE_FOOD_100K, ITEM_CODE_FOOD_1M, ITEM_CODE_FOOD_5M, ITEM_CODE_FOOD_10M,

ITEM_CODE_LUMBER, ITEM_CODE_LUMBER_1K, ITEM_CODE_LUMBER_5K, ITEM_CODE_LUMBER_10K, ITEM_CODE_LUMBER_50K,
ITEM_CODE_LUMBER_100K, ITEM_CODE_LUMBER_1M, ITEM_CODE_LUMBER_5M, ITEM_CODE_LUMBER_10M,

ITEM_CODE_STONE, ITEM_CODE_STONE_1K, ITEM_CODE_STONE_5K, ITEM_CODE_STONE_10K, ITEM_CODE_STONE_50K,
ITEM_CODE_STONE_100K, ITEM_CODE_STONE_1M, ITEM_CODE_STONE_5M, ITEM_CODE_STONE_10M,

ITEM_CODE_GOLD, ITEM_CODE_GOLD_1K, ITEM_CODE_GOLD_5K, ITEM_CODE_GOLD_10K, ITEM_CODE_GOLD_50K,
ITEM_CODE_GOLD_100K, ITEM_CODE_GOLD_1M, ITEM_CODE_GOLD_5M, ITEM_CODE_GOLD_10M,
)

USABLE_BOOST_CODE_MAP = {
'food': (ITEM_CODE_FOOD_BOOST_8H, ITEM_CODE_FOOD_BOOST_1D),
'lumber': (ITEM_CODE_LUMBER_BOOST_8H, ITEM_CODE_LUMBER_BOOST_1D),
Expand Down
47 changes: 38 additions & 9 deletions lokbot/farmer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from lokbot import logger, builtin_logger
from lokbot.exceptions import OtherException
from lokbot.enum import *
from lokbot.util import get_resource_index_by_item_code, run_functions_in_random_order


class LokFarmer:
Expand All @@ -32,15 +33,17 @@ def __init__(self, access_token, captcha_solver_config):
# knock
self.api.auth_set_device_info(device_info)
self.api.chat_logs(self.kingdom_enter.get('kingdom').get('worldId'))
self.api.kingdom_wall_info()
self.api.quest_main()
self.api.item_list()
self.api.kingdom_treasure_list()
self.api.event_list()
self.api.event_cvc_open()
self.api.event_roulette_open()
self.api.pkg_recommend()
self.api.pkg_list()
run_functions_in_random_order(
self.api.kingdom_wall_info,
self.api.quest_main,
self.api.item_list,
self.api.kingdom_treasure_list,
self.api.event_list,
self.api.event_cvc_open,
self.api.event_roulette_open,
self.api.pkg_recommend,
self.api.pkg_list,
)
self.api.auth_set_device_info(device_info)

# [food, lumber, stone, gold]
Expand Down Expand Up @@ -555,3 +558,29 @@ def alliance_farmer(self):
self.api.alliance_research_donate_all(code)
except OtherException:
pass

def caravan_farmer(self):
caravan = self.api.kingdom_caravan_list().get('caravan')

if not caravan:
return

for each_item in caravan.get('items', []):
if each_item.get('amount') < 1:
continue

if each_item.get('code') not in BUYABLE_CARAVAN_ITEM_CODE_LIST:
continue

if each_item.get('costItemCode') not in BUYABLE_CARAVAN_ITEM_CODE_LIST:
continue

resource_index = get_resource_index_by_item_code(each_item.get('costItemCode'))

if resource_index == -1:
continue

if each_item.get('cost') > self.resources[resource_index]:
continue

self.api.kingdom_caravan_buy(each_item.get('_id'))
31 changes: 31 additions & 0 deletions lokbot/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import random

from lokbot.enum import *


def get_resource_index_by_item_code(item_code):
"""
Returns the index of the item in the resource list
[0, 1, 2, 3 ]
[food, lumber, stone, gold]
"""
if (ITEM_CODE_FOOD_1K <= item_code <= ITEM_CODE_FOOD_10M) or (item_code == ITEM_CODE_FOOD):
return 0

if (ITEM_CODE_LUMBER_1K <= item_code <= ITEM_CODE_LUMBER_10M) or (item_code == ITEM_CODE_LUMBER):
return 1

if (ITEM_CODE_STONE_1K <= item_code <= ITEM_CODE_STONE_10M) or (item_code == ITEM_CODE_STONE):
return 2

if (ITEM_CODE_GOLD_1K <= item_code <= ITEM_CODE_GOLD_10M) or (item_code == ITEM_CODE_GOLD):
return 3

return -1


def run_functions_in_random_order(*funcs):
functions = list(funcs)
random.shuffle(functions)
for func in functions:
func()

0 comments on commit ba577f3

Please sign in to comment.