Compare commits
2 Commits
c85eeb9d74
...
bb4ee378d9
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bb4ee378d9 | ||
|
|
7498f413bf |
@ -44,6 +44,9 @@ async def send_message(channel):
|
|||||||
logger.error(f"Error in send_message task: {e}")
|
logger.error(f"Error in send_message task: {e}")
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# 用于标记是否是第一场比赛
|
||||||
|
first_match = True
|
||||||
|
|
||||||
for match_ in matches:
|
for match_ in matches:
|
||||||
data = await dota.serialize_match_for_discord(match_)
|
data = await dota.serialize_match_for_discord(match_)
|
||||||
logger.info(f"sending match {match_['match_id']}, {data}")
|
logger.info(f"sending match {match_['match_id']}, {data}")
|
||||||
@ -58,13 +61,12 @@ async def send_message(channel):
|
|||||||
if 'value' in field and field['value']:
|
if 'value' in field and field['value']:
|
||||||
friends_info = f"**{field['value']}** 的比赛:\n\n"
|
friends_info = f"**{field['value']}** 的比赛:\n\n"
|
||||||
|
|
||||||
# 将朋友信息和连胜连败消息放在内容开头
|
# 将朋友信息放在内容开头,连胜连败消息只在第一场比赛时添加
|
||||||
content = data['content']
|
content = data['content']
|
||||||
if streak_notifications:
|
if first_match and streak_notifications:
|
||||||
streak_msg = '\n'.join(streak_notifications) + '\n\n'
|
streak_msg = '\n'.join(streak_notifications) + '\n\n'
|
||||||
content = friends_info + streak_msg + content
|
content = friends_info + streak_msg + content
|
||||||
# 发送后清空通知列表,避免重复发送
|
first_match = False # 标记已经处理过第一场比赛
|
||||||
streak_notifications = []
|
|
||||||
else:
|
else:
|
||||||
content = friends_info + content
|
content = friends_info + content
|
||||||
|
|
||||||
|
|||||||
45
dota.py
45
dota.py
@ -14,6 +14,9 @@ player_client = opendota.PlayersApi()
|
|||||||
match_client = opendota.MatchesApi()
|
match_client = opendota.MatchesApi()
|
||||||
image_generator = ImageGenerator()
|
image_generator = ImageGenerator()
|
||||||
|
|
||||||
|
# 初始化全局变量,用于存储连胜连败更新
|
||||||
|
streak_updates = []
|
||||||
|
|
||||||
|
|
||||||
class BaseModel(peewee.Model):
|
class BaseModel(peewee.Model):
|
||||||
class Meta:
|
class Meta:
|
||||||
@ -217,6 +220,48 @@ class Friend(BaseModel):
|
|||||||
|
|
||||||
return data
|
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):
|
def update_streak(self, match_id, win):
|
||||||
"""更新连胜连败计数"""
|
"""更新连胜连败计数"""
|
||||||
# 避免重复计算同一场比赛
|
# 避免重复计算同一场比赛
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user