Skip to content

Commit 5579e78

Browse files
committed
Merge branch 'main' into defend-stencil
2 parents 26e3656 + fc7bb19 commit 5579e78

File tree

19 files changed

+5415
-9604
lines changed

19 files changed

+5415
-9604
lines changed

backend/cmd/backend/backend.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func isFlagSet(name string) bool {
1919
}
2020

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

2829
flag.Parse()
2930

31+
roundsConfig, err := config.LoadRoundsConfig(*roundsConfigFilename)
32+
if err != nil {
33+
panic(err)
34+
}
35+
3036
canvasConfig, err := config.LoadCanvasConfig(*canvasConfigFilename)
3137
if err != nil {
3238
panic(err)
@@ -49,7 +55,7 @@ func main() {
4955
databases := core.NewDatabases(databaseConfig)
5056
defer databases.Close()
5157

52-
core.ArtPeaceBackend = core.NewBackend(databases, canvasConfig, backendConfig, *admin)
58+
core.ArtPeaceBackend = core.NewBackend(databases, roundsConfig, canvasConfig, backendConfig, *admin)
5359

5460
routes.InitRoutes()
5561

backend/cmd/consumer/consumer.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,19 @@ func isFlagSet(name string) bool {
2020
}
2121

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

2829
flag.Parse()
2930

31+
roundsConfig, err := config.LoadRoundsConfig(*roundsConfigFilename)
32+
if err != nil {
33+
panic(err)
34+
}
35+
3036
canvasConfig, err := config.LoadCanvasConfig(*canvasConfigFilename)
3137
if err != nil {
3238
panic(err)
@@ -49,7 +55,7 @@ func main() {
4955
databases := core.NewDatabases(databaseConfig)
5056
defer databases.Close()
5157

52-
core.ArtPeaceBackend = core.NewBackend(databases, canvasConfig, backendConfig, false)
58+
core.ArtPeaceBackend = core.NewBackend(databases, roundsConfig, canvasConfig, backendConfig, false)
5359

5460
routes.InitBaseRoutes()
5561
indexer.InitIndexerRoutes()

backend/cmd/video-gen/video.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@ import (
1010
)
1111

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

1718
flag.Parse()
1819

20+
roundsConfig, err := config.LoadRoundsConfig(*roundsConfigFilename)
21+
if err != nil {
22+
panic(err)
23+
}
24+
1925
canvasConfig, err := config.LoadCanvasConfig(*canvasConfigFilename)
2026
if err != nil {
2127
panic(err)
@@ -34,7 +40,7 @@ func main() {
3440
databases := core.NewDatabases(databaseConfig)
3541
defer databases.Close()
3642

37-
core.ArtPeaceBackend = core.NewBackend(databases, canvasConfig, backendConfig, true)
43+
core.ArtPeaceBackend = core.NewBackend(databases, roundsConfig, canvasConfig, backendConfig, true)
3844

3945
routes.InitBaseRoutes()
4046
routes.InitCanvasRoutes()

backend/config/rounds.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package config
2+
3+
import (
4+
"encoding/json"
5+
"os"
6+
)
7+
8+
type Round3 struct {
9+
Width uint `json:"width"`
10+
Height uint `json:"height"`
11+
Timer uint `json:"timer"`
12+
StartTime string `json:"startTime"`
13+
EndTime string `json:"endTime"`
14+
}
15+
16+
type RoundsConfig struct {
17+
Round3 Round3 `json:"round3"`
18+
}
19+
20+
var DefaultRoundsConfig = &RoundsConfig{
21+
Round3: Round3{
22+
Width: 256,
23+
Height: 192,
24+
Timer: 5,
25+
StartTime: "2024-12-01T00:00:00Z",
26+
EndTime: "2025-01-01T00:00:00Z",
27+
},
28+
}
29+
30+
var DefaultRoundsConfigPath = "../configs/rounds.config.json"
31+
32+
func LoadRoundsConfig(roundsConfigPath string) (*RoundsConfig, error) {
33+
roundsConfig := &RoundsConfig{}
34+
35+
roundsConfigFile, err := os.Open(roundsConfigPath)
36+
if err != nil {
37+
return nil, err
38+
}
39+
defer roundsConfigFile.Close()
40+
41+
jsonParser := json.NewDecoder(roundsConfigFile)
42+
if err = jsonParser.Decode(roundsConfig); err != nil {
43+
return nil, err
44+
}
45+
46+
return roundsConfig, nil
47+
}

backend/core/backend.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type Backend struct {
1515
WSConnections []*websocket.Conn
1616
WSConnectionsLock sync.Mutex
1717

18+
RoundsConfig *config.RoundsConfig
1819
CanvasConfig *config.CanvasConfig
1920
BackendConfig *config.BackendConfig
2021

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

2425
var ArtPeaceBackend *Backend
2526

26-
func NewBackend(databases *Databases, canvasConfig *config.CanvasConfig, backendConfig *config.BackendConfig, adminMode bool) *Backend {
27+
func NewBackend(databases *Databases, roundsConfig *config.RoundsConfig, canvasConfig *config.CanvasConfig, backendConfig *config.BackendConfig, adminMode bool) *Backend {
2728
return &Backend{
2829
Databases: databases,
30+
RoundsConfig: roundsConfig,
2931
CanvasConfig: canvasConfig,
3032
BackendConfig: backendConfig,
3133
AdminMode: adminMode,

backend/routes/indexer/worlds.go

+38-21
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,33 @@ import (
1616

1717
func processCanvasCreatedEvent(event IndexerEvent) {
1818
canvasIdHex := event.Event.Keys[1]
19-
host := event.Event.Data[0][2:] // Remove 0x prefix
20-
nameHex := event.Event.Data[1][2:] // Remove 0x prefix
21-
widthHex := event.Event.Data[2]
22-
heightHex := event.Event.Data[3]
23-
timeBetweenPixelsHex := event.Event.Data[4]
24-
colorPaletteLenHex := event.Event.Data[5]
19+
host := event.Event.Data[0][2:] // Remove 0x prefix
20+
nameHex := event.Event.Data[1][2:] // Remove 0x prefix
21+
uniqueNameHex := event.Event.Data[2][2:] // Remove 0x prefix
22+
widthHex := event.Event.Data[3]
23+
heightHex := event.Event.Data[4]
24+
timeBetweenPixelsHex := event.Event.Data[5]
25+
colorPaletteLenHex := event.Event.Data[6]
2526

2627
colorPaletteLen, err := strconv.ParseInt(colorPaletteLenHex, 0, 64)
2728
if err != nil {
28-
PrintIndexerError("processCanvasCreatedEvent", "Failed to parse colorPaletteLenHex", canvasIdHex, host, nameHex, widthHex, heightHex, timeBetweenPixelsHex, colorPaletteLenHex, err)
29+
PrintIndexerError("processCanvasCreatedEvent", "Failed to parse colorPaletteLenHex", canvasIdHex, host, nameHex, uniqueNameHex, widthHex, heightHex, timeBetweenPixelsHex, colorPaletteLenHex, err)
2930
return
3031
}
3132
// Skip colors since they are processed in another event
3233

33-
startTimeHex := event.Event.Data[6+colorPaletteLen]
34-
endTimeHex := event.Event.Data[7+colorPaletteLen]
34+
startTimeHex := event.Event.Data[7+colorPaletteLen]
35+
endTimeHex := event.Event.Data[8+colorPaletteLen]
3536

3637
canvasId, err := strconv.ParseInt(canvasIdHex, 0, 64)
3738
if err != nil {
38-
PrintIndexerError("processCanvasCreatedEvent", "Failed to parse canvasIdHex", canvasIdHex, host, nameHex, widthHex, heightHex, timeBetweenPixelsHex, colorPaletteLenHex, err)
39+
PrintIndexerError("processCanvasCreatedEvent", "Failed to parse canvasIdHex", canvasIdHex, host, nameHex, uniqueNameHex, widthHex, heightHex, timeBetweenPixelsHex, colorPaletteLenHex, err)
3940
return
4041
}
4142

4243
decodedName, err := hex.DecodeString(nameHex)
4344
if err != nil {
44-
PrintIndexerError("processCanvasCreatedEvent", "Failed to decode nameHex", canvasIdHex, host, nameHex, widthHex, heightHex, timeBetweenPixelsHex, colorPaletteLenHex, err)
45+
PrintIndexerError("processCanvasCreatedEvent", "Failed to decode nameHex", canvasIdHex, host, nameHex, uniqueNameHex, widthHex, heightHex, timeBetweenPixelsHex, colorPaletteLenHex, err)
4546
return
4647
}
4748
trimmedName := []byte{}
@@ -55,40 +56,56 @@ func processCanvasCreatedEvent(event IndexerEvent) {
5556
}
5657
name := string(trimmedName)
5758

59+
decodedUniqueName, err := hex.DecodeString(uniqueNameHex)
60+
if err != nil {
61+
PrintIndexerError("processCanvasCreatedEvent", "Failed to decode uniqueNameHex", canvasIdHex, host, nameHex, uniqueNameHex, widthHex, heightHex, timeBetweenPixelsHex, colorPaletteLenHex, err)
62+
return
63+
}
64+
trimmedUniqueName := []byte{}
65+
trimming = true
66+
for _, b := range decodedUniqueName {
67+
if b == 0 && trimming {
68+
continue
69+
}
70+
trimming = false
71+
trimmedUniqueName = append(trimmedUniqueName, b)
72+
}
73+
uniqueName := string(trimmedUniqueName)
74+
5875
width, err := strconv.ParseInt(widthHex, 0, 64)
5976
if err != nil {
60-
PrintIndexerError("processCanvasCreatedEvent", "Failed to parse widthHex", canvasIdHex, host, nameHex, widthHex, heightHex, timeBetweenPixelsHex, colorPaletteLenHex, err)
77+
PrintIndexerError("processCanvasCreatedEvent", "Failed to parse widthHex", canvasIdHex, host, nameHex, uniqueNameHex, widthHex, heightHex, timeBetweenPixelsHex, colorPaletteLenHex, err)
6178
return
6279
}
6380

6481
height, err := strconv.ParseInt(heightHex, 0, 64)
6582
if err != nil {
66-
PrintIndexerError("processCanvasCreatedEvent", "Failed to parse heightHex", canvasIdHex, host, nameHex, widthHex, heightHex, timeBetweenPixelsHex, colorPaletteLenHex, err)
83+
PrintIndexerError("processCanvasCreatedEvent", "Failed to parse heightHex", canvasIdHex, host, nameHex, uniqueNameHex, widthHex, heightHex, timeBetweenPixelsHex, colorPaletteLenHex, err)
6784
return
6885
}
6986

7087
timeBetweenPixels, err := strconv.ParseInt(timeBetweenPixelsHex, 0, 64)
7188
if err != nil {
72-
PrintIndexerError("processCanvasCreatedEvent", "Failed to parse timeBetweenPixelsHex", canvasIdHex, host, nameHex, widthHex, heightHex, timeBetweenPixelsHex, colorPaletteLenHex, err)
89+
PrintIndexerError("processCanvasCreatedEvent", "Failed to parse timeBetweenPixelsHex", canvasIdHex, host, nameHex, uniqueNameHex, widthHex, heightHex, timeBetweenPixelsHex, colorPaletteLenHex, err)
7390
return
7491
}
7592

7693
startTime, err := strconv.ParseInt(startTimeHex, 0, 64)
7794
if err != nil {
78-
PrintIndexerError("processCanvasCreatedEvent", "Failed to parse startTimeHex", canvasIdHex, host, nameHex, widthHex, heightHex, timeBetweenPixelsHex, colorPaletteLenHex, err)
95+
PrintIndexerError("processCanvasCreatedEvent", "Failed to parse startTimeHex", canvasIdHex, host, nameHex, uniqueNameHex, widthHex, heightHex, timeBetweenPixelsHex, colorPaletteLenHex, err)
7996
return
8097
}
8198

8299
endTime, err := strconv.ParseInt(endTimeHex, 0, 64)
83100
if err != nil {
84-
PrintIndexerError("processCanvasCreatedEvent", "Failed to parse endTimeHex", canvasIdHex, host, nameHex, widthHex, heightHex, timeBetweenPixelsHex, colorPaletteLenHex, err)
101+
PrintIndexerError("processCanvasCreatedEvent", "Failed to parse endTimeHex", canvasIdHex, host, nameHex, uniqueNameHex, widthHex, heightHex, timeBetweenPixelsHex, colorPaletteLenHex, err)
85102
return
86103
}
87104

88105
// Insert into Worlds
89-
_, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO Worlds (world_id, host, name, width, height, time_between_pixels, start_time, end_time) VALUES ($1, $2, $3, $4, $5, $6, TO_TIMESTAMP($7), TO_TIMESTAMP($8))", canvasId, host, name, width, height, timeBetweenPixels, startTime, endTime)
106+
_, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO Worlds (world_id, host, name, unique_name, width, height, time_between_pixels, start_time, end_time) VALUES ($1, $2, $3, $4, $5, $6, $7, TO_TIMESTAMP($8), TO_TIMESTAMP($9))", canvasId, host, name, uniqueName, width, height, timeBetweenPixels, startTime, endTime)
90107
if err != nil {
91-
PrintIndexerError("processCanvasCreatedEvent", "Failed to insert into Worlds", canvasIdHex, host, nameHex, widthHex, heightHex, timeBetweenPixelsHex, colorPaletteLenHex, err)
108+
PrintIndexerError("processCanvasCreatedEvent", "Failed to insert into Worlds", canvasIdHex, host, nameHex, uniqueNameHex, widthHex, heightHex, timeBetweenPixelsHex, colorPaletteLenHex, err)
92109
return
93110
}
94111

@@ -103,11 +120,11 @@ func processCanvasCreatedEvent(event IndexerEvent) {
103120
canvas := make([]byte, totalByteSize)
104121
err := core.ArtPeaceBackend.Databases.Redis.Set(context.Background(), canvasRedisKey, canvas, 0).Err()
105122
if err != nil {
106-
PrintIndexerError("processCanvasCreatedEvent", "Failed to set canvas in redis", canvasIdHex, host, nameHex, widthHex, heightHex, timeBetweenPixelsHex, colorPaletteLenHex, err)
123+
PrintIndexerError("processCanvasCreatedEvent", "Failed to set canvas in redis", canvasIdHex, host, nameHex, uniqueNameHex, widthHex, heightHex, timeBetweenPixelsHex, colorPaletteLenHex, err)
107124
return
108125
}
109126
} else {
110-
PrintIndexerError("processCanvasCreatedEvent", "Canvas already exists in redis", canvasIdHex, host, nameHex, widthHex, heightHex, timeBetweenPixelsHex, colorPaletteLenHex, err)
127+
PrintIndexerError("processCanvasCreatedEvent", "Canvas already exists in redis", canvasIdHex, host, nameHex, uniqueNameHex, widthHex, heightHex, timeBetweenPixelsHex, colorPaletteLenHex, err)
111128
}
112129

113130
// Create base directories if they don't exist
@@ -127,7 +144,7 @@ func processCanvasCreatedEvent(event IndexerEvent) {
127144
}
128145

129146
generatedWorldImage := image.NewRGBA(image.Rect(0, 0, int(width), int(height)))
130-
baseColorHex := event.Event.Data[6]
147+
baseColorHex := event.Event.Data[7]
131148
baseColor := baseColorHex[len(baseColorHex)-6:] // Remove prefix
132149
baseColorR, err := strconv.ParseInt(baseColor[0:2], 16, 64)
133150
if err != nil {

backend/routes/rounds.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package routes
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
7+
"github.com/keep-starknet-strange/art-peace/backend/core"
8+
routeutils "github.com/keep-starknet-strange/art-peace/backend/routes/utils"
9+
)
10+
11+
func InitRoundsRoutes() {
12+
http.HandleFunc("/get-rounds-config", getRoundsConfig)
13+
}
14+
15+
func getRoundsConfig(w http.ResponseWriter, r *http.Request) {
16+
if r.Method != http.MethodGet {
17+
routeutils.WriteErrorJson(w, http.StatusMethodNotAllowed, "Method not allowed")
18+
return
19+
}
20+
21+
config := core.ArtPeaceBackend.RoundsConfig
22+
23+
// Marshal the config to JSON
24+
configJson, err := json.Marshal(config)
25+
if err != nil {
26+
routeutils.WriteErrorJson(w, http.StatusInternalServerError, "Error marshalling config to JSON")
27+
return
28+
}
29+
30+
routeutils.WriteDataJson(w, string(configJson))
31+
}

backend/routes/routes.go

+1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ func InitRoutes() {
2828
InitWorldsRoutes()
2929
InitStencilsRoutes()
3030
InitStencilsStaticRoutes()
31+
InitRoundsRoutes()
3132
}

0 commit comments

Comments
 (0)