|
1 | | -from telegram.ext import CommandHandler |
2 | | -from pycamp_bot.models import Pycampista |
3 | 1 | import random |
| 2 | +from datetime import datetime, timedelta |
| 3 | + |
| 4 | +from telegram.ext import CommandHandler |
| 5 | +from pycamp_bot.models import Pycampista, WizardAtPycamp |
| 6 | +from pycamp_bot.commands.auth import admin_needed |
| 7 | +from pycamp_bot.commands.manage_pycamp import get_active_pycamp |
| 8 | +from pycamp_bot.logger import logger |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +def compute_wizards_slots(pycamp): |
| 13 | + """ |
| 14 | + * Magos trabajan de 9 a 19, sacando almuerzo (13 a 14). |
| 15 | + * Magos trabajan desde el mediodía del primer día, hasta el mediodía del último día. |
| 16 | + Slots son [start; end) |
| 17 | + """ |
| 18 | + wizard_start = pycamp.init |
| 19 | + wizard_end = pycamp.end |
| 20 | + slots = [] |
| 21 | + current_period = wizard_start |
| 22 | + while current_period < wizard_end: |
| 23 | + slot_start = current_period |
| 24 | + slot_end = current_period + timedelta(minutes=pycamp.wizard_slot_duration) |
| 25 | + slots.append( |
| 26 | + (slot_start, slot_end) |
| 27 | + ) |
| 28 | + current_period = slot_end |
| 29 | + |
| 30 | + return slots |
| 31 | + |
| 32 | + |
| 33 | +def define_wizards_schedule(pycamp): |
| 34 | + """ |
| 35 | + Returns a dict whose keys are times and values are wizards (Pycampistas instances). |
4 | 36 |
|
| 37 | + """ |
| 38 | + slots_list = compute_wizards_slots(pycamp) |
| 39 | + wizards_list = pycamp.get_wizards() |
| 40 | + n_wizards = wizards_list.count() |
| 41 | + wizard_per_slot = {} |
| 42 | + idx = 0 |
| 43 | + for slot in slots_list: |
| 44 | + wizard = wizards_list[idx%n_wizards] |
| 45 | + n_iter = 0 # railguard |
| 46 | + while wizard.is_busy(slot): |
| 47 | + logger.info('Mago {} con conflicto en el slot {}. Pruebo otro.'.format(wizard.username, slot)) |
| 48 | + if n_iter == n_wizards: |
| 49 | + logger.warning('Queda el mago {} con conflicto en el slot {}'.format(wizard, slot)) |
| 50 | + break |
| 51 | + idx += 1 |
| 52 | + wizard = wizards_list[idx%n_wizards] |
| 53 | + n_iter += 1 |
| 54 | + wizard_per_slot[slot] = wizard |
| 55 | + idx += 1 |
| 56 | + return wizard_per_slot |
5 | 57 |
|
6 | 58 | async def become_wizard(update, context): |
7 | 59 | current_wizards = Pycampista.select().where(Pycampista.wizard is True) |
|
0 commit comments