file and folder organization, removal of monoliths
This commit is contained in:
64
cogs/assignments.py
Normal file
64
cogs/assignments.py
Normal file
@@ -0,0 +1,64 @@
|
||||
import asyncio
|
||||
from discord.ext import commands
|
||||
|
||||
class Assignments(commands.Cog):
|
||||
def __init__(self, bot, enemy_queue, active_assignments, enrolled_attackers, hit_check, reassign_delay):
|
||||
self.bot = bot
|
||||
self.enemy_queue = enemy_queue
|
||||
self.active_assignments = active_assignments
|
||||
self.enrolled_attackers = enrolled_attackers
|
||||
self.HIT_CHECK_INTERVAL = hit_check
|
||||
self.REASSIGN_DELAY = reassign_delay
|
||||
|
||||
# Start background task
|
||||
bot.loop.create_task(self.monitor_assignments_loop())
|
||||
|
||||
def get_next_attacker(self):
|
||||
if not self.enrolled_attackers:
|
||||
return None
|
||||
attacker = self.enrolled_attackers[0]
|
||||
self.enrolled_attackers.append(self.enrolled_attackers.pop(0)) # round-robin
|
||||
return attacker
|
||||
|
||||
async def monitor_assignments_loop(self):
|
||||
await self.bot.wait_until_ready()
|
||||
while not self.bot.is_closed():
|
||||
now = asyncio.get_event_loop().time()
|
||||
reassign_list = []
|
||||
for enemy_id, data in list(self.active_assignments.items()):
|
||||
elapsed = now - data["time_assigned"]
|
||||
if elapsed >= self.HIT_CHECK_INTERVAL and elapsed < self.HIT_CHECK_INTERVAL + 5:
|
||||
attacker_user = self.bot.get_user(data["attacker"])
|
||||
if attacker_user:
|
||||
try:
|
||||
await attacker_user.send(
|
||||
f"Reminder: You were assigned **{data['enemy']['name']}** and they are not down yet!"
|
||||
)
|
||||
except:
|
||||
pass
|
||||
if elapsed >= self.REASSIGN_DELAY:
|
||||
reassign_list.append(enemy_id)
|
||||
|
||||
for enemy_id in reassign_list:
|
||||
info = self.active_assignments.pop(enemy_id)
|
||||
await self.reassign_target(info["enemy"])
|
||||
|
||||
await asyncio.sleep(5)
|
||||
|
||||
async def reassign_target(self, enemy):
|
||||
attacker = self.get_next_attacker()
|
||||
if attacker is None:
|
||||
return
|
||||
|
||||
self.active_assignments[enemy["id"]] = {
|
||||
"enemy": enemy,
|
||||
"attacker": attacker,
|
||||
"time_assigned": asyncio.get_event_loop().time()
|
||||
}
|
||||
|
||||
attacker_user = self.bot.get_user(attacker)
|
||||
if attacker_user:
|
||||
try:
|
||||
await attacker_user.send(f"Target reassigned to you: **{enemy['name']}**!")
|
||||
except:
|
||||
pass
|
||||
Reference in New Issue
Block a user