Skip to content

Commit

Permalink
fix profile
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenruidigao committed Jan 10, 2025
1 parent 5228558 commit 7b9a9a5
Showing 1 changed file with 132 additions and 129 deletions.
261 changes: 132 additions & 129 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,158 +283,161 @@ module.exports = () => {
const authedUser = req.session && req.session.passport && req.session.passport.user;
const username = req.query.username;

getProfile(username).then(profile => {
let _profile;
Account.findOne({ username }, (err, account) => {
if (err) {
return new Error(err);
}

if (!profile) {
const noData = {
events: 0,
successes: 0
};
if (!account) {
res.status(404).send('Profile not found');
return;
}

const noTeamData = {
fascist: noData,
liberal: noData
};
getProfile(username).then(profile => {
let _profile;

const noPlayerNumberData = {
5: noTeamData,
6: noTeamData,
7: noTeamData,
8: noTeamData,
9: noTeamData,
10: noTeamData,
fascist: noData,
liberal: noData
};
if (profile) {
_profile = profile.toObject();
} else {
const noData = {
events: 0,
successes: 0
};

_profile = {
_id: username,
recentGames: [],
stats: {
actions: {
legacyShotAccuracy: noData,
legacyVoteAccuracy: noData,
shotAccuracy: noData,
voteAccuracy: noData
},
matches: {
allMatches: noData,
casualMatches: noTeamData,
emoteMatches: noTeamData,
fascist: noData,
greyMatches: noPlayerNumberData,
legacyMatches: noData,
liberal: noData,
practiceMatches: noTeamData,
rainbowMatches: noPlayerNumberData,
silentMatches: noTeamData
}
}
};
} else {
_profile = profile.toObject();
}
const noTeamData = {
fascist: noData,
liberal: noData
};

Account.findOne({ username }, (err, account) => {
if (err) {
return new Error(err);
}
const noPlayerNumberData = {
5: noTeamData,
6: noTeamData,
7: noTeamData,
8: noTeamData,
9: noTeamData,
10: noTeamData,
fascist: noData,
liberal: noData
};

if (account) {
_profile.created = moment(account.created).format('MM/DD/YYYY');
_profile.customCardback = account.gameSettings.customCardback;
_profile.bio = account.bio;
_profile.lastConnected = !!account.lastConnected ? moment(account.lastConnected).format('MM/DD/YYYY') : '';
_profile.badges = account.badges || [];
_profile.eloPercentile = Object.keys(account.eloPercentile).length ? account.eloPercentile : undefined;
_profile.maxElo =
account.gameSettings.staff && account.gameSettings.staff.disableVisibleElo ? undefined : Math.round(Number.parseFloat(account.maxElo || 1600));
_profile.pastElo =
account.gameSettings.staff && account.gameSettings.staff.disableVisibleElo
? undefined
: account.pastElo.toObject().length
? account.pastElo.toObject()
: [{ date: new Date(), value: Math.round(Number.parseFloat(account.eloOverall || 1600)) }];

_profile.overall = account.overall;

const defaultSeason = {
wins: 0,
losses: 0,
rainbowWins: 0,
rainbowLosses: 0,
elo: 1600,
xp: 0
_profile = {
_id: username,
recentGames: [],
stats: {
actions: {
legacyShotAccuracy: noData,
legacyVoteAccuracy: noData,
shotAccuracy: noData,
voteAccuracy: noData
},
matches: {
allMatches: noData,
casualMatches: noTeamData,
emoteMatches: noTeamData,
fascist: noData,
greyMatches: noPlayerNumberData,
legacyMatches: noData,
liberal: noData,
practiceMatches: noTeamData,
rainbowMatches: noPlayerNumberData,
silentMatches: noTeamData
}
}
};
}

_profile.season = account.seasons ? account.seasons.get(CURRENT_SEASON_NUMBER) || defaultSeason : defaultSeason;
_profile.created = moment(account.created).format('MM/DD/YYYY');
_profile.customCardback = account.gameSettings.customCardback;
_profile.bio = account.bio;
_profile.lastConnected = !!account.lastConnected ? moment(account.lastConnected).format('MM/DD/YYYY') : '';
_profile.badges = account.badges || [];
_profile.eloPercentile = Object.keys(account.eloPercentile).length ? account.eloPercentile : undefined;
_profile.maxElo =
account.gameSettings.staff && account.gameSettings.staff.disableVisibleElo ? undefined : Math.round(Number.parseFloat(account.maxElo || 1600));
_profile.pastElo =
account.gameSettings.staff && account.gameSettings.staff.disableVisibleElo
? undefined
: account.pastElo.toObject().length
? account.pastElo.toObject()
: [{ date: new Date(), value: Math.round(Number.parseFloat(account.eloOverall || 1600)) }];

_profile.overall = account.overall;

const defaultSeason = {
wins: 0,
losses: 0,
rainbowWins: 0,
rainbowLosses: 0,
elo: 1600,
xp: 0
};

if (account.staffRole) {
if (account.gameSettings.staff && account.gameSettings.staff.disableVisibleElo) {
delete _profile.overall.elo;
delete _profile.season.elo;
}
_profile.season = account.seasons ? account.seasons.get(CURRENT_SEASON_NUMBER) || defaultSeason : defaultSeason;

if (account.gameSettings.staff && account.gameSettings.staff.disableVisibleXP) {
delete _profile.overall.xp;
delete _profile.season.xp;
}
if (account.staffRole) {
if (account.gameSettings.staff && account.gameSettings.staff.disableVisibleElo) {
delete _profile.overall.elo;
delete _profile.season.elo;
}

_profile.isRainbowOverall = account.isRainbowOverall;
_profile.isRainbowSeason = account.isRainbowSeason;
_profile.staffRole = account.staffRole;
_profile.staff = {};
_profile.staff.disableVisibleXP = account.gameSettings.staff && account.gameSettings.staff.disableVisibleXP;
_profile.staff.disableVisibleElo = account.gameSettings.staff && account.gameSettings.staff.disableVisibleElo;
_profile.playerPronouns = account.gameSettings.playerPronouns || '';

Account.findOne({ username: authedUser }).then(acc => {
if (acc && account.username === acc.username) {
acc.gameSettings.hasUnseenBadge = false;
acc.save();
}
if (account.gameSettings.staff && account.gameSettings.staff.disableVisibleXP) {
delete _profile.overall.xp;
delete _profile.season.xp;
}
}

if (
acc &&
acc.staffRole &&
(acc.staffRole === 'trialmod' || acc.staffRole === 'moderator' || acc.staffRole === 'editor' || acc.staffRole === 'admin')
) {
try {
_profile.lastConnectedIP = '-' + obfIP(account.lastConnectedIP);
} catch (e) {
_profile.lastConnectedIP = "Couldn't find IP";
console.log(e);
}
_profile.isRainbowOverall = account.isRainbowOverall;
_profile.isRainbowSeason = account.isRainbowSeason;
_profile.staffRole = account.staffRole;
_profile.staff = {};
_profile.staff.disableVisibleXP = account.gameSettings.staff && account.gameSettings.staff.disableVisibleXP;
_profile.staff.disableVisibleElo = account.gameSettings.staff && account.gameSettings.staff.disableVisibleElo;
_profile.playerPronouns = account.gameSettings.playerPronouns || '';

Account.findOne({ username: authedUser }).then(acc => {
if (acc && account.username === acc.username) {
acc.gameSettings.hasUnseenBadge = false;
acc.save();
}

try {
_profile.signupIP = '-' + obfIP(account.signupIP);
} catch (e) {
_profile.signupIP = "Couldn't find IP";
console.log(e);
}
if (
acc &&
acc.staffRole &&
(acc.staffRole === 'trialmod' || acc.staffRole === 'moderator' || acc.staffRole === 'editor' || acc.staffRole === 'admin')
) {
try {
_profile.lastConnectedIP = '-' + obfIP(account.lastConnectedIP);
} catch (e) {
_profile.lastConnectedIP = "Couldn't find IP";
console.log(e);
}

_profile.lastConnected = moment(account.lastConnected).format('MM/DD/YYYY h:mm');
_profile.created = moment(account.created).format('MM/DD/YYYY h:mm');
try {
_profile.signupIP = '-' + obfIP(account.signupIP);
} catch (e) {
_profile.signupIP = "Couldn't find IP";
console.log(e);
}

if (acc.staffRole !== 'trialmod') {
_profile.blacklist = account.gameSettings.blacklist;
}
} else {
_profile.lastConnectedIP = undefined;
_profile.signupIP = undefined;
_profile.lastConnected = moment(account.lastConnected).format('MM/DD/YYYY h:mm');
_profile.created = moment(account.created).format('MM/DD/YYYY h:mm');

if (acc.staffRole !== 'trialmod') {
_profile.blacklist = account.gameSettings.blacklist;
}
} else {
_profile.lastConnectedIP = undefined;
_profile.signupIP = undefined;

if (account.gameSettings.isPrivate && !_profile.lastConnectedIP) {
if (account.gameSettings.isPrivate) {
// They are private and lastConnectedIP is set to undefined (ie. requester is not AEM)
res.status(404).send('Profile not found');
return;
}
}

res.json(_profile);
});
}
res.json(_profile);
});
});
});
});
Expand Down

0 comments on commit 7b9a9a5

Please sign in to comment.