-
Notifications
You must be signed in to change notification settings - Fork 7
feat: GnoPub #14
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
Open
divisio74
wants to merge
28
commits into
gnoverse:main
Choose a base branch
from
divisio74:feat/gnoPub
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: GnoPub #14
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
ad29253
feat: base gnopub function
divisio74 fb7a332
feat: UpdateLabel & SellCells
divisio74 394698b
Merge branch 'main' into feat/gnoPub
leohhhn 30f9cfc
Fix: Coding style
divisio74 22cf13a
Add: test_file for GridRender
divisio74 e21b347
Fix: check CI std lib
divisio74 aae2178
Fix: incorrect English + svgbtn package
divisio74 2acd643
Fix: gnoPUB -> gnoADS
divisio74 8958999
Fix: Simplify condition body -> RenderGrid()
divisio74 9eab2e1
Global Variable and simplify RenderGrid
divisio74 9441d86
Fix: Repeated condition coordinates
divisio74 5ece175
Fix: CI check grid.gno/render.gno
divisio74 5ca4482
Fix: README.md cells->cell
divisio74 0003d77
Fix: getCoordinate
divisio74 7810b78
Fix: README.md
divisio74 49e736c
Fix: README
divisio74 c59e3d5
Fix: render->: and Capital Letter
divisio74 99f4726
Fix: Init in type.gno -> init in grid.gno
divisio74 e6f97af
Add: TXlinks for tutorial in Render
divisio74 a9d61eb
Fix: FMT CI Check for render.gno
divisio74 172ac4a
Fix: Name of the package
divisio74 57f2a12
Add: Moul/txlink import/optimisation
divisio74 c3ce434
Fix: Espace variable -> repeat
divisio74 60e1b22
Fix: FMT CI Check
divisio74 57aeb67
Fix: Wrong url
divisio74 e5b782e
Fix: Exporting function Reset
divisio74 851866e
Fix: Manage Render files
divisio74 def816b
Fix: Fmt CI Check
divisio74 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| ## GnoPUB 📺 | ||
|
|
||
| # Intro | ||
|
|
||
| I aimed to recreate an advertising space similar to the Sandbox project, but encountered several challenges when trying to paste and integrate images onto the clickable surface. | ||
|
|
||
| For now, the concept focuses on building an advertising grid where individual slots can be bought and sold. Once a plot is purchased, it becomes a link to the buyer’s advertisement, which can be configured using the `UpdateCell` function. | ||
| Each ad is tied to a specific cell, allowing users to own multiple spaces for different promotional content. | ||
|
|
||
| # Tutorial | ||
|
|
||
| ### Step 1 | ||
| Buy a cell with the function `BuyCell` | ||
| ### Step 2 | ||
| Set the ads and the label with the function `UpdateCell` on yours cells | ||
| ### Step 3 | ||
| Sell your cells with the function `SellCell` | ||
|
|
||
| # RoadMap | ||
|
|
||
| - Add determined price (`n` / `a`) * `price` | ||
|
|
||
| Where `n is the number of cells` and `a is the number of available cells` | ||
|
|
||
| - Add number of clicks | ||
|
|
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| module = "gno.land/r/pierre115/gnopub" | ||
| gno = "0.9" |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| package gnopub | ||
|
|
||
| import ( | ||
| "chain/runtime" | ||
|
|
||
| "gno.land/p/nt/ownable" | ||
| "gno.land/p/nt/ufmt" | ||
| ) | ||
|
|
||
| var ( | ||
| OwnableAddress = ownable.NewWithAddress("g1e7r5eglptfejknps3xv2kxv6pc8wvd3wwtw6tg") | ||
|
|
||
| colors = []string{"#ffffff", "#f44336", "#2196f3", "#4caf50", "#ff9800"} // blanc, rouge, bleu, vert, orange | ||
|
|
||
| spacingCount = 23 | ||
|
|
||
| defaultlink = ufmt.Sprintf("/r/leon/hor:hall") | ||
|
|
||
| grid Grid | ||
| ) | ||
|
|
||
| const ( | ||
| cellSize = 100 | ||
| defaultcolor = "#ffffff" | ||
| ) | ||
|
|
||
| // Cell structure init | ||
| func init() { | ||
| grid = Grid{ | ||
| Width: 5, | ||
| Height: 5, | ||
| Cells: make(map[string]Cell), | ||
| } | ||
| } | ||
|
|
||
| // Function to validate the coordinates | ||
| func getCoordinate(x, y int) string { | ||
| if x < 0 || x >= grid.Width || y < 0 || y >= grid.Height { | ||
| return ufmt.Sprintf("Invalid coordinate : X=%d, Y=%d", x, y) | ||
| } | ||
| return "" | ||
| } | ||
|
|
||
| // Function to sell a cell | ||
| func SellCell(_ realm, x, y int) string { | ||
| if err := getCoordinate(x, y); err != "" { | ||
| return err | ||
| } | ||
|
|
||
| key := ufmt.Sprintf("%d,%d", x, y) | ||
| caller := runtime.PreviousRealm().Address() | ||
| cell, exists := grid.Cells[key] | ||
|
|
||
| if exists && cell.Owner == caller { | ||
| grid.Cells[key] = Cell{ | ||
| X: x, | ||
| Y: y, | ||
| Color: "", | ||
| Owner: "", | ||
| Url: "", | ||
| Label: "", | ||
| } | ||
| return ufmt.Sprintf("The cell was successfully sold.", x, y) | ||
| } | ||
|
|
||
| return "You are not the owner of this cell." | ||
| } | ||
|
|
||
| // Function to update the Ads and the Label of yours cells | ||
| func UpdateCell(_ realm, x, y int, url, label string) string { | ||
| if err := getCoordinate(x, y); err != "" { | ||
| return err | ||
| } | ||
|
|
||
| key := ufmt.Sprintf("%d,%d", x, y) | ||
| caller := runtime.PreviousRealm().Address() | ||
| cell, exists := grid.Cells[key] | ||
|
|
||
| if exists && cell.Owner == caller { | ||
| grid.Cells[key] = Cell{ | ||
| X: x, | ||
| Y: y, | ||
| Color: cell.Color, | ||
| Owner: cell.Owner, | ||
| Url: url, | ||
| Label: label, | ||
| } | ||
| return ufmt.Sprintf("The cell Url was successfully Updated!") | ||
| } | ||
|
|
||
| return "You are not the owner of this cell." | ||
| } | ||
|
|
||
| // Cell clicked function if the cells is not owned | ||
| func BuyCell(_ realm, x, y int) string { | ||
| if err := getCoordinate(x, y); err != "" { | ||
| return err | ||
| } | ||
|
|
||
| key := ufmt.Sprintf("%d,%d", x, y) | ||
| caller := runtime.PreviousRealm().Address() | ||
|
|
||
| // If the cells is not owned, buy it | ||
| cell, exists := grid.Cells[key] | ||
| if !exists || cell.Owner == "" { | ||
| grid.Cells[key] = Cell{ | ||
| X: x, | ||
| Y: y, | ||
| Color: colors[1], // red at first | ||
| Owner: caller, | ||
| Url: "", | ||
| Label: "", | ||
| } | ||
| return ufmt.Sprintf("The cell was successfully (%d,%d) purchased!", x, y) | ||
| } | ||
|
|
||
| // if it is the person who own it, change the color | ||
| if cell.Owner == caller { | ||
| currentColorIndex := 0 | ||
| for i, c := range colors { | ||
| if c == cell.Color { | ||
| currentColorIndex = i | ||
| break | ||
| } | ||
| } | ||
| nextColorIndex := (currentColorIndex + 1) % len(colors) | ||
| cell.Color = colors[nextColorIndex] | ||
| grid.Cells[key] = cell | ||
| return ufmt.Sprintf("The cells (%d,%d) was updated! New color: %s", x, y, getColorName(cell.Color)) | ||
| } | ||
|
|
||
| return "This cell is owned." | ||
| } | ||
|
|
||
| // Reset grid (admin only) | ||
| func Reset() string { | ||
| if !isAuthorized(runtime.PreviousRealm().Address()) { | ||
| panic("unauthorized") | ||
| } | ||
| grid.Cells = make(map[string]Cell) | ||
| return "The grid was successfully reset." | ||
| } | ||
|
|
||
| func isAuthorized(addr address) bool { | ||
| return addr == OwnableAddress.Owner() | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package gnopub | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| // Test for basic grid rendering | ||
| func TestRenderGrid(t *testing.T) { | ||
| got := renderGrid() | ||
|
|
||
| if got == "" { | ||
| t.Fatal("RenderGrid output is empty") | ||
| } | ||
|
|
||
| if !strings.Contains(got, "svg") { | ||
| t.Error("expected at least one SVG element in render output") | ||
| } | ||
|
|
||
| if !strings.Contains(got, "BuyCell") { | ||
| t.Error("expected 'BuyCells' tutorial step in render output") | ||
| } | ||
|
|
||
| if !strings.Contains(got, "available") { | ||
| t.Error("expected 'available' label for unowned cells") | ||
| } | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package gnopub | ||
|
|
||
| func getColorName(color string) string { | ||
| switch color { | ||
| case "#f44336": | ||
| return "Red" | ||
| case "#2196f3": | ||
| return "Blue" | ||
| case "#4caf50": | ||
| return "Green" | ||
| case "#ff9800": | ||
| return "Orange" | ||
| default: | ||
| return "White" | ||
| } | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| package gnopub | ||
|
|
||
| import ( | ||
| "chain/runtime" | ||
| "strings" | ||
|
|
||
| "gno.land/p/leon/svgbtn" | ||
| "gno.land/p/moul/txlink" | ||
| "gno.land/p/nt/ufmt" | ||
| ) | ||
|
|
||
| func Render(path string) string { | ||
| out := "# GnoADS 📺\n\n" | ||
| out += "### ADS Grid \n\n" | ||
|
|
||
| // Path handling | ||
| if path == "stats" { | ||
| return out + getStats() + "\n\n[← Retour](/r/gnopixel)" | ||
| } | ||
|
|
||
| // Main grid | ||
| out += renderGrid() + "\n\n" | ||
|
|
||
| // Tutorial | ||
| out += "## Tuto 📚\n\n" | ||
|
|
||
| out += "### Step 1: Buy a cell\n\n" | ||
| out += "Click on a white cell in the grid or use the BuyCell function:\n\n" | ||
| out += svgbtn.Button(120, 35, "#4caf50", "#ffffff", "Buy Cell (0,0)", "/r/pierre115/gnoads$help&func=BuyCell&x=0&y=0") + "\n\n" | ||
| out += "Replace x and y with your desired coordinates.\n\n" | ||
|
|
||
| out += "### Step 2: Update your cell\n\n" | ||
| out += "Set the ads URL and label for your cell:\n\n" | ||
| out += svgbtn.Button(120, 35, "#4caf50", "#ffffff", "Update Cell (0,0)", "/r/pierre115/gnoads$help&func=UpdateCell&x=0&y=0&url=https://github.com/gnolang/hackerspace/issues/122&label=test") + "\n\n" | ||
| out += "- `x`, `y`: coordinates of your cell\n" | ||
| out += "- `url`: your advertisement URL (must start with http)\n" | ||
| out += "- `label`: text to display\n\n" | ||
|
|
||
| out += "### Step 3: Change color\n\n" | ||
| out += "Click again on your owned cell to cycle through colors:\n\n" | ||
| out += svgbtn.Button(120, 35, "#4caf50", "#ffffff", "Change Color (0,0)", "/r/pierre115/gnoads$help&func=BuyCell&x=0&y=0") + "\n\n" | ||
|
|
||
| out += "### Step 4: Sell your cell\n\n" | ||
| out += "Release your cell back to the grid:\n\n" | ||
| out += svgbtn.Button(120, 35, "#4caf50", "#ffffff", "Sell Cell (0,0)", "/r/pierre115/gnoads$help&func=SellCell&x=0&y=0") + "\n\n" | ||
|
|
||
| // Utility buttons | ||
| out += "---\n\n" | ||
| out += "## Admin Pannel 🛠️\n\n" | ||
| out += svgbtn.DangerButton(100, 30, "Reset", "/r/gnopixel$help&func=reset") + "\n\n" | ||
|
|
||
| // Stats | ||
| out += getStats() | ||
|
|
||
| return out | ||
| } | ||
|
|
||
| // Render the grid | ||
| func renderGrid() string { | ||
| out := "" | ||
|
|
||
| // Grid cells | ||
| for y := 0; y < grid.Height; y++ { | ||
| for x := 0; x < grid.Width; x++ { | ||
| key := ufmt.Sprintf("%d,%d", x, y) | ||
| color := defaultcolor | ||
| link := defaultlink | ||
| // If the cell is owned, use its color | ||
| if cell, ok := grid.Cells[key]; ok { | ||
| color = cell.Color | ||
| caller := runtime.PreviousRealm().Address() | ||
|
|
||
| if cell.Url != "" { | ||
| link = cell.Url | ||
| } | ||
|
|
||
| // If the cell is owned by someone else, link to their pub if set | ||
| if cell.Owner != "" && cell.Owner != caller { | ||
| out += svgbtn.Button(cellSize, cellSize, color, "#333333", cell.Label, link) | ||
| out += strings.Repeat(" ", spacingCount) | ||
| continue | ||
| } | ||
| } | ||
| link = txlink.NewLink("BuyCell"). | ||
| AddArgs("x", ufmt.Sprintf("%d", x), "y", ufmt.Sprintf("%d", y)). | ||
| URL() | ||
| out += svgbtn.Button(cellSize, cellSize, color, "#333333", "available", link) | ||
| out += strings.Repeat(" ", spacingCount) | ||
| } | ||
| out += "\n\n" | ||
| } | ||
|
|
||
| return out | ||
| } | ||
|
|
||
| // Stats of the grid (per cells owners) | ||
| func getStats() string { | ||
| stats := make(map[address]int) | ||
| for _, cell := range grid.Cells { | ||
| if cell.Owner != "" { | ||
| stats[cell.Owner]++ | ||
| } | ||
| } | ||
|
|
||
| out := "## Stats 📊:\n" | ||
| if len(stats) == 0 { | ||
| out += "No cells used.\n" | ||
| return out | ||
| } | ||
|
|
||
| for owner, count := range stats { | ||
| out += ufmt.Sprintf("- %s: %d cell(s)\n", owner.String(), count) | ||
| } | ||
| return out | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package gnopub | ||
|
|
||
| // Cell structure | ||
| type Cell struct { | ||
| X int | ||
| Y int | ||
| Color string // Colors of the cells if owned | ||
| Owner address // Address of the owner | ||
| Url string // Url of the ads | ||
| Label string // Pseudo of the owner / name of the ads | ||
| } | ||
|
|
||
| // Global grid variable | ||
| type Grid struct { | ||
| Width int | ||
| Height int | ||
| Cells map[string]Cell | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.