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

Staging merge #39

Merged
merged 6 commits into from
Apr 18, 2024
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
27 changes: 27 additions & 0 deletions packages/backend/controllers/Admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,30 @@ export const kickPlayer = async (req: Request, res: Response) => {
res.status(500).json({ error: (err as Error).message });
}
};

export const varyHiddenPrivatekey = async (req: Request, res: Response) => {
try {
const { id } = req.params;
const { hiddenChars, hiddenPrivateKey, diceCount } = req.body;
const game = await Game.findById(id);

if (!game) {
return res.status(404).json({ error: "Game not found." });
}

if (diceCount < 1 || diceCount > 64) {
return res.status(400).json({ error: "Invalid dice count." });
}

game.hiddenChars = hiddenChars;
game.hiddenPrivateKey = hiddenPrivateKey;
game.diceCount = diceCount;
const updatedGame = await game.save();
const channel = ably.channels.get(`gameUpdate`);
channel.publish(`gameUpdate`, updatedGame);

res.status(200).json(updatedGame);
} catch (err) {
res.status(500).json({ error: (err as Error).message });
}
};
2 changes: 1 addition & 1 deletion packages/backend/models/Game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const gameSchema = new mongoose.Schema(
},
hiddenPrivateKey: {
type: String,
required: false,
required: true,
},
hiddenChars: {
type: Object,
Expand Down
3 changes: 2 additions & 1 deletion packages/backend/routes/admin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import express from "express";
import { createGame, changeGameMode, pauseGame, resumeGame, kickPlayer, restartWithNewPk } from "../controllers/Admin";
import { createGame, changeGameMode, pauseGame, resumeGame, kickPlayer, restartWithNewPk, varyHiddenPrivatekey } from "../controllers/Admin";
import { verifyToken } from "../middleware/auth";

const router = express.Router();
Expand All @@ -10,5 +10,6 @@ router.patch("/pause/:id", verifyToken, pauseGame);
router.patch("/resume/:id", verifyToken, resumeGame);
router.patch("/kickplayer/:id", verifyToken, kickPlayer);
router.patch("/restartwithnewpk/:id", verifyToken, restartWithNewPk);
router.patch("/varyhiddenprivatekey/:id", verifyToken, varyHiddenPrivatekey);

export default router;
65 changes: 57 additions & 8 deletions packages/nextjs/pages/game/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import Ably from "ably";
import QRCode from "qrcode.react";
import CopyToClipboard from "react-copy-to-clipboard";
import { useAccount, useBalance } from "wagmi";
import { CheckCircleIcon, DocumentDuplicateIcon } from "@heroicons/react/24/outline";
import {
CheckCircleIcon,
ChevronDoubleDownIcon,
ChevronDoubleUpIcon,
DocumentDuplicateIcon,
} from "@heroicons/react/24/outline";
import HostAnnouncement from "~~/components/dicedemo/HostAnnouncement";
import PlayerAnnouncement from "~~/components/dicedemo/PlayerAnnoucement";
import RestartWithNewPk from "~~/components/dicedemo/RestartWithNewPk";
Expand All @@ -14,7 +19,7 @@ import { Price } from "~~/components/scaffold-eth/Price";
import useGameData from "~~/hooks/useGameData";
import useSweepWallet from "~~/hooks/useSweepWallet";
import { Game } from "~~/types/game/game";
import { kickPlayer, pauseResumeGame, toggleMode } from "~~/utils/diceDemo/apiUtils";
import { kickPlayer, pauseResumeGame, toggleMode, varyHiddenPrivatekey } from "~~/utils/diceDemo/apiUtils";
import { calculateLength, compareResult, generateRandomHex } from "~~/utils/diceDemo/gameUtils";

function GamePage() {
Expand Down Expand Up @@ -357,9 +362,31 @@ function GamePage() {
<div className="font-bold py-2 border-y-white border-2 flex items-center px-4 justify-center my-2 ">
<h1 className=" tracking-wide">PRIVATE KEY</h1>
</div>
<p className=" whitespace-normal break-words px-2 blur transition duration-500 ease-in-out hover:blur-none cursor-pointer">
{Object.values(game?.hiddenPrivateKey)}
</p>
<div className="flex items-center">
<p className=" whitespace-normal break-words px-2 blur transition duration-500 ease-in-out hover:blur-none cursor-pointer w-[90%]">
{Object.values(game?.hiddenPrivateKey)}
</p>
<div>
<button
className="btn btn-sm btn-ghost tooltip tooltip-left"
data-tip="increase"
onClick={() => {
varyHiddenPrivatekey(game, token, "increase");
}}
>
<ChevronDoubleUpIcon className="text-xl font-bold h-5 w-5 " aria-hidden="true" />
</button>
<button
className="btn btn-sm btn-ghost tooltip tooltip-left"
data-tip="decrease"
onClick={() => {
varyHiddenPrivatekey(game, token, "decrease");
}}
>
<ChevronDoubleDownIcon className="text-xl font-bold h-5 w-5 " aria-hidden="true" />
</button>
</div>
</div>
</div>
)}
</div>
Expand Down Expand Up @@ -387,9 +414,31 @@ function GamePage() {
<div className="font-bold py-2 border-b-white border-2 flex items-center px-4 ">
<h1 className=" tracking-wide md:text-xl text-lg md:text-left text-center ">PRIVATE KEY</h1>
</div>
<p className="whitespace-normal break-words px-2 blur transition duration-500 ease-in-out hover:blur-none text-lg cursor-pointer">
{Object.values(game?.hiddenPrivateKey)}
</p>
<div className="flex items-center">
<p className="whitespace-normal break-words px-2 blur transition duration-500 ease-in-out hover:blur-none text-lg cursor-pointer w-[90%]">
{Object.values(game?.hiddenPrivateKey)}
</p>
<div>
<button
className="btn btn-sm btn-ghost tooltip tooltip-left"
data-tip="increase"
onClick={() => {
varyHiddenPrivatekey(game, token, "increase");
}}
>
<ChevronDoubleUpIcon className="text-xl font-bold h-5 w-5 " aria-hidden="true" />
</button>
<button
className="btn btn-sm btn-ghost tooltip tooltip-left"
data-tip="decrease"
onClick={() => {
varyHiddenPrivatekey(game, token, "decrease");
}}
>
<ChevronDoubleDownIcon className="text-xl font-bold h-5 w-5 " aria-hidden="true" />
</button>
</div>
</div>
</div>
)}

Expand Down
2 changes: 1 addition & 1 deletion packages/nextjs/server.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const serverConfig = {
isLocal: false,
isLocal: true,
localUrl: "http://localhost:6001",
liveUrl: "https://rich-ruby-cygnet-tie.cyclic.app/",
};
Expand Down
41 changes: 41 additions & 0 deletions packages/nextjs/utils/diceDemo/apiUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,49 @@ export const kickPlayer = async (game: Game, token: string, playerAddress: strin
});

const responseData = await response.json();
notification.success("Kicked " + playerAddress);
if (responseData.error) {
notification.error(responseData.error);
return;
}
};

export const varyHiddenPrivatekey = async (game: Game, token: string, vary: "increase" | "decrease") => {
let hiddenPrivateKey = game?.hiddenPrivateKey;
const hiddenChars = game?.hiddenChars;
const privateKey = game?.privateKey;
let diceCount = game?.diceCount;

const hiddCharsCopy = { ...hiddenChars };

if (vary === "increase") {
hiddenPrivateKey = "*".repeat(diceCount + 1) + privateKey.slice(diceCount + 1);
hiddCharsCopy[diceCount] = privateKey[diceCount];
diceCount++;
} else {
hiddenPrivateKey = "*".repeat(diceCount - 1) + privateKey.slice(diceCount - 1);
delete hiddCharsCopy[diceCount - 1];
diceCount--;
}

if (diceCount < 1 || diceCount > 64) {
notification.error("Invalid dice count.");
return;
}

try {
await fetch(`${serverUrl}/admin/varyhiddenprivatekey/${game?._id}`, {
method: "PATCH",
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ hiddenChars: hiddCharsCopy, hiddenPrivateKey: hiddenPrivateKey, diceCount: diceCount }),
});

notification.success("Updated hidden characters");
} catch (error) {
notification.error((error as Error).message);
return;
}
};
Loading