Skip to content

Commit

Permalink
Merge branch 'main' into ft_add_template_quest
Browse files Browse the repository at this point in the history
  • Loading branch information
mubarak23 authored Apr 22, 2024
2 parents 81102f3 + 1057592 commit 9c3d028
Show file tree
Hide file tree
Showing 65 changed files with 3,928 additions and 566 deletions.
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@
"contributions": [
"code"
]
},
{
"login": "thomas192",
"name": "0xK2",
"avatar_url": "https://avatars.githubusercontent.com/u/65908739?v=4",
"profile": "https://github.com/thomas192",
"contributions": [
"code"
]
}
],
"contributorsPerLine": 7,
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ Thanks goes to these wonderful people. Follow the [contributors guide](https://g
<td align="center" valign="top" width="14.28%"><a href="https://github.com/ptisserand"><img src="https://avatars.githubusercontent.com/u/544314?v=4?s=100" width="100px;" alt="ptisserand"/><br /><sub><b>ptisserand</b></sub></a><br /><a href="https://github.com/keep-starknet-strange/art-peace/commits?author=ptisserand" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="http://mubarak23.github.io/"><img src="https://avatars.githubusercontent.com/u/7858376?v=4?s=100" width="100px;" alt="Mubarak Muhammad Aminu"/><br /><sub><b>Mubarak Muhammad Aminu</b></sub></a><br /><a href="https://github.com/keep-starknet-strange/art-peace/commits?author=mubarak23" title="Code">💻</a></td>
</tr>
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/thomas192"><img src="https://avatars.githubusercontent.com/u/65908739?v=4?s=100" width="100px;" alt="0xK2"/><br /><sub><b>0xK2</b></sub></a><br /><a href="https://github.com/keep-starknet-strange/art-peace/commits?author=thomas192" title="Code">💻</a></td>
</tr>
</tbody>
<tfoot>
<tr>
Expand Down
12 changes: 8 additions & 4 deletions backend/config/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
)

type BackendScriptsConfig struct {
PlacePixelDevnet string `json:"place_pixel_devnet"`
AddTemplateHashDevnet string `json:"add_template_hash_devnet"`
PlacePixelDevnet string `json:"place_pixel_devnet"`
PlaceExtraPixelsDevnet string `json:"place_extra_pixels_devnet"`
AddTemplateDevnet string `json:"add_template_devnet"`
MintNFTDevnet string `json:"mint_nft_devnet"`
}

type BackendConfig struct {
Expand All @@ -21,8 +23,10 @@ var DefaultBackendConfig = BackendConfig{
Host: "localhost",
Port: 8080,
Scripts: BackendScriptsConfig{
PlacePixelDevnet: "../scripts/place_pixel.sh",
AddTemplateHashDevnet: "../scripts/add_template_hash.sh",
PlacePixelDevnet: "../scripts/place_pixel.sh",
PlaceExtraPixelsDevnet: "../scripts/place_extra_pixels.sh",
AddTemplateDevnet: "../scripts/add_template.sh",
MintNFTDevnet: "../scripts/mint_nft.sh",
},
Production: false,
}
Expand Down
34 changes: 19 additions & 15 deletions backend/routes/canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,26 @@ func InitCanvasRoutes() {
}

func initCanvas(w http.ResponseWriter, r *http.Request) {
// TODO: Check if canvas already exists
totalBitSize := core.ArtPeaceBackend.CanvasConfig.Canvas.Width * core.ArtPeaceBackend.CanvasConfig.Canvas.Height * core.ArtPeaceBackend.CanvasConfig.ColorsBitWidth
totalByteSize := (totalBitSize / 8)
if totalBitSize%8 != 0 {
// Round up to nearest byte
totalByteSize += 1
if core.ArtPeaceBackend.Databases.Redis.Exists(context.Background(), "canvas").Val() == 0 {
totalBitSize := core.ArtPeaceBackend.CanvasConfig.Canvas.Width * core.ArtPeaceBackend.CanvasConfig.Canvas.Height * core.ArtPeaceBackend.CanvasConfig.ColorsBitWidth
totalByteSize := (totalBitSize / 8)
if totalBitSize%8 != 0 {
// Round up to nearest byte
totalByteSize += 1
}

// Create canvas
canvas := make([]byte, totalByteSize)
ctx := context.Background()
err := core.ArtPeaceBackend.Databases.Redis.Set(ctx, "canvas", canvas, 0).Err()
if err != nil {
panic(err)
}

fmt.Println("Canvas initialized")
} else {
fmt.Println("Canvas already initialized")
}

canvas := make([]byte, totalByteSize)
ctx := context.Background()
err := core.ArtPeaceBackend.Databases.Redis.Set(ctx, "canvas", canvas, 0).Err()
if err != nil {
panic(err)
}

fmt.Println("Canvas initialized")
}

func getCanvas(w http.ResponseWriter, r *http.Request) {
Expand Down
127 changes: 127 additions & 0 deletions backend/routes/colors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package routes

import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"

"github.com/jackc/pgx/v5"
"github.com/keep-starknet-strange/art-peace/backend/core"
)

type Colors struct {
Hex string `json:"hex"`
}

func InitColorsRoutes() {
http.HandleFunc("/get-colors", GetAllColors)
http.HandleFunc("/get-color", GetSingleColor)
http.HandleFunc("/init-colors", InitColors)
}

func GetAllColors(w http.ResponseWriter, r *http.Request) {

var colors []Colors
rows, err := core.ArtPeaceBackend.Databases.Postgres.Query(context.Background(), "SELECT hex FROM colors")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}

defer rows.Close()

for rows.Next() {
var c Colors
err := rows.Scan(&c.Hex)
if err != nil {
log.Fatalf("Scan failed: %v\n", err)
}
colors = append(colors, c)
}
if err := rows.Err(); err != nil {
log.Fatalf("Error retrieving data: %v\n", err)
}

w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)

out, err := json.Marshal(colors)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Write([]byte(out))
}

func GetSingleColor(w http.ResponseWriter, r *http.Request) {

colorKey := r.URL.Query().Get("id")
if colorKey == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("ID not provided"))
return
}

var c Colors
row := core.ArtPeaceBackend.Databases.Postgres.QueryRow(context.Background(), "SELECT hex FROM colors WHERE key = $1", colorKey)
err := row.Scan(&c.Hex)
if err != nil {
if err == pgx.ErrNoRows {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("Color not found"))
} else {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
}
return
}

w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)

out, err := json.Marshal(c)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Write([]byte(out))
}

func InitColors(w http.ResponseWriter, r *http.Request) {
// TODO: Add authentication and/or check if colors already exist
reqBody, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}

var colors []string
err = json.Unmarshal(reqBody, &colors)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}

for _, color := range colors {
_, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO colors (hex) VALUES ($1)", color)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
}

w.WriteHeader(http.StatusOK)
w.Write([]byte("Colors initialized"))
fmt.Println("Colors initialized")
}
Loading

0 comments on commit 9c3d028

Please sign in to comment.