Skip to content

Commit

Permalink
backend
Browse files Browse the repository at this point in the history
  • Loading branch information
supreme2580 committed Dec 18, 2024
1 parent 71ce765 commit df5da9e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
17 changes: 15 additions & 2 deletions backend/routes/stencils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"image/color"
"image/png"
"io"
"log"
"net/http"
"os"
"os/exec"
Expand Down Expand Up @@ -589,16 +590,28 @@ func addStencilDevnet(w http.ResponseWriter, r *http.Request) {
return
}

// Add the stencil
shellCmd := core.ArtPeaceBackend.BackendConfig.Scripts.AddStencilDevnet
contract := os.Getenv("CANVAS_FACTORY_CONTRACT_ADDRESS")
cmd := exec.Command(shellCmd, contract, "add_stencil", strconv.Itoa(worldId), hash, strconv.Itoa(width), strconv.Itoa(height), strconv.Itoa(position))
_, err = cmd.Output()
output, err := cmd.CombinedOutput()
if err != nil {
log.Printf("Add stencil command failed: %v\nOutput: %s\nCommand: %v", err, string(output), cmd.String())
routeutils.WriteErrorJson(w, http.StatusInternalServerError, "Failed to add stencil to devnet")
return
}

routeutils.WriteResultJson(w, "Stencil added to devnet")
// Favorite the newly created stencil
shellCmd = core.ArtPeaceBackend.BackendConfig.Scripts.FavoriteStencilDevnet
cmd = exec.Command(shellCmd, contract, "favorite_stencil", strconv.Itoa(worldId), strconv.Itoa(position))
output, err = cmd.CombinedOutput()
if err != nil {
log.Printf("Favorite stencil command failed: %v\nOutput: %s\nCommand: %v", err, string(output), cmd.String())
routeutils.WriteErrorJson(w, http.StatusInternalServerError, "Failed to favorite newly created stencil")
return
}

routeutils.WriteResultJson(w, "Stencil added and favorited")
}

func removeStencilDevnet(w http.ResponseWriter, r *http.Request) {
Expand Down
53 changes: 48 additions & 5 deletions backend/routes/worlds.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package routes

import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/exec"
Expand Down Expand Up @@ -540,13 +542,24 @@ func createCanvasDevnet(w http.ResponseWriter, r *http.Request) {
contract := os.Getenv("CANVAS_FACTORY_CONTRACT_ADDRESS")

cmd := exec.Command(shellCmd, contract, "create_canvas", host, name, uniqueName, strconv.Itoa(width), strconv.Itoa(height), strconv.Itoa(timer), strconv.Itoa(len(palette)), paletteInput, strconv.Itoa(startTime), strconv.Itoa(endTime))
_, err = cmd.Output()
output, err := cmd.CombinedOutput()
if err != nil {
log.Printf("Create canvas command failed: %v\nOutput: %s\nCommand: %v", err, string(output), cmd.String())
routeutils.WriteErrorJson(w, http.StatusInternalServerError, fmt.Sprintf("Failed to create canvas: %v", err))
return
}

// Favorite the newly created canvas
shellCmd = core.ArtPeaceBackend.BackendConfig.Scripts.FavoriteWorldDevnet
cmd = exec.Command(shellCmd, contract, "favorite_canvas", uniqueName)
output, err = cmd.CombinedOutput()
if err != nil {
routeutils.WriteErrorJson(w, http.StatusInternalServerError, "Failed to create canvas")
log.Printf("Favorite canvas command failed: %v\nOutput: %s\nCommand: %v", err, string(output), cmd.String())
routeutils.WriteErrorJson(w, http.StatusInternalServerError, fmt.Sprintf("Failed to favorite newly created canvas: %v", err))
return
}

routeutils.WriteResultJson(w, "Canvas created")
routeutils.WriteResultJson(w, "Canvas created and favorited")
}

func favoriteWorldDevnet(w http.ResponseWriter, r *http.Request) {
Expand All @@ -561,7 +574,22 @@ func favoriteWorldDevnet(w http.ResponseWriter, r *http.Request) {
return
}

worldId := (*jsonBody)["worldId"]
// Try to get worldId either directly or from worldName
var worldId string
if (*jsonBody)["worldId"] != "" {
worldId = (*jsonBody)["worldId"]
} else if (*jsonBody)["worldName"] != "" {
// Get worldId from worldName
id, err := core.PostgresQueryOne[int]("SELECT world_id FROM worlds WHERE unique_name = $1", (*jsonBody)["worldName"])
if err != nil {
routeutils.WriteErrorJson(w, http.StatusBadRequest, "Invalid world name")
return
}
worldId = strconv.Itoa(*id)
} else {
routeutils.WriteErrorJson(w, http.StatusBadRequest, "Must provide either worldId or worldName")
return
}

shellCmd := core.ArtPeaceBackend.BackendConfig.Scripts.FavoriteWorldDevnet
contract := os.Getenv("CANVAS_FACTORY_CONTRACT_ADDRESS")
Expand All @@ -588,7 +616,22 @@ func unfavoriteWorldDevnet(w http.ResponseWriter, r *http.Request) {
return
}

worldId := (*jsonBody)["worldId"]
// Try to get worldId either directly or from worldName
var worldId string
if (*jsonBody)["worldId"] != "" {
worldId = (*jsonBody)["worldId"]
} else if (*jsonBody)["worldName"] != "" {
// Get worldId from worldName
id, err := core.PostgresQueryOne[int]("SELECT world_id FROM worlds WHERE unique_name = $1", (*jsonBody)["worldName"])
if err != nil {
routeutils.WriteErrorJson(w, http.StatusBadRequest, "Invalid world name")
return
}
worldId = strconv.Itoa(*id)
} else {
routeutils.WriteErrorJson(w, http.StatusBadRequest, "Must provide either worldId or worldName")
return
}

shellCmd := core.ArtPeaceBackend.BackendConfig.Scripts.UnfavoriteWorldDevnet
contract := os.Getenv("CANVAS_FACTORY_CONTRACT_ADDRESS")
Expand Down

0 comments on commit df5da9e

Please sign in to comment.