Skip to content

Commit

Permalink
Moved serving static files at the end of endpoints declaration (I sho…
Browse files Browse the repository at this point in the history
…uld think about a better way of serving these files.)
  • Loading branch information
Senryoku committed Jul 27, 2023
1 parent 435bac4 commit 4d6c720
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 55 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ Dockerfile
.dockerignore
node_modules
.git
tmp

data/scryfall-all-cards.json
116 changes: 61 additions & 55 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,14 @@ const checkDraftAction = function (userID: UserID, sess: Session, type: string,
// Personnal options
function setUserName(userID: UserID, sessionID: SessionID, userName: string) {
Connections[userID].userName = userName;
Sessions[sessionID].forUsers((uid: UserID) =>
Connections[uid]?.socket.emit("updateUser", {
userID: userID,
updatedProperties: {
userName: userName,
},
})
Sessions[sessionID].forUsers(
(uid: UserID) =>
Connections[uid]?.socket.emit("updateUser", {
userID: userID,
updatedProperties: {
userName: userName,
},
})
);
}
function setCollection(
Expand All @@ -203,13 +204,14 @@ function setCollection(
ack?.({ collection: processedCollection });

const hasCollection = processedCollection.size > 0;
Sessions[sessionID].forUsers((user) =>
Connections[user]?.socket.emit("updateUser", {
userID: userID,
updatedProperties: {
collection: hasCollection,
},
})
Sessions[sessionID].forUsers(
(user) =>
Connections[user]?.socket.emit("updateUser", {
userID: userID,
updatedProperties: {
collection: hasCollection,
},
})
);
}

Expand Down Expand Up @@ -270,13 +272,14 @@ function useCollection(userID: UserID, sessionID: SessionID, useCollection: bool
if (!isBoolean(useCollection) || useCollection === Connections[userID].useCollection) return;

Connections[userID].useCollection = useCollection;
Sessions[sessionID].forUsers((user) =>
Connections[user]?.socket.emit("updateUser", {
userID: userID,
updatedProperties: {
useCollection: useCollection,
},
})
Sessions[sessionID].forUsers(
(user) =>
Connections[user]?.socket.emit("updateUser", {
userID: userID,
updatedProperties: {
useCollection: useCollection,
},
})
);
}

Expand Down Expand Up @@ -716,12 +719,13 @@ function setSessionOwner(userID: UserID, sessionID: SessionID, newOwnerID: UserI
} else {
sess.owner = newOwnerID;
}
sess.forUsers((user) =>
Connections[user]?.socket.emit(
"sessionOwner",
sess.owner,
sess.owner && sess.owner in Connections ? Connections[sess.owner].userName : null
)
sess.forUsers(
(user) =>
Connections[user]?.socket.emit(
"sessionOwner",
sess.owner,
sess.owner && sess.owner in Connections ? Connections[sess.owner].userName : null
)
);
}

Expand Down Expand Up @@ -873,14 +877,15 @@ function importCube(userID: UserID, sessionID: SessionID, data: unknown, ack: (r
if (validateResponse(response, data, ack))
parseCustomCardList(Sessions[sessionID], response.data, {}, ack);
})
.catch((err) =>
ack?.(
new SocketError(
"Error retrieving cube.",
`Couldn't retrieve the card list from ${data.service}.`,
`Full error: ${err}`
.catch(
(err) =>
ack?.(
new SocketError(
"Error retrieving cube.",
`Couldn't retrieve the card list from ${data.service}.`,
`Full error: ${err}`
)
)
)
);
};

Expand All @@ -902,14 +907,15 @@ function importCube(userID: UserID, sessionID: SessionID, data: unknown, ack: (r
else parseCustomCardList(Sessions[sessionID], converted, { fallbackToCardName: true }, ack);
}
})
.catch((err) =>
ack?.(
new SocketError(
"Error retrieving cube",
`Couldn't retrieve the card list from ${data.service}.`,
`Full error: ${err}`
.catch(
(err) =>
ack?.(
new SocketError(
"Error retrieving cube",
`Couldn't retrieve the card list from ${data.service}.`,
`Full error: ${err}`
)
)
)
);
}
}
Expand Down Expand Up @@ -1643,11 +1649,14 @@ function removeUserFromSession(userID: UserID) {
InactiveSessions[sessionID] = getPoDSession(sess);
// Keep Rotisserie Draft around since they're typically played over long period of time.
if (!isRotisserieDraftState(sess.draftState))
InactiveSessions[sessionID].deleteTimeout = setTimeout(() => {
process.nextTick(() => {
if (InactiveSessions[sessionID]) delete InactiveSessions[sessionID];
});
}, 10 * 60 * 1000); // 10min should be plenty enough.
InactiveSessions[sessionID].deleteTimeout = setTimeout(
() => {
process.nextTick(() => {
if (InactiveSessions[sessionID]) delete InactiveSessions[sessionID];
});
},
10 * 60 * 1000
); // 10min should be plenty enough.
}
deleteSession(sessionID);
} else sess.notifyUserChange();
Expand All @@ -1661,15 +1670,7 @@ function removeUserFromSession(userID: UserID) {
///////////////////////////////////////////////////////////////////////////////
// Express server setup

// Serve files in the public directory
app.use(express.static("./client/dist/"));
app.use("/bracket", express.static("./client/dist/bracket.html"));

app.use("/draftqueue", express.static("./client/dist/index.html"));

///////////////////////////////////////////////////////////////////////////////
// Endpoints
// (TODO: Should be cleaned up)
// Endpoints (TODO: Should be cleaned up)

app.get("/healthCheck", (req, res) => {
res.sendStatus(200);
Expand Down Expand Up @@ -1883,6 +1884,11 @@ app.get("/api/getDraftQueueStatus", (req, res) => {
return res.json(getQueueStatus());
});

// Serve files in the public directory
app.use("/bracket", express.static("./client/dist/bracket.html"));
app.use("/draftqueue", express.static("./client/dist/index.html"));
app.use(express.static("./client/dist/"));

Promise.all([InactiveConnections, InactiveSessions]).then(() => {
httpServer.listen(port, () => {
console.log(`Listening on port ${port} (ready in ${process.uptime().toFixed(2)}s)`);
Expand Down

0 comments on commit 4d6c720

Please sign in to comment.