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

Commit

Permalink
feat(farmer): imp _on_field_objects_gather
Browse files Browse the repository at this point in the history
Signed-off-by: hldh214 <[email protected]>
  • Loading branch information
hldh214 committed Aug 19, 2023
1 parent f20dc4e commit e48d69a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
3 changes: 2 additions & 1 deletion lokbot/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,8 @@
OBJECT_CODE_SPARTOI = 20700506 # need rally

OBJECT_MINE_CODE_LIST = (
OBJECT_CODE_FARM, OBJECT_CODE_LUMBER_CAMP, OBJECT_CODE_QUARRY, OBJECT_CODE_GOLD_MINE, OBJECT_CODE_CRYSTAL_MINE
OBJECT_CODE_FARM, OBJECT_CODE_LUMBER_CAMP, OBJECT_CODE_QUARRY, OBJECT_CODE_GOLD_MINE, OBJECT_CODE_CRYSTAL_MINE,
OBJECT_CODE_DRAGON_SOUL_CAVERN
)

OBJECT_MONSTER_CODE_LIST = (
Expand Down
33 changes: 24 additions & 9 deletions lokbot/farmer.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ def __init__(self, token, captcha_solver_config):
self.train_queue_available = threading.Event()
self.kingdom_tasks = []
self.zones = []
self.available_dragos = self._get_available_dragos()

@staticmethod
def calc_time_diff_in_seconds(expected_ended):
Expand Down Expand Up @@ -583,6 +584,13 @@ def _prepare_march_troops(self, each_obj, march_type=MARCH_TYPE_GATHER):

return march_troops

def _get_available_dragos(self):
drago_lair_list = self.api.drago_lair_list()
dragos = drago_lair_list.get('dragos')
available_dragos = [each for each in dragos if each['lair']['status'] == DRAGO_LAIR_STATUS_STANDBY]

return available_dragos

def _on_field_objects_gather(self, each_obj):
if each_obj.get('occupied'):
return
Expand All @@ -597,15 +605,11 @@ def _on_field_objects_gather(self, each_obj):
return

if each_obj.get('code') == OBJECT_CODE_DRAGON_SOUL_CAVERN:
drago_lair_list = self.api.drago_lair_list()
dragos = drago_lair_list.get('dragos')
available_dragos = [each for each in dragos if each['lair']['status'] == DRAGO_LAIR_STATUS_STANDBY]

if not available_dragos:
if not self.available_dragos:
logger.warning('No available dragos')
return

self._start_march(to_loc, march_troops, MARCH_TYPE_GATHER, available_dragos[0]['_id'])
self._start_march(to_loc, march_troops, MARCH_TYPE_GATHER, self.available_dragos[0]['_id'])

self._start_march(to_loc, march_troops, MARCH_TYPE_GATHER)

Expand Down Expand Up @@ -715,7 +719,7 @@ def on_task_update(data):
retry=tenacity.retry_if_not_exception_type(FatalApiException),
reraise=True
)
def socf_thread(self, radius, object_code_list=(OBJECT_CODE_CRYSTAL_MINE, OBJECT_CODE_GOBLIN)):
def socf_thread(self, radius, targets):
"""
websocket connection of the field
:return:
Expand Down Expand Up @@ -755,6 +759,7 @@ def on_field_objects(data):
gzip_decompress = gzip.decompress(bytearray(packs))
data_decoded = self.api.b64xor_dec(gzip_decompress)
objects = data_decoded.get('objects')
target_code_set = set([target['code'] for target in targets])

logger.debug(f'Processing {len(objects)} objects')
for each_obj in objects:
Expand All @@ -763,11 +768,21 @@ def on_field_objects(data):

code = each_obj.get('code')

level_whitelist = [target['level'] for target in targets if target['code'] == code]
if not level_whitelist:
# not the one we are looking for
continue

level_whitelist = level_whitelist[0]
if level_whitelist and each_obj.get('level') not in level_whitelist:
logger.info(f'ignore: {each_obj}, level not in whitelist: {level_whitelist}')
continue

try:
if code in set(OBJECT_MINE_CODE_LIST).intersection(set(object_code_list)):
if code in set(OBJECT_MINE_CODE_LIST).intersection(target_code_set):
self._on_field_objects_gather(each_obj)

if code in set(OBJECT_MONSTER_CODE_LIST).intersection(set(object_code_list)):
if code in set(OBJECT_MONSTER_CODE_LIST).intersection(target_code_set):
self._on_field_objects_monster(each_obj)
except OtherException as error_code:
if str(error_code) in (
Expand Down

0 comments on commit e48d69a

Please sign in to comment.