-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.rb
129 lines (93 loc) · 2.36 KB
/
server.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
require 'sinatra'
require 'csv'
require 'sinatra/reloader'
def get_all_players_on_a_team(team_name, data_total)
players_on_team = []
data_total.each do |player|
if player["team"] == team_name
players_on_team << player
end
end
players_on_team
end
def get_all_players_by_position(position, data_total)
players_in_each_position = []
data_total.each do |player|
if player["position"] == position
players_in_each_position << player
end
end
players_in_each_position
end
def return_team_array(team_name,data_sep_by_team)
data_sep_by_team.each do |team_array|
if team_array.to_s.include?(team_name.to_s)
return team_array
end
end
end
def return_position_array(position, players_in_each_position)
players_in_each_position.each do |position_array|
if position_array.to_s.include?(position.to_s)
return position_array
end
end
end
def return_all_players_on_a_team(team)
players = []
CSV.foreach('lackp_starting_rosters.csv', headers:true) do |row|
if row["team"] == team
temp_player_array = []
temp_player_array << row["first_name"]
temp_player_array << row["last_name"]
temp_player_array << row["position"]
players << temp_player_array
end
end
players
end
data_total = []
CSV.foreach('lackp_starting_rosters.csv', headers:true) do |row|
data_total << row
end
teams = []
data_total.each do |row|
teams << row["team"]
end
teams.uniq!
data_sep_by_team = []
teams.each do |team|
data_sep_by_team << get_all_players_on_a_team(team, data_total)
end
positions = []
data_total.each do |row|
positions << row["position"]
end
positions.uniq!
players_in_each_position = []
positions.each do |position|
players_in_each_position << get_all_players_by_position(position, data_total)
end
teams_hash = {}
teams.each do |team_name|
teams_hash[team_name] = return_team_array(team_name,data_sep_by_team)
end
positions_hash = {}
positions.each do |position|
positions_hash[position] = return_position_array(position, players_in_each_position)
end
get '/' do
@teams = teams
@positions = positions
erb :index
end
get '/team/:team' do
@team = params[:team]
@team_players = return_all_players_on_a_team(@team)
erb :teams
end
get '/position/:position' do
@position = params[:position]
@players_in_each_position = positions_hash[@position]
erb :positions
end