Skip to content

Commit

Permalink
Merge pull request #6173 from MerlionRock/dev
Browse files Browse the repository at this point in the history
Updated Sniper and Telegram Features
  • Loading branch information
Jcolomar authored Jul 31, 2017
2 parents ecf4f9e + c9d43fa commit 84e50ac
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 12 deletions.
18 changes: 6 additions & 12 deletions pokemongo_bot/cell_workers/sniper.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
import calendar
import difflib
import hashlib

from geopy.distance import great_circle

from random import uniform
from operator import itemgetter, methodcaller
from itertools import izip
Expand All @@ -20,6 +17,7 @@
from pokemongo_bot.worker_result import WorkerResult
from pokemongo_bot.event_handlers.telegram_handler import TelegramSnipe
from pokemongo_bot.cell_workers.pokemon_catch_worker import PokemonCatchWorker
from pokemongo_bot.cell_workers.utils import wait_time_sec, distance, convert

# Represents a URL source and its mappings
class SniperSource(object):
Expand Down Expand Up @@ -361,15 +359,17 @@ def snipe(self, pokemon):
# Backup position before anything
last_position = self.bot.position[0:2]
teleport_position = [pokemon['latitude'], pokemon['longitude']]
teleport_distance = self._get_distance(last_position, teleport_position)
sleep_time = self._get_sleep_sec(teleport_distance)
#teleport_distance = self._get_distance(last_position, teleport_position)
teleport_distance = convert(distance(last_position[0],last_position[1],float(pokemon['latitude']),float(pokemon['longitude'])),"m","km")
#sleep_time = self._get_sleep_sec(teleport_distance)
sleep_time = wait_time_sec(teleport_distance)

if sleep_time > 900:
success = False
exists = False
self._log('Sniping distance is more than supported distance, abort sniping')
else:
self._log('Base on distance, pausing for {} sec'.format(sleep_time))
self._log('Base on distance, pausing for {0:.2f} Mins'.format(sleep_time/60))

# Teleport, so that we can see nearby stuff
self.bot.hb_locked = True
Expand Down Expand Up @@ -695,9 +695,3 @@ def _teleport_back_and_catch(self, position_array, pokemon):
api_encounter_response = catch_worker.create_encounter_api_call()
self._teleport_back(position_array)
catch_worker.work(api_encounter_response)

def _get_distance(self, location_from, location_to):
return great_circle(location_from, location_to).kilometers

def _get_sleep_sec(self, distance):
return distance * 60
19 changes: 19 additions & 0 deletions pokemongo_bot/cell_workers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,25 @@ def encode(cellid):
encoder._VarintEncoder()(output.append, cellid)
return ''.join(output)

def wait_time_sec(distance):
if distance <= 1:
return distance*60
elif distance <= 3:
return (distance/3)*(60*2)
elif distance <= 7:
return (distance/7)*(60*5)
elif distance <= 10:
return (distance/10)*(60*7)
elif distance <= 12:
return (distance/12)*(60*8)
elif distance <= 18:
return (distance/18)*(60*10)
elif distance <= 30:
return (distance/30)*(60*15)
else:
return distance*60



def distance(lat1, lon1, lat2, lon2):
p = 0.017453292519943295
Expand Down
107 changes: 107 additions & 0 deletions pokemongo_bot/event_handlers/telegram_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@
from telegram.utils import request
from chat_handler import ChatHandler
from pokemongo_bot.inventory import Pokemons
from pokemongo_bot import inventory
from pokemongo_bot.item_list import Item
from pokemongo_bot.cell_workers.utils import wait_time_sec, distance, convert

DEBUG_ON = False
SUCCESS = 1
ERROR_XP_BOOST_ALREADY_ACTIVE = 3

class TelegramSnipe(object):
ENABLED = False
ID = int(0)
POKEMON_NAME = ''
LATITUDE = float(0)
LONGITUDE = float(0)
SNIPE_DISABLED = False

class TelegramClass:
update_id = None
Expand Down Expand Up @@ -213,6 +219,24 @@ def request_snipe(self, update, pkm, lat, lng):
outMsg = 'Catching pokemon: ' + TelegramSnipe.POKEMON_NAME + ' at Latitude: ' + str(TelegramSnipe.LATITUDE) + ' Longitude: ' + str(TelegramSnipe.LONGITUDE) + '\n'
self.sendMessage(chat_id=update.message.chat_id, parse_mode='Markdown', text="".join(outMsg))

def request_snipe_time(self, update, lat, lng):
last_position = self.bot.position[0:2]
snipe_distance = convert(distance(last_position[0],last_position[1],float(lat),float(lng)),"m","km")
time_to_snipe = wait_time_sec(snipe_distance)/60
if time_to_snipe <= 900:
outMsg = "Estimate Time to Snipe: " + "{0:.2f}".format(time_to_snipe) + " Mins. Distance: " + "{0:.2f}".format(snipe_distance) + "KM"
self.sendMessage(chat_id=update.message.chat_id, parse_mode='Markdown', text="".join(outMsg))
else:
self.sendMessage(chat_id=update.message.chat_id, parse_mode='Markdown', text="Sniping distance is more than supported distance")

def request_snipe_disable(self, update, config):
if config.lower() == "true":
TelegramSnipe.SNIPE_DISABLED = True
return True
else:
TelegramSnipe.SNIPE_DISABLED = False
return False

def send_evolved(self, update, num, order):
evolved = self.chat_handler.get_evolved(num, order)
outMsg = ''
Expand All @@ -225,6 +249,45 @@ def send_evolved(self, update, num, order):
else:
self.sendMessage(chat_id=update.message.chat_id, parse_mode='Markdown',
text="No Evolutions Found.\n")

def request_luckyegg_count(self,update):
lucky_egg = inventory.items().get(Item.ITEM_LUCKY_EGG.value) # @UndefinedVariable
self.sendMessage(chat_id=update.message.chat_id, parse_mode='Markdown',
text="Lucky Egg Count: " + str(lucky_egg.count))


def request_luckyegg(self,update):
lucky_egg = inventory.items().get(Item.ITEM_LUCKY_EGG.value) # @UndefinedVariable

if lucky_egg.count == 0:
return False

response_dict = self.bot.use_lucky_egg()

if not response_dict:
self.bot.logger.info("Telegram Request: Failed to use lucky egg!")
self.sendMessage(chat_id=update.message.chat_id, parse_mode='Markdown',
text="Failed to use lucky egg!\n")
return False

result = response_dict.get("responses", {}).get("USE_ITEM_XP_BOOST", {}).get("result", 0)

if result == SUCCESS:
lucky_egg.remove(1)
self.bot.logger.info("Telegram Request: Used lucky egg, {} left.".format(lucky_egg.count))
self.sendMessage(chat_id=update.message.chat_id, parse_mode='Markdown',
text="Used lucky egg, " + str(lucky_egg.count) + " left.")
return True
elif result == ERROR_XP_BOOST_ALREADY_ACTIVE:
self.bot.logger.info("Telegram Request: Lucky egg already active, {} left.".format(lucky_egg.count))
self.sendMessage(chat_id=update.message.chat_id, parse_mode='Markdown',
text="Lucky egg already active, " + str(lucky_egg.count) + " left.")
return True
else:
self.bot.logger.info("Telegram Request: Failed to use lucky egg!")
self.sendMessage(chat_id=update.message.chat_id, parse_mode='Markdown',
text="Failed to use lucky egg!\n")
return False

def send_pokestops(self, update, num):
pokestops = self.chat_handler.get_pokestops( num)
Expand Down Expand Up @@ -329,6 +392,9 @@ def send_start(self, update):
"/released <num> <cp-or-iv-or-dated> - show top x released, sorted by CP, IV, or Date",
"/vanished <num> <cp-or-iv-or-dated> - show top x vanished, sorted by CP, IV, or Date",
"/snipe <PokemonName> <Lat> <Lng> - to snipe a pokemon at location Latitude, Longitude",
"/snipetime <Lag> <Lng> - return time that will be teaken to snipe at given location",
"/luckyegg - activate luckyegg",
"/luckyeggcount - return number of luckyegg",
"/softbans - info about possible softbans"
)
self.sendMessage(chat_id=update.message.chat_id, parse_mode='Markdown',
Expand Down Expand Up @@ -466,6 +532,47 @@ def run(self):
self.sendMessage(chat_id=update.message.chat_id, parse_mode='Markdown',
text="An Error has occured")
continue
if re.match(r'^/snipetime ', update.message.text):
try:
(cmd, lat, lng) = self.tokenize(update.message.text, 3)
self.request_snipe_time(update, lat, lng)
except:
self.sendMessage(chat_id=update.message.chat_id, parse_mode='Markdown',
text="An Error has occured")
continue

if re.match(r'^/luckyeggcount', update.message.text):
try:
self.request_luckyegg_count(update)
except:
self.sendMessage(chat_id=update.message.chat_id, parse_mode='Markdown',
text="An Error has occured")
continue
if re.match(r'^/luckyegg', update.message.text):
try:
if self.request_luckyegg(update):
self.bot.logger.info("Telegram has called for lucky egg. Success.")
else:
self.bot.logger.info("Telegram has called for lucky egg. Failed.")
except:
self.sendMessage(chat_id=update.message.chat_id, parse_mode='Markdown',
text="An Error has occured")
continue
if re.match(r'^/snipedisabled ', update.message.text):
try:
(cmd, config) = self.tokenize(update.message.text, 2)
success = self.request_snipe_disable(update, config)
if success:
msg = "Sniper disabled"
else:
msg = "Sniper set as default"

self.sendMessage(chat_id=update.message.chat_id, parse_mode='Markdown',
text=msg)
except:
self.sendMessage(chat_id=update.message.chat_id, parse_mode='Markdown',
text="An Error has occured")
continue
if re.match(r'^/softbans ', update.message.text):
(cmd, num) = self.tokenize(update.message.text, 2)
self.send_softbans(update, num)
Expand Down

0 comments on commit 84e50ac

Please sign in to comment.