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

Commit

Permalink
feat(farmer): add interval for train_troop_thread && sync version
Browse files Browse the repository at this point in the history
Signed-off-by: hldh214 <[email protected]>
  • Loading branch information
hldh214 committed Jul 3, 2023
1 parent d990303 commit 947cd02
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
3 changes: 2 additions & 1 deletion config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@
"enabled": true,
"kwargs": {
"troop_code": 50100101,
"speedup": true
"speedup": true,
"interval": 3600
}
}
]
Expand Down
2 changes: 1 addition & 1 deletion lokbot/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ def field_march_start(self, data):

def get_version():
first = 1
second = 1630
second = 1652
third = httpx.get('https://play.leagueofkingdoms.com/json/version-live.json').json().get('table')
fourth = httpx.get(f'https://play.leagueofkingdoms.com/bundles/webgl/kingdominfo_{second}').json()
fourth = [each for each in fourth if each.get('name') == 'ui'][0].get('version')
Expand Down
21 changes: 9 additions & 12 deletions lokbot/farmer.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def __init__(self, token, captcha_solver_config):
"country": "USA",
"language": "English",
"bundle": "",
"version": "1.1630.136.218",
"version": "1.1652.138.219",
"platform": "web",
"pushId": ""
})
Expand Down Expand Up @@ -1042,9 +1042,10 @@ def _random_choice_building(self, building_code):
buildings = self.kingdom_enter.get('kingdom', {}).get('buildings', [])
return random.choice([building for building in buildings if building['code'] == building_code])

def train_troop_thread(self, troop_code, speedup=False):
def train_troop_thread(self, troop_code, speedup=False, interval=3600):
"""
train troop
:param interval:
:param troop_code:
:param speedup:
:return:
Expand All @@ -1058,18 +1059,14 @@ def train_troop_thread(self, troop_code, speedup=False):
if worker_used:
if worker_used[0].get('status') == STATUS_CLAIMED:
self.api.kingdom_task_claim(self._random_choice_building(BUILDING_CODE_MAP['barrack'])['position'])
logger.info('train_troop: one loop completed, sleep for 3h')
threading.Timer(
3 * 3600,
self.train_troop_thread,
[troop_code, speedup]
).start()
logger.info(f'train_troop: one loop completed, sleep for {interval} seconds')
threading.Timer(interval, self.train_troop_thread, [troop_code, speedup, interval]).start()
return

if worker_used[0].get('status') == STATUS_PENDING:
self.train_queue_available.wait() # wait for train queue available from `sock_thread`
self.train_queue_available.clear()
threading.Thread(target=self.train_troop_thread, args=[troop_code, speedup]).start()
threading.Thread(target=self.train_troop_thread, args=[troop_code, speedup, interval]).start()
return

# if there is not enough resource, train how much possible
Expand All @@ -1078,8 +1075,8 @@ def train_troop_thread(self, troop_code, speedup=False):
troop_training_capacity = total_troops_capacity_according_to_resources

if not troop_training_capacity:
logger.info('train_troop: no resource, sleep for 3h')
threading.Timer(3 * 3600, self.train_troop_thread, [troop_code, speedup]).start()
logger.info('train_troop: no resource, sleep for 1h')
threading.Timer(3600, self.train_troop_thread, [troop_code, speedup, interval]).start()
return

res = self.api.train_troop(troop_code, troop_training_capacity)
Expand All @@ -1089,7 +1086,7 @@ def train_troop_thread(self, troop_code, speedup=False):

self.train_queue_available.wait() # wait for train queue available from `sock_thread`
self.train_queue_available.clear()
threading.Thread(target=self.train_troop_thread, args=[troop_code, speedup]).start()
threading.Thread(target=self.train_troop_thread, args=[troop_code, speedup, interval]).start()

def free_chest_farmer_thread(self, _type=0):
"""
Expand Down

0 comments on commit 947cd02

Please sign in to comment.