Skip to content

Commit

Permalink
make homepage client-rendered
Browse files Browse the repository at this point in the history
  • Loading branch information
JayAgra committed Oct 5, 2023
1 parent c633c3d commit 520cd7e
Show file tree
Hide file tree
Showing 20 changed files with 93 additions and 211 deletions.
2 changes: 1 addition & 1 deletion routes/api/casino/blackjack/stand.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const crypto = require("crypto");

function stand(req, res, db, possibleCards, casinoToken) {
async function stand(req, res, db, possibleCards, casinoToken) {
let stmt = `SELECT score FROM scouts WHERE discordID=?`;
let values = [req.user.id];
db.get(stmt, values, (err, dbQueryResult) => {
Expand Down
2 changes: 1 addition & 1 deletion routes/api/casino/blackjack/startingCards.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const crypto = require("crypto");

function startingCards(req, res, db, possibleCards, casinoToken) {
async function startingCards(req, res, db, possibleCards, casinoToken) {
var cards = [];
var cardValues = 0;
var numOfAces = 0;
Expand Down
2 changes: 1 addition & 1 deletion routes/api/casino/blackjack/wonViaBlackjack.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const crypto = require("crypto");

function wonViaBlackjack(req, res, db, casinoToken) {
async function wonViaBlackjack(req, res, db, casinoToken) {
let stmt = `SELECT score FROM scouts WHERE discordID=?`;
let values = [req.user.id];
db.get(stmt, values, (err, dbQueryResult) => {
Expand Down
2 changes: 1 addition & 1 deletion routes/api/casino/plinko/endGame.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const crypto = require("crypto");

function endGame(req, res, db, casinoToken) {
async function endGame(req, res, db, casinoToken) {
let stmt = `SELECT score FROM scouts WHERE discordID=?`;
let values = [req.user.id];
db.get(stmt, values, (err, dbQueryResult) => {
Expand Down
2 changes: 1 addition & 1 deletion routes/api/casino/plinko/startGame.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const crypto = require("crypto");

function startGame(req, res, db, casinoToken) {
async function startGame(req, res, db, casinoToken) {
let pointStmt = `UPDATE scouts SET score = score - 15 WHERE discordID=?`;
let pointValues = [req.user.id];
db.run(pointStmt, pointValues, (err) => {
Expand Down
2 changes: 1 addition & 1 deletion routes/api/casino/slots/slotSpin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function slotSpin(req, res, db) {
async function slotSpin(req, res, db) {
const spin = [
Math.floor(Math.random() * 7 + 1),
Math.floor(Math.random() * 7 + 1),
Expand Down
2 changes: 1 addition & 1 deletion routes/api/casino/spinner/spinWheel.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function spinWheel(req, res, db) {
async function spinWheel(req, res, db) {
// 12 spins
const spins = [10, 20, 50, -15, -25, -35, -100, -50, 100, 250, -1000, 1250];

Expand Down
2 changes: 1 addition & 1 deletion routes/api/events/pitscoutedteams.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function pitscoutedteams(req, res, db, season) {
async function pitscoutedteams(req, res, db, season) {
var teams = [];
const stmt = `SELECT * FROM pit WHERE event=? AND season=?`;
const values = [req.params.event, season];
Expand Down
2 changes: 1 addition & 1 deletion routes/api/events/teams.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function invalidJSON(str) {
}


function teams(req, res, frcapi, season) {
async function teams(req, res, frcapi, season) {
if (req.params.event !== "WOOD") {
var dbody = new EventEmitter();
var options = {
Expand Down
2 changes: 1 addition & 1 deletion routes/api/notes/createNote.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function createNote(req, res, db, season) {
async function createNote(req, res, db, season) {
const stmt =
"INSERT INTO notes (team, season, event, note) VALUES(?, ?, ?, 'no note yet')";
const values = [req.params.team, season, req.params.event];
Expand Down
2 changes: 1 addition & 1 deletion routes/api/notes/getNotes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function getNotes(req, res, db, season) {
async function getNotes(req, res, db, season) {
const stmt = `SELECT * FROM notes WHERE event=? AND season=? AND team=?`;
const values = [req.params.event, season, req.params.team];
db.get(stmt, values, (err, dbQueryResult) => {
Expand Down
2 changes: 1 addition & 1 deletion routes/api/notes/updateNotes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function updateNotes(req, res, db, season) {
async function updateNotes(req, res, db, season) {
let body = "";

req.on("data", (chunk) => {
Expand Down
2 changes: 1 addition & 1 deletion routes/api/scouts/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function isMe(req) {
}
}

function profile(req, res, db) {
async function profile(req, res, db) {
const stmt = `SELECT discordID, score, discordProfile, username, discriminator, addedAt, badges FROM scouts WHERE discordID=?`;
const values = [isMe(req)];
db.get(stmt, values, (err, dbQueryResult) => {
Expand Down
2 changes: 1 addition & 1 deletion routes/browse.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function browse(req, res, db, dirname, season) {
async function browse(req, res, db, dirname, season) {
if (req.query.number && req.query.event) {
if (req.query.number == "ALL" || req.query.number == "*" || req.query.number == "0000" || req.query.number == "0") {
const stmt = `SELECT * FROM main WHERE event=? AND season=? ORDER BY team ASC`;
Expand Down
2 changes: 1 addition & 1 deletion routes/detail.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function detail(req, res, db, dirname, season) {
async function detail(req, res, db, dirname, season) {
if (req.query.team && req.query.event && req.query.page) {
const stmt = `SELECT * FROM main WHERE team=? AND event=? AND season=? ORDER BY id DESC LIMIT 1 OFFSET ?`;
const values = [
Expand Down
180 changes: 25 additions & 155 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,188 +2,58 @@ const DiscordOauth2 = require("discord-oauth2");
const getOauthData = new DiscordOauth2();

const {
scoutteama,
scoutteamb,
leadscout,
drive,
pit,
teamServerID
} = require("../config.json");

function findTopRole(roles) {
var rolesOut = [];
if (roles.indexOf(leadscout) >= 0) {
rolesOut.push([
"Lead Scout",
"rgb(233, 30, 99)",
"rgba(233, 30, 99, 0.1)",
]);
}
if (roles.indexOf(drive) >= 0) {
rolesOut.push([
"Drive Team",
"rgb(241, 196, 15)",
"rgba(241, 196, 15, 0.1)",
]);
}
if (roles.indexOf(pit) >= 0) {
rolesOut.push([
"Pit Team",
"rgb(230, 126, 34)",
"rgba(230, 126, 34, 0.1)",
]);
}
if (roles.indexOf(scoutteama) >= 0) {
rolesOut.push([
"Scout Team A",
"rgb(26, 188, 156)",
"rgba(26, 188, 156, 0.1)",
]);
}
if (roles.indexOf(scoutteamb) >= 0) {
rolesOut.push([
"Scout Team B",
"rgb(52, 152, 219)",
"rgba(52, 152, 219, 0.1)",
]);
}
rolesOut.push([
"Default Role",
"rgb(200, 200, 200)",
"rgba(200, 200, 200, 0.1)",
]);
return rolesOut;
function isLeadScout(roles) {
return (roles.indexOf(leadscout) >= 0);
}

async function index(req, res, db, dirname, leadToken) {
async function index(req, res, dirname, leadToken) {
// change index.ejs based on the user's roles
try {
if (!req.cookies.role) {
// set cookie if not exists
// I am setting a cookie because it takes a while to wait for role data from API

if (oauthDataCookieSet[0][0] === "Lead Scout") {
if (!req.cookies.lead) {
// client does not have lead cookie
// is client a lead scout?
if (await Promise.resolve(getOauthData.getGuildMember(req.user.accessToken, teamServerID).then((data) => {return isLeadScout(data.roles);}))) {
// if yes, send the cookie and load page as lead scout
res.cookie("lead", leadToken, {
expire: 7200000 + Date.now(),
sameSite: "Lax",
secure: true,
httpOnly: true,
});
}

var oauthDataCookieSet = await Promise.resolve(
getOauthData
.getGuildMember(req.user.accessToken, teamServerID)
.then((data) => {
return findTopRole(data.roles);
})
);

// btoa and atob bad idea
// Buffer.from(str, 'base64') and buf.toString('base64') instead
res.cookie("role", JSON.stringify(oauthDataCookieSet), {
expire: 7200000 + Date.now(),
sameSite: "Lax",
secure: true,
httpOnly: true,
});
if (
oauthDataCookieSet[0][0] == "Pit Team" ||
oauthDataCookieSet[0][0] == "Drive Team"
) {
res.render("../src/index.ejs", {
root: dirname,
order1: "2",
order2: "0",
order3: "1",
order4: "3",
additionalURLs: "<span></span>",
});
} else if (oauthDataCookieSet[0][0] == "Lead Scout") {
res.cookie("lead", leadToken, {
// 1 hour
expire: 3600000 + Date.now(),
sameSite: "Lax",
secure: true,
httpOnly: true,
});
res.render("../src/index.ejs", {
root: dirname,
order1: "0",
order2: "3",
order3: "2",
order4: "1",
additionalURLs: `<a href="manage" class="gameflair1" style="order: 4; margin-bottom: 5%;">Manage Submissions<br></a>`,
});
} else {
res.render("../src/index.ejs", {
root: dirname,
order1: "0",
order2: "3",
order3: "2",
order4: "1",
additionalURLs: "<span></span>",
});
}
} else {
var oauthData = JSON.parse(req.cookies.role);

if (oauthData[0][0] === "Lead Scout") {
res.cookie("lead", leadToken, {
// send this one too, visible to client js
res.cookie("isLead", "true", {
expire: 7200000 + Date.now(),
sameSite: "Lax",
secure: true,
httpOnly: true,
httpOnly: false,
});
}

if (
oauthData[0][0] == "Pit Team" ||
oauthData[0][0] == "Drive Team"
) {
res.render("../src/index.ejs", {
root: dirname,
order1: "2",
order2: "0",
order3: "1",
order4: "3",
additionalURLs: "<span></span>",
});
} else if (oauthData[0][0] == "Lead Scout") {
res.cookie("lead", leadToken, {
res.sendFile("src/index.html", {
root: dirname,
});
} else {
// i have the wrong cooky!
if (req.cookies.lead !== leadToken) {
res.cookie("isLead", "false", {
expire: 7200000 + Date.now(),
sameSite: "Lax",
secure: true,
httpOnly: true,
});
res.render("../src/index.ejs", {
root: dirname,
order1: "0",
order2: "3",
order3: "2",
order4: "1",
additionalURLs: `<a href="manage" class="gameflair1" style="order: 4; margin-bottom: 5%;">Manage Submissions<br></a>`,
});
} else {
res.render("../src/index.ejs", {
root: dirname,
order1: "0",
order2: "3",
order3: "2",
order4: "1",
additionalURLs: "<span></span>",
httpOnly: false,
});
}
// send index.html
res.sendFile("src/index.html", {
root: dirname,
})
}
} catch (err) {
res.render("../src/index.ejs", {
res.sendFile("src/index.html", {
root: dirname,
order1: "0",
order2: "3",
order3: "2",
order4: "1",
additionalURLs: `<span></span>`,
});
})
}
}

Expand Down
Loading

0 comments on commit 520cd7e

Please sign in to comment.