feat(dota.py, matches.py): 增加 models,增加获取朋友最近比赛的逻辑
增加 models,增加获取朋友最近比赛的逻辑 Signed-off-by: Ching <loooching@gmail.com>
This commit is contained in:
parent
e109649c95
commit
4b95403735
87
dota.py
Normal file
87
dota.py
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
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)
|
||||||
15
matches.py
15
matches.py
@ -1,18 +1,5 @@
|
|||||||
import opendota
|
import opendota
|
||||||
import datetime
|
import datetime
|
||||||
import players
|
import players
|
||||||
|
import dota
|
||||||
import utils
|
import utils
|
||||||
|
|
||||||
match_client = opendota.MatchesApi()
|
|
||||||
|
|
||||||
def serialize_match(match_id):
|
|
||||||
match = match_client.get_matches_by_match_id(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,
|
|
||||||
}
|
|
||||||
return match_data
|
|
||||||
|
|||||||
@ -1,6 +1,10 @@
|
|||||||
|
import dota
|
||||||
|
|
||||||
def serialize_player(player):
|
def serialize_player(player):
|
||||||
|
friend = dota.Friend.get_or_none(steam_id=player.account_id)
|
||||||
player_data = {
|
player_data = {
|
||||||
'personaname': player.personaname,
|
'personaname': player.personaname,
|
||||||
|
'nickname': friend.name if friend else None,
|
||||||
'kills': player.kills,
|
'kills': player.kills,
|
||||||
'deaths': player.deaths,
|
'deaths': player.deaths,
|
||||||
'assists': player.assists,
|
'assists': player.assists,
|
||||||
@ -11,5 +15,6 @@ def serialize_player(player):
|
|||||||
'win': player.win,
|
'win': player.win,
|
||||||
'level': player.level,
|
'level': player.level,
|
||||||
'is_radiant': player.is_radiant,
|
'is_radiant': player.is_radiant,
|
||||||
|
'hero_id': dota.Hero.get(hero_id=player.hero_id).localized_name,
|
||||||
}
|
}
|
||||||
return player_data
|
return player_data
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user