def sql_create_event(data_insert, cursor, cnx): """ Create a new event row in the event table. :param data_insert: prepared data insert (event's id, event's date, game's name, event's title) :param cursor: MySQL cursor object for executing command and fetching result :param cnx: MySQL connection for verifying changes :return: new event table after update """ try: get_id_from_title(data_insert['title'], cursor) except InvalidEventTitleError: pass else: raise ExistingEventError if int(data_insert['team_size']) <= 0: raise TeamSizeError cursor.execute('insert into event ' '(event_id, date, game_id, title, team_size) ' 'values (%(event_id)s, %(date)s, %(game_id)s, ' '%(title)s, %(team_size)s)', data_insert) # add new event cnx.commit() # commit changes to database cursor.execute('select * from event where title = %s', (data_insert['title'],)) return cursor.fetchall() # return embed message to send via events channel
def sql_query_event(event_title, cursor): if event_title == "ALL": return sql_get_events(cursor) elif event_title == "FUTURE": return sql_get_events(cursor, False) try: event_id = get_id_from_title(event_title,cursor) except InvalidEventTitleError: return "Invalid Event Title" else: cursor.execute('select * from event where event_id = %s', (event_id,)) return cursor.fetchall()
async def sort_teams(self, ctx, event_title, sort_type): """ Sorts event teams with given sort type :param ctx: :param event_title: title of event to sort :param sort_type: type of sort to use :return: void """ sort_type = sort_type.lower() try: event_id = get_id_from_title(event_title, self.cursor) event_channel = self.bot.get_channel(self.event_channel_id) # Get event channel msg = await event_channel.fetch_message(event_id) # Get event message team_size = sql_get_team_size(event_id, self.cursor) # Get size of teams from event teams = get_teams_from_embed(msg.embeds[0], team_size) # Get teams of event if sort_type == 'full': a = [x for x in teams[0] if x != '-----'] b = [x for x in teams[1] if x != '-----'] c = a + b random.shuffle(c) clen = int(len(c)/2) teams[0] = c[clen:] teams[1] = c[:clen] elif sort_type == 'random': random.shuffle(teams[0]) random.shuffle(teams[1]) else: await ctx.send(sort_type + " is not a valid shuffle type!") return teams[0] = rebase_team(teams[0], team_size) # Order team 0 teams[1] = rebase_team(teams[1], team_size) # Order team 1 embed = modify_embed_message_teams(msg.embeds[0], teams) await msg.edit(embed=embed) await ctx.send("Successfully sorted teams in event " + event_title + " with shuffle type " + sort_type + "!") except InvalidEventTitleError: await ctx.send("Error: trying to sort an event that does not exist")
def sql_fetch_template(self, event_name): try: event_id = get_id_from_title(self.cursor, event_name) registered = get_registrations(self.cursor, event_id) except InvalidEventTitleError: raise InvalidEventTitleError except RegistrationEmptyError: raise RegistrationEmptyError pass else: header = 'display_name,user_id,event_id,kills,deaths,win,length,win_score,lose_score\r\n' filename = event_name.replace(" ", "_") + ".csv" fil = BytesIO() fil.write(bytes(header, "utf-8")) for u in registered: fil.write( bytes("%s,%s,%s,,,,,,\r\n" % (u[0], u[1], event_id), "utf-8")) fil.seek(0) return filename, fil
async def delete_event(self, ctx, event_title): """ Delete an event :param event_title: title of event :return: new event table or error message """ try: event_id = get_id_from_title(event_title, self.cursor) sql_delete_event(ctx.author.id, event_id, self.cursor, self.cnx) except AdminPermissionError: await ctx.send("Permission error: only admins may delete events") except InvalidEventTitleError: await ctx.send("Error: trying to delete an event that does not exist") else: sql_delete_all_registrations(event_id, self.cursor, self.cnx) # Remove all registrations for event event_channel = self.bot.get_channel(self.event_channel_id) # Get event channel msg = await event_channel.fetch_message(event_id) # Get event message await msg.delete() await ctx.send("Successfully deleted event " + event_title + "!")