feat(players.py, matches.py, utils.py): 补充比赛序列化方法,增加 utils

补充比赛序列化方法,增加 utils

Signed-off-by: Ching <loooching@gmail.com>
This commit is contained in:
Ching 2023-09-15 14:58:14 +08:00
parent 4b31d228c6
commit e109649c95
3 changed files with 28 additions and 14 deletions

View File

@ -1,23 +1,18 @@
import opendota
import datetime
import players
import utils
match_client = opendota.MatchesApi()
def serialize_player(player):
player_data = {
'personaname': player.personaname,
'kills': player.kills,
'deaths': player.deaths,
'assists': player.assists,
'total_gold': player.total_gold,
'last_hits': player.last_hits,
'denies': player.denies,
}
return player_data
def serialize_match(match_id):
match = match_client.get_matches_by_match_id(match_id)
match_data = {
'players': [serialize_player(player) for player in match.players],
'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

15
players.py Normal file
View File

@ -0,0 +1,15 @@
def serialize_player(player):
player_data = {
'personaname': player.personaname,
'kills': player.kills,
'deaths': player.deaths,
'assists': player.assists,
'total_gold': player.total_gold,
'last_hits': player.last_hits,
'denies': player.denies,
'party_id': player.party_id,
'win': player.win,
'level': player.level,
'is_radiant': player.is_radiant,
}
return player_data

4
utils.py Normal file
View File

@ -0,0 +1,4 @@
def convert_seconds_to_hms(total_seconds):
hours, remainder = divmod(total_seconds, 3600)
minutes, seconds = divmod(remainder, 60)
return hours, minutes, seconds