88 lines
2.4 KiB
Python
88 lines
2.4 KiB
Python
import peewee
|
|
import opendota
|
|
import datetime
|
|
|
|
import players
|
|
import utils
|
|
|
|
|
|
db = peewee.SqliteDatabase('dota.db')
|
|
hero_client = opendota.HeroesApi()
|
|
player_client = opendota.PlayersApi()
|
|
match_client = opendota.MatchesApi()
|
|
|
|
class BaseModel(peewee.Model):
|
|
class Meta:
|
|
database = db
|
|
|
|
|
|
class Hero(BaseModel):
|
|
hero_id = peewee.IntegerField(primary_key=True)
|
|
name = peewee.CharField()
|
|
localized_name = peewee.CharField()
|
|
primary_attr = peewee.CharField()
|
|
attack_type = peewee.CharField()
|
|
roles = peewee.CharField()
|
|
|
|
@classmethod
|
|
def fetch_heroes(cls):
|
|
heroes = hero_client.get_heroes()
|
|
for hero in heroes:
|
|
cls.get_or_create(
|
|
hero_id=hero.id,
|
|
defaults={
|
|
'name': hero.name,
|
|
'localized_name': hero.localized_name,
|
|
'primary_attr': hero.primary_attr,
|
|
'attack_type': hero.attack_type,
|
|
'roles': ','.join(hero.roles),
|
|
}
|
|
)
|
|
|
|
|
|
class Match(BaseModel):
|
|
match_id = peewee.IntegerField(primary_key=True)
|
|
start_time = peewee.DateTimeField()
|
|
duration = peewee.IntegerField()
|
|
radiant_win = peewee.BooleanField()
|
|
party_size = peewee.IntegerField()
|
|
|
|
def serialize_match(self):
|
|
match = match_client.get_matches_by_match_id(self.match_id)
|
|
match_data = {
|
|
'players': [players.serialize_player(player) for player in match.players],
|
|
'dire_score': match.dire_score,
|
|
'radiant_score': match.radiant_score,
|
|
'start_time': datetime.datetime.fromtimestamp(match.start_time).strftime('%Y-%m-%d %H:%M:%S'),
|
|
'duration': '%d:%02d:%02d' % utils.convert_seconds_to_hms(match.duration),
|
|
'radiant_win': match.radiant_win,
|
|
'party_size': self.party_size,
|
|
}
|
|
return match_data
|
|
|
|
|
|
class Friend(BaseModel):
|
|
steam_id = peewee.IntegerField(primary_key=True)
|
|
name = peewee.CharField()
|
|
|
|
def get_recent_matches(self):
|
|
return player_client.get_players_by_account_id_select_matches(self.steam_id, limit=1)
|
|
|
|
|
|
def get_friends_recent_matches():
|
|
for friend in Friend.select():
|
|
for match in friend.get_recent_matches():
|
|
if not Match.select().where(Match.match_id == match.match_id).exists():
|
|
match_ = Match.create(
|
|
match_id=match.match_id,
|
|
start_time=datetime.datetime.fromtimestamp(match.start_time),
|
|
duration=match.duration,
|
|
radiant_win=match.radiant_win,
|
|
party_size=match.party_size,
|
|
)
|
|
notify_match(match_.serialize_match())
|
|
|
|
|
|
def notify_match(match):
|
|
print(match)
|