Example #1
0
def checkInProgressGames(tourney_id):
	"""This is called periodically to look for games that are finished.  If we find
	a finished game, we record the winner"""

	#Find all games that we think aren't finished
	activeGames = games.Game.all().filter("winner =", None).filter("tourney_id =", tourney_id)

	for g in activeGames:
		#call WarLight's GameFeed API so that it can tell us if it's finished or not
		apiret = main.hitapi('/API/GameFeed?GameID=' + str(g.wlnetGameID), {})
		data = json.loads(apiret)
		state = data.get('state', 'err')

		if state == 'err': raise Exception("GameFeed API failed.  Message = " + data.get('error', apiret))

		if state == 'Finished':
			#It's finished. Record the winner and save it back.
			winner = findWinner(data,tourney_id)
			logging.info('Identified the winner of game ' + str(g.wlnetGameID) + ' is ' + str(winner))
			loser = findLoser(data,tourney_id)
			logging.info('Identified the loser of game ' + str(g.wlnetGameID) + ' is ' + str(loser))
			g.winner = winner.key().id()
			g.loser = loser.key().id()
			#g.winningTeamName = 'bob' #winner.key().name()
			g.dateEnded = datetime.datetime.now()
			g.save()
		else:
			#It's still going.
			
			#terminate games that have not started for a long time. 
			if state == 'WaitingForPlayers':
				latest_joining_time = g.dateCreated +datetime.timedelta(0,60*main.getHowLongYouHaveToJoinGames(tourney_id)) 
				if datetime.datetime.now() > latest_joining_time:
					logging.info('game ' + str(g.wlnetGameID) + ' has taken too long to start')
					#game has taken too long to start.
					
					game_id = g.key().id()
					logging.info('game_id='+str(game_id))
					for gp in games.GamePlayer.all().filter("tourney_id =", tourney_id):
						logging.info(gp)
					game_players = games.GamePlayer.all().filter("gameID =", game_id).filter("tourney_id =", tourney_id)
					players_ids = [p.playerID for p in game_players]
					logging.info(players_ids)
					random.shuffle(players_ids)
					g.winner = players_ids[0]
					g.loser = players_ids[1]
					g.legitimateGame = False
					g.dateEnded = datetime.datetime.now()
					g.save()
			
			
			logging.info('Game ' + str(g.wlnetGameID) + ' is not finished, state=' + state + ', numTurns=' + data['numberOfTurns']+' ----------------------------------------------------------------------------')
Example #2
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)
			})
Example #3
0
def checkInProgressGames(tourney_id, tourney_clotconfig):
	"""This is called periodically to look for games that are finished.  If we find
	a finished game, we record the winner"""

	#Find all games that we think aren't finished
	activeGames = games.Game.all().filter("winner =", None).filter("tourney_id =", tourney_id)#.run(batch_size=1000)

	for g in activeGames:
		#call WarLight's GameFeed API so that it can tell us if it's finished or not
		apiret = main.hitapi('/API/GameFeed?GameID=' + str(g.wlnetGameID), {})
		data = json.loads(apiret)
		state = data.get('state', 'err')
		
		#logging.info('+++++++++++++++++++++++++++++++++++++++++++')
		#logging.info('data: ' + str(data) + ' ')
		#logging.info('+++++++++++++++++++++++++++++++++++++++++++')
		
		player_data = data.get('players')
		
		#logging.info('player_data: ' + str(player_data) + ' ')
		#logging.info('+++++++++++++++++++++++++++++++++++++++++++')

		if state == 'err': raise Exception("GameFeed API failed.  Message = " + data.get('error', apiret))

		if state == 'Finished':
			#It's finished. Record the winner and save it back.
			winner = findWinner(data,tourney_id)
			logging.info('Identified the winner of game ' + str(g.wlnetGameID) + ' is ' + str(winner))
			loser = findLoser(data,tourney_id)
			logging.info('Identified the loser of game ' + str(g.wlnetGameID) + ' is ' + str(loser))
			g.winner = winner.key().id()
			g.loser = loser.key().id()
			g.dateEnded = datetime.datetime.now()
			g.save()
		else:
			#It's still going.
			logging.info('Game ' + str(g.wlnetGameID) + ' is not finished, state=' + state + ', numTurns=' + data['numberOfTurns']+' ----------------------------------------------------------------------------')
			
			#terminate games that have not started for a long time. 
			if state == 'WaitingForPlayers':
				latest_joining_time = g.dateCreated +datetime.timedelta(0,60*main.getHowLongYouHaveToJoinGames(tourney_id, tourney_clotconfig)) 
				if datetime.datetime.now() > latest_joining_time:
					logging.info('game ' + str(g.wlnetGameID) + ' has taken too long to start')
					logging.info('data: ' + str(data) + ' ')

					#find who Joined and who did not.
					good_player_wlids = []
					bad_player_wlids = []
					logging.info('')
					logging.info('players are:')
					for p in player_data:
						logging.info(p)
						if p['state']=='Playing':                  #only 'Playing' is the acceptable status - anything else means you did not join (ie ignored, or declined). 
							good_player_wlids.append(int(p['id']))
						else:
							bad_player_wlids.append(int(p['id']))
					
					#shuffle randomly in case we need to select winner at random
					random.shuffle(good_player_wlids)
					random.shuffle(bad_player_wlids)
					
					#set the winner and loser
					if len(good_player_wlids)==1 and len(bad_player_wlids)==1:
						logging.info('1 player Joined, the other did not ')
						g.winner = findPlayerID_FromWarlightID(good_player_wlids[0],tourney_id)
						g.loser = findPlayerID_FromWarlightID(bad_player_wlids[0],tourney_id)
					elif len(bad_player_wlids)==2:
						logging.info('neither player Joined, winner/loser will be selected randomly')
						g.winner = findPlayerID_FromWarlightID(bad_player_wlids[0],tourney_id)
						g.loser = findPlayerID_FromWarlightID(bad_player_wlids[1],tourney_id)
					else:
						assert False   #must not occur
					g.dateEnded = datetime.datetime.now()
					
					logging.info('player ' + str(g.winner) + ' won')
					logging.info('player ' + str(g.loser) + ' lost')
					g.save()