# Path: trueskill_functions.py
# MU = 25
# 
# SIGMA = 8.333333333333334
# 
# CONS_MU = MU - (STD*SIGMA)
# 
# BETA = 4.166666666666667
# 
# TAU = 0.08333333333333334
# 
# DRAW_PROBABILITY = 0.0
# 
# def populate_trueskills(user):
# 	"""Populates TrueSkill associations of User
# 
# 	Args:
#   	user: A User object.
#   
#  	Returns:
#   	If User already has Global and appropriate Regional TrueSkill, returns True.
#   	Adds TrueSkill association for Global if it does not already exist.
#   	Deletes TrueSkill association for Region if User no longer is associated with that region.
# 	"""
# 	
# 	# case for when User has no TrueSkills; if User has region, populate both Global and region Trueskill
# 	if len(user.trueskills)<=0:
# 		user.trueskills.append(TrueSkill(region="Global", mu=MU, sigma=SIGMA, cons_mu=CONS_MU))
# 		if user.region is not None:
# 			user.trueskills.append(TrueSkill(region=user.region.region, mu=MU, sigma=SIGMA, cons_mu=CONS_MU))
# 	# case for when User is initialized with no region (Tournament has no region attribute during)
# 	# if the one regional TrueSkill is Global, and user.region is not None, populate regional TrueSkill
# 	elif len(user.trueskills)==1: 
# 		if user.trueskills[0].region=="Global" and user.region is not None:
# 			user.trueskills.append(TrueSkill(region=user.region.region, mu=MU, sigma=SIGMA, cons_mu=CONS_MU))
# 			db.session.commit()
# 		else:
# 			return True
# 	# If User has 2 Trueskills (Global and Region), check to make sure he should have a regional TrueSkill (if he belongs to a region)
#   # if User Region does not match TrueSkill region, remove it
# 	elif len(user.trueskills)>=2:
# 		if user.region is None or user.region.region!=user.trueskills[1].region:
# 			user.trueskills.remove(user.trueskills[1])
# 			db.session.commit()
# 	else:
# 		return True


from app import app, db
from app.models import *
from sanitize_utils import *
from trueskill import setup, Rating, quality_1vs1, rate_1vs1
from trueskill_functions import MU, SIGMA, CONS_MU, BETA, TAU, DRAW_PROBABILITY, populate_trueskills
from misc_utils import *
import datetime
import sys
  for set in won_sets:
    set.winner_tag = new_tag
    print_ignore(set)
  
  lost_sets = user.get_lost_sets()
  for set in lost_sets:
    set.loser_tag = new_tag
    print_ignore(set)

  user.tag = new_tag
  
  db.session.commit()
  print "UPDATED USER: ", print_ignore(user)
  return user

# transfers the data the User represented by joined_tag has to User root_tag, while deleting the User represented by joined_tag
# currently doesn't actually link the Users or tag in any way before deletion
# currently doesn't change Matches
def merge_user(root_tag, joined_tag):
  root_user = User.query.filter(User.tag==root_tag).first()
  joined_user = User.query.filter(User.tag==joined_tag).first()
  if root_user is None: 
    print "root_user not found"
    return
  elif joined_user is None: 
    print "joined_user " + joined_tag + " not found"
    return

  # transfer Set data by simply editing Sets to have the root_user as the winner/loser tag and id
  joined_sets = joined_user.get_all_sets()

# Path: trueskill_functions.py
# MU = 25
# 
# SIGMA = 8.333333333333334
# 
# CONS_MU = MU - (STD*SIGMA)
# 
# BETA = 4.166666666666667
# 
# TAU = 0.08333333333333334
# 
# DRAW_PROBABILITY = 0.0
# 
# def populate_trueskills(user):
# 	"""Populates TrueSkill associations of User
# 
# 	Args:
#   	user: A User object.
#   
#  	Returns:
#   	If User already has Global and appropriate Regional TrueSkill, returns True.
#   	Adds TrueSkill association for Global if it does not already exist.
#   	Deletes TrueSkill association for Region if User no longer is associated with that region.
# 	"""
# 	
# 	# case for when User has no TrueSkills; if User has region, populate both Global and region Trueskill
# 	if len(user.trueskills)<=0:
# 		user.trueskills.append(TrueSkill(region="Global", mu=MU, sigma=SIGMA, cons_mu=CONS_MU))
# 		if user.region is not None:
# 			user.trueskills.append(TrueSkill(region=user.region.region, mu=MU, sigma=SIGMA, cons_mu=CONS_MU))
# 	# case for when User is initialized with no region (Tournament has no region attribute during)
# 	# if the one regional TrueSkill is Global, and user.region is not None, populate regional TrueSkill
# 	elif len(user.trueskills)==1: 
# 		if user.trueskills[0].region=="Global" and user.region is not None:
# 			user.trueskills.append(TrueSkill(region=user.region.region, mu=MU, sigma=SIGMA, cons_mu=CONS_MU))
# 			db.session.commit()
# 		else:
# 			return True
# 	# If User has 2 Trueskills (Global and Region), check to make sure he should have a regional TrueSkill (if he belongs to a region)
#   # if User Region does not match TrueSkill region, remove it
# 	elif len(user.trueskills)>=2:
# 		if user.region is None or user.region.region!=user.trueskills[1].region:
# 			user.trueskills.remove(user.trueskills[1])
# 			db.session.commit()
# 	else:
# 		return True


# Path: maintenance_utils.py
from app import app, db
from app.models import *
from sanitize_utils import *
from trueskill import setup, Rating, quality_1vs1, rate_1vs1
from trueskill_functions import MU, SIGMA, CONS_MU, BETA, TAU, DRAW_PROBABILITY, populate_trueskills
from misc_utils import *
import datetime
import sys

  for set in won_sets:
    set.winner_tag = new_tag
    print_ignore(set)
  
  lost_sets = user.get_lost_sets()
  for set in lost_sets:
    set.loser_tag = new_tag
    print_ignore(set)

  user.tag = new_tag
  
  db.session.commit()
  print "UPDATED USER: ", print_ignore(user)
  return user

# transfers the data the User represented by joined_tag has to User root_tag, while deleting the User represented by joined_tag
# currently doesn't actually link the Users or tag in any way before deletion
# currently doesn't change Matches
def merge_user(root_tag, joined_tag):
  root_user = User.query.filter(User.tag==root_tag).first()
  joined_user = User.query.filter(User.tag==joined_tag).first()
  if root_user is None: 
    print "root_user not found"
    return
  elif joined_user is None: 
    print "joined_user " + joined_tag + " not found"
    return

  # transfer Set data by simply editing Sets to have the root_user as the winner/loser tag and id
  joined_sets = joined_user.get_all_sets()

  for set in joined_sets: