Skip to content

Commit 5c75447

Browse files
committed
✨Short term solution for disconnect before Ancient problem
1 parent c3e33f7 commit 5c75447

File tree

5 files changed

+56
-9
lines changed

5 files changed

+56
-9
lines changed

sql/2_dota.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ CREATE TABLE
2121
lobby_type INT NOT NULL,
2222
game_mode INT NOT NULL,
2323
outcome INT DEFAULT (NULL),
24-
live INT DEFAULT (1);
24+
live INT DEFAULT (1),
25+
failed INT DEFAULT (0)
2526
);
2627

2728
CREATE TABLE

src/core/bot.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ class IreBot(commands.AutoBot):
162162
* etc.
163163
164164
Maybe I will change this in future to be less confusing, but I think current situation is fine.
165+
165166
"""
166167

167168
if TYPE_CHECKING:
@@ -550,7 +551,7 @@ def error_ping(self) -> str:
550551
"""Error Role ping used to notify the developer(-s) about some errors."""
551552
return "<@&1337106675433340990>" if self.test else "<@&1116171071528374394>"
552553

553-
# UTILITIES
554+
# UTILITIES AND SHORTCUTS
554555

555556
def is_online(self, user_id: str) -> bool:
556557
"""Whether the user is online.
@@ -559,3 +560,7 @@ def is_online(self, user_id: str) -> bool:
559560
For proper request - we need to use twitchio's `.fetch_streams` method.
560561
"""
561562
return s.online if (s := self.streamers.get(user_id, None)) else False
563+
564+
def get_partial_owner(self) -> twitchio.PartialUser:
565+
"""A shortcut to get a partial user object for the bot's owner."""
566+
return self.create_partialuser(self.owner_id)

src/modules/personal/stable.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class StableCommands(IrePersonalComponent):
9393
"""Stable commands.
9494
9595
Stable in a sense that unlike commands in `temporary.py`
96-
these commands are unlikely to be axed for a long time.
96+
these commands are unlikely to be deleted from my channel for a long time.
9797
9898
Notes
9999
-----

src/modules/personal/timers.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from twitchio.ext import commands
99

10-
from core import IrePersonalComponent
10+
from core import IrePersonalComponent, ireloop
1111
from utils import const
1212

1313
if TYPE_CHECKING:
@@ -65,6 +65,7 @@ async def stream_online_start_the_task(self, online: twitchio.StreamOffline) ->
6565
self.index = 0
6666
self.lines_count = 0
6767
self.bot.add_listener(self.count_messages, event="event_message")
68+
self.boink_announcement.start()
6869

6970
@commands.Component.listener(name="stream_offline")
7071
async def stream_offline_cancel_the_task(self, offline: twitchio.StreamOffline) -> None:
@@ -73,6 +74,7 @@ async def stream_offline_cancel_the_task(self, offline: twitchio.StreamOffline)
7374
return
7475

7576
self.bot.remove_listener(self.count_messages)
77+
self.boink_announcement.cancel()
7678

7779
# @commands.Component.listener(name="message")
7880
async def count_messages(self, message: twitchio.ChatMessage) -> None:
@@ -106,6 +108,15 @@ async def count_messages(self, message: twitchio.ChatMessage) -> None:
106108
self.lines_count = 0
107109
self._most_recent = datetime.datetime.now(datetime.UTC)
108110

111+
@ireloop(hours=5)
112+
async def boink_announcement(self) -> None:
113+
"""It's time to boink."""
114+
if self.boink_announcement.current_loop == 0:
115+
return
116+
117+
owner = self.bot.get_partial_owner()
118+
await owner.send_announcement(message="It's time to Boink", moderator=self.bot.bot_id, color="primary")
119+
109120

110121
async def setup(bot: IreBot) -> None:
111122
"""Load IreBot module. Framework of twitchio."""

src/modules/public/dota_rp_flow/component.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,31 @@ class Dashboard(Activity): ...
100100

101101
@dataclass(slots=True)
102102
class PlayingPartial(Activity):
103+
"""Partial Playing Match.
104+
105+
Partial in a sense that full `PlayingMatch` objects get built from objects of `PlayingPartial` class.
106+
"""
107+
103108
watchable_game_id: str
104109

105110

106111
@dataclass(slots=True)
107112
class SpectatingPartial(Activity):
113+
"""Partial Spectating Match.
114+
115+
Partial in a sense that full `SpectatingMatch` objects get built from objects of `SpectatingPartial` class.
116+
"""
117+
108118
watching_server: str
109119

110120

111121
@dataclass(slots=True)
112122
class UnsupportedPartial(Activity):
123+
"""Partial Unsupported Match.
124+
125+
Partial in a sense that full `UnsupportedMatch` objects get built from objects of `UnsupportedPartial` class.
126+
"""
127+
113128
msg: str
114129

115130

@@ -1130,10 +1145,9 @@ async def process_pending_matches(self) -> None:
11301145
log.debug("Processing pending matches.")
11311146

11321147
query = """
1133-
SELECT m.match_id
1134-
FROM ttv_dota_matches m
1135-
JOIN ttv_dota_match_players p ON m.match_id = p.match_id
1136-
WHERE m.outcome IS NULL AND m.live = $1;
1148+
SELECT match_id, failed
1149+
FROM ttv_dota_matches
1150+
WHERE outcome IS NULL AND live = $1 AND failed < 12;
11371151
"""
11381152
rows = await self.bot.pool.fetch(query, LiveIndicator.Pending)
11391153
if not rows:
@@ -1142,7 +1156,23 @@ async def process_pending_matches(self) -> None:
11421156
return
11431157

11441158
for row in rows:
1145-
minimal = await self.bot.dota.create_partial_match(row["match_id"]).minimal()
1159+
try:
1160+
# If streamer disconnects before ancient falls (i.e., preemptive disconnects or when game is "Safe to leave")
1161+
# Then `.minimal` won't give any results as the game is still live but streamer's RP is different
1162+
# So we need to deal with errors of not getting response from it.
1163+
# This also happens if streamer disconnects-reconnects in the middle of the match.
1164+
minimal = await self.bot.dota.create_partial_match(row["match_id"]).minimal()
1165+
except ValueError:
1166+
# this way any matches that errored out more 12 times gonna be ignored
1167+
# not sure how I feel about such solution;
1168+
query = """
1169+
UPDATE ttv_dota_matches
1170+
SET failed = failed + 1
1171+
WHERE match_id = $1;
1172+
"""
1173+
await self.bot.pool.execute(query, row["match_id"])
1174+
continue
1175+
11461176
query = """
11471177
UPDATE ttv_dota_matches
11481178
SET outcome = $1, live = $3

0 commit comments

Comments
 (0)