# 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
    if sanitized_tag==new_tag:
      # this means user was not matched to sanitized tag, so query for user with tag==new_tag
      root_user = User.query.filter(User.tag==new_tag).first()
      if root_user is not None:
        print "ROOT USER", root_user
        merge_user(root_user.tag, user.tag)
      else:
        # if still can't find root tag to merge with, then root tag doesn't exist. Change the tag
        change_tag(user.tag, new_tag)
    else:
      # if found sanitized User, merge them
      merge_user(sanitized_tag, user.tag)  
    print '\n'

# Given a User tag and region name, changes user.region and changes regional trueskill if region is valid, otherwise deletes it
def change_region(tag, region_name):
  user = User.query.filter(User.tag==tag).first()
  region = Region.query.filter(Region.region==region_name).first()

  if not user and not region:
    return "User or Region not found"

  if user.region is not None and user.region.region==region_name:
    return "Region %s is already region of %s" % (region_name, tag)
  user.region = region
  
  # Repopulate regional trueskill for new region, to the default. First call deletes obsolete region, second call repopulates regional trueskill with new region. 
  # If new region is None, deletes obsolete region and does nothing else
  # If user.region was already None, does nothing
  populate_trueskills(user)

# 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

    if sanitized_tag==new_tag:
      # this means user was not matched to sanitized tag, so query for user with tag==new_tag
      root_user = User.query.filter(User.tag==new_tag).first()
      if root_user is not None:
        print "ROOT USER", root_user
        merge_user(root_user.tag, user.tag)
      else:
        # if still can't find root tag to merge with, then root tag doesn't exist. Change the tag
        change_tag(user.tag, new_tag)
    else:
      # if found sanitized User, merge them
      merge_user(sanitized_tag, user.tag)  
    print '\n'

# Given a User tag and region name, changes user.region and changes regional trueskill if region is valid, otherwise deletes it
def change_region(tag, region_name):
  user = User.query.filter(User.tag==tag).first()
  region = Region.query.filter(Region.region==region_name).first()

  if not user and not region:
    return "User or Region not found"

  if user.region is not None and user.region.region==region_name:
    return "Region %s is already region of %s" % (region_name, tag)
  user.region = region
  
  # Repopulate regional trueskill for new region, to the default. First call deletes obsolete region, second call repopulates regional trueskill with new region. 
  # If new region is None, deletes obsolete region and does nothing else
  # If user.region was already None, does nothing
  populate_trueskills(user)

  populate_trueskills(user)