diff --git a/main.py b/main.py index 6d2b1f5..65dc95f 100644 --- a/main.py +++ b/main.py @@ -55,13 +55,15 @@ def main ( snack_time = SnackTime (game) CurrentTime (game, DEERJIKA_FONT) + broadcast: Broadcast | None = None try: broadcast = Broadcast (os.environ['BROADCAST_CODE']) - except Exception: - pass + except KeyError: + broadcast = None waiting_balloon = (False, '', '') last_flags_poll: float = 0 + traced_af_ids: list[int] = [] while True: now_m = time.monotonic () @@ -76,15 +78,19 @@ def main ( deerjika.talk (waiting_balloon[1], waiting_balloon[2]) waiting_balloon = (False, '', '') - if now_m - last_flags_poll >= 10: + if now_m - last_flags_poll >= 1: last_flags_poll = now_m + log (f"balloon: { balloon.enabled }, snack: { snack_time.enabled }") try: DB.begin_transaction () answer_flags = (AnsweredFlag.where ('platform', Platform.YOUTUBE.value) .where ('answered', False) + .where_not_in ('id', traced_af_ids) .get ()) + log (f"pending: { len (answer_flags) }") if answer_flags: answer_flag = random.choice (answer_flags) + log (f"flag_id: { answer_flag.id }, answer_id: { answer_flag.answer_id }") answer = Answer.find (answer_flag.answer_id) match QueryType (answer.query_rel.query_type): case QueryType.YOUTUBE_COMMENT: @@ -98,10 +104,19 @@ def main ( waiting_balloon = (True, query.content, answer.content) answer_flag.answered = True answer_flag.save () + case _: + traced_af_ids.append (answer_flag.id) DB.commit () - add_query (broadcast) except Exception as ex: DB.rollback () + log ('EXCEPTION in poll loop') + print (ex) + + try: + if broadcast is not None: + add_query (broadcast) + except Exception as ex: + log ('EXCEPTION in adding a query') print (ex) game.redraw () @@ -582,8 +597,9 @@ class Balloon (GameObject): query = self.query if CommonModule.len_by_full (query) > 21: query = CommonModule.mid_by_full (query, 0, 19.5) + '...' - answer = Surface ((375, ((CommonModule.len_by_full (self.answer) - 1) // 16 + 1) * 23.4375), - pygame.SRCALPHA) + answer = Surface ( + (375, int (((CommonModule.len_by_full (self.answer) - 1) // 16 + 1) * 23.4375)), + pygame.SRCALPHA) for i in range (int (CommonModule.len_by_full (self.answer) - 1) // 16 + 1): answer.blit (DEERJIKA_FONT.render ( CommonModule.mid_by_full (self.answer, 16 * i, 16), True, (192, 0, 0)), @@ -991,5 +1007,12 @@ def add_query ( DB.commit () +def log ( + msg: str, +) -> None: + print (f"[{ datetime.now ().isoformat (sep = ' ', timespec = 'seconds') }] { msg }", + flush = True) + + if __name__ == '__main__': main ()