Skip to content

Commit

Permalink
Templates, factions ready, touch ups, and mainnet setup
Browse files Browse the repository at this point in the history
  • Loading branch information
b-j-roberts committed Jul 8, 2024
1 parent 05274a4 commit 674619a
Show file tree
Hide file tree
Showing 84 changed files with 3,660 additions and 411 deletions.
60 changes: 32 additions & 28 deletions backend/config/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@ import (
)

type BackendScriptsConfig struct {
PlacePixelDevnet string `json:"place_pixel_devnet"`
PlaceExtraPixelsDevnet string `json:"place_extra_pixels_devnet"`
AddTemplateDevnet string `json:"add_template_devnet"`
ClaimTodayQuestDevnet string `json:"claim_today_quest_devnet"`
MintNFTDevnet string `json:"mint_nft_devnet"`
LikeNFTDevnet string `json:"like_nft_devnet"`
UnlikeNFTDevnet string `json:"unlike_nft_devnet"`
VoteColorDevnet string `json:"vote_color_devnet"`
NewUsernameDevnet string `json:"new_username_devnet"`
ChangeUsernameDevnet string `json:"change_username_devnet"`
IncreaseDayDevnet string `json:"increase_day_devnet"`
JoinChainFactionDevnet string `json:"join_chain_faction_devnet"`
JoinFactionDevnet string `json:"join_faction_devnet"`
LeaveFactionDevnet string `json:"leave_faction_devnet"`
PlacePixelDevnet string `json:"place_pixel_devnet"`
PlaceExtraPixelsDevnet string `json:"place_extra_pixels_devnet"`
AddTemplateDevnet string `json:"add_template_devnet"`
ClaimTodayQuestDevnet string `json:"claim_today_quest_devnet"`
MintNFTDevnet string `json:"mint_nft_devnet"`
LikeNFTDevnet string `json:"like_nft_devnet"`
UnlikeNFTDevnet string `json:"unlike_nft_devnet"`
VoteColorDevnet string `json:"vote_color_devnet"`
NewUsernameDevnet string `json:"new_username_devnet"`
ChangeUsernameDevnet string `json:"change_username_devnet"`
IncreaseDayDevnet string `json:"increase_day_devnet"`
JoinChainFactionDevnet string `json:"join_chain_faction_devnet"`
JoinFactionDevnet string `json:"join_faction_devnet"`
LeaveFactionDevnet string `json:"leave_faction_devnet"`
AddFactionTemplateDevnet string `json:"add_faction_template_devnet"`
RemoveFactionTemplateDevnet string `json:"remove_faction_template_devnet"`
}

type WebSocketConfig struct {
Expand Down Expand Up @@ -48,20 +50,22 @@ var DefaultBackendConfig = BackendConfig{
Port: 8080,
ConsumerPort: 8081,
Scripts: BackendScriptsConfig{
PlacePixelDevnet: "../scripts/place_pixel.sh",
PlaceExtraPixelsDevnet: "../scripts/place_extra_pixels.sh",
AddTemplateDevnet: "../scripts/add_template.sh",
ClaimTodayQuestDevnet: "../scripts/claim_today_quest.sh",
MintNFTDevnet: "../scripts/mint_nft.sh",
LikeNFTDevnet: "../scripts/like_nft.sh",
UnlikeNFTDevnet: "../scripts/unlike_nft.sh",
VoteColorDevnet: "../scripts/vote_color.sh",
NewUsernameDevnet: "../scripts/new_username.sh",
ChangeUsernameDevnet: "../scripts/change_username.sh",
IncreaseDayDevnet: "../scripts/increase_day_index.sh",
JoinChainFactionDevnet: "../scripts/join_chain_faction.sh",
JoinFactionDevnet: "../scripts/join_faction.sh",
LeaveFactionDevnet: "../scripts/leave_faction.sh",
PlacePixelDevnet: "../scripts/place_pixel.sh",
PlaceExtraPixelsDevnet: "../scripts/place_extra_pixels.sh",
AddTemplateDevnet: "../scripts/add_template.sh",
ClaimTodayQuestDevnet: "../scripts/claim_today_quest.sh",
MintNFTDevnet: "../scripts/mint_nft.sh",
LikeNFTDevnet: "../scripts/like_nft.sh",
UnlikeNFTDevnet: "../scripts/unlike_nft.sh",
VoteColorDevnet: "../scripts/vote_color.sh",
NewUsernameDevnet: "../scripts/new_username.sh",
ChangeUsernameDevnet: "../scripts/change_username.sh",
IncreaseDayDevnet: "../scripts/increase_day_index.sh",
JoinChainFactionDevnet: "../scripts/join_chain_faction.sh",
JoinFactionDevnet: "../scripts/join_faction.sh",
LeaveFactionDevnet: "../scripts/leave_faction.sh",
AddFactionTemplateDevnet: "../scripts/add_faction_template.sh",
RemoveFactionTemplateDevnet: "../scripts/remove_faction_template.sh",
},
Production: false,
WebSocket: WebSocketConfig{
Expand Down
4 changes: 3 additions & 1 deletion backend/core/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package core
import (
"fmt"
"net/http"
"sync"

"github.com/gorilla/websocket"

Expand All @@ -12,7 +13,8 @@ import (
type Backend struct {
Databases *Databases
// TODO: Is this thread safe?
WSConnections []*websocket.Conn
WSConnections []*websocket.Conn
WSConnectionsLock sync.Mutex

CanvasConfig *config.CanvasConfig
BackendConfig *config.BackendConfig
Expand Down
12 changes: 10 additions & 2 deletions backend/routes/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ func setContractAddress(w http.ResponseWriter, r *http.Request) {
}

type GameData struct {
Day int `json:"day"`
EndTime int `json:"endTime"`
Day int `json:"day"`
EndTime int `json:"endTime"`
Host string `json:"host"`
}

func getGameData(w http.ResponseWriter, r *http.Request) {
Expand All @@ -61,9 +62,16 @@ func getGameData(w http.ResponseWriter, r *http.Request) {
return
}

host := os.Getenv("ART_PEACE_HOST")
if host == "" {
routeutils.WriteErrorJson(w, http.StatusInternalServerError, "Failed to get host")
return
}

gameData := GameData{
Day: *day,
EndTime: endTimeInt,
Host: host,
}
jsonGameData, err := json.Marshal(gameData)
if err != nil {
Expand Down
11 changes: 7 additions & 4 deletions backend/routes/factions.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type FactionUserData struct {
FactionId int `json:"factionId"`
Allocation int `json:"allocation"`
Name string `json:"name"`
Leader string `json:"leader"`
Members int `json:"members"`
Joinable bool `json:"joinable"`
Icon string `json:"icon"`
Expand All @@ -47,6 +48,7 @@ type FactionUserData struct {
type FactionData struct {
FactionId int `json:"factionId"`
Name string `json:"name"`
Leader string `json:"leader"`
Members int `json:"members"`
IsMember bool `json:"isMember"`
Joinable bool `json:"joinable"`
Expand Down Expand Up @@ -165,7 +167,7 @@ func getMyFactions(w http.ResponseWriter, r *http.Request) {
// TODO: Paginate and accumulate the allocations for each faction

query := `
SELECT m.faction_id, f.allocation, f.name, COALESCE((SELECT COUNT(*) FROM factionmembersinfo WHERE faction_id = m.faction_id), 0) as members, f.joinable, COALESCE(icon, '') as icon, COALESCE(telegram, '') as telegram, COALESCE(twitter, '') as twitter, COALESCE(github, '') as github, COALESCE(site, '') as site
SELECT m.faction_id, f.allocation, f.name, f.leader, COALESCE((SELECT COUNT(*) FROM factionmembersinfo WHERE faction_id = m.faction_id), 0) as members, f.joinable, COALESCE(icon, '') as icon, COALESCE(telegram, '') as telegram, COALESCE(twitter, '') as twitter, COALESCE(github, '') as github, COALESCE(site, '') as site
FROM factionmembersinfo m
LEFT JOIN factions f ON m.faction_id = f.faction_id
LEFT JOIN FactionLinks l ON m.faction_id = l.faction_id
Expand Down Expand Up @@ -200,7 +202,7 @@ func getFactions(w http.ResponseWriter, r *http.Request) {
offset := (page - 1) * pageLength

query := `
SELECT f.faction_id, name, COALESCE((SELECT COUNT(*) FROM factionmembersinfo fm WHERE f.faction_id = fm.faction_id), 0) as members,
SELECT f.faction_id, name, leader, COALESCE((SELECT COUNT(*) FROM factionmembersinfo fm WHERE f.faction_id = fm.faction_id), 0) as members,
COALESCE((SELECT COUNT(*) FROM factionmembersinfo fm WHERE f.faction_id = fm.faction_id AND user_address = $1), 0) > 0 as is_member, f.joinable,
COALESCE(icon, '') as icon, COALESCE(telegram, '') as telegram, COALESCE(twitter, '') as twitter, COALESCE(github, '') as github, COALESCE(site, '') as site
FROM factions f
Expand All @@ -224,7 +226,7 @@ func getMyChainFactions(w http.ResponseWriter, r *http.Request) {
}

query := `
SELECT f.faction_id, name, COALESCE((SELECT COUNT(*) FROM chainfactionmembersinfo fm WHERE f.faction_id = fm.faction_id), 0) as members,
SELECT f.faction_id, name, 'N/A' as leader, COALESCE((SELECT COUNT(*) FROM chainfactionmembersinfo fm WHERE f.faction_id = fm.faction_id), 0) as members,
COALESCE((SELECT COUNT(*) FROM chainfactionmembersinfo fm WHERE f.faction_id = fm.faction_id AND user_address = $1), 0) > 0 as is_member, true as joinable,
COALESCE(icon, '') as icon, COALESCE(telegram, '') as telegram, COALESCE(twitter, '') as twitter, COALESCE(github, '') as github, COALESCE(site, '') as site
FROM chainfactionmembersinfo m
Expand All @@ -249,7 +251,7 @@ func getChainFactions(w http.ResponseWriter, r *http.Request) {
}

query := `
SELECT f.faction_id, name, COALESCE((SELECT COUNT(*) FROM chainfactionmembersinfo fm WHERE f.faction_id = fm.faction_id), 0) as members,
SELECT f.faction_id, name, 'N/A' as leader, COALESCE((SELECT COUNT(*) FROM chainfactionmembersinfo fm WHERE f.faction_id = fm.faction_id), 0) as members,
COALESCE((SELECT COUNT(*) FROM chainfactionmembersinfo fm WHERE f.faction_id = fm.faction_id AND user_address = $1), 0) > 0 as is_member, true as joinable,
COALESCE(icon, '') as icon, COALESCE(telegram, '') as telegram, COALESCE(twitter, '') as twitter, COALESCE(github, '') as github, COALESCE(site, '') as site
FROM ChainFactions f
Expand Down Expand Up @@ -368,6 +370,7 @@ func joinChainFactionDevnet(w http.ResponseWriter, r *http.Request) {
return
}

// TODO : 64 characters is the max length for a chainId
if len(chainId) > 31 {
routeutils.WriteErrorJson(w, http.StatusBadRequest, "chainId too long (max 31 characters)")
return
Expand Down
21 changes: 21 additions & 0 deletions backend/routes/indexer/faction.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,27 @@ func revertFactionCreatedEvent(event IndexerEvent) {
}
}

func processFactionLeaderChangedEvent(event IndexerEvent) {
factionIdHex := event.Event.Keys[1]
newLeader := event.Event.Data[0][2:] // Remove 0x prefix

factionId, err := strconv.ParseInt(factionIdHex, 0, 64)
if err != nil {
PrintIndexerError("processFactionLeaderChangedEvent", "Failed to parse factionId", factionIdHex, newLeader)
return
}

_, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "UPDATE Factions SET leader = $1 WHERE faction_id = $2", newLeader, factionId)
if err != nil {
PrintIndexerError("processFactionLeaderChangedEvent", "Failed to update faction leader in postgres", factionIdHex, newLeader)
return
}
}

func revertFactionLeaderChangedEvent(event IndexerEvent) {
// TODO: Implement
}

func processFactionJoinedEvent(event IndexerEvent) {
factionIdHex := event.Event.Keys[1]
userAddress := event.Event.Keys[2][2:] // Remove 0x prefix
Expand Down
Loading

0 comments on commit 674619a

Please sign in to comment.