d1160a66e0
Почистил код. Удалил лишнее.
54 lines
2.4 KiB
Python
54 lines
2.4 KiB
Python
from modules.DB import UseDB
|
|
from loguru import logger
|
|
import random
|
|
|
|
|
|
def get_random_event():
|
|
db = UseDB("events")
|
|
response = db.find_document({})
|
|
random_event = random.choices([i["_id"] for i in response], weights=[i["probability"] for i in response])
|
|
return db.find_document({"_id": random_event[0]})[0]
|
|
|
|
|
|
def recalculation_events(well):
|
|
if well:
|
|
db = UseDB("events")
|
|
not_well_events = db.find_document({"well": False})
|
|
for i in not_well_events:
|
|
db.update_document({"_id": i["_id"]}, {"probability": i["probability"] + 5})
|
|
well_events = db.find_document({"well": True})
|
|
cooficent = len(not_well_events) * 5 / len(well_events)
|
|
for i in well_events:
|
|
db.update_document({"_id": i["_id"]}, {"probability": i["probability"] - cooficent})
|
|
else:
|
|
db = UseDB("events")
|
|
well_events = db.find_document({"well": True})
|
|
for i in well_events:
|
|
db.update_document({"_id": i["_id"]}, {"probability": i["probability"] + 5})
|
|
not_well_events = db.find_document({"well": False})
|
|
cooficent = len(well_events) * 5 / len(not_well_events)
|
|
for i in not_well_events:
|
|
db.update_document({"_id": i["_id"]}, {"probability": i["probability"] - cooficent})
|
|
|
|
|
|
def start_calculating():
|
|
logger.remove()
|
|
logger.add("conf/log.log", level="INFO")
|
|
logger.info("Валидация началась")
|
|
db = UseDB("events")
|
|
response = db.find_document({})
|
|
sum_percent_probability = sum([i["probability"] for i in response])
|
|
sum_percent_probability_in_events = sum([sum(i["probability_consequence"]) for i in response])
|
|
if sum_percent_probability != 100:
|
|
logger.critical("Сумма вероятностей всех ивентов не равна 100, это плохо, не надо так")
|
|
quit(0)
|
|
if sum_percent_probability_in_events != len(response) * 100:
|
|
logger.critical("Проблема в каком-то ивенте, сумма вероятностей не правильная")
|
|
quit(0)
|
|
for i in response:
|
|
if not (len(i["variants"]) == len(i["consequence"]) == len(i["probability_consequence"])):
|
|
logger.critical(f"Ошибка в ивенте {i['name']}. Разница в длинах variants, consequence "
|
|
f"и probability_consequence")
|
|
quit(0)
|
|
logger.info("Валидация игры успешно прошла")
|