Пример #1
0
def js_player_dollar_value(player_id, dataset, is_pitcher):
	stats = tnpldraft.draftapp.stats.PopulationStats()
	stats.UpdateStats(dataset)
	player = stats.get_player(player_id)
	if player is None:
		raise Exception('No player found')
	elif player.get_position() == 'P':
		return {
			'ERA': player.era_dollar(stats),
			'WHIP': player.whip_dollar(stats),
			'W': player.w_dollar(stats),
			'K': player.k_dollar(stats),
			'S': player.s_dollar(stats),
			'total': player.adjusted_dollar(stats)
		}
	else:
		return {
			'BA': player.h_dollar(stats),
			'R': player.r_dollar(stats),
			'HR': player.hr_dollar(stats),
			'RBI': player.rbi_dollar(stats),
			'SB': player.sb_dollar(stats),
			'total': player.adjusted_dollar(stats)
		}
Пример #2
0
def teams(request):
	if len(request.GET) > 0:
		dataset_form = DataSetForm(request.GET)
	else:
		dataset_form = DataSetForm({'dataset': '2013'})

	if not dataset_form.is_valid():
		return HttpResponseBadRequest()

	stats = tnpldraft.draftapp.stats.PopulationStats()
	stats.UpdateStats(dataset_form.cleaned_data['dataset'])

	cursor = connection.cursor()
	cursor.execute('''
		SELECT
			draftapp_tnplteam.id,
			draftapp_tnplteam.name,
			Master.lahmanid,
			draftapp_tnplownership.salary
		FROM
			draftapp_tnplteam
		LEFT OUTER JOIN
			draftapp_tnplownership
		ON (draftapp_tnplownership.team_id = draftapp_tnplteam.id)
		LEFT OUTER JOIN
			Master
		USING(playerID);
	''')

	teams = {}
	for row in cursor:
		team_id = row[0]
		team_name = row[1]
		player_id = row[2]
		if row[3] is not None:
			salary = float(row[3])
		else:
			salary = None

		if teams.has_key(team_id):
			team = teams[team_id]
		else:
			team = {
				'hitter_totals': {
					'BA_dollar': 0.0,
					'R_dollar': 0.0,
					'HR_dollar': 0.0,
					'RBI_dollar': 0.0,
					'SB_dollar': 0.0,
					'salary': 0.0,
					'num_players': 0,
				},
				'pitcher_totals': {
					'ERA_dollar': 0.0,
					'WHIP_dollar': 0.0,
					'W_dollar': 0.0,
					'K_dollar': 0.0,
					'S_dollar': 0.0,
					'salary': 0.0,
					'num_players': 0,
				},
				'unknown_totals': {
					'salary': 0.0,
					'num_players': 0,
				},
				'name': team_name,
			}
			teams[team_id] = team
		if player_id is not None and salary is not None:
			player = stats.get_player(player_id)
			if player is None:
				u_totals = team['unknown_totals']
				u_totals['salary'] += salary
				u_totals['num_players'] += 1
			elif player.get_position() == 'P':
				p_totals = team['pitcher_totals']
				p_totals['ERA_dollar'] += player.era_dollar(stats)
				p_totals['WHIP_dollar'] += player.whip_dollar(stats)
				p_totals['W_dollar'] += player.w_dollar(stats)
				p_totals['K_dollar'] += player.k_dollar(stats)
				p_totals['S_dollar'] += player.s_dollar(stats)
				p_totals['salary'] += salary
				p_totals['num_players'] += 1
			else:
				h_totals = team['hitter_totals']
				h_totals['BA_dollar'] += player.h_dollar(stats)
				h_totals['R_dollar'] += player.r_dollar(stats)
				h_totals['HR_dollar'] += player.hr_dollar(stats)
				h_totals['RBI_dollar'] += player.rbi_dollar(stats)
				h_totals['SB_dollar'] += player.sb_dollar(stats)
				h_totals['salary'] += salary
				h_totals['num_players'] += 1
			
	
	return render_to_response('teams.html',
				  {'teams': teams,
				   'teams_js': json.dumps(teams),
				   'dataset_form': dataset_form},
				  context_instance=RequestContext(request))
Пример #3
0
def team(request, team_id):
	team = get_object_or_404(TNPLTeam, id=team_id)
	name = team.name

	if len(request.GET) > 0:
		dataset_form = DataSetForm(request.GET)
	else:
		dataset_form = DataSetForm({'dataset': '2013'})

	if not dataset_form.is_valid():
		return HttpResponseBadRequest()

	stats = tnpldraft.draftapp.stats.PopulationStats()
	stats.UpdateStats(dataset_form.cleaned_data['dataset'])

	cursor = connection.cursor()
	cursor.execute('''
		SELECT
			Master.lahmanid,
			Master.nameFirst,
			Master.nameLast,
			draftapp_tnplownership.salary
		from draftapp_tnplownership
		JOIN Master USING(playerID)
		WHERE team_id = %s;''',
		(team.id,))

	salary_spent = 0.0
	num_players = 0

	hitters = []
	pitchers = []
	unknown = []
	for row in cursor:
		player_id = row[0]
		salary = float(row[3])
		player_name = '%s %s' % (row[1], row[2])
		num_players += 1
		salary_spent += salary
		player = stats.get_player(player_id)
		if player is None:
			x = {
				'id': player_id,
				'name': player_name,
				'salary': salary,
				'positions': set('U')
			}
			unknown.append(x)
		elif player.get_position() == 'P':
			x = {
				'id': player_id,
				'name': player_name,
				'salary': salary,
				'IP': player.ipouts / 3.0,
				'ER': player.er,
				'ERA': player.era(),
				'ERA_dollar': player.era_dollar(stats),
				'H': player.h,
				'BB': player.bb,
				'WHIP': player.whip(),
				'WHIP_dollar': player.whip_dollar(stats),
				'W': player.w,
				'W_dollar': player.w_dollar(stats),
				'K': player.k,
				'K_dollar': player.k_dollar(stats),
				'S': player.s,
				'S_dollar': player.s_dollar(stats),
				'POS': 'P',
				'POS_dollar': player.pos_dollar(stats),
				'total_dollar': player.adjusted_dollar(stats),
				'positions': set(['P']),
			}
			pitchers.append(x)
		else:
			x = {
				'id': player_id,
				'name': player_name,
				'salary': salary,
				'AB': player.ab,
				'H': player.h,
				'BA': player.ba(),
				'BA_dollar': player.h_dollar(stats),
				'R': player.r,
				'R_dollar': player.r_dollar(stats),
				'HR': player.hr,
				'HR_dollar': player.hr_dollar(stats),
				'RBI': player.rbi,
				'RBI_dollar': player.rbi_dollar(stats),
				'SB': player.sb,
				'SB_dollar': player.sb_dollar(stats),
				'POS': player.get_position(),
				'POS_dollar': player.pos_dollar(stats),
				'total_dollar': player.adjusted_dollar(stats),
				'positions': player.positions,
			}
			hitters.append(x)

	hitter_totals = {}
	for attr in ('salary', 'AB', 'H', 'BA_dollar', 'R', 'R_dollar', 'HR',
		     'HR_dollar', 'RBI', 'RBI_dollar', 'SB', 'SB_dollar',
		     'POS_dollar', 'total_dollar'):
		hitter_totals[attr] = sum([x[attr] for x in hitters])
	if hitter_totals['AB'] == 0:
		hitter_totals['BA'] = 0.0
	else:
		hitter_totals['BA'] = float(hitter_totals['H']) / hitter_totals['AB']

	pitcher_totals = {}
	for attr in ('salary', 'IP', 'ER', 'ERA_dollar', 'H', 'BB',
		     'WHIP_dollar', 'W', 'W_dollar', 'K', 'K_dollar', 'S',
		     'S_dollar', 'POS_dollar', 'total_dollar'):
		pitcher_totals[attr] = sum([x[attr] for x in pitchers])
	if pitcher_totals['IP'] == 0:
		pitcher_totals['ERA'] = 0.0
		pitcher_totals['WHIP'] = 0.0
	else:
		games_pitched = pitcher_totals['IP'] / 9.0
		pitcher_totals['ERA'] = pitcher_totals['ER'] / games_pitched
		walks_and_hits = pitcher_totals['H'] + pitcher_totals['BB']
		pitcher_totals['WHIP'] = walks_and_hits / pitcher_totals['IP']

	unknown_totals = {
		'salary': sum([x['salary'] for x in unknown])
	}
	

	if num_players >= tnpldraft.draftapp.stats.NUM_PLAYERS:
		salary_remaining = 0.0
		positions_remaining = 0
		avg_salary = 0.0
	else:
		salary_remaining = tnpldraft.draftapp.stats.SALARY_PER_TEAM - salary_spent
		positions_remaining = tnpldraft.draftapp.stats.NUM_PLAYERS - num_players
		avg_salary = salary_remaining / positions_remaining

	# Determine what positions are still draftable
	valid_lineups = dict([(x, []) for x in ('P', 'C', '1B', '2B', '3B', 'SS', 'MI', 'CI', 'OF', 'U')])
	def get_all_lineups(lineup, players):
		if len(players) == 0:
			lineup_copy = lineup.copy()
			for k,v in lineup_copy.iteritems():
				lineup_copy[k] = v[:]
			for pos in [k for (k,v) in lineup_copy.iteritems() if len(v) < tnpldraft.draftapp.stats.POS_COUNT[k]]:
				valid_lineups[pos].append(lineup_copy)
			return
		for pos in players[0]['positions']:
			if tnpldraft.draftapp.stats.POS_COUNT[pos] - len(lineup[pos]) == 0:
				continue
			lineup[pos].append(players[0])
			get_all_lineups(lineup, players[1:])
			lineup[pos].pop()

	empty_lineup = dict([(x, []) for x in ('P', 'C', '1B', '2B', '3B', 'SS', 'MI', 'CI', 'OF', 'U')])
	get_all_lineups(empty_lineup, pitchers + hitters)
	draftable_positions = set([k for (k,v) in valid_lineups.iteritems() if len(v) > 0])
	print "Draftable positions: ", draftable_positions

	return render_to_response('team.html', {'name': name,
						'team_id': team.id,
						'salary_spent': salary_spent,
						'salary_remaining': salary_remaining,
						'num_players': num_players,
						'positions_remaining': positions_remaining,
						'avg_salary': avg_salary,
						'hitters': hitters,
						'hitter_totals': hitter_totals,
						'hitter_totals_js': json.dumps(hitter_totals),
						'pitchers': pitchers,
						'pitcher_totals': pitcher_totals,
						'pitcher_totals_js': json.dumps(pitcher_totals),
						'unknown': unknown,
						'unknown_totals': unknown_totals,
						'dataset_form': dataset_form,
						'draftable_positions': draftable_positions,
					       }, context_instance=RequestContext(request))