def compose_timers_message(self, merbs, trackers): embed_timers = discord.Embed(title=self.title, description=self.description, color=0x444444) merbs.order('eta') counter_all = counter_in_window = 0 for merb in merbs.merbs: if merb.has_eta() and merb.is_target() and counter_all < 24: counter_all = counter_all + 1 field_content = "" name_content = merb.name if merb.pop > merb.tod and timeh.now() < merb.window['start']: if not timeh.halfway_to_start_window(merb): name_content = f"{self.emojii_pop} %s" % name_content field_content += " popped %s ago\n" % timeh.countdown(merb.pop, timeh.now()) else: field_content += "window roughly opens in %s\n" % timeh.countdown(timeh.now(), merb.eta) else: if merb.is_in_window(): counter_in_window = counter_in_window + 1 name_content = f"{self.emojii_open_window} %s" % name_content field_content += " *__in window__* for the next " else: field_content += "window opens in " field_content += timeh.countdown(timeh.now(), merb.eta) + "\n" if merb.plus_minus: active_trackers = merb.get_trackers() if active_trackers: field_content += "Active trackers: " for tracker in active_trackers: try: tracker_name = config.authenticator.users[tracker].name mode = trackers.get_tracker(tracker)['mode'] field_content += tracker_name if mode: field_content += " (%s)" % mode field_content += " - " except: pass field_content = field_content[:-3] field_content += "\n" else: field_content += "" field_content += "-" embed_timers.add_field(name=name_content, value=field_content, inline=False) footer = "%d total, %d in window.\nLast update %s UTC" % (counter_all, counter_in_window, timeh.now().strftime(config.DATE_FORMAT_PRINT)) embed_timers.set_footer(text=footer) return embed_timers
def print_dkp_tracking_msg(who, what, mode, start_time): if start_time > timeh.now(): total_duration = "" else: total_duration = "(Total Time: %s)" % timeh.countdown(start_time, timeh.now()) output_msg = f"{who} tracked {what} " if mode: output_msg += f"[{mode}] " output_msg += f"= {total_duration}" return output_msg
def print_all(self, limit_hours=None): if not limit_hours: limit_hours = self.max_respawn_time now = timeh.now() self.order('eta') output = list() for merb in self.merbs: date_limit = now + datetime.timedelta(hours=limit_hours) date_diff = date_limit - merb.eta hour_diff = date_diff.total_seconds() / 3600 if timeh.now() < merb.eta and hour_diff >= 0: output.append(merb.print_short_info(v_single=False)) return output
def time_remaining(name, eta, plus_minus, window, spawns, accuracy, target): now = timeh.now() postfix = "" prefix = "" output = "[" + name + "] " approx = " " if accuracy <= 0 or spawns > 6: approx = "~ " if accuracy <= -1 or spawns >= 10: approx = "~~ " if not plus_minus: if now > eta: output += "ToD too old. Please update it if you have a chance! " else: output += "%swill spawn in %s" % (approx, timeh.countdown( now, eta)) else: if now > window['end']: output += "window is close. Please update ToD if u have a chance! " elif now < window['start']: output += "%swindow will open in %s " % (approx, timeh.countdown(now, eta)) elif window['start'] <= now <= window['end']: prefix = "" postfix = "## " output += "%sin window until %s " % (approx, timeh.countdown(now, eta)) if target: postfix += ".target" return prefix + output + postfix + "\n"
def get_duration(self, user_id): if user_id in self.users: return timeh.countdown( self.users[user_id][self.get_tracked_merbs(user_id)], timeh.now()) else: return None
def print_all_targets(self): self.order('eta') output = list() for merb in self.merbs: if merb.target and timeh.now() < merb.eta: output.append(merb.print_short_info(v_target_tag=True)) return output
def print_stop_tracking_msg(who, what, mode, start_time): output_msg = '' if start_time > timeh.now(): total_duration = "" else: total_duration = "(Total Time: %s)" % timeh.countdown(start_time, timeh.now()) if who == 'You': output_msg += f"You stop tracking {what} " else: output_msg += f"{who} stops tracking {what} " if mode: output_msg += f"[{mode}] " output_msg += f"= {total_duration}" return output_msg
def print_all_by_tag(self, tag): self.order('eta') output = list() for merb in self.merbs: if merb.check_tag(tag) and timeh.now() < merb.eta: output.append(merb.print_short_info()) return output
def is_in_window(self): now = timeh.now() if (self.window['start'] < now < self.window['end']) and self.plus_minus: return True else: return False
def assemble_start_tracking_messages(self, what, tracked_merbs, mode=''): messages = [] output_private_content = '' if mode: mode = f"[{mode}]" for merb in tracked_merbs: if merb.is_in_window(): output_private_content += f"You start tracking {merb.name} {mode}\n" else: time_to_start = timeh.countdown(timeh.now(), merb.window['start']) output_private_content += f"You will start tracking {merb.name} in {time_to_start} {mode}\n" output_private_content += "\nRemember to {!track stop} when leaving or {!pop merb} at spawn time.\n" \ "If you want to Batphone please {!pop merb_name BP}" messages.append({ 'destination': self.input_author, 'content': output_private_content, 'decoration': 'CSS' }) messages.append({ 'destination': self.d_client.get_channel(config.BROADCAST_CHANNEL), 'content': f"{self.input_author.name} starts tracking {what.capitalize()} {mode}", 'decoration': 'YELLOW' }) return messages
def track_msg(author, merb, track_mode, time, time_future): if not time_future: output = "%s starts tracking [%s] %s" % (author, merb.name, track_mode) else: time_remaining = timeh.countdown(timeh.now(), time) output = "%s will start tracking [%s] in %s %s" % (author, merb.name, time_remaining, track_mode) return output
def print_all_in_window(self): self.order('eta') output = list() for merb in self.merbs: if merb.window['start'] <= timeh.now() <= merb.window['end']: output.append(merb.print_short_info(v_single=False)) return output
def get_all(self, timezone, mode="countdown", limit_hours=None): if not limit_hours: limit_hours = self.max_respawn_time now = timeh.now() self.order('eta') output = list() for merb in self.merbs: date_limit = now + datetime.timedelta(hours=limit_hours) date_diff = date_limit - merb.eta hour_diff = date_diff.total_seconds() / 3600 if timeh.now() < merb.eta and hour_diff >= 0: # Show online merb eta in the future if mode == "countdown": output.append(merb.print_short_info()) else: output.append(merb.print_long_info(timezone)) return output
def get_self_track_info(user_id, trackers): tracker = trackers.get_tracker(user_id) if not tracker: return "You are not tracking :(" output = 'You are tracking: ' for merb in tracker['merbs']: output += "[%s] " % merb.name output += "since %s" % timeh.countdown(tracker['date'], timeh.now()) return output
def cmd_earthquake(self): output_messages = list() eq_content = "%s BROADCASTS, 'Minions gather, their forms appearing" \ " as time and space coalesce.'" % self.input_author.name self.lp.snippet = "Earthquake" # Assume now for when no time if not self.lp.my_date: self.lp.my_date = timeh.now() # BATPHONE IT if config.BATPHONE: output_messages.append({ 'destination': self.d_client.get_channel(config.BROADCAST_BP_CHANNEL), 'content': f"{config.TAG_BATPHONE} EARTHQUAKE", 'decoration': None }) output_messages.append({ 'destination': self.d_client.get_channel(config.BROADCAST_CHANNEL), 'content': eq_content, 'decoration': 'BLOCK' }) output_messages.append({ 'destination': self.input_channel, 'content': "Earthquake!!!", 'decoration': 'BLOCK' }) for merb in self.merbs.merbs: merb.update_pop(self.lp.my_date, str(self.input_author), self.lp.snippet) for tracker in self.trackers.stop_all_trackers(): tracker_id = next(iter(tracker)) output_messages += self.assemble_stop_tracking_messages( tracker_id, tracker[tracker_id]['info']) # save merbs timers self.merbs.save_timers() # save targets self.merbs.save_targets() # Save trackers self.trackers.save() return output_messages
def get_all_missing(self, timezone, tag): self.order('window_end') output = list() now = timeh.now() for merb in self.merbs: if tag: if merb.check_tag(tag) and now > merb.eta: output.append(merb.print_last_update(timezone)) else: continue elif now > merb.eta: output.append(merb.print_last_update(timezone)) return output
def time_remaining(merb, v_target_tag=True): now = timeh.now() postfix = "" prefix = "" output = "- [" + merb.name + "] " approx = "" if merb.accuracy <= 0 or merb.spawns == 1: approx = "{roughly} " if merb.accuracy <= -1 or merb.spawns > 1: approx = "{very roughly} " if not merb.plus_minus: if now > merb.eta: output += "ToD is too old. " else: output += "will %sspawn in %s " % (approx, timeh.countdown(now, merb.eta)) else: if merb.is_alive(): output += "popped %s ago " % timeh.countdown(merb.pop, timeh.now()) elif now > merb.window['end']: output += "window is closed " elif now < merb.window['start']: output += "window will %sopen in %s " % (approx, timeh.countdown(now, merb.eta)) elif merb.window['start'] <= now <= merb.window['end']: prefix = "" postfix = "## " output += "is %sin .window until %s " % (approx, timeh.countdown(now, merb.eta)) if merb.spawns >= 1: output += "(%s respawn since last update) " % merb.spawns if merb.target == "auto" and v_target_tag: postfix += ".sticky_target" if merb.target == "manual" and v_target_tag: postfix += ".new_target" return prefix + output + postfix + "\n"
async def minute_digest(): tic = 60 while True: await asyncio.sleep(tic) now = timeh.now() for merb in merbs.merbs: # update merb eta merb.eta = merb.get_new_eta() minutes_diff = (merb.eta - now).total_seconds() // 60.0 # broadcast the alarm 30 minutes before a target spawns if merb.target and minutes_diff == 30: print_info = merb.print_short_info(v_trackers=True) + "\n" message = config.TAG_ALERT + "\n" + messagecomposer.prettify( print_info, "RED") try: await client.get_channel(config.BROADCAST_CHANNEL ).send(message) logger_sirken.info(f"Broadcast alarm sent for {merb.name}") except Exception as e: logger_sirken.error( f"Broadcast alarm not sent! Error: {e}") # send a pm to watchers for user in watch.users: destination = discord.utils.get(client.get_all_members(), id=user) if watch.check(user, merb.name, minutes_diff) and not merb.is_in_window(): try: await destination.send( messagecomposer.prettify(merb.print_short_info(), "CSS")) logging.info( "PM Alarm sent to %s: %s | ETA: %s | DIFF MINUTES: %s" % (user, merb.name, merb.eta, minutes_diff)) except Exception as e: logger_sirken.error(f"PM Alarm not sent! Error: {e}") # UPDATE EMBED TIMERS try: await embed_timers.update_message(client, merbs, trackers) # logger_sirken.info("Embed timers updated.") except Exception as e: logger_sirken.error(f"Embed timers not updated! Error: {e}")
async def minute_digest(): tic = 60 while True: await asyncio.sleep(tic) now = timeh.now() for merb in merbs.merbs: # update merb eta merb.eta = merb.get_eta() minutes_diff = (merb.eta - now).total_seconds() // 60.0 for user in watch.users: destination = discord.utils.get(client.get_all_members(), id=user) if watch.check(user, merb.name, minutes_diff) and not merb.in_window(): await destination.send( messagecomposer.prettify(merb.print_short_info(), "CSS")[0]) logging.debug( "ALARM TO %s: %s | ETA: %s | DIFF MINUTES: %s" % (user, merb.name, merb.eta, minutes_diff))
def make_text_file(date, start, stop): print date, start, stop try: sdchannels = ['NHL Centre Ice 10', 'NBA League Pass 10'] conn = sqlite3.connect('uctvDb') c = conn.cursor() f = open(dir + r"\uctvSportsSchedule.txt", 'wb') query = c.execute( '''SELECT sport,startTime,event,channelName,HDNo,SDNo from liveSports where date =? and startTime between ? and ? order by sport,startTime,event''', (date, start, stop)) sportslist = [ dict(sport=row[0], startTime=row[1], event=row[2], channelName=row[3], HDNo=row[4], SDNo=row[5]) for row in query.fetchall() ] #convert time from 24 hour to AM/PM for i in sportslist: i['startTime'] = th.convert_to_am_pm(i['startTime']) #Remove accents for i in sportslist: i['event'] = remove_accents(i['event']) for i in sportslist: #convert floats to string i['SDNo'] = str(i['SDNo']) #add '0' to digital SD channels from sdchannels list if i['channelName'] in sdchannels: i['SDNo'] = i['SDNo'] + '0' #Remove '0' from analog channels e.g = 23 instead of 23.0 if i['SDNo']: if i['SDNo'][-1] == '0' and i['channelName'] not in sdchannels: i['SDNo'] = i['SDNo'][0:-2] #Have to limit length of string to 43 characters for the infocaster if len(i['event']) > 44: i['event'] = i['event'][:45] #Build TXT file. Have to add spaces to format and make up for lack of customization in Imagine software csport = None f.write("\n") for i in sportslist: sport = i['sport'] if csport == sport: row = ",%s,%s,%s,%s,%s\n" % (i['startTime'], i['event'], i['channelName'], i['HDNo'], i['SDNo']) f.write(row) else: row = "%s,,,,,\n" % i['sport'] f.write(row) row = ",%s,%s,%s,%s,%s\n" % (i['startTime'], i['event'], i['channelName'], i['HDNo'], i['SDNo']) f.write(row) csport = i['sport'] print "%s - Text file created at %s" % (th.now(), dir) conn.close() f.close() except Exception as e: print "Make infocaster Text failed with error %s" % e
def has_eta(self): if timeh.now() < self.eta: return True else: return False
def is_alive(self): if self.living and timeh.now() < ( self.pop + datetime.timedelta(minutes=config.MAX_MERB_LIFE)): return True return False
def process(self, line): if not line: return False # deal only with lines that start with ! if line[0] != "!": return False else: line = line[1:].lower() splitted_line = line.split(" ", 1) self.cmd = splitted_line[0] try: self.param = splitted_line[1] except IndexError: self.param = None if self.cmd == "help": return True if self.param: # check if reload parameter is provided self.find_word_reload() # check if timezone parameter is provided self.set_tz() # check if info parameter is provided self.find_word_info() # check if off is provided self.find_word_off() # check if all is provided self.find_word_all() # check if target is provided self.find_word_target() # check if tag is provided self.find_tag() # check if window is provided self.find_word_window() # check if approx is provided self.find_word_approx() # check if time is provided self.find_word_sirken() # check if time is provided self.find_time() # check if date is provided self.find_date() # check if "now" is provided if self.find_now(): self.my_date = timeh.now() # check if "yesterday" is provided self.find_yesterday() # check if "xx minutes ago are provided mins_ago = self.find_mins_ago() if mins_ago: self.my_date = timeh.from_mins_ago(mins_ago) if not self.my_date: self.my_date = timeh.assemble_date(self.parsed_time, self.parsed_date, self.timezone, self.days_back) # Find Merbs self.find_merb() return True
def cmd_track(self): output_messages = list() author_tracker = self.trackers.get_tracker(self.input_author.id) # GET INFORMATION ABOUT SELF TRACKING if "info" in self.lp.key_words: return [{ 'destination': self.input_author, 'content': messagecomposer.get_self_track_info(self.input_author.id, self.trackers), 'decoration': 'CSS' }] # STOP TRACKING elif "off" in self.lp.key_words or "stop" in self.lp.key_words or "end" in self.lp.key_words: if not author_tracker: return [{ 'destination': self.input_author, 'content': "You are not tracking anything", 'decoration': 'CSS' }] stopped_tracker = self.trackers.stop_tracker_by_user( self.input_author.id) output_messages += self.assemble_stop_tracking_messages( self.input_author.id, stopped_tracker) # Save trackers self.trackers.save() self.merbs.save_timers() return output_messages # START SINGLE TRACKING elif "start" in self.lp.key_words: # Don'start if you are already tracking if author_tracker: return [{ 'destination': self.input_author, 'content': "You are already tracking [%s]\n" "Please {!track stop} before tracking something else" % author_tracker['what'], 'decoration': 'CSS' }] # TRACK MODE if "fte" in self.lp.key_words: track_mode = "fte" elif "buff" in self.lp.key_words: track_mode = "buff" elif "coth" in self.lp.key_words: track_mode = "coth" elif "et" in self.lp.key_words: track_mode = "et" else: track_mode = "" # START TRACKING A GROUP if self.lp.tag: started_tracker = self.trackers.add_tracker( self.input_author.id, self.lp.tag, timeh.now(), track_mode, self.merbs) if not started_tracker: return [{ 'destination': self.input_author, 'content': "There are no merbs to track in %s\n" "Type {!get %s} for updated times" % (self.lp.tag, self.lp.tag), 'decoration': 'CSS' }] tracked_merbs = self.trackers.get_tracked_merb_by_user( self.input_author.id) output_messages += self.assemble_start_tracking_messages( self.lp.tag, tracked_merbs, track_mode) # Save trackers self.trackers.save() self.merbs.save_timers() return output_messages else: # Merb not found if not self.lp.merb_found: if self.lp.merb_guessed: output_content = "Merb not found. Did you mean %s?" % self.lp.merb_guessed.name else: output_content = "Merb not found" return [{ 'destination': self.input_author, 'content': output_content, 'decoration': 'BLOCK' }] # Merb has no eta elif not self.lp.merb_found.has_eta(): return [{ 'destination': self.input_author, 'content': "%s has not ETA. ToD too old?" % self.lp.merb_found.name, 'decoration': 'BLOCK' }] # Merb is not a window type elif not self.lp.merb_found.plus_minus: return [{ 'destination': self.input_author, 'content': "%s is not a window type!" % self.lp.merb_found.name, 'decoration': 'BLOCK' }] # Merb window opening is too far away elif timeh.next_future( 12) < self.lp.merb_found.window['start']: return [{ 'destination': self.input_author, 'content': "%s's window is out of range" % self.lp.merb_found.name, 'decoration': 'BLOCK' }] else: started_tracker = self.trackers.add_tracker( self.input_author.id, self.lp.merb_found, timeh.now(), track_mode, self.merbs) output_messages += self.assemble_start_tracking_messages( self.lp.merb_found.name, [self.lp.merb_found], track_mode) # Save trackers self.trackers.save() self.merbs.save_timers() return output_messages else: return [{ 'destination': self.input_author, 'content': "Missing parameters!\nType {!help track} for the correct use", 'decoration': 'CSS' }]
def update_merb(self, mode="tod"): output_messages = list() mode_word = "" # Check for approx, exact for default approx = 1 if "approx" in self.lp.key_words: approx = 0 # Assume now for !pop without time if not self.lp.my_date and mode == "pop": self.lp.my_date = timeh.now() if not self.lp.merb_found and self.lp.merb_guessed: return [{ 'destination': self.input_author, 'content': "Merb not found. Did you mean %s?" % self.lp.merb_guessed.name, 'decoration': 'BLOCK' }] if not self.lp.merb_found and not self.lp.merb_guessed: return [{ 'destination': self.input_author, 'content': "Merb not found!", 'decoration': 'BLOCK' }] if not self.lp.my_date: return [{ 'destination': self.input_author, 'content': "Time Syntax error.\n Type {!help %s} for the correct use" % mode, 'decoration': 'BLOCK' }] # A Printable, timezone converted date output_tz_time = timeh.change_naive_to_tz(self.lp.my_date, self.lp.timezone).strftime( config.DATE_FORMAT_PRINT) # Is a target? is_target = self.lp.merb_found.is_target() # UPDATE THE TOD if mode == "tod": self.lp.merb_found.update_tod(self.lp.my_date, str(self.input_author), self.lp.snippet, approx) mode_word = "died" # UPDATE THE POP if mode == "pop": if not self.lp.merb_found.update_pop( self.lp.my_date, str(self.input_author), self.lp.snippet): return [{ 'destination': self.input_author, 'content': "%s has a more recent tod time. Pop is rejected" % self.lp.merb_found.name, 'decoration': 'BLOCK' }] mode_word = "popped" # BATPHONE IT if config.BATPHONE and "bp" in self.lp.key_words: output_messages.append({ 'destination': self.d_client.get_channel(config.BROADCAST_BP_CHANNEL), 'content': f"{config.TAG_BATPHONE} {self.lp.merb_found.name}", 'decoration': None }) # Send a raid-add command to DKP BOT if Target if config.DKP_BRIDGE: output_messages.append({ 'destination': self.d_client.get_channel(config.DKP_ADD_RAID_CHANNEL), 'content': f"{config.DKP_ADD_RAID_COMMAND} " f"{self.lp.merb_found.get_shortest_alias()} " f"[{self.lp.merb_found.name}]", 'decoration': False }) # Answer message output_messages.append({ 'destination': self.input_channel, 'content': "[%s] %s at {%s %s}" % (self.lp.merb_found.name, mode_word, output_tz_time, self.lp.timezone), 'decoration': "CSS" }) # Recap of merb status output_messages.append({ 'destination': self.d_client.get_channel(config.BROADCAST_CHANNEL), 'content': messagecomposer.merb_update_recap(self.lp.merb_found, mode, "UTC"), 'decoration': "CSS" }) # Stop trackers tracking the merb and alert them stopped_trackers = self.trackers.stop_trackers_by_merb( self.lp.merb_found) for tracker in stopped_trackers: tracker_id = next(iter(tracker)) # Check if tracking other merbs tracker_ended = tracker[tracker_id]['ended'] tracker_info = tracker[tracker_id]['info'] if not tracker_ended: private_content = "%s %s but you are still tracking " % ( self.lp.merb_found.name, mode_word) for merb in tracker_info['merbs']: private_content += "[%s] " % merb.name private_content += "\nTo stop tracking please {!track stop}" else: output_messages += self.assemble_stop_tracking_messages( tracker_id, tracker[tracker_id]['info']) # save merbs timers self.merbs.save_timers() # save targets self.merbs.save_targets() # Save trackers self.trackers.save() return output_messages
def process(self, line): if not line: return False # deal only with lines that start with ! if line[0] != "!": return False else: line = line[1:].lower() splitted_line = line.split(" ", 1) self.cmd = splitted_line[0] try: self.param = splitted_line[1] except IndexError: self.param = None if self.cmd == "help": return True if self.param: # find snippet self.find_snippet() # check if timezone parameter is provided self.set_tz() # find and strip words self.find_word("info") self.find_word("off") self.find_word("start") self.find_word("stop") self.find_word("end", r"\b(ends?)\b") self.find_word("all") self.find_word("fte") self.find_word("buff") self.find_word("et") self.find_word("coth") self.find_word("bp") self.find_word("target", r"\b(targets?)\b") self.find_word("window", r"\b(windows?)\b") self.find_word("approx", r"\b(approx|around|circa)\b") self.find_word("today") self.find_word("sirken") self.find_word("sticky") self.find_word("reload") # check if tag is provided self.find_tag() # TIME/DATE CALCULATIONS # check if time is provided self.find_time() # check if date is provided self.find_date() # check if "now" is provided if self.find_word("now"): self.my_date = timeh.now() # check if "yesterday" is provided if self.find_word("yesterday"): self.days_back = 1 # check if "xx minutes ago are provided mins_ago = self.find_minutes_ago() if mins_ago: self.my_date = timeh.from_mins_ago(mins_ago) if not self.my_date: self.my_date = timeh.assemble_date(self.parsed_time, self.parsed_date, self.timezone, self.days_back) # FINALLY FIND A MERB self.find_merb() return True