Skip to content
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

Open
wants to merge 56 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 45 commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
7792ae7
WIP
Noctem Apr 28, 2017
5321881
WIP2
Noctem Apr 28, 2017
b7d0ad1
WIP3
Noctem Apr 30, 2017
d2441b7
Use pogeo's AltitudeCache
Noctem May 2, 2017
ec1cc6f
Update solve_captchas
Noctem May 2, 2017
9861703
DB improvements
Noctem May 2, 2017
da68e70
Small Travis improvement
Noctem May 3, 2017
013d745
WIP4
Noctem May 10, 2017
b0cd60f
Update JS
Noctem May 10, 2017
b06b4fc
Update jquery to 3.2.1
Noctem May 11, 2017
88e5f2d
lint and clang-format main.js
Noctem May 11, 2017
e8d4f31
Update displaying scan area for new shape types
Noctem May 11, 2017
90ad98b
Speed up PokéStop responses
Noctem May 11, 2017
7784e98
Use AioSightingCache in web_sanic
Noctem May 12, 2017
6ca81f8
Update web.py for pogeo changes
Noctem May 13, 2017
9b72cb7
Skip tasks that are now done server-side
Noctem May 13, 2017
9075028
Add favicon and tidy up HTML
Noctem May 13, 2017
5ed1dcf
Merge branch 'develop' into WIP
Noctem May 17, 2017
8ff947c
Change requirements to use pogeo develop branch
Noctem May 17, 2017
98b9cab
Merge branch 'develop' into WIP
Noctem May 19, 2017
90441a1
Merge branch 'develop' into WIP
Noctem May 19, 2017
d291663
Use AioSpawnCache
Noctem May 19, 2017
e4baa9c
Use pogeo shapes instead of shapely for landmarks
Noctem May 23, 2017
b810e2b
Add a script for converting landmarks config
Noctem May 28, 2017
12d1a85
Bug fixes
Noctem May 30, 2017
0ea754e
Merge branch 'develop' into WIP
Noctem May 30, 2017
aa6770d
Merge remote-tracking branch 'upstream/develop' into develop
Jun 11, 2017
b2d9ecd
c
Jun 11, 2017
ff23ca3
Merge master
Jun 12, 2017
f687ebc
6304
Jun 12, 2017
f8e8190
adding cp
Jun 12, 2017
993ca61
adding cp
Jun 12, 2017
a1785e2
Correction CP
Jun 12, 2017
eb4c1e8
CP
Jun 12, 2017
af9171a
Merge remote-tracking branch 'upstream/develop' into develop
Jun 21, 2017
96d84c0
Adding lure on demand
Jun 21, 2017
4ffff07
LURE_ON_DEMAND default false
Jun 22, 2017
741363a
Define LURE_ON_DEMAND
Jun 22, 2017
8a2c5e1
Merge remote-tracking branch 'upstream/develop' into develop
Jun 25, 2017
a9f28f4
Adding raid infos to db and cache
Jun 25, 2017
147a86d
Remove personal field
Jun 25, 2017
9f7bfa2
Resetting error codes
sebast1219 Jun 26, 2017
1793fdb
Removing cp from this branch
sebast1219 Jun 26, 2017
599e32e
Reset branch to raid only
sebast1219 Jun 26, 2017
441a9bc
Clean branch
sebast1219 Jun 26, 2017
74c8967
Default values for raid to None
sebast1219 Jun 26, 2017
0ce0822
Correcting cp type
sebast1219 Jun 26, 2017
c843568
Adding seed and correcting cache
sebast1219 Jun 27, 2017
1aeba9a
Correcting update when pokemon spawns in raid
sebast1219 Jun 27, 2017
3d43101
raid_seed as varchar
sebast1219 Jun 27, 2017
77abfa5
Removed notifDiscord on update
sebast1219 Jun 27, 2017
1df901a
Changed complete to boolean
Jun 29, 2017
f166a67
Changing complete type
Jun 29, 2017
ead87e8
Testing boolean
Jun 29, 2017
5aaaba1
Correcting raid_seed update
Jun 29, 2017
b5f4d8e
Str of raid_seed
Jun 29, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions monocle/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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):
Expand All @@ -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)
Copy link
Contributor

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?

Copy link
Author

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

move_1 = Column(SmallInteger)
move_2 = Column(SmallInteger)
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link

@thordurk91 thordurk91 Jun 26, 2017

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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

Copy link
Author

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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'

Expand Down Expand Up @@ -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']
Expand Down Expand Up @@ -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()
2 changes: 2 additions & 0 deletions monocle/db_proc.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def run(self):
self.count += 1
elif item_type == 'fort':
db.add_fort_sighting(session, item)
elif item_type == 'raid':
db.add_raid_sighting(session, item)
elif item_type == 'pokestop':
db.add_pokestop(session, item)
elif item_type == 'target':
Expand Down
44 changes: 41 additions & 3 deletions monocle/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'] = ""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these should be set to 0 as they are being saved as integer

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these lines (after if fort.HasField('raid_info'):) should be part of normalize_raid

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It already does...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean the code should be moved to the normalize_raid method
only leaving {} here

if fort.HasField('raid_info') and fort not in RAID_CACHE:
    db_proc.add(self.normalize_raid(fort_raid))


if fort_raid not in RAID_CACHE:
db_proc.add(self.normalize_raid(fort_raid))

if more_points:
try:
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

put a space in between :)

from monocle import altitudes, db_proc, spawns


Expand Down Expand Up @@ -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)
Expand Down
Empty file modified scripts/pickle_landmarks.example.py
100755 → 100644
Empty file.