Пример #1
0
def setRanks_WithTiebreaks(tourney_id):
	"""this function ranks players by number of wins.  
	then, players who tie on number of wins are separated 
	by how many wins their beaten opponets have.  
	i.e. beating someone who won lots of games is better 
	for your ranking than beating someone with few wins. """
	
	logging.info('in setRanks_WithTiebreaks()')
	
	head_to_head_biggermat, head_to_head_2d = new_utility_functions.getHeadToHeadTable(tourney_id)
	
	the_ids = deepcopy(head_to_head_biggermat[0][1:])
	#logging.info('the_ids:')
	#logging.info(the_ids)
	
	the_players = players.Player.all().filter("tourney_id =", tourney_id)#.run(batch_size=1000)
	num_players = len(list(the_players))
	assert(num_players==len(the_ids))
	
	num_wins = [0]*num_players
	for i in range(0,num_players):
		#logging.info(i)
		for j in range(0,num_players):
			num_wins[i] += head_to_head_2d[i][j][0]
	
	#logging.info('num_wins = '+str(num_wins))
	
	sum_of_wins_of_players_you_beat = [0]*num_players
	for i in range(0,num_players):
		#logging.info(i)
		for j in range(0,num_players):
			sum_of_wins_of_players_you_beat[i] += head_to_head_2d[i][j][0]*num_wins[j]

	#logging.info('sum_of_wins_of_players_you_beat = '+str(sum_of_wins_of_players_you_beat))

	points_and_playerid=[]
	for i in range(0,num_players):
		points_and_playerid.append(
				[ num_wins[i] +0.0001*sum_of_wins_of_players_you_beat[i] , the_ids[i] ]
				)
	#logging.info('points_and_playerid:')
	#logging.info(points_and_playerid)
	points_and_playerid.sort(reverse=True)
	#logging.info('points_and_playerid:')
	#logging.info(points_and_playerid)


	for p in the_players:
		player_id = int(p.player_id)
		rank = -1
		points = -1.0
		for i in range(0,len(points_and_playerid)):
			if points_and_playerid[i][1]==player_id:
				rank = i+1
				points = points_and_playerid[i][0]
		p.currentRank = rank
		p.numWins = int(round(points))
		p.save()
	
	logging.info('leaving setRanks_WithTiebreaks()')
Пример #2
0
def getMatchedList_Swiss(tourney_id):
	"""pair up the players according to the swiss tournament matching rules"""

	#head_to_head_biggermat
	head_to_head_biggermat, head_to_head_2d = new_utility_functions.getHeadToHeadTable(tourney_id)

	num_players = len(head_to_head_biggermat)-1
	assert(num_players>=2)

	#in this function, we use ids from 0 to num_players.
	#here, we create a mapping from these ids to the database's player_ids, 
	#randomly shuffled to try and avoid paterns in the matching
	temp = deepcopy(head_to_head_biggermat[0][1:])
	playersidlocal_playerids = [temp[i] for i in range(0,num_players)]
	#logging.info('playersidlocal_playerids')
	#logging.info(playersidlocal_playerids)

	num_wins = []
	num_games = []
	for i in range(0,num_players):
		num_wins.append(0)
		num_games.append(0)
		for j  in range(0,num_players):
			num_wins[i] += head_to_head_2d[i][j][0]
			num_games[i] += (head_to_head_2d[i][j][0]+head_to_head_2d[i][j][0])/2

	#deal with odd num_players
	player_who_sits_out = -1
	if num_players%2 == 1:
		tmp = zip(num_games,range(0,num_players))
		tmp.sort()
		#logging.info('tmp')
		#logging.info(tmp)
		player_who_sits_out = tmp[-1][1] #this player is one who has highest number of games
		logging.info('player_who_sits_out')
		logging.info(player_who_sits_out)

	#make edges
	edges=[]
	for i in range(0,num_players):
		for j  in range(i+1,num_players):
			if (head_to_head_2d[i][j][0]==0) and (head_to_head_2d[i][j][1]==0):
				if (player_who_sits_out!=i) and (player_who_sits_out!=j):
					# i,j have not played yet
					abs_win_diff = abs(num_wins[i]-num_wins[j])
					edges.append((i,j,-abs_win_diff+0.1*random.random())) #note the - sign.  the 0.1*random.random() is to randomise things WITHOUT changing the ranking of num wins etc. 
				else:
					edges.append((i,j,-1000000)) #a big number, to ensure we never make this match
	#logging.info('edges')
	#logging.info(edges)

	#call the clever matching algorithm
	matchups = maxWeightMatching(edges,True)
	#logging.info('matchups')
	#logging.info(matchups)
	if not matchups:
		return [] #we havent got any matchups.  likely that the tournament is over. 

	#package matchups into right format
	doneq = [False] * num_players
	new_match_list = []
	for i in range(0,len(matchups)):
		if not doneq[i]:
			doneq[i]=True
			j = matchups[i]
			if j != -1:
				doneq[j]=True
			
				i_id = playersidlocal_playerids[i]
				j_id = playersidlocal_playerids[j]
			
				new_match_list.append(str(i_id))
				new_match_list.append(str(j_id))
	##if player_who_sits_out >= 0:
	##	new_match_list.append(str(playersidlocal_playerids[player_who_sits_out])) #he wont get a game though, because of the odd number

	#logging.info('new_match_list')
	#logging.info(new_match_list)
	
	return new_match_list
Пример #3
0
def createGames_RoundRobin(tourney_id):
	"""This is called periodically to check for new games that need to be created.
	the roundrobin part is that we want everyone to play everyone else.
	so the players not currently in games are just paired up with each other,
	so long as they have not yet played each other.
	"""
	logging.info('')
	logging.info('in createGames_RoundRobin()')

	if main.hasTourneyFinished(tourney_id):
		logging.info('round robin tourney has finished')
		return

	#Retrieve all games that are ongoing
	activeGames = list(games.Game.all().filter("winner =", None).filter("tourney_id =", tourney_id))
	activeGameIDs = dict([[g.key().id(), g] for g in activeGames])
	logging.info("Active games: " + str(activeGameIDs))

	#Throw all of the player IDs that are in these ongoing games into a dictionary
	playerIDsInGames = dict([[gp.playerID, gp] for gp in games.GamePlayer.all().filter("tourney_id =", tourney_id) if gp.gameID in activeGameIDs])

	#Find all players who aren't in the dictionary (and therefore aren't in any games) and also have not left the CLOT (isParticipating is true)
	allPlayers = players.Player.all().filter("tourney_id =", tourney_id)
	
	all_players_vec = [p for p in allPlayers]
	logging.info("all_players_vec: ")
	logging.info(all_players_vec)
	all_players_keys_ids_vec = [p.key().id()  for p in allPlayers]
	logging.info("all_players_keys_ids_vec: " + str(all_players_keys_ids_vec))
	player_ids_in_games_vec = [p for p in playerIDsInGames]
	logging.info("player_ids_in_games_vec: " + str(player_ids_in_games_vec))
	
	playersNotInGames = [p for p in allPlayers if p.isParticipating and p.key().id() not in playerIDsInGames]
	logging.info("Players not in games: ")
	logging.info(playersNotInGames)

	#------------------------
	#now pair up players who are not in games.  IF they have not played each otehr yet.

	#get the head-to-head matrix, so we can see who has played who
	head_to_head_biggermat, head_to_head_2d = new_utility_functions.getHeadToHeadTable(tourney_id)
	##logging.info('head_to_head_2d:')
	##logging.info(head_to_head_2d)

	#
	the_ids = deepcopy(head_to_head_biggermat[0][1:])
	logging.info('the_ids:')
	logging.info(the_ids)

	#Randomize the order
	random.shuffle(playersNotInGames)

	#loop over all possible pairs, and pair IF they have not played each other yet
	paired_yet = [False]*len(playersNotInGames)
	list_for_pairing = []
	for i in range(0,len(playersNotInGames)-1):
		if not paired_yet[i]:
			pi = playersNotInGames[i]
			pi_id = int(pi.player_id)
			pi_index = the_ids.index(pi_id)  #find where in the head-to-head matrix this player is.
			
			logging.info('pi:')
			logging.info(pi)
			logging.info(pi_id)
			logging.info(pi_index)
			
			for j in range(i+1,len(playersNotInGames)):
				if (not paired_yet[j]) and (not paired_yet[i]):
					pj = playersNotInGames[j]
					pj_id = int(pj.player_id)
					pj_index = the_ids.index(pj_id)   #find where in the head-to-head matrix this player is.
					
					logging.info('pj:')
					logging.info(pj)
					logging.info(pj_id)
					logging.info(pj_index)
			
					if (head_to_head_2d[pi_index][pj_index][0]==0) and (head_to_head_2d[pj_index][pi_index][0]==0):  
						#they have not played each other.
						#so match them.
						paired_yet[i] = True
						paired_yet[j] = True
						list_for_pairing.append(pi)
						list_for_pairing.append(pj)
						logging.info('paired '+str(pi)+' '+str(pj))

	##debug
	logging.info("new player order is: ")
	logging.info(list_for_pairing)
	for pair in clot.pairs(list_for_pairing):
		logging.info(pair)
	##end of debug

	#The template ID defines the settings used when the game is created.  You can create your own template on warlight.net and enter its ID here
	templateID = main.getTemplateID(tourney_id)

	#Create a game for everyone not in a game.
	gamesCreated = [games.createGame(pair, templateID, tourney_id) for pair in clot.pairs(list_for_pairing)]
	logging.info("Created games " + str(gamesCreated))
	
	if (len(activeGames)==0) and (len(list_for_pairing)==0):
		if main.isTourneyInPlay(tourney_id):
			#tourney is in play, but no games are going on, and we found no games we could create.
			#so the tourney is over
			main.endTourney(tourney_id)
			logging.info('')
			logging.info('all games have been played, so TOURNAMENT IS OVER !!!!!!!!!!!!!!')
			logging.info('')
Пример #4
0
def index_new(request,tourney_id):
	"""Request / """
	logging.info('in index_new(' +str(tourney_id)+ ')')

	tourney_id = int(tourney_id)
	logging.info('tourney_id = '+str(tourney_id))
	tourney_clotconfig = main.getClotConfig(tourney_id)

	if not main.doesTourneyExist(tourney_id, tourney_clotconfig):
		logging.info('tourney does not exist, redirecting user to tourneys info instead')
		return shortcuts.render_to_response('tourney_does_not_exist.html' )

	#arrange players by rank
	the_players = players.Player.all().filter("tourney_id =", tourney_id)#.run(batch_size=1000)
	the_players = sorted(the_players, key=lambda z: z.currentRank)

	gamePlayers = main.group(games.GamePlayer.all().filter("tourney_id =", tourney_id), lambda z: z.gameID)  #.run(batch_size=1000)

	#arrange games by reverse of created date
	the_games = games.Game.all().filter("tourney_id =", tourney_id)#.run(batch_size=1000)
	the_games = sorted(the_games, key=lambda z: z.dateCreated, reverse=True)
	#for game in the_games:
	#	logging.info('game: '+str(game))
	#	logging.info('game.winningTeamName = '+str(game.winningTeamName))


	#do the head-to-head table
	biggermat, head_to_head_2d = new_utility_functions.getHeadToHeadTable(tourney_id)
	biggermat_str = deepcopy(biggermat)
	for i in range(1,len(biggermat_str)):
		for j in range(1,len(biggermat_str[i])):
			if i==j:
				biggermat_str[i][j] = "---"
			else:
				biggermat_str[i][j] = str(biggermat_str[i][j][0]) + "-" + str(biggermat_str[i][j][1])

	#see if players are gated
	players_gated_string = "players may join or leave"
	if main.arePlayersGated(tourney_id, tourney_clotconfig):
		players_gated_string = "players may NOT join or leave"

	#get tourney_status_string
	tourney_status_string = 'Tourney Not Yet Started'
	if main.isTourneyInPlay(tourney_id, tourney_clotconfig):
		if str(main.getTourneyType(tourney_id, tourney_clotconfig)) == 'swiss':
			tourney_status_string = 'Tourney In Progress.  Round '+str(main.getRoundNumber(tourney_id, tourney_clotconfig))+' of '+str(main.getNumRounds(tourney_id, tourney_clotconfig))
		else:
			tourney_status_string = 'Tourney In Progress.'
	elif main.hasTourneyFinished(tourney_id, tourney_clotconfig):
		winner = the_players[0]
		winner_name = winner.name
		tourney_status_string = 'Tourney has finished.  Congratulations to '+str(winner_name)+'!'

	minNumPlayersString= 'minNumPlayers: '+str(main.getMinimumNumberOfPlayers(tourney_id, tourney_clotconfig))
	maxNumPlayersString= 'maxNumPlayers: '+str(main.getMaximumNumberOfPlayers(tourney_id, tourney_clotconfig))
	starttimeString = 'starttime will be:  '+str(main.getStarttime(tourney_id, tourney_clotconfig))+'    provided we have minimum number of players.'
	currentTimeString = 'current time =     '+str(main.getCurrentTime())
	tourney_type_string = str(main.getTourneyType(tourney_id, tourney_clotconfig)) + ' tourney'
	how_long_you_have_to_join_games_string = 'You have '+str(main.getHowLongYouHaveToJoinGames(tourney_id, tourney_clotconfig))+' minutes to join your auto-created games.  After that you may lose that game!!'
	template_id = main.getTemplateID(tourney_id, tourney_clotconfig)

	#things for specific tourney types
	if main.getTourneyType(tourney_id, tourney_clotconfig)=='swiss':
		swiss_games_info_table = tournament_swiss.getTourneyRoundsAndGameInfo(tourney_id)
	else:
		swiss_games_info_table = 0
	#end of things for specific tourney types

	return shortcuts.render_to_response('tourney_home.html',{'players': the_players, 'config': tourney_clotconfig, 'games': the_games, 
			'biggermat':biggermat_str,
			'players_gated_string':players_gated_string,
			'minNumPlayersString':minNumPlayersString,
			'maxNumPlayersString':maxNumPlayersString,
			'tourney_status_string':tourney_status_string,
			'starttimeString':starttimeString,
			'currentTimeString':currentTimeString,
			'tourney_type_string':tourney_type_string,
			'how_long_you_have_to_join_games_string':how_long_you_have_to_join_games_string,
			'template_title_string':'Game Template',
			'template_id':template_id,
			'swiss_games_info_table':swiss_games_info_table,
			'join_url':'/tourneys/'+str(tourney_id)+'/join',
			'leave_url':'/tourneys/'+str(tourney_id)+'/leave',
			'tourney_id':str(tourney_id)
			})