-
Notifications
You must be signed in to change notification settings - Fork 10
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
getFullCanvas #39
Merged
Merged
getFullCanvas #39
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,126 +4,215 @@ pragma solidity ^0.8.20; | |
import "@openzeppelin/contracts/access/Ownable.sol"; | ||
|
||
contract PixelCanvas is Ownable { | ||
// Canvas Dimensions | ||
uint256 public constant CANVAS_WIDTH = 64; | ||
uint256 public constant CANVAS_HEIGHT = 64; | ||
|
||
// Color Palette (Limited Options) | ||
enum Color { | ||
WHITE, | ||
BLACK, | ||
RED, | ||
GREEN, | ||
BLUE, | ||
YELLOW, | ||
PURPLE, | ||
ORANGE | ||
} | ||
|
||
// Pixel Structure | ||
struct Pixel { | ||
address author; | ||
Color color; | ||
uint256 timestamp; | ||
} | ||
|
||
// Mapping to store modified pixels | ||
mapping(uint256 => mapping(uint256 => Pixel)) public canvas; | ||
|
||
|
||
// Events | ||
event PixelPlaced(address indexed author, uint256 x, uint256 y, Color color); | ||
event Withdrawal(address indexed owner, uint256 amount); | ||
|
||
// Constructor to initialize default Buidlguidl Batch11 drawing | ||
constructor() Ownable(msg.sender){ | ||
|
||
// Initial drawing representing Buidlguidl Batch11 logo | ||
initializeBuidlGuidlLogo(); | ||
} | ||
|
||
/** | ||
* @dev Initialize a default Buidlguidl Batch11 inspired pixel art | ||
* This is a simplified representation and can be customized | ||
*/ | ||
function initializeBuidlGuidlLogo() private { | ||
for (uint256 x = 10; x < 20; x++) { | ||
for (uint256 y = 10; y < 50; y++) { | ||
canvas[x][y] = Pixel({ | ||
author: msg.sender, | ||
color: Color.BLUE, | ||
timestamp: block.timestamp | ||
}); | ||
} | ||
} | ||
|
||
// 11 representation with some pixels | ||
for (uint256 x = 30; x < 40; x++) { | ||
for (uint256 y = 20; y < 30; y++) { | ||
canvas[x][y] = Pixel({ | ||
author: msg.sender, | ||
color: Color.GREEN, | ||
timestamp: block.timestamp | ||
}); | ||
} | ||
} | ||
|
||
// Add some distinctive pixels to represent Buidlguidl spirit | ||
canvas[32][25] = Pixel({ | ||
author: msg.sender, | ||
color: Color.RED, | ||
timestamp: block.timestamp | ||
}); | ||
} | ||
|
||
/** | ||
* @dev Place a pixel on the canvas | ||
* @param x X-coordinate of the pixel | ||
* @param y Y-coordinate of the pixel | ||
* @param color Color of the pixel | ||
*/ | ||
function placePixel(uint256 x, uint256 y, Color color) external { | ||
require(x < CANVAS_WIDTH, "X coordinate out of bounds"); | ||
require(y < CANVAS_HEIGHT, "Y coordinate out of bounds"); | ||
|
||
canvas[x][y] = Pixel({ | ||
author: msg.sender, | ||
color: color, | ||
timestamp: block.timestamp | ||
}); | ||
|
||
emit PixelPlaced(msg.sender, x, y, color); | ||
} | ||
|
||
|
||
/** | ||
* @dev Get pixel information | ||
* @param x X-coordinate of the pixel | ||
* @param y Y-coordinate of the pixel | ||
* @return Pixel details | ||
*/ | ||
function getPixel(uint256 x, uint256 y) external view returns (Pixel memory) { | ||
require(x < CANVAS_WIDTH, "X coordinate out of bounds"); | ||
require(y < CANVAS_HEIGHT, "Y coordinate out of bounds"); | ||
return canvas[x][y]; | ||
} | ||
|
||
|
||
|
||
function withdraw() external onlyOwner { | ||
uint256 balance = address(this).balance; | ||
require(balance > 0, "No funds to withdraw"); | ||
|
||
|
||
(bool success, ) = payable(owner()).call{value: balance}(""); | ||
require(success, "Transfer failed"); | ||
|
||
|
||
emit Withdrawal(owner(), balance); | ||
} | ||
|
||
// Fallback and receive functions to accept Ether | ||
fallback() external payable {} | ||
|
||
receive() external payable {} | ||
// Canvas Dimensions | ||
uint256 public constant CANVAS_WIDTH = 64; | ||
uint256 public constant CANVAS_HEIGHT = 64; | ||
|
||
// Color Palette (Limited Options) | ||
enum Color { | ||
WHITE, | ||
BLACK, | ||
RED, | ||
GREEN, | ||
BLUE, | ||
YELLOW, | ||
PURPLE, | ||
ORANGE | ||
} | ||
|
||
// Pixel Structure | ||
struct Pixel { | ||
address author; | ||
Color color; | ||
uint256 timestamp; | ||
} | ||
|
||
// Mapping to store modified pixels | ||
mapping(uint256 => mapping(uint256 => Pixel)) public canvas; | ||
|
||
// Events | ||
event PixelPlaced( | ||
address indexed author, | ||
uint256 x, | ||
uint256 y, | ||
Color color | ||
); | ||
event Withdrawal(address indexed owner, uint256 amount); | ||
|
||
// Constructor to initialize default Buidlguidl Batch11 drawing | ||
constructor() Ownable(msg.sender) { | ||
// Initial drawing representing Buidlguidl Batch11 logo | ||
initializeBuidlGuidlLogo(); | ||
} | ||
|
||
/** | ||
* @dev Initialize a default Buidlguidl Batch11 inspired pixel art | ||
* This is a simplified representation and can be customized | ||
*/ | ||
function initializeBuidlGuidlLogo() private { | ||
uint256 startx = 28; | ||
uint256 starty = 28; | ||
|
||
canvas[startx][starty + 1] = Pixel({ | ||
author: msg.sender, | ||
color: Color.BLACK, | ||
timestamp: block.timestamp | ||
}); | ||
|
||
canvas[startx + 1][starty] = Pixel({ | ||
author: msg.sender, | ||
color: Color.BLACK, | ||
timestamp: block.timestamp | ||
}); | ||
|
||
canvas[startx + 1][starty + 1] = Pixel({ | ||
author: msg.sender, | ||
color: Color.BLACK, | ||
timestamp: block.timestamp | ||
}); | ||
|
||
canvas[startx + 1][starty + 2] = Pixel({ | ||
author: msg.sender, | ||
color: Color.BLACK, | ||
timestamp: block.timestamp | ||
}); | ||
|
||
canvas[startx + 1][starty + 3] = Pixel({ | ||
author: msg.sender, | ||
color: Color.BLACK, | ||
timestamp: block.timestamp | ||
}); | ||
|
||
canvas[startx + 1][starty + 4] = Pixel({ | ||
author: msg.sender, | ||
color: Color.BLACK, | ||
timestamp: block.timestamp | ||
}); | ||
|
||
canvas[startx][starty + 4] = Pixel({ | ||
author: msg.sender, | ||
color: Color.BLACK, | ||
timestamp: block.timestamp | ||
}); | ||
canvas[startx + 2][starty + 4] = Pixel({ | ||
author: msg.sender, | ||
color: Color.BLACK, | ||
timestamp: block.timestamp | ||
}); | ||
|
||
startx = 32; | ||
|
||
canvas[startx][starty + 1] = Pixel({ | ||
author: msg.sender, | ||
color: Color.BLACK, | ||
timestamp: block.timestamp | ||
}); | ||
|
||
canvas[startx + 1][starty] = Pixel({ | ||
author: msg.sender, | ||
color: Color.BLACK, | ||
timestamp: block.timestamp | ||
}); | ||
|
||
canvas[startx + 1][starty + 1] = Pixel({ | ||
author: msg.sender, | ||
color: Color.BLACK, | ||
timestamp: block.timestamp | ||
}); | ||
|
||
canvas[startx + 1][starty + 2] = Pixel({ | ||
author: msg.sender, | ||
color: Color.BLACK, | ||
timestamp: block.timestamp | ||
}); | ||
|
||
canvas[startx + 1][starty + 3] = Pixel({ | ||
author: msg.sender, | ||
color: Color.BLACK, | ||
timestamp: block.timestamp | ||
}); | ||
|
||
canvas[startx + 1][starty + 4] = Pixel({ | ||
author: msg.sender, | ||
color: Color.BLACK, | ||
timestamp: block.timestamp | ||
}); | ||
|
||
canvas[startx][starty + 4] = Pixel({ | ||
author: msg.sender, | ||
color: Color.BLACK, | ||
timestamp: block.timestamp | ||
}); | ||
canvas[startx + 2][starty + 4] = Pixel({ | ||
author: msg.sender, | ||
color: Color.BLACK, | ||
timestamp: block.timestamp | ||
}); | ||
} | ||
|
||
/** | ||
* @dev Place a pixel on the canvas | ||
* @param x X-coordinate of the pixel | ||
* @param y Y-coordinate of the pixel | ||
* @param color Color of the pixel | ||
*/ | ||
function placePixel(uint256 x, uint256 y, Color color) external { | ||
require(x < CANVAS_WIDTH, "X coordinate out of bounds"); | ||
require(y < CANVAS_HEIGHT, "Y coordinate out of bounds"); | ||
|
||
canvas[x][y] = Pixel({ | ||
author: msg.sender, | ||
color: color, | ||
timestamp: block.timestamp | ||
}); | ||
|
||
emit PixelPlaced(msg.sender, x, y, color); | ||
} | ||
|
||
/** | ||
* @dev Get pixel information | ||
* @param x X-coordinate of the pixel | ||
* @param y Y-coordinate of the pixel | ||
* @return Pixel details | ||
*/ | ||
function getPixel( | ||
uint256 x, | ||
uint256 y | ||
) external view returns (Pixel memory) { | ||
require(x < CANVAS_WIDTH, "X coordinate out of bounds"); | ||
require(y < CANVAS_HEIGHT, "Y coordinate out of bounds"); | ||
return canvas[x][y]; | ||
} | ||
|
||
/** | ||
* @dev Get the entire canvas as a 2D array of pixels | ||
* @return Array of arrays containing all pixels | ||
*/ | ||
function getFullCanvas() external view returns (Pixel[][] memory) { | ||
Pixel[][] memory fullCanvas = new Pixel[][](CANVAS_WIDTH); | ||
|
||
for (uint256 x = 0; x < CANVAS_WIDTH; x++) { | ||
fullCanvas[x] = new Pixel[](CANVAS_HEIGHT); | ||
for (uint256 y = 0; y < CANVAS_HEIGHT; y++) { | ||
fullCanvas[x][y] = canvas[x][y]; | ||
} | ||
} | ||
|
||
return fullCanvas; | ||
} | ||
Comment on lines
+191
to
+202
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In here you reintroducing the array again, we wanted to get rid of and exchanged it with a mapping. |
||
|
||
function withdraw() external onlyOwner { | ||
uint256 balance = address(this).balance; | ||
require(balance > 0, "No funds to withdraw"); | ||
|
||
(bool success, ) = payable(owner()).call{ value: balance }(""); | ||
require(success, "Transfer failed"); | ||
|
||
emit Withdrawal(owner(), balance); | ||
} | ||
|
||
// Fallback and receive functions to accept Ether | ||
fallback() external payable {} | ||
|
||
receive() external payable {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's very repetitive, why not having a loop? What costs gas is writing to the blockchain.