def createGames(request, container): """This is called periodically to check for new games that need to be created. You should replace this with your own logic for how games are to be created. Right now, this function just randomly pairs up players who aren't in a game.""" #Retrieve all games that are ongoing activeGames = [g for g in container.games if g.winner is None] activeGameIDs = dict([[g.key.id(), g] for g in activeGames]) logging.info("Active games: " + unicode(activeGameIDs)) #Throw all of the player IDs that are in these ongoing games into a dictionary playerIDsInActiveGames = set(flatten([g.players for g in activeGames])) #Find all players who aren't any active games and also have not left the CLOT (isParticipating is true) playersNotInGames = [ container.players[p] for p in container.lot.playersParticipating if p not in playerIDsInActiveGames ] logging.info("Players not in games: " + ','.join([unicode(p) for p in playersNotInGames])) #Randomize the order random.shuffle(playersNotInGames) #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 = 251301 #Create a game for everyone not in a game. gamesCreated = [ createGame(request, container, pair, templateID) for pair in pairs(playersNotInGames) ] logging.info("Created games " + unicode(','.join([unicode(g) for g in gamesCreated])))
def createGames(request, container): """This is called periodically to check for new games that need to be created. You should replace this with your own logic for how games are to be created. Right now, this function just randomly pairs up players who aren't in a game.""" #Retrieve all games that are ongoing activeGames = [g for g in container.games if g.winner is None] activeGameIDs = dict([[g.key.id(), g] for g in activeGames]) logging.info("Active games: " + unicode(activeGameIDs)) #Throw all of the player IDs that are in these ongoing games into a dictionary playerIDsInActiveGames = set(flatten([g.players for g in activeGames])) #Find all players who aren't any active games and also have not left the CLOT (isParticipating is true) playersNotInGames = [container.players[p] for p in container.lot.playersParticipating if p not in playerIDsInActiveGames] logging.info("Players not in games: " + ','.join([unicode(p) for p in playersNotInGames])) #Randomize the order random.shuffle(playersNotInGames) #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 = 251301 #Create a game for everyone not in a game. gamesCreated = [createGame(request, container, pair, templateID) for pair in pairs(playersNotInGames)] logging.info("Created games " + unicode(','.join([unicode(g) for g in gamesCreated])))
def createGames(request, container): """This is called periodically to check for new games that need to be created. You should replace this with your own logic for how games are to be created. Right now, this function just randomly pairs up players who aren't in a game.""" # Read configuration settings for ladder readConfigForMDLadder() #Recent games. All players who have played each other recently, will not be paired together. recentGames = [] #Active games also count as recent. activeGames = [] for g in container.games: if g.dateEnded is None: recentGames.append(g) activeGames.append(g) else: delta = (datetime.now() - g.dateEnded) timeElapsed = delta.total_seconds() if int(timeElapsed) < timeBetweenGamesInHours * 60 * 60 : recentGames.append(g) #Retrieve player pairs from all games that are ongoing activeGameIDs = dict([[g.key.id(), g] for g in activeGames]) logging.info("Active games: " + unicode(activeGameIDs)) #Throw all of the player IDs that are in these ongoing games into a list playerIDsInActiveGames = flatten([g.players for g in activeGames]) # Map of {player:gameCount} activeGameCountForPlayer = {} for p in playerIDsInActiveGames: activeGameCountForPlayer[p] = activeGameCountForPlayer.get(p, 0) + 1 # Map of {player:NumOfGamesToBeAllotted} playersToBeAllocatedNewGames = {} for p in container.lot.playersParticipating: player = container.players[p] newGames = player.numberOfGamesAtOnce - activeGameCountForPlayer.get(p, 0) if newGames < 0 : newGames = 0 playersToBeAllocatedNewGames[p] = newGames logging.info("Games to be allotted: " + str(playersToBeAllocatedNewGames)) #Create a game for everyone not in a game. #From a list of templates, a random one is picked for each game gamesCreated = [] for pair in createPlayerPairs(container.lot.playerRanks, playersToBeAllocatedNewGames, recentGames): chosenTemplateId = int(choice(templates)) overriddenBonuses = getOverriddenBonuses(chosenTemplateId) players = [container.players[p] for p in pair] g = createGame(request, container, players, chosenTemplateId, overriddenBonuses) gamesCreated.append(g) logging.info("Created games " + unicode(','.join([unicode(g) for g in gamesCreated])))
def createGames(request, container): """This is called periodically to check for new games that need to be created. You should replace this with your own logic for how games are to be created. Right now, this function just randomly pairs up players who aren't in a game.""" # Read configuration settings for ladder readConfigForMDLadder() # Recent games. All players who have played each other recently, will not be paired together. recentGames = [] # Active games also count as recent. activeGames = [] for g in container.games: if g.dateEnded is None: recentGames.append(g) activeGames.append(g) else: delta = datetime.now() - g.dateEnded timeElapsed = delta.total_seconds() if int(timeElapsed) < timeBetweenGamesInHours * 60 * 60: recentGames.append(g) # Retrieve player pairs from all games that are ongoing activeGameIDs = dict([[g.key.id(), g] for g in activeGames]) logging.info("Active games: " + unicode(activeGameIDs)) # Throw all of the player IDs that are in these ongoing games into a list playerIDsInActiveGames = flatten([g.players for g in activeGames]) # Map of {player:gameCount} activeGameCountForPlayer = {} for p in playerIDsInActiveGames: activeGameCountForPlayer[p] = activeGameCountForPlayer.get(p, 0) + 1 # Map of {player:NumOfGamesToBeAllotted} playersToBeAllocatedNewGames = {} for p in container.lot.playersParticipating: player = container.players[p] newGames = player.numberOfGamesAtOnce - activeGameCountForPlayer.get(p, 0) if newGames < 0: newGames = 0 playersToBeAllocatedNewGames[p] = newGames logging.info("Games to be allotted: " + str(playersToBeAllocatedNewGames)) # Create a game for everyone not in a game. # From a list of templates, a random one is picked for each game gamesCreated = [] for pair in createPlayerPairs(container.lot.playerRanks, playersToBeAllocatedNewGames, recentGames): chosenTemplateId = int(choice(templates)) overriddenBonuses = getOverriddenBonuses(chosenTemplateId) players = [container.players[p] for p in pair] g = createGame(request, container, players, chosenTemplateId, overriddenBonuses) gamesCreated.append(g) logging.info("Created games " + unicode(",".join([unicode(g) for g in gamesCreated])))
def createGames_Swiss(tourney_id, tourney_clotconfig): """This is called periodically to check if a round has finished. If so, new games are created. the 'swiss' part is that we match players based on ranking - we match players with similar rankings who have not yet played each other. if there are an odd number of players, then someone randomly misses a game (and we prefer players who have not yet missed a game) """ logging.info('') logging.info('in createGames_Swiss()') if main.hasTourneyFinished(tourney_id, tourney_clotconfig): logging.info('swiss tourney has finished') return #Retrieve all games that are ongoing activeGames = list(games.Game.all().filter("winner =", None).filter("tourney_id =", tourney_id)) ###.run(batch_size=1000)) activeGameIDs = dict([[g.key().id(), g] for g in activeGames]) logging.info("Active games: " + str(activeGameIDs)) if activeGames: logging.info('games still in progress. cannot start next round until these games finish.') else: logging.info('no games in progress. so we move on to the next round.') if main.getRoundNumber(tourney_id, tourney_clotconfig) == main.getNumRounds(tourney_id, tourney_clotconfig): main.endTourney(tourney_id, tourney_clotconfig) logging.info('') logging.info('all rounds have been played, so TOURNAMENT IS OVER !!!!!!!!!!!!!!') logging.info('') return players_ids_matched_list = getMatchedList_Swiss(tourney_id) if not players_ids_matched_list: main.endTourney(tourney_id, tourney_clotconfig) logging.info('') logging.info('seems everyone has played everyone else, so TOURNAMENT IS OVER !!!!!!!!!!!!!!') logging.info('') return players_ids_names_dict = dict([[gp.player_id, gp] for gp in players.Player.all().filter("tourney_id =", tourney_id)]) ###.run(batch_size=1000)]) logging.info('players_ids_names_dict') logging.info(players_ids_names_dict) players_names_matched_list = [players_ids_names_dict[i] for i in players_ids_matched_list] #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, tourney_clotconfig) #Create a game for everyone not in a game. gamesCreated = [games.createGame(pair, templateID, tourney_id) for pair in clot.pairs(players_names_matched_list)] logging.info("Created games " + str(gamesCreated)) main.incrementRoundNumber(tourney_id, tourney_clotconfig) logging.info("\n ------------------------------------ \n swiss tourney round " + str(main.getRoundNumber(tourney_id, tourney_clotconfig))+ " starting. \n ---------------------------") logging.info('')
def createGames_RandomMatchup(tourney_id): """This is called periodically to check for new games that need to be created. You should replace this with your own logic for how games are to be created. Right now, this function just randomly pairs up players who aren't in a game.""" #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) #Randomize the order random.shuffle(playersNotInGames) #debug random.shuffle(playersNotInGames) logging.info("new player order is: ") logging.info(playersNotInGames) for pair in clot.pairs(playersNotInGames): logging.info(pair) random.shuffle(playersNotInGames) logging.info("new player order is: ") logging.info(playersNotInGames) for pair in clot.pairs(playersNotInGames): 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(playersNotInGames)] logging.info("Created games " + str(gamesCreated))
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('')