def _notifyTaskTested(self, handle, task, accepted): funnyInsults = [ "%s failed on system tests for task %s. What a looser.💩", "%s should probably look for a different hobby.💁🏻♂️ The system tests failed for task %s.", "📉 %s failed the system tests for task %s. *So sad! It's true.*", "%s didn't manage to solve task %s. The system tests failed. You can remove this friend using the command `/remove_friend`👋🏻", "Hmmm...🤔 Probably the Codeblocks debugger did not work for %s. The solution for task %s was not good enough. It failed on system tests.", "Div. 5 is near for %s 👋🏻. The system tests failed for task %s.", "%s failed systest for task %s. Did they hardcode the samples?" ] if accepted: msg = "✔️ You got accepted on system tests for task " + task for chatId in db.getChatIds( handle): # only to user with this handle Chat.getChat(chatId).sendMessage(msg) else: insult = funnyInsults[random.randint( 0, len(funnyInsults) - 1)] % (util.formatHandle(handle), task) neutralMsg = "%s failed on system tests for task %s." % ( util.formatHandle(handle), task) for chatId in db.getWhoseFriendsSystemTestFail( handle): # to all users with this friend chat = Chat.getChat(chatId) if chat.polite: chat.sendMessage(neutralMsg) else: chat.sendMessage(insult)
def _notifyTaskSolved(self, handle, task, rejectedAttemptCount, time, official): if official: msg = "💡* [" + util.formatSeconds(time) + "]* " else: msg = "💡 *[UPSOLVING]* " msg += util.formatHandle(handle) + " has solved task " + task if rejectedAttemptCount > 0: msg += " *after " + str( rejectedAttemptCount) + " wrong submissions*" usersToNotify = db.getWhoseFriendsContestSolved( handle) if official else db.getWhoseFriendsUpsolving(handle) for chatId in usersToNotify: Chat.getChat(chatId).sendNotification(msg)
def _sendAllSummary(self, contest): # cache rating for all users chats = [Chat.getChat(c) for c in db.getAllChatPartners()] # The getUserInfo command returns False if there is a unknown user in the list # the user is then removed by the CF error handling routine. A retry is neccessary though. retries = 20 while retries > 0: handles = [c.handle for c in chats if c.handle] infos = cf.getUserInfos(handles) if infos: for info in infos: self.userRatings[info['handle']] = info.get('rating', -1) break retries -= 1 time.sleep(5) logger.debug( f"sending summary for contest {contest['id']}. Cached {len(self.userRatings)} ratings in {20-retries+1} try." ) for chat in chats: msg = self._getContestAnalysis(contest, chat) if len(msg) > 0: # only send if analysis is not empty msg = contest['name'] + " has finished:\n" + msg chat.sendMessage(msg) standings.sendContestStandings(chat, contest['id'], sendIfEmpty=False)
def deleteFriend(handle): logger.debug("deleting friends with handle " + handle) query = "DELETE FROM friends WHERE friend = %s" insertDB(query, (handle, )) query = "SELECT chatId FROM tokens WHERE handle = %s" chatIds = [r[0] for r in queryDB(query, (handle, ))] logger.debug(f"deleting chat handle {handle} for chats {chatIds}") for chatId in chatIds: Chat.getChat(chatId).handle = None # write through to DB
def _notifyAllUpcoming(self, contest, shouldNotifyFun): for chatId in db.getAllChatPartners(): chat = Chat.getChat(chatId) if self._reminderSent[chat.chatId][contest['id']]: msgId = self._reminderSent[chat.chatId][contest['id']] chat.deleteMessage(msgId) self._updateReminderSent(chat.chatId, contest['id'], None) if shouldNotifyFun(chat): description = upcoming.getDescription(contest, chat) callback = lambda msgId, chatId=chatId, contestId=contest['id'] : self._updateReminderSent(chatId, contestId, msgId) chat.sendMessage(description, callback=callback)
def handleCallbackQuery(callback): chat = ChatFunctions.getChat(str(callback['message']['chat']['id'])) data = callback['data'] if not ":" in data: logger.critical("Invalid callback data: " + data) return pref, suff = data.split(":", 1) funs = { "settings": handleSettingsCallback, "general": general_settings.handleSetupCallback, "behavior": behavior_settings.handleChatCallback, "friend_notf": notify_settings.handleChatCallback, "width": widthSelector.handleWidthChange, } if pref not in funs: logger.critical("Invalid callback prefix: " + pref + ", data: " + suff) else: retMsg = funs[pref](chat, suff, callback) tg.requestSpooler.put( lambda: tg.sendAnswerCallback(chat.chatId, callback['id'], retMsg), priority=0)
def startTelegramBot(): initContestServices() tg.TelegramUpdateService().start() while True: msg = input() handleMessage(Chat.getChat('0'), msg)
def startTestingMode(): tg.testFlag = True initContestServices() while True: msg = input() handleMessage(Chat.getChat('0'), msg)
def _doTask(self): logger.debug('starting to update all friends') for chatId in db.getAllChatPartners(): updateFriends(Chat.getChat(chatId)) logger.debug('updating all friends finished')
def _updateStandings(self, contest, chatIds: List[str]): for chatId in chatIds: chat = Chat.getChat(chatId) standings.updateStandingsForChat(contest, chat)