Skip to content

Commit

Permalink
Webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
JabLuszko committed Sep 15, 2024
1 parent bfc066e commit 8be92ff
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 2 deletions.
35 changes: 34 additions & 1 deletion mapadroid/db/DbWebhookReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
from mapadroid.db.helper.PokemonHelper import PokemonHelper
from mapadroid.db.helper.PokestopHelper import PokestopHelper
from mapadroid.db.helper.RaidHelper import RaidHelper
from mapadroid.db.helper.StationHelper import StationHelper
from mapadroid.db.helper.WeatherHelper import WeatherHelper
from mapadroid.db.model import Raid, Gym, GymDetail, Weather, TrsQuest, Pokestop, Pokemon, TrsSpawn, PokemonDisplay, \
PokestopIncident
PokestopIncident, Station
from mapadroid.utils.WebhookJsonEncoder import WebhookJsonEncoder
from mapadroid.utils.logging import get_logger, LoggerEnums
from mapadroid.utils.madGlobals import MonSeenTypes
Expand Down Expand Up @@ -197,3 +198,35 @@ async def get_mon_changed_since(session: AsyncSession, _timestamp: int,
"size": mon.size
})
return ret

@staticmethod
async def get_stations_changed_since(session: AsyncSession, _timestamp: int):
logger.debug2("DbWebhookReader::get_stations_changed_since called with timestamp {}", _timestamp)
stations_changed: List[Station] = await StationHelper.get_changed_since(session, _timestamp=_timestamp)

ret = []
for station in stations_changed:
ret.append({
"station_id": station.station_id,
"battle_level": station.battle_level,
"battle_spawn": int(station.battle_spawn.timestamp()),
"battle_start": int(station.battle_window_start.timestamp()),
"battle_end": int(station.battle_window_end.timestamp()),
"battle_pokemon_move_1": station.battle_pokemon_move_1,
"battle_pokemon_move_2": station.battle_pokemon_move_2,
"battle_pokemon_bread_mode": station.battle_pokemon_bread_mode,
"battle_pokemon_id": station.battle_pokemon_id,
"battle_pokemon_costume": station.battle_pokemon_costume,
"battle_pokemon_form": station.battle_pokemon_form,
"battle_pokemon_gender": station.battle_pokemon_gender,
"battle_pokemon_alignment": station.battle_pokemon_alignment,
"start_time": station.start_time,
"end_time": station.end_time,
"name": station.name,
"longitude": station.latitude,
"latitude": station.longitude,
"inactive": station.inactive,
"bread_battle_available": station.bread_battle_available,
"last_scanned": station.last_updated
})
return ret
70 changes: 69 additions & 1 deletion mapadroid/webhook/webhookworker.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __init__(self, args, db_wrapper: DbWrapper, mapping_manager: MappingManager,
self.__pokemon_types: Set[MonSeenTypes] = set()
self.__mapping_manager: MappingManager = mapping_manager
self.__valid_types: Set[str] = {
'pokemon', 'raid', 'weather', 'quest', 'gym', 'pokestop'
'pokemon', 'raid', 'weather', 'quest', 'gym', 'pokestop', 'station'
}
self.__valid_mon_types: Set[MonSeenTypes] = {
MonSeenTypes.encounter, MonSeenTypes.wild, MonSeenTypes.nearby_stop, MonSeenTypes.nearby_cell,
Expand Down Expand Up @@ -685,6 +685,13 @@ async def __create_payload(self):
async with self.__db_wrapper as session, session:
# TODO: Single transaction...
try:
# stations
if 'station' in self.__webhook_types:
stations = self.__prepare_station_data(
await DbWebhookReader.get_stations_changed_since(session, self.__last_check)
)
full_payload += stations

# raids
if 'raid' in self.__webhook_types:
raids = self.__prepare_raid_data(
Expand Down Expand Up @@ -761,3 +768,64 @@ async def __run_worker(self):
await asyncio.sleep(self.__worker_interval_sec)

logger.info("Stopping webhook worker thread")

def __prepare_station_data(self, station_data):
ret = []

for station in station_data:
if self.__is_in_excluded_area([station["latitude"], station["longitude"]]):
continue

station_payload = {
"station_id": station["station_id"],
"latitude": station["latitude"],
"longitude": station["longitude"],
"start": station["start_time"],
"end": station["end_time"],
"name": station["name"],
"inactive": station["inactive"],
"bread_battle_available": station["bread_battle_available"],
"last_updated": station["last_scanned"],
}

if station["battle_spawn"] is not None:
station_payload["battle_spawn"] = station["battle_spawn"]
station_payload["battle_start"] = station["battle_start"]
station_payload["battle_end"] = station["battle_end"]
station_payload["battle_level"] = station["battle_level"]

if station["battle_pokemon_id"] is not None:
station_payload["battle_pokemon_id"] = station["battle_pokemon_id"]

if station["battle_pokemon_form"] is not None:
station_payload["battle_pokemon_form"] = station["battle_pokemon_form"]

if station["battle_pokemon_gender"] is not None:
station_payload["battle_pokemon_gender"] = station["battle_pokemon_gender"]

if station["battle_pokemon_costume"] is not None:
station_payload["battle_pokemon_costume"] = station["battle_pokemon_costume"]

if station["battle_pokemon_alignment"] is not None:
station_payload["battle_pokemon_alignment"] = station["battle_pokemon_alignment"]

if station["battle_pokemon_move_1"] is not None:
station_payload["battle_pokemon_move_1"] = station["battle_pokemon_move_1"]

if station["battle_pokemon_move_2"] is not None:
station_payload["battle_pokemon_move_2"] = station["battle_pokemon_move_2"]

if station["battle_pokemon_bread_mode"] is not None:
station_payload["battle_pokemon_bread_mode"] = station["battle_pokemon_bread_mode"]

if station["battle_pokemon_bread_mode"] is not None:
station_payload["battle_pokemon_bread_mode"] = station["battle_pokemon_bread_mode"]

# create final message
entire_payload = {"type": "station", "message": station_payload}

# add to payload
ret.append(entire_payload)

return ret

0 comments on commit 8be92ff

Please sign in to comment.