Skip to content

Commit

Permalink
merged with main
Browse files Browse the repository at this point in the history
  • Loading branch information
supreme2580 committed Dec 16, 2024
2 parents 4d2a087 + fc7bb19 commit 2916b67
Show file tree
Hide file tree
Showing 9 changed files with 367 additions and 147 deletions.
8 changes: 7 additions & 1 deletion backend/cmd/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func isFlagSet(name string) bool {
}

func main() {
roundsConfigFilename := flag.String("rounds-config", config.DefaultRoundsConfigPath, "Rounds config file")
canvasConfigFilename := flag.String("canvas-config", config.DefaultCanvasConfigPath, "Canvas config file")
databaseConfigFilename := flag.String("database-config", config.DefaultDatabaseConfigPath, "Database config file")
backendConfigFilename := flag.String("backend-config", config.DefaultBackendConfigPath, "Backend config file")
Expand All @@ -27,6 +28,11 @@ func main() {

flag.Parse()

roundsConfig, err := config.LoadRoundsConfig(*roundsConfigFilename)
if err != nil {
panic(err)
}

canvasConfig, err := config.LoadCanvasConfig(*canvasConfigFilename)
if err != nil {
panic(err)
Expand All @@ -49,7 +55,7 @@ func main() {
databases := core.NewDatabases(databaseConfig)
defer databases.Close()

core.ArtPeaceBackend = core.NewBackend(databases, canvasConfig, backendConfig, *admin)
core.ArtPeaceBackend = core.NewBackend(databases, roundsConfig, canvasConfig, backendConfig, *admin)

routes.InitRoutes()

Expand Down
8 changes: 7 additions & 1 deletion backend/cmd/consumer/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ func isFlagSet(name string) bool {
}

func main() {
roundsConfigFilename := flag.String("rounds-config", config.DefaultRoundsConfigPath, "Rounds config file")
canvasConfigFilename := flag.String("canvas-config", config.DefaultCanvasConfigPath, "Canvas config file")
databaseConfigFilename := flag.String("database-config", config.DefaultDatabaseConfigPath, "Database config file")
backendConfigFilename := flag.String("backend-config", config.DefaultBackendConfigPath, "Backend config file")
production := flag.Bool("production", false, "Production mode")

flag.Parse()

roundsConfig, err := config.LoadRoundsConfig(*roundsConfigFilename)
if err != nil {
panic(err)
}

canvasConfig, err := config.LoadCanvasConfig(*canvasConfigFilename)
if err != nil {
panic(err)
Expand All @@ -49,7 +55,7 @@ func main() {
databases := core.NewDatabases(databaseConfig)
defer databases.Close()

core.ArtPeaceBackend = core.NewBackend(databases, canvasConfig, backendConfig, false)
core.ArtPeaceBackend = core.NewBackend(databases, roundsConfig, canvasConfig, backendConfig, false)

routes.InitBaseRoutes()
indexer.InitIndexerRoutes()
Expand Down
8 changes: 7 additions & 1 deletion backend/cmd/video-gen/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ import (
)

func main() {
roundsConfigFilename := flag.String("rounds-config", config.DefaultRoundsConfigPath, "Rounds config file")
canvasConfigFilename := flag.String("canvas-config", config.DefaultCanvasConfigPath, "Canvas config file")
databaseConfigFilename := flag.String("database-config", config.DefaultDatabaseConfigPath, "Database config file")
backendConfigFilename := flag.String("backend-config", config.DefaultBackendConfigPath, "Backend config file")

flag.Parse()

roundsConfig, err := config.LoadRoundsConfig(*roundsConfigFilename)
if err != nil {
panic(err)
}

canvasConfig, err := config.LoadCanvasConfig(*canvasConfigFilename)
if err != nil {
panic(err)
Expand All @@ -34,7 +40,7 @@ func main() {
databases := core.NewDatabases(databaseConfig)
defer databases.Close()

core.ArtPeaceBackend = core.NewBackend(databases, canvasConfig, backendConfig, true)
core.ArtPeaceBackend = core.NewBackend(databases, roundsConfig, canvasConfig, backendConfig, true)

routes.InitBaseRoutes()
routes.InitCanvasRoutes()
Expand Down
47 changes: 47 additions & 0 deletions backend/config/rounds.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package config

import (
"encoding/json"
"os"
)

type Round3 struct {
Width uint `json:"width"`
Height uint `json:"height"`
Timer uint `json:"timer"`
StartTime string `json:"startTime"`
EndTime string `json:"endTime"`
}

type RoundsConfig struct {
Round3 Round3 `json:"round3"`
}

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

var DefaultRoundsConfigPath = "../configs/rounds.config.json"

func LoadRoundsConfig(roundsConfigPath string) (*RoundsConfig, error) {
roundsConfig := &RoundsConfig{}

roundsConfigFile, err := os.Open(roundsConfigPath)
if err != nil {
return nil, err
}
defer roundsConfigFile.Close()

jsonParser := json.NewDecoder(roundsConfigFile)
if err = jsonParser.Decode(roundsConfig); err != nil {
return nil, err
}

return roundsConfig, nil
}
4 changes: 3 additions & 1 deletion backend/core/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Backend struct {
WSConnections []*websocket.Conn
WSConnectionsLock sync.Mutex

RoundsConfig *config.RoundsConfig
CanvasConfig *config.CanvasConfig
BackendConfig *config.BackendConfig

Expand All @@ -23,9 +24,10 @@ type Backend struct {

var ArtPeaceBackend *Backend

func NewBackend(databases *Databases, canvasConfig *config.CanvasConfig, backendConfig *config.BackendConfig, adminMode bool) *Backend {
func NewBackend(databases *Databases, roundsConfig *config.RoundsConfig, canvasConfig *config.CanvasConfig, backendConfig *config.BackendConfig, adminMode bool) *Backend {
return &Backend{
Databases: databases,
RoundsConfig: roundsConfig,
CanvasConfig: canvasConfig,
BackendConfig: backendConfig,
AdminMode: adminMode,
Expand Down
31 changes: 31 additions & 0 deletions backend/routes/rounds.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package routes

import (
"encoding/json"
"net/http"

"github.com/keep-starknet-strange/art-peace/backend/core"
routeutils "github.com/keep-starknet-strange/art-peace/backend/routes/utils"
)

func InitRoundsRoutes() {
http.HandleFunc("/get-rounds-config", getRoundsConfig)
}

func getRoundsConfig(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
routeutils.WriteErrorJson(w, http.StatusMethodNotAllowed, "Method not allowed")
return
}

config := core.ArtPeaceBackend.RoundsConfig

// Marshal the config to JSON
configJson, err := json.Marshal(config)
if err != nil {
routeutils.WriteErrorJson(w, http.StatusInternalServerError, "Error marshalling config to JSON")
return
}

routeutils.WriteDataJson(w, string(configJson))
}
1 change: 1 addition & 0 deletions backend/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ func InitRoutes() {
InitWorldsRoutes()
InitStencilsRoutes()
InitStencilsStaticRoutes()
InitRoundsRoutes()
}
9 changes: 9 additions & 0 deletions configs/rounds.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"round3": {
"width": 256,
"height": 192,
"timer": 5,
"startTime": "2024-12-06T00:00:00Z",
"endTime": "2025-01-01T00:00:00Z"
}
}
Loading

0 comments on commit 2916b67

Please sign in to comment.