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

v0.3.8 #94

Merged
merged 3 commits into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions apps/client/src/app/room/[code]/game/[gameId]/game.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { EventFrom, State } from "xstate";
import { useMachine } from "@xstate/react";
import { AnimatePresence } from "framer-motion";
import { useQuery } from "@tanstack/react-query";
import { redirect } from "next/navigation";
import { useRouter } from "next/navigation";

import {
GameInfo,
Expand Down Expand Up @@ -36,6 +36,8 @@ export default function Game({ gameInfo, session }: GameProps) {
// Socket for real-time communication
const socket = useContext(SocketContext);

const router = useRouter();

// Wait until the client mounts to avoid hydration errors
const [isMounted, setIsMounted] = useState(false);

Expand Down Expand Up @@ -138,9 +140,9 @@ export default function Game({ gameInfo, session }: GameProps) {
const handlePlayAnotherGame = useCallback(
(gameId: number) => {
send("NEXT");
redirect(`/room/${gameInfo.game.roomCode}/game/${gameId}`);
router.push(`/room/${gameInfo.game.roomCode}/game/${gameId}`);
},
[gameInfo.game.roomCode, send],
[gameInfo.game.roomCode, router, send],
);

// Send new state to server
Expand Down
2 changes: 1 addition & 1 deletion apps/client/src/components/game/prompt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const Prompt = ({
const gameId = gameInfo.game.id;
const userId = session.user.id;
const currRound = state.context.round;
const maxRegenerations = 3;
const maxRegenerations = 2;

const userGameRoundGenerations = gameInfo.gameRoundGenerations.filter(
(generation) =>
Expand Down
3 changes: 1 addition & 2 deletions apps/client/src/utils/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ export async function getGameInfo(gameId: number, sessionToken: string) {

export type GetGameLeaderboardResponse = {
leaderboard: { user: User; points: number; standing: number }[];
// winningGenerations: { question: Question; generation: Generation }[];
allGenerations: { question: Question; generation: Generation; user: User }[];
};

Expand Down Expand Up @@ -287,7 +286,7 @@ export const generateSDXLImages = async (prompt: string) => {
body: JSON.stringify({ prompt }),
});

if (response.status !== 200) {
if (!response.ok) {
throw new Error(`Request failed with status ${response.status}`);
}

Expand Down
23 changes: 20 additions & 3 deletions apps/server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,32 @@ export function buildServer() {
// Rate limiter
// Based on https://redis.io/commands/incr#pattern-rate-limiter-1
app.use(async (req, res, next) => {
const isIPBlocked = await redis.get(`BLOCKED_${req.ip}`);

if (isIPBlocked) {
res.status(429).send("Too many requests - try again tomorrow");
return;
}

let redisIncr: number;
try {
redisIncr = await redis.incr(req.ip);
} catch (err) {
} catch (error) {
console.error(`Could not increment rate limit key for ${req.ip}`);
throw err;
throw error;
}
if (redisIncr > 10) {
res.status(429).send("Too many requests - try again later");
const today = new Date();
const tomorrow = new Date(today);
tomorrow.setDate(tomorrow.getDate() + 1);
tomorrow.setHours(0, 0, 0, 0);
await redis.set(`BLOCKED_${req.ip}`, tomorrow.toISOString());
await redis.expireat(
`BLOCKED_${req.ip}`,
Math.floor(tomorrow.getTime() / 1000)
);
console.log("Blocked the IP Address:", req.ip);
res.status(429).send("Too many requests - try again tomorrow");
return;
}
await redis.expire(req.ip, 10);
Expand Down