research/uefa/seed_june/uefa_ueluecl24_seeder.py
2024-10-15 10:41:25 +02:00

520 lines
18 KiB
Python
Executable File

# %%
PROJECT_PATH = '/home/md/Work/ligalytics/leagues_stable/'
import os, sys
sys.path.insert(0, PROJECT_PATH)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "leagues.settings")
os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"
from leagues import settings
# settings.DATABASES['default']['NAME'] = PROJECT_PATH+'/db.sqlite3'
settings.DATABASES['default']['ENGINE'] = 'django.db.backends.postgresql_psycopg2'
settings.DATABASES['default']['HOST'] = '0.0.0.0'
settings.DATABASES['default']['PORT'] = '5432'
settings.DATABASES['default']['USER'] = 'postgres'
settings.DATABASES['default']['PASSWORD'] = 'secret123'
settings.DATABASES['default']['NAME'] = 'mypgsqldb'
settings.DATABASES['default']['ATOMIC_REQUESTS'] = False
settings.DATABASES['default']['AUTOCOMMIT'] = True
settings.DATABASES['default']['CONN_MAX_AGE'] = 0
settings.DATABASES['default']['CONN_HEALTH_CHECKS'] = False
settings.DATABASES['default']['OPTIONS'] = {}
os.environ["XPRESSDIR"] = "/opt/xpressmp"
os.environ["XPRESS"] = "/opt/xpressmp/bin"
os.environ["LD_LIBRARY_PATH"] = os.environ["XPRESSDIR"] + "/lib"
os.environ["DYLD_LIBRARY_PATH"] = os.environ["XPRESSDIR"] + "/lib"
os.environ["SHLIB_PATH"] = os.environ["XPRESSDIR"] + "/lib"
os.environ["LIBPATH"] = os.environ["XPRESSDIR"] + "/lib"
os.environ["PYTHONPATH"] = os.environ["XPRESSDIR"] + "/lib"
os.environ["CLASSPATH"] = os.environ["XPRESSDIR"] + "/lib/xprs.jar"
os.environ["CLASSPATH"] = os.environ["XPRESSDIR"] + "/lib/xprb.jar" + os.pathsep + os.environ["CLASSPATH"]
os.environ["CLASSPATH"] = os.environ["XPRESSDIR"] + "/lib/xprm.jar" + os.pathsep + os.environ["CLASSPATH"]
os.environ["PATH"] = os.environ["XPRESSDIR"] + "/bin" + os.pathsep + os.environ["PATH"]
import django
django.setup()
from django.shortcuts import HttpResponseRedirect
from django.http import HttpResponse, JsonResponse
from django.utils import timezone
from django.urls import reverse
from django.core.files.storage import FileSystemStorage
from django.core.mail import send_mail
from django_tex.shortcuts import render_to_pdf
from celery.result import AsyncResult
import googlemaps
import timeit
import random
import json
import builtins as __builtin__
import csv
from leagues.celery import celery
from leagues.settings import EMAIL_DEFAULT_FROM, EMAIL_DEFAULT_TO
from leagues.settings import RUN_ENV, INSTANCE, DEBUG
from common.tasks import log_telegram
from common.functions import *
from scheduler.models import *
from scheduler.helpers import *
from scheduler.widgets import widget_context_kpis
from scheduler.solver.optimizer import optimize_2phases, optimize_sequentially
import scheduler.solver.optimizer as so
from draws.solver import optimize_draws
import time as timer
from qualifiers.helpers import import_globals
# %%
scenario = Scenario.objects.get(id=10313)
teams = scenario.season.scheduler_teams.all()
# %%
from draws.models import SuperGroup,Draw
base_draw = Draw.objects.filter(season=scenario.season).first()
supergroups = SuperGroup.objects.filter(draw=base_draw)
GameRequirement.objects.filter(scenario=scenario).delete()
for supergroup in supergroups:
games = optimize_draws.simulate_draws(supergroup.id, 1,report=False)
gamereqs = []
top_games = []
eng_top = []
fra_top = []
ger_top = []
ita_top = []
esp_top = []
for g in games:
team1 = teams.get(id=g[0])
team2 = teams.get(id=g[1])
attractivity = team1.attractivity*team2.attractivity
if attractivity >= 20:
top_games.append((g[0],g[1],attractivity))
if team1.countryObj.shortname == "ENG" and team2.countryObj != team1.countryObj and team2.attractivity >= 3 or \
team2.countryObj.shortname == "ENG" and team1.countryObj != team2.countryObj and team1.attractivity >= 3:
eng_top.append((g[0],g[1],attractivity))
if team1.countryObj.shortname == "FRA" and team2.countryObj != team1.countryObj and team2.attractivity >= 3 or \
team2.countryObj.shortname == "FRA" and team1.countryObj != team2.countryObj and team1.attractivity >= 3:
fra_top.append((g[0],g[1],attractivity))
if team1.countryObj.shortname == "GER" and team2.countryObj != team1.countryObj and team2.attractivity >= 3 or \
team2.countryObj.shortname == "GER" and team1.countryObj != team2.countryObj and team1.attractivity >= 3:
ger_top.append((g[0],g[1],attractivity))
if team1.countryObj.shortname == "ITA" and team2.countryObj != team1.countryObj and team2.attractivity >= 3 or \
team2.countryObj.shortname == "ITA" and team1.countryObj != team2.countryObj and team1.attractivity >= 3:
ita_top.append((g[0],g[1],attractivity))
if team1.countryObj.shortname == "ESP" and team2.countryObj != team1.countryObj and team2.attractivity >= 3 or \
team2.countryObj.shortname == "ESP" and team1.countryObj != team2.countryObj and team1.attractivity >= 3:
esp_top.append((g[0],g[1],attractivity))
gamereqs.append(GameRequirement(scenario=scenario, team1=teams.get(id=g[0]), team2=teams.get(id=g[1]), number=1))
GameRequirement.objects.bulk_create(gamereqs)
encgroups = EncGroup.objects.filter(scenario=scenario)
Encounter.objects.filter(scenario=scenario).delete()
for g in sorted(top_games, key=lambda x: x[2], reverse=True):
enc = Encounter(scenario=scenario,encounterGroup=encgroups.get(name="Top Games"))
enc.save()
enc.homeTeams.add(g[0])
enc.awayTeams.add(g[1])
eng_top = sorted(eng_top, key=lambda x: x[2], reverse=True)[:8]
for g in eng_top:
enc = Encounter(scenario=scenario,encounterGroup=encgroups.get(name="England top games"))
enc.save()
enc.homeTeams.add(g[0])
enc.awayTeams.add(g[1])
fra_top = sorted(fra_top, key=lambda x: x[2], reverse=True)[:8]
for g in fra_top:
enc = Encounter(scenario=scenario,encounterGroup=encgroups.get(name="France top games"))
enc.save()
enc.homeTeams.add(g[0])
enc.awayTeams.add(g[1])
ger_top = sorted(ger_top, key=lambda x: x[2], reverse=True)[:8]
for g in ger_top:
enc = Encounter(scenario=scenario,encounterGroup=encgroups.get(name="German top games"))
enc.save()
enc.homeTeams.add(g[0])
enc.awayTeams.add(g[1])
ita_top = sorted(ita_top, key=lambda x: x[2], reverse=True)[:8]
for g in ita_top:
enc = Encounter(scenario=scenario,encounterGroup=encgroups.get(name="Italian top games"))
enc.save()
enc.homeTeams.add(g[0])
enc.awayTeams.add(g[1])
esp_top = sorted(esp_top, key=lambda x: x[2], reverse=True)[:8]
for g in esp_top:
enc = Encounter(scenario=scenario,encounterGroup=encgroups.get(name="Spain top games"))
enc.save()
enc.homeTeams.add(g[0])
enc.awayTeams.add(g[1])
# adjust distribution of topgames
EncWish.objects.filter(scenario=scenario,reason="ITA top games should not be scheduled more than 60% of the time on the same weekday").update(minGames=int(len(ita_top)*0.4),maxGames=int(len(ita_top)*0.6+0.5))
EncWish.objects.filter(scenario=scenario,reason="ENG top games should not be scheduled more than 60% of the time on the same weekday").update(minGames=int(len(eng_top)*0.4),maxGames=int(len(eng_top)*0.6+0.5))
EncWish.objects.filter(scenario=scenario,reason="FRA top games should not be scheduled more than 60% of the time on the same weekday").update(minGames=int(len(fra_top)*0.4),maxGames=int(len(fra_top)*0.6+0.5))
EncWish.objects.filter(scenario=scenario,reason="ESP top games should not be scheduled more than 60% of the time on the same weekday").update(minGames=int(len(esp_top)*0.4),maxGames=int(len(esp_top)*0.6+0.5))
EncWish.objects.filter(scenario=scenario,reason="GER top games should not be scheduled more than 60% of the time on the same weekday").update(minGames=int(len(ger_top)*0.4),maxGames=int(len(ger_top)*0.6+0.5))
exit()
# %%
t_dict = {
"Getafe C.F.": 87960,
"FC Shkupi": 2606993,
"Maccabi Tel-Aviv FC": 57477,
"Malatyaspor": 78058,
"RCD Espanyol de Barcelona": 54189,
"Tottenham Hotspur": 1652,
"Trabzonspor A.Ş.": 52731,
"AS Monaco": 50023,
"Fotbal Club FCSB": 50065,
"FK Radnički Niš": 52734,
"F91 Dudelange": 59028,
"Progrès Niederkorn": 52311,
"IFK Norrköping": 50099,
"FC Levadia Tallinn": 77482,
"LASK Linz": 63405,
"GKS Piast Gliwice": 2600545,
"MKS Cracovia Kraków": 88133,
"Sabail": 2608569,
"RC Strasbourg": 59857,
"FK Makedonija Skopje": 64395,
"PFC Arsenal Tula": 2606414,
"U Craiova 1948 Club Sportiv": 64503,
"VfL Borussia Mönchengladbach": 52757,
"PFC Lokomotiv Plovdiv 1926": 57466,
"Football Club Zenit": 52826,
"Lechia Gdańsk": 52763,
"Hapoel Beer-Sheva FC": 59340,
"SP La Fiorita": 64508,
"KF Feronikeli": 2608281,
"Cardiff MUFC": 59337,
"Barry Town UFC": 53069,
"Beşiktaş": 50157,
"Mons Calpe Sports Club": 2608011,
"FC St.Gallen 1879": 51151,
"FC Zimbru Chişinău": 59036,
"Hapoel Be'er Sheva FC": 59340,
"PFC Arda Kardzhali 1924": 2610060,
"FC Torpedo-Belaz Zhodino": 79551,
"Panevezys": 2608927,
"Ballkani": 2609552,
"Dnipro-1": 2609193,
"Union Saint-Gilloise": 64125,
"Omonoia FC": 50077,
"R. Charleroi SC": 52886,
"Manchester City FC": 52919,
"Manchester United FC": 52682,
"Barry Town United FC": 53069,
"FC CSKA 1948": 2610262,
"Sabah": 2609356,
"FC Bologna": 52969,
"FC Dynamo Brest": 64374,
"Kisvarda Master Good": 2605958,
"FC Bologna": 52969,
"Beşiktaş": 50157,
"Omonoia FC": 50077,
"WKS Śląsk Wrocław": 52655,
"FC Ararat Yerevan": 59318,
"FCB Magpies": 2606279,
"KKS Lech Poznań": 64227,
"Sumgait FC": 2603921,
"KF Dukagjini": 2608311,
"FC Haka Valkeakoski": 52802,
"FC Hegelmann Litauen": 2607108,
"FCV Farul": 2604753,
"FC Petrocub Hîncești": 2607112,
"Kolos Kovalivka": 2609189,
"St. Patrick Athletic FC": 50133,
"Manchester United FC": 52682,
"Trabzonspor AS": 52731,
"Sepsi OSK Sfantu Gheorghe": 2608575,
"Raków Czestochowa": 60566,
"FK TSC Bačka Topola": 2610140,
"İstanbul Başakşehir FK": 2600288,
}
for t in teams:
gt = t.global_id
if not gt:
gt = GlobalTeam.objects.filter(name=t.name).first()
if not gt:
gt = GlobalTeam.objects.filter(fame_id=t_dict.get(t.name)).first()
if not gt:
print(t.name)
t.name = gt.name
t.global_id = gt
t.external_id = gt.fame_id
t.save()
# %%
# %%
new_teams = []
uel_teams = []
teams.update(active=False)
with open('calendar_juneteams/uel_teams.csv', newline='') as csvfile:
reader = csv.reader(csvfile)
next(reader, None)
for row in reader:
team_name = row[1].split("(")[0].strip()
if row[0] != "":
# print(row)
team_pos = int(row[0])
team_name = row[1].split("(")[0].strip()
team_coeff = row[2]
t = teams.filter(name=team_name).order_by('-logo').first()
gt = GlobalTeam.objects.filter(name=team_name).first()
if not t:
print(f"Team {team_name} not found")
if not gt:
print(f"Global team {team_name} not found")
else:
t = Team.objects.create(
season=scenario.season,
name=team_name,
shortname=gt.shortname,
position=team_pos,
latitude=gt.latitude,
longitude=gt.longitude,
countryObj=Country.objects.get(season=scenario.season,shortname=gt.country.uefa),
active=True
)
print("Created team", team_name)
if gt:
t.global_id = gt
t.external_id = gt.fame_id
t.shortname = gt.shortname
t.active=True
t.position = team_pos
if team_pos <= 9:
t.pot = 1
elif team_pos <= 18:
t.pot = 2
elif team_pos <= 27:
t.pot = 3
else:
t.pot = 4
t.save()
uel_teams.append(t.id)
new_teams = []
uecl_teams= []
# teams.update(active=False)
with open('calendar_juneteams/uecl_teams.csv', newline='') as csvfile:
reader = csv.reader(csvfile)
next(reader, None)
for row in reader:
team_name = row[1].split("(")[0].strip()
if row[0] != "":
# print(row)
team_pos = int(row[0])
team_name = row[1].split("(")[0].strip()
team_coeff = row[2]
t = teams.filter(name=team_name).order_by('-logo').first()
gt = GlobalTeam.objects.filter(name=team_name).first()
if not t:
print(f"Team {team_name} not found")
if not gt:
print(f"Global team {team_name} not found")
else:
t = Team.objects.create(
season=scenario.season,
name=team_name,
shortname=gt.shortname,
position=team_pos,
latitude=gt.latitude,
longitude=gt.longitude,
countryObj=Country.objects.get(season=scenario.season,shortname=gt.country.uefa),
active=True
)
print("Created team", team_name)
if gt:
t.global_id = gt
t.external_id = gt.fame_id
t.shortname = gt.shortname
t.active=True
t.position = team_pos
if team_pos <= 6:
t.pot = 1
elif team_pos <= 12:
t.pot = 2
elif team_pos <= 18:
t.pot = 3
elif team_pos <= 24:
t.pot = 4
elif team_pos <= 30:
t.pot = 5
else:
t.pot = 6
t.save()
uecl_teams.append(t.id)
# %%
CET_minus_1 = ['ENG','POR','SCO','ISL','FRO']
CET_plus_1 = ['TUR','GRE','ROU','LTU','FIN','EST','MDA','CYP','AZE','ISR','UKR']
conf = Conference.objects.get_or_create(scenario=scenario,name=f"UEL")
conf[0].teams.clear()
conf = Conference.objects.get_or_create(scenario=scenario,name=f"UECL")
conf[0].teams.clear()
for i in range(1,5):
conf = Conference.objects.get_or_create(scenario=scenario,name=f"UEL-Pot {i}")
conf[0].teams.clear()
for i in range(1,7):
conf = Conference.objects.get_or_create(scenario=scenario,name=f"UECL-Pot {i}")
conf[0].teams.clear()
for i in range(1,6):
conf = Conference.objects.get_or_create(scenario=scenario,name=f"Global Coeff {i}")
conf[0].teams.clear()
conf = Conference.objects.get_or_create(scenario=scenario,name=f"Domestic Coeff {i}")
conf[0].teams.clear()
conf = Conference.objects.get_or_create(scenario=scenario,name="CET")
conf[0].teams.clear()
conf = Conference.objects.get_or_create(scenario=scenario,name="CET-1")
conf[0].teams.clear()
conf = Conference.objects.get_or_create(scenario=scenario,name="CET+1")
conf[0].teams.clear()
for team in Team.objects.filter(season=scenario.season,active=True):
teamObj = Team.objects.filter(id=team.id)
if team.id in uel_teams:
competition = "UEL"
elif team.id in uecl_teams:
competition = "UECL"
else:
print("Team not found in any competition")
global_coeff = int(team.attractivity)
domestic_coeff = int(team.attractivity*10)%10
team_country = team.countryObj.shortname
Conference.objects.filter(scenario=scenario,name=competition).first().teams.add(teamObj.first())
Conference.objects.filter(scenario=scenario,name=f"{competition}-Pot {team.pot}").first().teams.add(teamObj.first())
if global_coeff in range(1,6):
Conference.objects.filter(scenario=scenario,name=f"Global Coeff {global_coeff}").first().teams.add(teamObj.first())
if domestic_coeff in range(1,6):
Conference.objects.filter(scenario=scenario,name=f"Domestic Coeff {domestic_coeff}").first().teams.add(teamObj.first())
if team_country in CET_minus_1:
Conference.objects.filter(scenario=scenario,name="CET-1").first().teams.add(teamObj.first())
elif team_country in CET_plus_1:
Conference.objects.filter(scenario=scenario,name="CET+1").first().teams.add(teamObj.first())
else:
Conference.objects.filter(scenario=scenario,name="CET").first().teams.add(teamObj.first())
if competition == "UEL":
teamObj.update(coefficient=5-team.pot)
else:
teamObj.update(coefficient=7-team.pot)
for conf in Conference.objects.filter(scenario=scenario):
for t in conf.teams.filter(active=False):
conf.teams.remove(t)
for haw in HAWish.objects.filter(scenario=scenario):
for t in haw.teams.filter(active=False):
haw.teams.remove(t)
for enc in EncWish.objects.filter(scenario=scenario):
for t in enc.teams1.filter(active=False):
enc.teams1.remove(t)
for t in enc.teams2.filter(active=False):
enc.teams1.remove(t)
for pair in Pairing.objects.filter(scenario=scenario):
if pair.team1.active==False or pair.team2.active==False:
pair.active=False
pair.save()
from draws.models import SuperGroup
sg = SuperGroup.objects.filter(draw__season=scenario.season).get(name="UEL")
sg.teams.clear()
for t in Conference.objects.get(scenario=scenario,name="UEL").teams.all():
sg.teams.add(t)
sg = SuperGroup.objects.filter(draw__season=scenario.season).get(name="UECL")
sg.teams.clear()
for t in Conference.objects.get(scenario=scenario,name="UECL").teams.all():
sg.teams.add(t)
# %%
Blocking.objects.filter(scenario=scenario).delete()
# %%