Skip to content

Commit

Permalink
Factions, quests, touch ups, ux, and other features/improvements full…
Browse files Browse the repository at this point in the history
…y integrated
  • Loading branch information
b-j-roberts committed Jun 29, 2024
1 parent 22d92b8 commit 55c4ae3
Show file tree
Hide file tree
Showing 66 changed files with 2,539 additions and 725 deletions.
6 changes: 6 additions & 0 deletions backend/config/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ type BackendScriptsConfig struct {
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"`
}

type WebSocketConfig struct {
Expand Down Expand Up @@ -56,6 +59,9 @@ var DefaultBackendConfig = BackendConfig{
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",
},
Production: false,
WebSocket: WebSocketConfig{
Expand Down
31 changes: 31 additions & 0 deletions backend/quests/claim.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package quests

import "github.com/keep-starknet-strange/art-peace/backend/core"

var QuestClaimData = map[int]func(*Quest, string) []int {
NFTMintQuestType: NFTMintQuestClaimData,
}

func (q *Quest) GetQuestClaimData(user string) []int {
if f, ok := QuestClaimData[q.Type]; ok {
return f(q, user)
}
return nil
}

func NFTMintQuestClaimData(q *Quest, user string) []int {
nftQuestInputs := NewNFTQuestInputs(q.InputData)
if nftQuestInputs.IsDaily {
tokenId, err := core.PostgresQueryOne[int]("SELECT token_id FROM NFTs WHERE minter = $1 AND day_index = $2", user, nftQuestInputs.ClaimDay)
if err != nil {
return nil
}
return []int{*tokenId}
} else {
tokenId, err := core.PostgresQueryOne[int]("SELECT token_id FROM NFTs WHERE minter = $1", user)
if err != nil {
return nil
}
return []int{*tokenId}
}
}
12 changes: 12 additions & 0 deletions backend/quests/inputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ type HodlQuestInputs struct {
Amount int
}

type NFTQuestInputs struct {
IsDaily bool
ClaimDay uint32
}

func NewPixelQuestInputs(encodedInputs []int) *PixelQuestInputs {
return &PixelQuestInputs{
PixelsNeeded: uint32(encodedInputs[0]),
Expand All @@ -37,3 +42,10 @@ func NewHodlQuestInputs(encodedInputs []int) *HodlQuestInputs {
Amount: encodedInputs[0],
}
}

func NewNFTQuestInputs(encodedInputs []int) *NFTQuestInputs {
return &NFTQuestInputs{
IsDaily: encodedInputs[0] == 1,
ClaimDay: uint32(encodedInputs[1]),
}
}
22 changes: 12 additions & 10 deletions backend/quests/quests.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,23 @@ const (
TemplateQuestType
UnruggableQuestType
VoteQuestType
ChainFactionQuestType
FactionQuestType
UsernameQuestType
)

var OnchainQuestTypes = map[string]int{
"AuthorityQuest": AuthorityQuestType,
"HodlQuest": HodlQuestType,
"NFTMintQuest": NFTMintQuestType,
"PixelQuest": PixelQuestType,
"RainbowQuest": RainbowQuestType,
"TemplateQuest": TemplateQuestType,
"UnruggableQuest": UnruggableQuestType,
"VoteQuest": VoteQuestType,
"FactionQuest": FactionQuestType,
"UsernameQuest": UsernameQuestType,
"AuthorityQuest": AuthorityQuestType,
"HodlQuest": HodlQuestType,
"NFTMintQuest": NFTMintQuestType,
"PixelQuest": PixelQuestType,
"RainbowQuest": RainbowQuestType,
"TemplateQuest": TemplateQuestType,
"UnruggableQuest": UnruggableQuestType,
"VoteQuest": VoteQuestType,
"ChainFactionQuest": ChainFactionQuestType,
"FactionQuest": FactionQuestType,
"UsernameQuest": UsernameQuestType,
}

type Quest struct {
Expand Down
50 changes: 34 additions & 16 deletions backend/quests/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ import (
)

var QuestChecks = map[int]func(*Quest, string) (int, int){
AuthorityQuestType: CheckAuthorityStatus,
HodlQuestType: CheckHodlStatus,
NFTMintQuestType: CheckNftStatus,
PixelQuestType: CheckPixelStatus,
RainbowQuestType: CheckRainbowStatus,
TemplateQuestType: CheckTemplateStatus,
UnruggableQuestType: CheckUnruggableStatus,
VoteQuestType: CheckVoteStatus,
FactionQuestType: CheckFactionStatus,
UsernameQuestType: CheckUsernameStatus,
AuthorityQuestType: CheckAuthorityStatus,
HodlQuestType: CheckHodlStatus,
NFTMintQuestType: CheckNftStatus,
PixelQuestType: CheckPixelStatus,
RainbowQuestType: CheckRainbowStatus,
TemplateQuestType: CheckTemplateStatus,
UnruggableQuestType: CheckUnruggableStatus,
VoteQuestType: CheckVoteStatus,
FactionQuestType: CheckFactionStatus,
ChainFactionQuestType: CheckChainFactionStatus,
UsernameQuestType: CheckUsernameStatus,
}

func (q *Quest) CheckStatus(user string) (progress int, needed int) {
Expand All @@ -41,12 +42,20 @@ func CheckHodlStatus(q *Quest, user string) (progress int, needed int) {
}

func CheckNftStatus(q *Quest, user string) (progress int, needed int) {
nfts_minted_by_user, err := core.PostgresQueryOne[int]("SELECT COUNT(*) FROM NFTs WHERE minter = $1", user)

if err != nil {
return 0, 1
}
return *nfts_minted_by_user, 1
nftQuestInputs := NewNFTQuestInputs(q.InputData)
if nftQuestInputs.IsDaily {
nfts_minted_by_user, err := core.PostgresQueryOne[int]("SELECT COUNT(*) FROM NFTs WHERE minter = $1 AND day_index = $2", user, nftQuestInputs.ClaimDay)
if err != nil {
return 0, 1
}
return *nfts_minted_by_user, 1
} else {
nfts_minted_by_user, err := core.PostgresQueryOne[int]("SELECT COUNT(*) FROM NFTs WHERE minter = $1", user)
if err != nil {
return 0, 1
}
return *nfts_minted_by_user, 1
}
}

func CheckPixelStatus(q *Quest, user string) (progress int, needed int) {
Expand Down Expand Up @@ -95,6 +104,15 @@ func CheckVoteStatus(q *Quest, user string) (progress int, needed int) {
return *count, 1
}

func CheckChainFactionStatus(q *Quest, user string) (progress int, needed int) {
count, err := core.PostgresQueryOne[int]("SELECT COUNT(*) FROM ChainFactionMembersInfo WHERE user_address = $1", user)
if err != nil {
return 0, 1
}

return *count, 1
}

func CheckFactionStatus(q *Quest, user string) (progress int, needed int) {
count, err := core.PostgresQueryOne[int]("SELECT COUNT(*) FROM FactionMembersInfo WHERE user_address = $1", user)
if err != nil {
Expand Down
40 changes: 40 additions & 0 deletions backend/routes/contract.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package routes

import (
"encoding/json"
"io"
"net/http"
"os"
"strconv"

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

func InitContractRoutes() {
http.HandleFunc("/get-contract-address", getContractAddress)
http.HandleFunc("/set-contract-address", setContractAddress)
http.HandleFunc("/get-game-data", getGameData)
}

func getContractAddress(w http.ResponseWriter, r *http.Request) {
Expand All @@ -33,3 +37,39 @@ func setContractAddress(w http.ResponseWriter, r *http.Request) {
os.Setenv("ART_PEACE_CONTRACT_ADDRESS", string(data))
routeutils.WriteResultJson(w, "Contract address set")
}

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

func getGameData(w http.ResponseWriter, r *http.Request) {
day, err := core.PostgresQueryOne[int](`SELECT day_index from days ORDER BY day_index DESC LIMIT 1`)
if err != nil {
routeutils.WriteErrorJson(w, http.StatusInternalServerError, "Failed to get day")
return
}

endTime := os.Getenv("ART_PEACE_END_TIME")
if endTime == "" {
routeutils.WriteErrorJson(w, http.StatusInternalServerError, "Failed to get end time")
return
}
endTimeInt, err := strconv.Atoi(endTime)
if err != nil {
routeutils.WriteErrorJson(w, http.StatusInternalServerError, "Failed to convert end time to int")
return
}

gameData := GameData{
Day: *day,
EndTime: endTimeInt,
}
jsonGameData, err := json.Marshal(gameData)
if err != nil {
routeutils.WriteErrorJson(w, http.StatusInternalServerError, "Failed to marshal game data")
return
}

routeutils.WriteDataJson(w, string(jsonGameData))
}
Loading

0 comments on commit 55c4ae3

Please sign in to comment.