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