# Path: sort_utils.py
# def sort_setlist(setlist):
#   sorted_setlist = sorted(setlist, key=attrgetter('tournament.date', 'id'))
#   try:
#   	return sorted_setlist
#   except:
#   	UnicodeEncodeError
# 
# Path: misc_utils.py
# def print_ignore(input):
# 	try:
# 		print input
# 	except UnicodeError:
# 		pass


from app import app, db
from app.models import *
from sort_utils import sort_setlist
from misc_utils import print_ignore
from trueskill import setup, Rating, quality_1vs1, rate_1vs1
import time
	print "CURRENT TrueSkill ({0}):".format(region_name), winner_user.tag, winner_user_rating, "VS.", loser_user.tag, loser_user_rating
	
	# Record set result, victory for winner_user and loss for loser_user
	new_winner_rating, new_loser_rating = rate_1vs1(winner_user_rating, loser_user_rating)
	print "UPDATED TrueSkill ({0}):".format(region_name),  winner_user.tag, new_winner_rating, "VS.", loser_user.tag, new_loser_rating

	# Store and overwrite existing trueskill object with new Rating values
	skills_dict[winner_user.tag][region_num].region=region_name
	skills_dict[winner_user.tag][region_num].mu=new_winner_rating.mu
	skills_dict[winner_user.tag][region_num].sigma=new_winner_rating.sigma
	skills_dict[winner_user.tag][region_num].cons_mu=new_winner_rating.mu - STD*new_winner_rating.sigma

	skills_dict[loser_user.tag][region_num].region=region_name
	skills_dict[loser_user.tag][region_num].mu=new_loser_rating.mu
	skills_dict[loser_user.tag][region_num].sigma=new_loser_rating.sigma
	skills_dict[loser_user.tag][region_num].cons_mu=new_loser_rating.mu - STD*new_loser_rating.sigma

def assign_trueskills_dict(skills_dict):
	for user_key in skills_dict:
		user = User.query.filter(User.tag==user_key).first()
		user.trueskills = skills_dict[user_key]
	return skills_dict

def recalculate_trueskills_dict():
	# Generate dictionary with User.tag as key, User.trueskills as values
	skills_dict = trueskills_dict()

	# Iterate through all Sets in order and recalculate Trueskill; currently in order of set.id
	# order Sets by tournament date, then by set id, oldest being at index 0
	setlist = Set.query.all()

# Path: sort_utils.py
# def sort_setlist(setlist):
#   sorted_setlist = sorted(setlist, key=attrgetter('tournament.date', 'id'))
#   try:
#   	return sorted_setlist
#   except:
#   	UnicodeEncodeError
# 
# Path: misc_utils.py
# def print_ignore(input):
# 	try:
# 		print input
# 	except UnicodeError:
# 		pass


# Path: trueskill_functions.py
from app import app, db
from app.models import *
from sort_utils import sort_setlist
from misc_utils import print_ignore
from trueskill import setup, Rating, quality_1vs1, rate_1vs1
import time

	print "CURRENT TrueSkill ({0}):".format(region_name), winner_user.tag, winner_user_rating, "VS.", loser_user.tag, loser_user_rating
	
	# Record set result, victory for winner_user and loss for loser_user
	new_winner_rating, new_loser_rating = rate_1vs1(winner_user_rating, loser_user_rating)
	print "UPDATED TrueSkill ({0}):".format(region_name),  winner_user.tag, new_winner_rating, "VS.", loser_user.tag, new_loser_rating

	# Store and overwrite existing trueskill object with new Rating values
	skills_dict[winner_user.tag][region_num].region=region_name
	skills_dict[winner_user.tag][region_num].mu=new_winner_rating.mu
	skills_dict[winner_user.tag][region_num].sigma=new_winner_rating.sigma
	skills_dict[winner_user.tag][region_num].cons_mu=new_winner_rating.mu - STD*new_winner_rating.sigma

	skills_dict[loser_user.tag][region_num].region=region_name
	skills_dict[loser_user.tag][region_num].mu=new_loser_rating.mu
	skills_dict[loser_user.tag][region_num].sigma=new_loser_rating.sigma
	skills_dict[loser_user.tag][region_num].cons_mu=new_loser_rating.mu - STD*new_loser_rating.sigma

def assign_trueskills_dict(skills_dict):
	for user_key in skills_dict:
		user = User.query.filter(User.tag==user_key).first()
		user.trueskills = skills_dict[user_key]
	return skills_dict

def recalculate_trueskills_dict():
	# Generate dictionary with User.tag as key, User.trueskills as values
	skills_dict = trueskills_dict()

	# Iterate through all Sets in order and recalculate Trueskill; currently in order of set.id
	# order Sets by tournament date, then by set id, oldest being at index 0
	setlist = Set.query.all()

	sorted_setlist = sort_setlist(setlist)