-
Notifications
You must be signed in to change notification settings - Fork 151
/
app.js
203 lines (185 loc) · 6.77 KB
/
app.js
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
const express = require('express');
const admin = require('firebase-admin');
const app = express();
// TODO: Uncomment out line 13
// Refer to Picture Example Folder for help for below instructions. (hit the gear for settings, click projecgt settings, then click service accounts)
// In your firebase project settings it will give you an option to "create service account".
// This generates a service account json file. Download it, and put the file in this project.
// Enter the path to your service account json file below where it says "REPLACE_WITH_SERVICE_ACCOUNT"
// If you need more help with this step go here: https://firebase.google.com/docs/admin/setup
// const serviceAccount = require("./REPLACE_WITH_SERVICE_ACCOUNT.json");
// TODO: Uncomment out line 17-21
// Enter your database url from firebase where it says <DATABASE_NAME> below.
// Refer to picture for reference. It's the 2nd property.
// admin.initializeApp({
// credential: admin.credential.cert(serviceAccount),
// databaseURL: "https://<DATABASE_NAME>.firebaseio.com/"
// });
app.set('port', (process.env.PORT || 3001));
app.get('*', (req, res) => {
res.send('Madden Companion Exporter');
});
app.post('/:username/:platform/:leagueId/leagueteams', (req, res) => {
const db = admin.database();
const ref = db.ref();
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
const { leagueTeamInfoList: teams } = JSON.parse(body);
const {params: { username, leagueId }} = req;
teams.forEach(team => {
const teamRef = ref.child(`data/${username}/${leagueId}/teams/${team.teamId}`);
teamRef.set(team);
});
res.sendStatus(200);
});
});
app.post('/:username/:platform/:leagueId/standings', (req, res) => {
const db = admin.database();
const ref = db.ref();
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
const { teamStandingInfoList: teams } = JSON.parse(body);
const {params: { username, leagueId }} = req;
teams.forEach(team => {
const teamRef = ref.child(
`data/${username}/${leagueId}/teams/${team.teamId}`
);
teamRef.set(team);
});
res.sendStatus(200);
});
});
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
app.post(
'/:username/:platform/:leagueId/week/:weekType/:weekNumber/:dataType',
(req, res) => {
const db = admin.database();
const ref = db.ref();
const {
params: { username, leagueId, weekType, weekNumber, dataType },
} = req;
const basePath = `data/${username}/${leagueId}/`;
// "defense", "kicking", "passing", "punting", "receiving", "rushing"
const statsPath = `${basePath}stats`;
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
switch (dataType) {
case 'schedules': {
const weekRef = ref.child(
`${basePath}schedules/${weekType}/${weekNumber}`
);
const { gameScheduleInfoList: schedules } = JSON.parse(body);
weekRef.set(schedules);
break;
}
case 'teamstats': {
const { teamStatInfoList: teamStats } = JSON.parse(body);
teamStats.forEach(stat => {
const weekRef = ref.child(
`${statsPath}/${weekType}/${weekNumber}/${stat.teamId}/team-stats`
);
weekRef.set(stat);
});
break;
}
case 'defense': {
const { playerDefensiveStatInfoList: defensiveStats } = JSON.parse(body);
defensiveStats.forEach(stat => {
const weekRef = ref.child(
`${statsPath}/${weekType}/${weekNumber}/${stat.teamId}/player-stats/${stat.rosterId}`
);
weekRef.set(stat);
});
break;
}
default: {
const property = `player${capitalizeFirstLetter(
dataType
)}StatInfoList`;
const stats = JSON.parse(body)[property];
stats.forEach(stat => {
const weekRef = ref.child(
`${statsPath}/${weekType}/${weekNumber}/${stat.teamId}/player-stats/${stat.rosterId}`
);
weekRef.set(stat);
});
break;
}
}
res.sendStatus(200);
});
}
);
// ROSTERS
app.post('/:username/:platform/:leagueId/freeagents/roster', (req, res) => {
const db = admin.database();
const ref = db.ref();
const {
params: { username, leagueId, teamId }
} = req;
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
const { rosterInfoList } = JSON.parse(body);
const dataRef = ref.child(
`data/${username}/${leagueId}/freeagents`
);
const players = {};
rosterInfoList.forEach(player => {
players[player.rosterId] = player;
});
dataRef.set(players, error => {
if (error) {
console.log('Data could not be saved.' + error);
} else {
console.log('Data saved successfully.');
}
});
res.sendStatus(200);
});
});
app.post('/:username/:platform/:leagueId/team/:teamId/roster', (req, res) => {
const db = admin.database();
const ref = db.ref();
const {
params: { username, leagueId, teamId }
} = req;
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
const { rosterInfoList } = JSON.parse(body);
const dataRef = ref.child(
`data/${username}/${leagueId}/teams/${teamId}/roster`
);
const players = {};
rosterInfoList.forEach(player => {
players[player.rosterId] = player;
});
dataRef.set(players, error => {
if (error) {
console.log('Data could not be saved.' + error);
} else {
console.log('Data saved successfully.');
}
});
res.sendStatus(200);
});
});
app.listen(app.get('port'), () =>
console.log('Madden Data is running on port', app.get('port'))
);