Skip to content
This repository has been archived by the owner on Jul 4, 2018. It is now read-only.

Commit

Permalink
Corrected and rewritten way to leave the planet and join new
Browse files Browse the repository at this point in the history
Minor changes in the code
  • Loading branch information
MarshalX committed Jun 27, 2018
1 parent 53dbaad commit ded1d8a
Showing 1 changed file with 51 additions and 29 deletions.
80 changes: 51 additions & 29 deletions SalienSnake.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,17 @@ def check_zone(planet, zone):
zone_info = zone_item

if zone_info.get('captured', True):
logger.info('Information has become wrong! I give new data...')
logger.info('Commander: Information has become wrong! I give new data...')

Commander.planet, Commander.zone = Commander.find_best_planet_and_zone()

logger.info('Commander: New information arrived! Planet {}, zone {} ({}%)!'.format(
Commander.planet['id'],
Commander.zone['zone_position'],
int(Commander.zone['capture_progress'] * 100)
))
else:
logger.info('Information on the zone is relevant!')
logger.info('Commander: Information on the zone is relevant!')

Commander.lock.release()

Expand All @@ -241,43 +247,53 @@ def __init__(self, token, name, language=None):
NamedThread.__init__(self)

self.name = name
self.planet, self.zone = {}, {}
self.API = SteamApi(token, language)
self.player = self.API.get_player_info()
self._API = SteamApi(token, language)
self.player, self.planet, self.zone = {}, {}, {}

def leave_planet(self, planet_id):
self.info('Trying to leave the current planet...')
def get_active_planet(self):
self.player = self._API.get_player_info()

while True:
self.API.leave_game_instance(planet_id)
return self.player['response'].get('active_planet')

def leave_current_planet(self, planet_id=None):
if not planet_id:
planet_id = self.get_active_planet()

if self.API.response_headers['x-eresult'] == '1':
self.info('Successfully left the planet!')
if not planet_id:
return

break
elif self.API.response_headers['x-eresult'] == '11':
# The bot did not finish the zone on another planet
time.sleep(30)
self.info('Trying to leave the current planet (#{})...'.format(planet_id))

time.sleep(1)
self._API.leave_game_instance(planet_id)

x_eresult = int(self._API.response_headers.get('x-eresult', -1))
if x_eresult == 1:
self.info('Successfully left the planet #{}!'.format(planet_id))
elif x_eresult == 11:
self.info('Waiting for the end of the battle to leave the planet (maximum 2 minutes).')

time.sleep(120)
self.leave_current_planet()
else:
self.warning('API. LeaveGame. X-eresult: {}; x-error_message: {}'.format(
x_eresult, self._API.response_headers.get('x-error_message')))

def join_planet(self, planet):
cid = self.planet.get('id')
if cid != planet['id']:
if cid is not None:
self.leave_planet(cid)
current_planet = self.planet.get('id')
if current_planet != planet['id']:
self.leave_current_planet(current_planet)

self.API.join_planet(planet['id'])
self._API.join_planet(planet['id'])
self.planet = planet

self.info('Yes, sir! Joined planet #{}'.format(self.planet['id']))

def join_zone(self, zone):
self.zone = zone

self.API.join_zone(self.zone['zone_position'])
self._API.join_zone(self.zone['zone_position'])

if self.API.response_headers['x-eresult'] == '27':
if self._API.response_headers['x-eresult'] == '27':
raise AttributeError()

self.info('Attacking zone {}; {}'.format(
Expand All @@ -286,15 +302,21 @@ def join_zone(self, zone):
))

def run(self):
# Getting information about the user and leaving the active planet if it does not suit us
active_planet = self.get_active_planet()
if active_planet and Commander.planet['id'] != active_planet:
self.leave_current_planet(active_planet)
else:
self.planet['id'] = active_planet

self.info('I\'m already on necessary planet')

self.info('Current score = {}/{}; Current Level = {}'.format(
self.player['response']['score'],
self.player['response']['next_level_score'],
self.player['response']['level']
))

if 'active_planet' in self.player['response']:
self.leave_planet(self.player['response']['active_planet'])

while True:
self.join_planet(Commander.planet)

Expand All @@ -312,15 +334,15 @@ def run(self):
score = 120 * (5 * (2 ** (self.zone['difficulty'] - 1)))

try:
score_stats = self.API.report_score(score)
score_stats = self._API.report_score(score)

self.info('Current score = {}/{}; Current level = {}'.format(
score_stats['response']['new_score'],
score_stats['response']['next_level_score'],
score_stats['response']['new_level']
))
except KeyError:
x_eresult = int(self.API.response_headers.get('x-eresult', -1))
x_eresult = int(self._API.response_headers.get('x-eresult', -1))

if x_eresult == 93:
self.warning('API. ReportScore. Request sent too early.')
Expand All @@ -336,7 +358,7 @@ def run(self):
'from the Duldrumz by the Steam Community.')
else:
self.warning('API. ReportScore. X-eresult: {}; x-error_message: {}'.format(
x_eresult, self.API.response_headers.get('x-error_message')))
x_eresult, self._API.response_headers.get('x-error_message')))

Commander.check_zone(self.planet, self.zone)

Expand Down

0 comments on commit ded1d8a

Please sign in to comment.