From bb4ee378d93f0564617b6144ced35704ee15370a Mon Sep 17 00:00:00 2001 From: Ching L Date: Thu, 11 Sep 2025 15:26:18 +0800 Subject: [PATCH] feat: add method to recalculate streak from recent matches - Add recalculate_streak_from_recent_matches to Friend class - Fetches last 20 matches and recalculates win/loss streaks - Sorts matches chronologically to ensure accurate calculation --- dota.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/dota.py b/dota.py index 9f3a429..ac3e204 100644 --- a/dota.py +++ b/dota.py @@ -220,6 +220,48 @@ class Friend(BaseModel): return data + def recalculate_streak_from_recent_matches(self): + """获取近20场比赛并重新计算连胜连败记录""" + try: + # 获取近20场比赛 + recent_matches = self.get_recent_matches(limit=20) + if not recent_matches: + logger.warning(f"No recent matches found for {self.name}") + return False + + # 按时间从旧到新排序(start_time升序) + recent_matches.sort(key=lambda x: x.start_time) + + # 重置连胜连败计数 + self.win_streak = 0 + self.loss_streak = 0 + + # 从最旧的比赛开始计算连胜连败 + for match in recent_matches: + # 判断是否获胜 + player_won = match.radiant_win == (match.player_slot < 128) + + if player_won: + self.win_streak += 1 + self.loss_streak = 0 + else: + self.loss_streak += 1 + self.win_streak = 0 + + # 更新最后一场比赛的ID,避免重复计算 + if recent_matches: + self.last_match_id = recent_matches[-1].match_id + + # 保存更新后的数据 + self.save() + + logger.info(f"Updated streak for {self.name}: {self.win_streak} wins, {self.loss_streak} losses") + return True + + except Exception as e: + logger.error(f"Failed to recalculate streak for {self.name}: {e}") + return False + def update_streak(self, match_id, win): """更新连胜连败计数""" # 避免重复计算同一场比赛