Skip to content

Commit

Permalink
Begining to glue some parts together
Browse files Browse the repository at this point in the history
  • Loading branch information
b-j-roberts committed Jan 8, 2025
1 parent 42ea806 commit cb64d1f
Show file tree
Hide file tree
Showing 15 changed files with 2,921 additions and 7,699 deletions.
12 changes: 6 additions & 6 deletions backend/config/rounds.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ type Round3 struct {
Width uint `json:"width"`
Height uint `json:"height"`
Timer uint `json:"timer"`
StartTime string `json:"startTime"`
EndTime string `json:"endTime"`
StartTime uint `json:"startTime"`
EndTime uint `json:"endTime"`
}

type RoundsConfig struct {
Expand All @@ -19,11 +19,11 @@ type RoundsConfig struct {

var DefaultRoundsConfig = &RoundsConfig{
Round3: Round3{
Width: 518,
Height: 396,
Width: 256,
Height: 192,
Timer: 5,
StartTime: "2024-12-01T00:00:00Z",
EndTime: "2025-01-01T00:00:00Z",
StartTime: 0,
EndTime: 3000000000000,
},
}

Expand Down
11 changes: 0 additions & 11 deletions backend/routes/canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,3 @@ func getCanvas(w http.ResponseWriter, r *http.Request) {

w.Write([]byte(val))
}

type CanvasMetadata struct {
CanvasId int `json:"canvasId"`
Name string `json:"name"`
Width int `json:"width"`
Height int `json:"height"`
Host string `json:"host"`
StartTime int64 `json:"startTime"`
EndTime int64 `json:"endTime"`
FavoriteCount int `json:"favoriteCount"`
}
81 changes: 26 additions & 55 deletions backend/routes/worlds.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func InitWorldsRoutes() {
http.HandleFunc("/get-world-id", getWorldId)
http.HandleFunc("/get-world", getWorld)
http.HandleFunc("/get-worlds", getWorlds)
http.HandleFunc("/get-home-worlds", getHomeWorlds)
http.HandleFunc("/get-new-worlds", getNewWorlds)
http.HandleFunc("/get-favorite-worlds", getFavoriteWorlds)
// TODO: Hot/top use user interactivity instead of favorite count
Expand All @@ -36,7 +37,6 @@ func InitWorldsRoutes() {
http.HandleFunc("/unfavorite-world-devnet", unfavoriteWorldDevnet)
http.HandleFunc("/place-world-pixel-devnet", placeWorldPixelDevnet)
}
http.HandleFunc("/get-all-worlds", getAllWorlds)
}

func InitWorldsStaticRoutes() {
Expand Down Expand Up @@ -152,6 +152,31 @@ func getWorlds(w http.ResponseWriter, r *http.Request) {
routeutils.WriteDataJson(w, string(worlds))
}

func getHomeWorlds(w http.ResponseWriter, r *http.Request) {
// TODO: Top compute
address := r.URL.Query().Get("address")
if address == "" {
address = "0"
}

roundConfig := core.ArtPeaceBackend.RoundsConfig.Round3

query := `
SELECT
*
FROM
worlds
WHERE width = $1 AND height = $2 AND time_between_pixels = $3 AND start_time = TO_TIMESTAMP($4) AND end_time = TO_TIMESTAMP($5)
ORDER BY worlds.world_id DESC
LIMIT 13`
worlds, err := core.PostgresQueryJson[WorldData](query, roundConfig.Width, roundConfig.Height, roundConfig.Timer, roundConfig.StartTime, roundConfig.EndTime)
if err != nil {
routeutils.WriteErrorJson(w, http.StatusInternalServerError, "Failed to retrieve Worlds")
return
}
routeutils.WriteDataJson(w, string(worlds))
}

func getNewWorlds(w http.ResponseWriter, r *http.Request) {
address := r.URL.Query().Get("address")
if address == "" {
Expand Down Expand Up @@ -691,57 +716,3 @@ func doesWorldNameExist(name string) (bool, error) {
}
return *exists, nil
}

func getAllWorlds(w http.ResponseWriter, r *http.Request) {
// Get query parameters
address := r.URL.Query().Get("address")
if address == "" {
address = "0"
}

// Parse pagination
pageLength, err := strconv.Atoi(r.URL.Query().Get("pageLength"))
if err != nil || pageLength <= 0 {
pageLength = 25
}
if pageLength > 50 {
pageLength = 50
}

page, err := strconv.Atoi(r.URL.Query().Get("page"))
if err != nil || page <= 0 {
page = 1
}
offset := (page - 1) * pageLength

query := `
SELECT
worlds.*,
COALESCE(worldfavorites.favorite_count, 0) AS favorites,
COALESCE((
SELECT true FROM worldfavorites
WHERE user_address = $1
AND worldfavorites.world_id = worlds.world_id
), false) as favorited
FROM
worlds
LEFT JOIN (
SELECT
world_id,
COUNT(*) AS favorite_count
FROM
worldfavorites
GROUP BY
world_id
) worldfavorites ON worlds.world_id = worldfavorites.world_id
ORDER BY worlds.world_id DESC
LIMIT $2 OFFSET $3`

worlds, err := core.PostgresQueryJson[WorldData](query, address, pageLength, offset)
if err != nil {
routeutils.WriteErrorJson(w, http.StatusInternalServerError, "Failed to retrieve Worlds")
return
}

routeutils.WriteDataJson(w, string(worlds))
}
8 changes: 4 additions & 4 deletions configs/rounds.config.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"round3": {
"width": 518,
"height": 396,
"width": 256,
"height": 192,
"timer": 5,
"startTime": "2024-12-06T00:00:00Z",
"endTime": "2025-01-01T00:00:00Z"
"startTime": 1734654796,
"endTime": 1737333196
}
}
Loading

0 comments on commit cb64d1f

Please sign in to comment.