Skip to content

Commit

Permalink
Color pixel websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
b-j-roberts committed Feb 21, 2025
1 parent 348714b commit 7326d74
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
14 changes: 13 additions & 1 deletion frontend-next/src/components/canvas/canvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,26 @@ export const Canva = (props: any) => {
}
useEffect(() => {
if (!props.gameUpdate) return;
if (props.gameUpdate.type !== "pixel") return;
if (props.gameUpdate.messageType !== "pixel") return;
if (props.gameUpdate.worldId !== props.worldId) return;
const x = props.gameUpdate.position % props.width;
const y = Math.floor(props.gameUpdate.position / props.width);
colorPixel(x, y, props.gameUpdate.colorId);
props.setLastPlacedTime(props.gameUpdate.timestamp * 1000);
props.setGameUpdate(null);
}, [props.gameUpdate]);
useEffect(() => {
if (!props.gameUpdate) return;
if (props.gameUpdate.messageType !== "colorWorldPixel") return;
if (parseInt(props.gameUpdate.worldId) !== props.worldId) return;
const strPos = props.gameUpdate.position;
const numPos = parseInt(strPos);
const x = numPos % props.width;
const y = Math.floor(numPos / props.width);
const colorId = parseInt(props.gameUpdate.color);
colorPixel(x, y, colorId);
props.setGameUpdate(null);
}, [props.gameUpdate]);

const addStagingPixel = (x: number, y: number, colorId: number) => {
let newStaging = props.stagingPixels;
Expand Down
11 changes: 8 additions & 3 deletions frontend-next/src/screens/canvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ const Canvas = (props: any) => {
const stagedPixel = stagedPixels[0];
stagedPixels = stagedPixels.slice(1);
updateGame({
type: "pixel",
messageType: "pixel",
position: stagedPixel.position,
colorId: stagedPixel.colorId,
worldId: commitWorldId,
Expand Down Expand Up @@ -347,6 +347,9 @@ const Canvas = (props: any) => {
const updateGame = (update: any) => {
setGameUpdates([...gameUpdates, update]);
}
const updatesGame = (updates: any[]) => {
setGameUpdates([...gameUpdates, ...updates]);
}
useEffect(() => {
if (gameUpdates.length === 0) {
return;
Expand Down Expand Up @@ -378,8 +381,10 @@ const Canvas = (props: any) => {
}, [readyState]);
useEffect(() => {
const processMessage = async (message: any) => {
if (message) {
console.log(message);
const supportedTypes = ["colorWorldPixel"];
if (message && message.length > 0) {
const updates = message.filter((update: any) => supportedTypes.includes(update.messageType));
updatesGame(updates);
}
};
processMessage(lastJsonMessage);
Expand Down
4 changes: 4 additions & 0 deletions onchain/src/multi_canvas.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,10 @@ pub mod MultiCanvas {

fn check_game_running(self: @ContractState, canvas_id: u32) {
let block_timestamp = starknet::get_block_timestamp();
assert(
block_timestamp >= self.canvases.read(canvas_id).start_time,
'This world has not started.'
);
assert(block_timestamp <= self.canvases.read(canvas_id).end_time, 'This world ended.')
}

Expand Down

0 comments on commit 7326d74

Please sign in to comment.