# Path: misc_utils.py
# def print_ignore(input):
# 	try:
# 		print input
# 	except UnicodeError:
# 		pass


import sys
import urllib3
import json
import requests
import challonge
from app.models import *
from bs4 import BeautifulSoup
from pprint import pprint
from date_utils import *
from trueskill_functions import *
from misc_utils import print_ignore

		# Query for associated Tournament
		if sub_tournament.name is not None:
			assocs_tournament = Tournament.query.filter(Tournament.name==sub_tournament.name).first()
		else:
			print "Tournament not Found"

		if assign_placings is True:
			if (-1 * set.round_number) in placings_dict:
				print "Loser places", placings_dict[(-1 * set.round_number)]
				found_placement = Placement.query.filter(and_(Placement.tournament_name==assocs_tournament.name, Placement.user_id==loser_user.id)).first()
				found_placement.placement = placings_dict[(-1 * set.round_number)]
			elif set.round_number==placings_dict["last_winner_round"]:
				print "LAST WINNER ROUND"
				winner_placement = Placement.query.filter(and_(Placement.tournament_name==assocs_tournament.name, Placement.user_id==winner_user.id)).first()
				loser_placement = Placement.query.filter(and_(Placement.tournament_name==assocs_tournament.name, Placement.user_id==loser_user.id)).first()
				winner_placement.placement = 1
				loser_placement.placement = 2

		new_set = Set(tournament_id=assocs_tournament.id,
	                  tournament_name=assocs_tournament.name,
	                  round_type=set.round_number,
	                  winner_tag=winner_user.tag,
	                  loser_tag=loser_user.tag,
	                  winner_id=winner_user.id,
	                  loser_id=loser_user.id,
	                  winner_score=set.winner_score,
	                  loser_score=set.loser_score,
	                  total_matches=set.total_matches)
		db.session.add(new_set) 

# Path: misc_utils.py
# def print_ignore(input):
# 	try:
# 		print input
# 	except UnicodeError:
# 		pass


# Path: parsers/challonge_parse.py
import sys
import urllib3
import json
import requests
import challonge
from app.models import *
from bs4 import BeautifulSoup
from pprint import pprint
from date_utils import *
from trueskill_functions import *
from misc_utils import print_ignore


		# Query for associated Tournament
		if sub_tournament.name is not None:
			assocs_tournament = Tournament.query.filter(Tournament.name==sub_tournament.name).first()
		else:
			print "Tournament not Found"

		if assign_placings is True:
			if (-1 * set.round_number) in placings_dict:
				print "Loser places", placings_dict[(-1 * set.round_number)]
				found_placement = Placement.query.filter(and_(Placement.tournament_name==assocs_tournament.name, Placement.user_id==loser_user.id)).first()
				found_placement.placement = placings_dict[(-1 * set.round_number)]
			elif set.round_number==placings_dict["last_winner_round"]:
				print "LAST WINNER ROUND"
				winner_placement = Placement.query.filter(and_(Placement.tournament_name==assocs_tournament.name, Placement.user_id==winner_user.id)).first()
				loser_placement = Placement.query.filter(and_(Placement.tournament_name==assocs_tournament.name, Placement.user_id==loser_user.id)).first()
				winner_placement.placement = 1
				loser_placement.placement = 2

		new_set = Set(tournament_id=assocs_tournament.id,
	                  tournament_name=assocs_tournament.name,
	                  round_type=set.round_number,
	                  winner_tag=winner_user.tag,
	                  loser_tag=loser_user.tag,
	                  winner_id=winner_user.id,
	                  loser_id=loser_user.id,
	                  winner_score=set.winner_score,
	                  loser_score=set.loser_score,
	                  total_matches=set.total_matches)
		db.session.add(new_set) 

		print_ignore(set)