-
Notifications
You must be signed in to change notification settings - Fork 151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Raids [BACKEND ONLY] #306
base: develop
Are you sure you want to change the base?
Raids [BACKEND ONLY] #306
Changes from 45 commits
7792ae7
5321881
b7d0ad1
d2441b7
ec1cc6f
9861703
da68e70
013d745
b0cd60f
b06b4fc
88e5f2d
e8d4f31
90ad98b
7784e98
6ca81f8
9b72cb7
9075028
5ed1dcf
8ff947c
98b9cab
90441a1
d291663
e4baa9c
b810e2b
12d1a85
0ea754e
aa6770d
b2d9ecd
ff23ca3
f687ebc
f8e8190
993ca61
a1785e2
eb4c1e8
af9171a
96d84c0
4ffff07
741363a
8a2c5e1
a9f28f4
147a86d
9f7bfa2
1793fdb
599e32e
441a9bc
74c8967
0ce0822
c843568
1aeba9a
3d43101
77abfa5
1df901a
f166a67
ead87e8
5aaaba1
b5f4d8e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -191,10 +191,46 @@ def unpickle(self): | |
except (FileNotFoundError, TypeError, KeyError): | ||
pass | ||
|
||
class RaidCache: | ||
"""Simple cache for storing fort sightings""" | ||
def __init__(self): | ||
self.raids = {} | ||
self.class_version = 2 | ||
self.unpickle() | ||
|
||
def __len__(self): | ||
return len(self.raids) | ||
|
||
def add(self, raid): | ||
self.raids[raid['external_id']] = str(raid['raid_spawn_ms']) + str(raid['pokemon_id']) | ||
|
||
def __contains__(self, raid): | ||
try: | ||
return self.raids[raid['external_id']] == str(raid['raid_spawn_ms']) + str(raid['pokemon_id']) | ||
except KeyError: | ||
return False | ||
|
||
def pickle(self): | ||
state = self.__dict__.copy() | ||
state['db_hash'] = spawns.db_hash | ||
state['bounds_hash'] = hash(bounds) | ||
dump_pickle('raids', state) | ||
|
||
def unpickle(self): | ||
try: | ||
state = load_pickle('raids', raise_exception=True) | ||
if all((state['class_version'] == self.class_version, | ||
state['db_hash'] == spawns.db_hash, | ||
state['bounds_hash'] == hash(bounds))): | ||
self.__dict__.update(state) | ||
except (FileNotFoundError, TypeError, KeyError): | ||
pass | ||
|
||
|
||
SIGHTING_CACHE = SightingCache() | ||
MYSTERY_CACHE = MysteryCache() | ||
FORT_CACHE = FortCache() | ||
RAID_CACHE = RaidCache() | ||
|
||
Base = declarative_base() | ||
|
||
|
@@ -289,6 +325,12 @@ class Fort(Base): | |
backref='fort', | ||
order_by='FortSighting.last_modified' | ||
) | ||
|
||
raids = relationship( | ||
'RaidSighting', | ||
backref='fort', | ||
order_by='RaidSighting.raid_spawn_ms' | ||
) | ||
|
||
|
||
class FortSighting(Base): | ||
|
@@ -310,6 +352,28 @@ class FortSighting(Base): | |
) | ||
|
||
|
||
class RaidSighting(Base): | ||
__tablename__ = 'fort_raids' | ||
id = Column(Integer, primary_key=True) | ||
fort_id = Column(Integer, ForeignKey('forts.id')) | ||
raid_battle_ms = Column(Integer, index=True) | ||
raid_spawn_ms = Column(Integer, index=True) | ||
raid_end_ms = Column(Integer, index=True) | ||
raid_level = Column(Integer) | ||
complete = Column(TINY_TYPE) | ||
pokemon_id = Column(TINY_TYPE) | ||
cp = Column(TINY_TYPE) | ||
move_1 = Column(SmallInteger) | ||
move_2 = Column(SmallInteger) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do the moves matter at all? as everyone will get something else after catching it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. moves are the same for everyone in the battle against the raid boss. Thus it is a strategy part knowing if he has certain moves There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you sure? what i heard is not same when in battle and after caught There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I won't use it but that could be usefull to counter him There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @walaoaaa1234 the moveset is the same for the raid boss battle but afterwards we get a randomized moveset for the capturable version. |
||
|
||
__table_args__ = ( | ||
UniqueConstraint( | ||
'fort_id', | ||
'raid_spawn_ms', | ||
name='fort_id_raid_spawn_ms_unique' | ||
), | ||
) | ||
|
||
class Pokestop(Base): | ||
__tablename__ = 'pokestops' | ||
|
||
|
@@ -488,6 +552,40 @@ def add_fort_sighting(session, raw_fort): | |
session.add(obj) | ||
FORT_CACHE.add(raw_fort) | ||
|
||
def add_raid_sighting(session, raw_raid): | ||
# Check if fort exists | ||
fort = session.query(Fort) \ | ||
.filter(Fort.external_id == raw_raid['external_id']) \ | ||
.first() | ||
if fort.id and session.query(exists().where(and_( | ||
RaidSighting.fort_id == fort.id, | ||
RaidSighting.raid_spawn_ms == raw_raid['raid_spawn_ms'], | ||
RaidSighting.pokemon_id != raw_raid['pokemon_id'] | ||
))).scalar(): | ||
#Update pokemon_id etc. | ||
update_raid(session,fort.id,raw_raid) | ||
if fort.id and session.query(exists().where(and_( | ||
RaidSighting.fort_id == fort.id, | ||
RaidSighting.raid_spawn_ms == raw_raid['raid_spawn_ms'] | ||
))).scalar(): | ||
# Why is it not in the cache? It should be there! | ||
RAID_CACHE.add(raw_raid) | ||
return | ||
else: | ||
obj = RaidSighting( | ||
fort=fort, | ||
raid_battle_ms=raw_raid['raid_battle_ms'], | ||
raid_spawn_ms=raw_raid['raid_spawn_ms'], | ||
raid_end_ms=raw_raid['raid_end_ms'], | ||
raid_level=raw_raid['raid_level'], | ||
complete=raw_raid['complete'], | ||
pokemon_id=raw_raid['pokemon_id'], | ||
cp=raw_raid['cp'], | ||
move_1=raw_raid['move_1'], | ||
move_2=raw_raid['move_2'], | ||
) | ||
session.add(obj) | ||
RAID_CACHE.add(raw_raid) | ||
|
||
def add_pokestop(session, raw_pokestop): | ||
pokestop_id = raw_pokestop['external_id'] | ||
|
@@ -789,3 +887,25 @@ def get_all_spawn_coords(session, pokemon_id=None): | |
if conf.REPORT_SINCE: | ||
points = points.filter(Sighting.expire_timestamp > SINCE_TIME) | ||
return points.all() | ||
|
||
def update_raid(session, fort_id, raw): | ||
query = session.execute(''' | ||
UPDATE fort_raids | ||
SET | ||
pokemon_id = '{pokemon_id}', | ||
cp = '{cp}', | ||
move_1 = '{move_1}', | ||
move_2 = '{move_2}' | ||
WHERE | ||
fort_id = {fort_id} | ||
AND raid_spawn_ms = {raid_spawn_ms} | ||
|
||
'''.format( | ||
fort_id=fort_id, | ||
raid_spawn_ms=raw['raid_spawn_ms'], | ||
pokemon_id=raw['pokemon_id'], | ||
cp=raw['cp'], | ||
move_1=raw['move_1'], | ||
move_2=raw['move_2'], | ||
)) | ||
session.commit() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ | |
from cyrandom import choice, randint, uniform | ||
from pogeo import get_distance | ||
|
||
from .db import FORT_CACHE, MYSTERY_CACHE, SIGHTING_CACHE | ||
from .db import FORT_CACHE, RAID_CACHE, MYSTERY_CACHE, SIGHTING_CACHE | ||
from .utils import round_coords, load_pickle, get_device_info, get_start_coords, Units, randomize_point | ||
from .shared import get_logger, LOOP, SessionManager, run_threaded, ACCOUNTS | ||
from . import altitudes, avatar, bounds, db_proc, spawns, sanitized as conf | ||
|
@@ -830,8 +830,29 @@ async def visit_point(self, point, spawn_id, bootstrap, | |
if fort.id not in FORT_CACHE.pokestops: | ||
pokestop = self.normalize_pokestop(fort) | ||
db_proc.add(pokestop) | ||
elif fort not in FORT_CACHE: | ||
db_proc.add(self.normalize_gym(fort)) | ||
else: | ||
if fort not in FORT_CACHE: | ||
db_proc.add(self.normalize_gym(fort)) | ||
if fort.HasField('raid_info'): | ||
fort_raid = {} | ||
fort_raid['external_id'] = fort.id | ||
fort_raid['raid_battle_ms'] = fort.raid_info.raid_battle_ms | ||
fort_raid['raid_spawn_ms'] = fort.raid_info.raid_spawn_ms | ||
fort_raid['raid_end_ms'] = fort.raid_info.raid_end_ms | ||
fort_raid['raid_level'] = fort.raid_info.raid_level | ||
fort_raid['complete'] = fort.raid_info.complete | ||
fort_raid['pokemon_id'] = "" | ||
fort_raid['cp'] = "" | ||
fort_raid['move_1'] = "" | ||
fort_raid['move_2'] = "" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these should be set to |
||
if fort.raid_info.HasField('raid_pokemon'): | ||
fort_raid['pokemon_id'] = fort.raid_info.raid_pokemon.pokemon_id | ||
fort_raid['cp'] = fort.raid_info.raid_pokemon.cp | ||
fort_raid['move_1'] = fort.raid_info.raid_pokemon.move_1 | ||
fort_raid['move_2'] = fort.raid_info.raid_pokemon.move_2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these lines (after There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It already does... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean the code should be moved to the
|
||
|
||
if fort_raid not in RAID_CACHE: | ||
db_proc.add(self.normalize_raid(fort_raid)) | ||
|
||
if more_points: | ||
try: | ||
|
@@ -1266,6 +1287,23 @@ def normalize_gym(raw): | |
'last_modified': raw.last_modified_timestamp_ms // 1000, | ||
} | ||
|
||
@staticmethod | ||
def normalize_raid(raw): | ||
return { | ||
'type': 'raid', | ||
'external_id': raw['external_id'], | ||
'raid_battle_ms': raw['raid_battle_ms'] // 1000, | ||
'raid_spawn_ms': raw['raid_spawn_ms'] // 1000, | ||
'raid_end_ms': raw['raid_end_ms'] // 1000, | ||
'raid_level': raw['raid_level'], | ||
'complete': raw['complete'], | ||
'pokemon_id': raw['pokemon_id'], | ||
'cp': raw['cp'], | ||
'move_1': raw['move_1'], | ||
'move_2': raw['move_2'], | ||
# 'last_modified': raw.last_modified_timestamp_ms // 1000, | ||
} | ||
|
||
@staticmethod | ||
def normalize_pokestop(raw): | ||
return { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,7 @@ | |
from monocle.utils import get_address, dump_pickle | ||
from monocle.worker import Worker | ||
from monocle.overseer import Overseer | ||
from monocle.db import FORT_CACHE | ||
from monocle.db import FORT_CACHE,RAID_CACHE | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. put a space in between :) |
||
from monocle import altitudes, db_proc, spawns | ||
|
||
|
||
|
@@ -151,6 +151,7 @@ def cleanup(overseer, manager): | |
print('Dumping pickles...') | ||
dump_pickle('accounts', ACCOUNTS) | ||
FORT_CACHE.pickle() | ||
RAID_CACHE.pickle() | ||
altitudes.pickle() | ||
if conf.CACHE_CELLS: | ||
dump_pickle('cells', Worker.cells) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are the CP not the same for the same pokemon?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the CP of the boss ;) Not everytime the same imo