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, # isoformat utc+8 'start_time': datetime.datetime.fromtimestamp(match_.start_time).strftime('%Y-%m-%dT%H:%M:%S.000+08:00'), 'duration': '%d:%02d:%02d' % utils.convert_seconds_to_hms(match_.duration), 'radiant_win': match_.radiant_win, 'party_size': self.party_size, 'match_id': self.match_id, } 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(): 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_obj = 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, ) matches.append(match_obj.serialize_match()) return matches def serialize_match_for_discord(match_): # { # "content": "3黑(我, 受风,小金 )00:34:23 赢", # "tts": false, # "embeds": [ # { # "id": 652627557, # "title": "天辉 - 32", # "description": "我(LV23 大鱼人): **2 杀 5 死 3 助** | 12345 经济 | 13442 伤害\n我(LV23 大鱼人): 2 杀 5 死 3 助 | 12345 经济 | 13442 伤害\n我(LV23 大鱼人): 2 杀 5 死 3 助 | 12345 经济 | 13442 伤害\n我(LV23 大鱼人): 2 杀 5 死 3 助 | 12345 经济 | 13442 伤害\n我(LV23 大鱼人): 2 杀 5 死 3 助 | 12345 经济 | 13442 伤害", # "color": 6732650, # "fields": [ # { # "id": 878517961, # "name": "夜魇 - 23", # "value": "我(LV23 大鱼人): 2 杀 5 死 3 助 | 12345 经济 | 13442 伤害\n我(LV23 大鱼人): 2 杀 5 死 3 助 | 12345 经济 | 13442 伤害\n我(LV23 大鱼人): 2 杀 5 死 3 助 | 12345 经济 | 13442 伤害\n我(LV23 大鱼人): 2 杀 5 死 3 助 | 12345 经济 | 13442 伤害\n我(LV23 大鱼人): 2 杀 5 死 3 助 | 12345 经济 | 13442 伤害", # "inline": false # } # ], # "author": { # "name": "opendota", # "url": "https://www.opendota.com/matches/7335993790" # }, # "timestamp": "2022-01-01T13:22:00.000Z" # } # ], # "components": [], # "actions": {} # } party = [player['nickname'] for player in match_['players'] if player['nickname']] is_radiant = False for player in match_['players']: if player['nickname']: is_radiant = player['is_radiant'] break win = is_radiant == match_['radiant_win'] if match_['party_size'] > 1: content = f"{match_['party_size']}黑({','.join(party)}){match_['duration']} {'赢' if win else '输'}" radiant = [] dire = [] for player in match_['players']: desc = f"{player['nickname'] or player['personaname']}(LV{player['level']} {player['hero']}): {player['kills']} 杀 {player['deaths']} 死 {player['assists']} 助 | {player['total_gold']} 经济 | {player['hero_damage']} 伤害" if player['is_radiant']: radiant.append(desc) else: dire.append(desc) color = 6732650 if win else 16724787 # 66bb6a or FF3333 data = { "content": content, "embeds": [ { "description": '\n'.join(radiant), "color": color, "fields": [ { "name": "夜魇 - %s" % match_['dire_score'], "value": '\n'.join(dire), "inline": False } ], "title": "天辉 - %s" % match_['radiant_score'], "author": { "name": "opendota", "url": "https://www.opendota.com/matches/%s" % match_['match_id'] }, "timestamp": match_['start_time'] } ], } return data