async def save_message_to_db(self, message): # Guardo cada mensaje a la db de la guild, con un total de 250 messages por canal guild_document = self.database.get_document("guilds", {"_id": message.guild.id}) if not guild_document: raise Exception channels = guild_document["channels"] last_messages = [] channel_found = False for chan in channels: if chan["id"] == message.channel.id: last_messages = chan["last_50_messages"] channel_found = True break if not channel_found: # Canal no existe, lo agrego y updateo db. hardcodeado bien rico. guild_document["channels"].append({ "id": message.channel.id, "last_50_messages": [], "tasks": [] }) print("CANAL no existia, agregandolo a la DB.") if len(last_messages) >= 250: del last_messages[0] last_messages.append(message.id) self.database.update_document("guilds", guild_document) debug_log(f"Mensaje guardado en la db exitosamente", "shizu")
async def run_check_tasks(self): while True: await self.shizu_tasks.check_tasks() debug_log( "durmiendo 10 segundos antes de ejecutar siguiente check de tasks...", "shizu") await asyncio.sleep(10)
async def give_experience(self, message): if message.author.bot and message.author.id != bot_vars.BOT_ID: return doc = {"_id": message.author.id} db_member = self.database.get_document("members", doc) current_message_time = datetime.now() previous_message_time = db_member["last_message_time"] should_add_exp = True # Para nuevos integrantes al comienzo el "last_message_time" es cero, por lo que primero verifico si no es cero if previous_message_time: should_add_exp = ( abs(previous_message_time - current_message_time) ).total_seconds() > 30 # pasaron 30 segs desde ultimo mensaje? if should_add_exp: member_message_count = db_member["message_count"] debug_log(f"Total mensajes: {member_message_count})", "shizu") experience_to_add = len( message.content) * 0.2 + member_message_count * 0.4 + 60 lvlup = self.database.add_experience(message.author, experience_to_add) if lvlup: new_lvl = db_member[ "level"] + 1 # Al level "viejo" (de la query antes de subir de lvl) le sumo uno para evitar hacer dos querys await message.channel.send(message.author.mention + " Avanzaste a nivel " + str(new_lvl) + "!") else: debug_log( f"No se pudo agregar exp a {message.author.name} porque no paso el tiempo requerido entre mensaje y mensaje", "shizu")
def update_task(self, update_task_doc): if update_task_doc: task_modified = self.update_document("tasks", update_task_doc) debug_log("Actualizando los datos de una tarea existente", "db") else: debug_log("Error: El diccionario recibido es None.") return task_modified
def add_field(self, spec, collection_name, field_doc): """ Agrega un campo no existente a un documento ya existente spec = diccionario con el documento a buscar, ejemplo {"_id" : 12132} collection = collection en donde se agregara el campo, ejemplo "members" document = diccionario a agregar, ejemplo {"likes" : 0} """ self.database[collection_name].update_one(spec, {"$set" : field_doc}) debug_log(f"Agregando el documento {field_doc} en la coleccion {collection_name}", "db")
def save_guild(self, guild_doc): # Agrega la guild solo si no existe en DB if not self.document_exists("guilds", guild_doc): guild_doc_id = self.add_document("guilds", guild_doc) debug_log("La guild {0} fue guardada en la DB.".format(guild_doc["name"])) return guild_doc_id debug_log("La guild {0} ya existe en la DB, ignorando!...".format(guild_doc["name"])) return None
def save_member(self, member_doc): """ Guarda el usuario UNICAMENTE si NO existe en la DB. Para actualizar utilizar update_document """ if not self.document_exists("members", {"_id" : member_doc["_id"]}): # No existe, guardo nueveo user en db. current_member = self.add_document("members", member_doc) debug_log("El usuario {0} fue guardado en la DB.".format(member_doc["name"])) return current_member debug_log("El usuario {0} ya existe en la DB, ignorando!...".format(member_doc["name"])) return None
def add_experience(self, member, exp_to_add): # Retorna True si subio de nivel # Metodo que agrega experiencia al usuario y sube de nivel en caso de que cumpla con las condiciones para subir. lvlup = False db_member = self.get_document("members", {"_id" : member.id}) new_exp = db_member["experience"] + exp_to_add debug_log(f"Agregando {exp_to_add} de experiencia", "db") current_lvl = db_member["level"] print("EXP PARA PROX LVL:", self.get_lvl_experience(current_lvl)) if new_exp >= self.get_lvl_experience(current_lvl): current_lvl += 1 # Subo de nivel! lvlup = True db_member["experience"] = new_exp db_member["level"] = current_lvl db_member["last_message_time"] = datetime.now() db_member["message_count"] += 1 self.update_document("members", db_member) return lvlup
def test_07(): core.debug_log("Running Test 07") budgets = [5, 10, 5] c = 10 expected_output = [3, 3, 4] core.run(c, budgets, expected_output)
def test_06(): core.debug_log("Running Test 06") budgets = [10, 100, 100] c = 100 expected_output = [10, 45, 45] core.run(c, budgets, expected_output)
def test_05(): core.debug_log("Running Test 05") budgets = [3, 3, 3] c = 3 expected_output = [1, 1, 1] core.run(c, budgets, expected_output)
def test_01(): core.debug_log("Running Test 01") budgets = [3, 100, 100] c = 100 expected_output = [3, 48, 49] core.run(c, budgets, expected_output)
def test_04(): core.debug_log("Running Test 04") budgets = [20, 20, 20] c = 100 expected_output = "IMPOSSIBLE" core.run(c, budgets, expected_output)
def test_03(): core.debug_log("Running Test 03") budgets = [100, 1, 60] c = 100 expected_output = [1, 49, 50] core.run(c, budgets, expected_output)
def test_02(): core.debug_log("Running Test 02") budgets = [40, 40, 40] c = 100 expected_output = [33, 33, 34] core.run(c, budgets, expected_output)
import core # Used when running in CodinGame to read input from stdinput # if __name__ == "__main__": # Test 01 core.debug_log("Test01") core.run(1, 11, "1 1 1 3 1 2 2 1 1 3 3 1 1 2 1 3 2 1 1 3 2 1 2 2 2 1") # Test 04 core.debug_log("Test04") core.run(25, 10, "3 1 1 3 1 1 2 2 2 1 1 3 1 1 1 2 3 1 1 3 3 2 2 1 1 25") # Test 06 core.debug_log("Test06") core.run( 33, 25, "3 1 1 3 1 1 2 2 2 1 1 3 1 1 1 2 3 1 1 3 3 2 1 1 1 2 1 3 1 2 2 1 1 2 3 1 1 3 1 1 1 2 3 1 1 2 1 1 1 3 3 1 1 2 1 1 1 3 1 2 2 1 1 2 1 3 2 1 1 3 1 2 1 1 1 3 2 2 2 1 1 2 3 1 1 3 1 1 2 2 1 1 1 2 1 3 1 2 2 1 1 2 3 1 1 3 1 1 2 2 2 1 1 2 1 1 1 3 3 1 1 2 1 1 1 3 1 1 2 2 2 1 1 2 1 1 1 3 1 2 2 1 1 3 1 2 1 1 1 3 2 2 2 1 1 2 1 3 2 1 1 3 2 1 3 2 2 1 2 3 2 1 1 2 1 1 1 3 1 2 1 1 1 2 1 3 3 2 2 1 1 2 3 1 1 3 1 1 2 2 2 1 1 3 1 1 1 2 2 1 2 2 1 1 1 3 1 2 2 1 1 2 1 3 2 1 1 3 1 2 1 1 1 3 2 2 2 1 1 2 3 1 1 3 1 1 2 2 2 1 1 3 1 1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