def plan_mid_notifications(): time_before_battle_to_notify = [ datetime.timedelta(minutes=2), datetime.timedelta(minutes=1), datetime.timedelta(seconds=30), datetime.timedelta(seconds=20), datetime.timedelta(seconds=15), datetime.timedelta(seconds=10), ] battle_time = count_next_battle_time() for time in time_before_battle_to_notify: job.run_once(message_before_battle, battle_time - time)
def check_mid_feedback_time_access(bot, update): """ Функция проверки возможности писать в чат МИДа (запрещено в промежуток +- 15 минут от битвы) """ remaining_before_battle_time = count_next_battle_time( ) - datetime.datetime.now(tz=moscow_tz).replace(tzinfo=None) battle_interval = datetime.timedelta(hours=8) forbid_interval = datetime.timedelta(minutes=MID_REQUEST_FORBID_MINUTES) if remaining_before_battle_time <= forbid_interval or \ remaining_before_battle_time >= battle_interval - forbid_interval: bot.send_message(chat_id=update.message.chat_id, text="Совет отбыл на войну. Ожидайте его возращения.") return False return True
def send_order(bot, chat_callback_id, divisions, castle_target, defense, tactics, potions, time=None): time_to_battle = count_next_battle_time() - datetime.datetime.now(tz=moscow_tz).replace(tzinfo=None) if time_to_battle >= datetime.timedelta(seconds=55): recashe_order_chats() time_begin = datetime.datetime.now() time_add_str = count_next_battle_time().strftime("%H:%M") if time is None else time.strftime("%H:%M") pot_str = "" for i, p in enumerate(potions): if p: pot_str += potions_to_order[i] response = "{3}⚔:{0}\n{1}{2}\n\n{4}" \ "\n".format(castle_target, "🛡:{}\n".format(castle_target if defense == "Attack!" else defense) if defense is not None else "", "<a href=\"https://t.me/share/url?url={}\">{}</a>".format(tactics, tactics) if tactics != "" else "", "{}\n".format(time_add_str) if time_add_str != "" else time_add_str, pot_str) if '⚔:\uD83D\uDDA4Деф!🛡\n🛡:\uD83D\uDDA4Деф!🛡' in response: response = response.replace("⚔:\uD83D\uDDA4Деф!🛡\n🛡:\uD83D\uDDA4Деф!🛡", "🖤 ФД! 🖤") if datetime.date.today() < datetime.date(year=2021, day=3, month=1): response += "\n\n🎇Happy New Year!🎇\n" buttons = get_order_buttons(castle_target, defense) orders_sent = 0 if divisions == 'ALL': for chat in order_chats: if chat[3] != 'Траст': bot.send_order(order_id=globals.order_id, chat_id=chat[0], response=response, pin_enabled=chat[1], notification=not chat[2], reply_markup=buttons) orders_sent += 1 else: current_divisions = [] for i in range(0, len(divisions)): if divisions[i]: current_divisions.append(division_const[i]) if len(current_divisions) == 1 and current_divisions[0] == 'Луки': # Пин только лукам response = response.replace("⚔", "🏹") buttons.inline_keyboard[0][0].text = buttons.inline_keyboard[0][0].text.replace("⚔", "🏹") for chat in order_chats: if chat[3] in current_divisions: bot.send_order(order_id=globals.order_id, chat_id=chat[0], response=response, pin_enabled=chat[1], notification=not chat[2], reply_markup=buttons) orders_sent += 1 threading.Thread(target=wait_debug, args=(bot, orders_sent)).start() response = "" orders_OK = 0 orders_failed = 0 while orders_OK + orders_failed < orders_sent: current = order_backup_queue.get() if current.order_id == globals.order_id: if current.OK: orders_OK += 1 else: orders_failed += 1 response += current.text else: order_backup_queue.put(current) logging.warning("Incorrect order_id, received {0}, now it is {1}".format(current, globals.order_id)) globals.order_id += 1 time_end = datetime.datetime.now() time_delta = time_end - time_begin stats = "Выполнено в <b>{0}</b>, отправлено в <b>{1}</b> чатов, " \ "ошибка при отправке в <b>{2}</b> чатов, " \ "рассылка заняла <b>{3}</b>\n\n".format(datetime.datetime.now(tz=moscow_tz), orders_OK, orders_failed, time_delta) + response bot.send_message(chat_id = chat_callback_id, text=stats, parse_mode='HTML')
def plan_battle_jobs(): plan_mid_notifications() next_battle_time = moscow_tz.localize( count_next_battle_time()).astimezone(tz=local_tz).replace(tzinfo=None) job.run_once(after_battle, next_battle_time) job.run_once( grassroots_update_players, next_battle_time - datetime.timedelta(hours=1, minutes=41, seconds=30)) job.run_once(grassroots_update_stock, next_battle_time - datetime.timedelta(hours=0, minutes=37, seconds=39), context={"change_send": False}) job.run_once(grassroots_update_stock, next_battle_time - datetime.timedelta(hours=0, minutes=15, seconds=39), context={"change_send": False}) job.run_once(grassroots_update_stock, next_battle_time - datetime.timedelta(hours=0, minutes=7, seconds=39), context={"change_send": False}) job.run_once(grassroots_update_stock, next_battle_time - datetime.timedelta(hours=0, minutes=3, seconds=12), context={"change_send": False}) job.run_once(grassroots_update_stock, next_battle_time + datetime.timedelta(hours=0, minutes=4, seconds=0), context={"change_send": True}) # job.run_once(grassroots_update_stock, next_battle_time + datetime.timedelta(hours=0, minutes=0, seconds=1), # context={"change_send": False}) job.run_once(update_stock_for_fails, next_battle_time + datetime.timedelta(hours=0, minutes=12, seconds=0), context={"change_send": True}) job.run_once(update_stock_for_fails, next_battle_time + datetime.timedelta(hours=0, minutes=20, seconds=0), context={"change_send": True}) job.run_once( autospend_start, next_battle_time - datetime.timedelta(hours=0, minutes=14, seconds=00)) # job.run_once(grassroots_update_stock, 0.1, context={"change_send": True}) # job.run_once(update_stock_for_fails, 5, context={"change_send": True}) time_to_send = next_battle_time - datetime.timedelta(hours=1) now = datetime.datetime.now(tz=moscow_tz).replace(tzinfo=None) if time_to_send > now: job.run_once(send_potion_stats, time_to_send, context=[False]) time_to_send = next_battle_time - datetime.timedelta(minutes=30) if time_to_send > now: job.run_once(send_potion_stats, time_to_send, context=[False]) time_to_send = next_battle_time - datetime.timedelta(minutes=7, seconds=30) if time_to_send > now: job.run_once(send_potion_stats, time_to_send, context=[True]) rangers_notify_start(bot=dispatcher.bot, update=SUPER_ADMIN_ID) plan_clear_alliance_results()