Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Main #31

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
207 changes: 105 additions & 102 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,72 +1,76 @@
const express = require('express');
const admin = require('firebase-admin');
const bodyParser = require('body-parser');

const app = express();

// let cacheRosterData = null;

// 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");
const serviceAccount = require("./madden-companion-project-firebase-adminsdk-u16ts-0a223df9a2.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/"
// });
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://madden-companion-project-default-rtdb.firebaseio.com"
});

app.set('port', (process.env.PORT || 3001));

app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));

app.get('*', (req, res) => {
res.send('Madden Companion Exporter');
});

// app.get('/roster/get', (request, response) => {
// res.send(cacheRosterData);
// })

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;
const {
params: { username, leagueId },
body: { leagueTeamInfoList: teams },
} = req;

teams.forEach(team => {
const teamRef = ref.child(`data/${username}/${leagueId}/teams/${team.teamId}`);
teamRef.set(team);
});
teams.forEach(team => {
const teamRef = ref.child(
`data/${username}/${leagueId}/teams/${team.teamId}`
);
teamRef.update(team);
});

res.sendStatus(200);
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();
const {
params: { username, leagueId },
body: { teamStandingInfoList: teams },
} = req;

teams.forEach(team => {
const teamRef = ref.child(
`data/${username}/${leagueId}/teams/${team.teamId}`
);
teamRef.update(team);
});
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);
res.sendStatus(200);
});
});

function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
Expand All @@ -83,73 +87,78 @@ app.post(
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': {

switch (dataType) {
case 'schedules': {
const weekRef = ref.child(
`${basePath}schedules/${weekType}/${weekNumber}`
);
const {
body: { gameScheduleInfoList: schedules },
} = req;
weekRef.update(schedules);
break;
}
case 'teamstats': {
const {
body: { teamStatInfoList: teamStats },
} = req;
teamStats.forEach(stat => {
const weekRef = ref.child(
`${basePath}schedules/${weekType}/${weekNumber}`
`${statsPath}/${weekType}/${weekNumber}/${stat.teamId}/team-stats`
);
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;
}
weekRef.update(stat);
});
break;
}
case 'defense': {
const {
body: { playerDefensiveStatInfoList: defensiveStats },
} = req;
defensiveStats.forEach(stat => {
const weekRef = ref.child(
`${statsPath}/${weekType}/${weekNumber}/${stat.teamId}/player-stats/${stat.rosterId}`
);
weekRef.update(stat);
});
break;
}
default: {
const { body } = req;
const property = `player${capitalizeFirstLetter(
dataType
)}StatInfoList`;
const stats = body[property];
stats.forEach(stat => {
const weekRef = ref.child(
`${statsPath}/${weekType}/${weekNumber}/${stat.teamId}/player-stats/${stat.rosterId}`
);
weekRef.update(stat);
});
break;
}
}

res.sendStatus(200);
});
}
);

// ROSTERS
app.post('/:username/:platform/:leagueId/freeagents/roster', (req, res) => {
const db = admin.database();
console.info('database object: ', db);
const ref = db.ref();
const {
params: { username, leagueId, teamId }
} = req;
let body = '';
req.on('data', chunk => {
body += chunk.toString();
console.info('data event:', body);
cacheRosterData = body;
});
req.on('end', () => {
const { rosterInfoList } = JSON.parse(body);
console.info('end', rosterInfoList);
const dataRef = ref.child(
`data/${username}/${leagueId}/freeagents`
);
Expand All @@ -172,31 +181,25 @@ app.post('/:username/:platform/:leagueId/team/:teamId/roster', (req, res) => {
const db = admin.database();
const ref = db.ref();
const {
params: { username, leagueId, teamId }
params: { username, leagueId, teamId },
body: { rosterInfoList },
} = req;
let body = '';
req.on('data', chunk => {
body += chunk.toString();
const dataRef = ref.child(
`data/${username}/${leagueId}/teams/${teamId}/roster`
);
const players = {};
rosterInfoList.forEach(player => {
players[player.rosterId] = player;
});
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);
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'))
Expand Down
7 changes: 7 additions & 0 deletions database.rules.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
/* Visit https://firebase.google.com/docs/database/security to learn more about security rules. */
"rules": {
".read": false,
".write": false
}
}
13 changes: 13 additions & 0 deletions madden-companion-project-firebase-adminsdk-u16ts-0a223df9a2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"type": "service_account",
"project_id": "madden-companion-project",
"private_key_id": "0a223df9a23ce4b2ce81bf8890407e90b2825b99",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCUt/UfWu4a3SKO\nCR50qYJ50SZZ1fEDffsPLH7kZDnSUdveTS3a05IKQKmpUiO2EmNchdgMy4fJLTZ8\nMct5qTwVCmQQwKQG6ox4XT7wVCsI8tza7ji7V22iy1+xuYftGrsFBmjRJqWQPCIZ\na1/BAtoh96lje1Ox0SpI/IidKDl9WMofbWiHA7UIyQPIDUQ4akEaMUSYp19jI+7M\npcMz57AKSych6h6QAgjyNol6mV4N9pyNFBTPR6rEolL1MbME2Jm/p4JwAfBeXx1w\nkxb5DPUzT7M+DzO5KAXL59zgPIB1SVy9uR+zP750KyLEgPVFeIrM9F/INZnCJhlF\nHd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8jLyiY2yc/WrEz7ykCgYEApp8/eN3OM7jt9vSFrHLA\nw6Cs8v4QyDu0AcWmdzHVoyA83bMt7BuyUOdYBwBaynyvDEjedtZZM3vAk1SMTU0W\ndBU5jzLPv8B96yRA8UxauEXM9dJtaHmOmeRN5UAudvB+ucCDGjhirxFaZuFmhJVG\nAM0Jq7C/RpwptdDo1WmmOa4=\n-----END PRIVATE KEY-----\n",
"client_email": "firebase-adminsdk-u16ts@madden-companion-project.iam.gserviceaccount.com",
"client_id": "104592923713409434476",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-u16ts%40madden-companion-project.iam.gserviceaccount.com",
"universe_domain": "googleapis.com"
}
Loading