Skip to content
This repository has been archived by the owner on Apr 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #15 from Programming-Club-Ahmedabad-University/ser…
Browse files Browse the repository at this point in the history
…ver/leaderboard-fetch

cf api fetch(changes shifted to server/leaderboard-fetch branch)
  • Loading branch information
JeelRajodiya authored Aug 20, 2023
2 parents e568cef + 50164ca commit deffb18
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 13 deletions.
19 changes: 19 additions & 0 deletions client/app/Test/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"use client";
import { useEffect, useState } from "react";

export default function Test() {
const ws = new WebSocket("ws://localhost:3001");
const [data, setData] = useState(null);
useEffect(() => {
ws.onmessage = (e) => {
// a message was received

setData(e.data);
};
return () => {
ws.close();
};
}, []);

return <div>{data}</div>;
}
26 changes: 15 additions & 11 deletions server/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { loadEnvVariables } from "./util/functions";

import { loadEnvVariables, updateLeaderboard } from "./util/functions";
loadEnvVariables(__dirname);
// keep the above imports always at the top they are used to load the environment variables

Expand All @@ -9,28 +8,33 @@ import WebSocket from "ws";
import redisClient from "./util/redis";

var app = express();

app.use("/", async function (req: Request, res: Response, next) {
const count = await redisClient.incr("count");

return res.send(count.toString());
});

function webSocketHandler(ws: WebSocket) {
ws.on("message", function (message: string) {
console.log("received: %s", message);
});
let i = 0;
let interval = setInterval(() => {
ws.send(i);
i += 10;
}, 10000);
let interval = setInterval(async () => {
const leaderboard = await redisClient.get("leaderboard");
if (!leaderboard)
return ws.send(
JSON.stringify({
leaderboard: [],
})
);

ws.send(leaderboard);
}, 1000);

ws.send("something");
ws.on("close", function () {
console.log("closing connection");
clearInterval(interval);
});
}

setInterval(() => updateLeaderboard(redisClient as any, 468732), 4000);
export default app;

export { webSocketHandler };
107 changes: 107 additions & 0 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
"dependencies": {
"@types/express": "^4.17.17",
"cookie-parser": "^1.4.6",
"cross-fetch": "^4.0.0",
"crypto": "^1.0.1",
"cryptos": "^1.0.0",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"morgan": "^1.10.0",
Expand Down
2 changes: 1 addition & 1 deletion server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ app.use(function (
res.render("error");
});

var port = normalizePort(process.env.PORT || "3000");
var port = normalizePort(process.env.PORT || "3001");
app.set("port", port);

/**
Expand Down
35 changes: 34 additions & 1 deletion server/util/functions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import fs from "fs";
import dotenv from "dotenv";
import path from "path";
const requiredEnvVariables = ["REDIS_PASS", "REDIS_HOST"];
import { RedisClientType } from "redis";
import crypto from "crypto";
import fetch from "cross-fetch";
const requiredEnvVariables = [
"REDIS_PASS",
"REDIS_HOST",
"CF_SECRET",
"CF_API_KEY",
];
export function loadEnvVariables(dirname: string): void {
try {
const envFilePath = path.resolve(dirname, ".env");
Expand Down Expand Up @@ -42,3 +50,28 @@ export function normalizePort(val: string) {

return false;
}

export async function updateLeaderboard(
redisClient: RedisClientType,
contestId: number
) {
const curr_time = Math.floor(new Date().getTime() / 1000);
const apiKey = process.env.CF_API_KEY;
const secret = process.env.CF_SECRET;
if (!apiKey || !secret) {
throw new Error("API key or secret is missing");
}

const signature = crypto
.createHash("sha512")
.update(
`123456/contest.standings?apiKey=${apiKey}&contestId=${contestId}&time=${curr_time}#${secret}`
)
.digest("hex");

const requestUrl = `https://codeforces.com/api/contest.standings?contestId=${contestId}&apiKey=${apiKey}&time=${curr_time}&apiSig=123456${signature}`;

const resp = await fetch(requestUrl);
const data = await resp.json();
await redisClient.set("leaderboard", JSON.stringify(data));
}

0 comments on commit deffb18

Please sign in to comment.