Skip to content

Commit

Permalink
Ability to preview map in playground
Browse files Browse the repository at this point in the history
  • Loading branch information
delasy committed Oct 21, 2024
1 parent 0f02259 commit 15cd47e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
2 changes: 2 additions & 0 deletions app/admin/review/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { useMutation, useQuery } from "convex/react";
import { Map } from "@/components/Map";
import { PlayMapButton } from "@/components/PlayMapButton";
import { Button } from "@/components/ui/button";
import {
Card,
Expand Down Expand Up @@ -50,6 +51,7 @@ export default function AdminReviewPage() {
>
Reject
</Button>
<PlayMapButton mapId={map._id} />
</CardFooter>
</Card>
))}
Expand Down
19 changes: 18 additions & 1 deletion app/playground/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import * as React from "react";
import { useAction, useMutation, useQuery } from "convex/react";
import { ChevronLeft, UploadIcon } from "lucide-react";
import { useSearchParams } from "next/navigation";
import { Map } from "@/components/Map";
import { MapStatus } from "@/components/MapStatus";
import { ModelSelector } from "@/components/ModelSelector";
Expand All @@ -11,6 +12,7 @@ import { Button } from "@/components/ui/button";
import { Card } from "@/components/ui/card";
import { useToast } from "@/components/ui/use-toast";
import { api } from "@/convex/_generated/api";
import { Id } from "@/convex/_generated/dataModel";
import { errorMessage } from "@/lib/utils";
import { ZombieSurvival } from "@/simulators/zombie-survival";

Expand All @@ -20,6 +22,13 @@ export default function PlaygroundPage() {
const isAdmin = useQuery(api.users.isAdmin);
const submitMap = useMutation(api.maps.submitMap);
const testMap = useAction(api.maps.testMap);
const searchParams = useSearchParams();
const mapId = searchParams.get("map") as Id<"maps"> | null;
const adminMapMaybe = useQuery(
api.maps.adminGetMapById,
!isAdmin || mapId === null ? "skip" : { mapId },
);
const adminMap = adminMapMaybe ?? null;
const { toast } = useToast();
const [map, setMap] = React.useState<string[][]>([
[" ", " ", " ", " ", " "],
Expand Down Expand Up @@ -97,7 +106,9 @@ export default function PlaygroundPage() {
function handleChangeMap(value: string[][]) {
setMap(value);
setError(null);
window.localStorage.setItem(STORAGE_MAP_KEY, JSON.stringify(value));
if (adminMap === null) {
window.localStorage.setItem(STORAGE_MAP_KEY, JSON.stringify(value));
}
}

function handleChangeModel(value: string) {
Expand Down Expand Up @@ -167,6 +178,12 @@ export default function PlaygroundPage() {
}
}, []);

React.useEffect(() => {
if (adminMap !== null) {
setMap(adminMap.grid);
}
}, [adminMap]);

const visualizing = solution !== null || visualizingUserSolution;

return (
Expand Down
16 changes: 16 additions & 0 deletions components/PlayMapButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"use client";

import * as React from "react";
import { ExternalLinkIcon } from "lucide-react";
import Link from "next/link";
import { Button } from "./ui/button";

export function PlayMapButton({ mapId }: { mapId: string }) {
return (
<Button asChild variant="outline" size="icon">
<Link href={`/playground?map=${mapId}`}>
<ExternalLinkIcon size={16} />
</Link>
</Button>
);
}
15 changes: 14 additions & 1 deletion convex/maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import {
query,
} from "./_generated/server";
import { Prompt } from "./prompts";
import { adminMutationBuilder, authenticatedMutation } from "./users";
import {
adminMutationBuilder,
adminQueryBuilder,
authenticatedMutation,
} from "./users";

const LEVELS = [
{
Expand Down Expand Up @@ -263,6 +267,15 @@ export const getMapByLevel = query({
},
});

export const adminGetMapById = adminQueryBuilder({
args: {
mapId: v.id("maps"),
},
handler: async (ctx, args) => {
return await ctx.db.get(args.mapId);
},
});

export const playMapAction = internalAction({
args: {
gameId: v.id("games"),
Expand Down

0 comments on commit 15cd47e

Please sign in to comment.