def go(request): """Create a player. GET shows a blank form, POST processes it.""" logging.info('in join.go') form = JoinForm(data=request.POST or None) #now deal with the form etc if not request.POST: return shortcuts.render_to_response('join.html', {'form': form}) if not form.is_valid(): return shortcuts.render_to_response('join.html', {'form': form}) #see if we are letting more players join. tourney_id = int(form.clean_data['tourney_id']) tourney_clotconfig = main.getClotConfig(tourney_id)#.run(batch_size=1000) if not tourney_clotconfig: form.errors['tourney_id'] = 'tourney_id is invalid.' return shortcuts.render_to_response('join.html', {'form': form}) players_are_gated_q = False if main.arePlayersGated(tourney_id, tourney_clotconfig): players_are_gated_q = True logging.info('players_are_gated_q = '+str(players_are_gated_q)) return http.HttpResponseRedirect('/players_are_gated') if players.numPlayersParticipating(tourney_id) >= main.getMaximumNumberOfPlayers(tourney_id, tourney_clotconfig): logging.info('too many players') return http.HttpResponseRedirect('/cannot_join') inviteToken = form.clean_data['inviteToken'] #Call the warlight API to get the name, color, and verify that the invite token is correct apiret = main.hitapi('/API/ValidateInviteToken', { 'Token': inviteToken }) if not "tokenIsValid" in apiret: form.errors['inviteToken'] = 'The supplied invite token is invalid. Please ensure you copied it from WarLight.net correctly.' return shortcuts.render_to_response('join.html', {'form': form}) tourney_password = str(form.clean_data['tourney_password']) if main.getIfRequirePasswordToJoin(tourney_id, tourney_clotconfig): if tourney_password != main.getTourneyPassword(tourney_id, tourney_clotconfig): form.errors['tourney_password'] = '******' return shortcuts.render_to_response('join.html', {'form': form}) #Ensure this invite token doesn't already exist existing = players.Player.all().filter('inviteToken =', inviteToken).filter("tourney_id =", tourney_id).get() if existing: #If someone tries to join when they're already in the DB, just set their isParticipating flag back to true existing.isParticipating = True existing.save() return http.HttpResponseRedirect('tourneys/' + str(tourney_id) + '/player/' + str(existing.key().id())) data = json.loads(apiret) player_name = data['name'] if type(data['name']) is unicode: logging.info('dealing with unicode player name ...') player_name = player_name.encode('ascii','ignore') #this deals with special characters that would mess up our code, by removing them. logging.info('player_name:') logging.info(player_name) logging.info('player-name looks ok or not?') player = players.Player(inviteToken=inviteToken, name=player_name, color=data['color'], isMember=data['isMember'].lower() == 'true') if main.getIsMembersOnly(tourney_id, tourney_clotconfig) and not player.isMember: form.errors['inviteToken'] = 'This site only allows members to join. See the Membership tab on WarLight.net for information about memberships.' return shortcuts.render_to_response('join.html', {'form': form}) player.put() player.player_id = str(player.key().id()) player.tourney_id = tourney_id player.save() logging.info("Created player") logging.info(player) return http.HttpResponseRedirect('tourneys/' + str(tourney_id) + '/player/' + str(player.key().id()))
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) })