Skip to content

Commit

Permalink
Add newclear endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Flashfyre committed May 10, 2024
1 parent e97e5f7 commit 2704e64
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ secret.key
*.iml
*.ipr
*.iws
.vscode/launch.json
1 change: 1 addition & 0 deletions api/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func Init(mux *http.ServeMux) {
mux.HandleFunc("POST /savedata/update", handleSaveData)
mux.HandleFunc("GET /savedata/delete", handleSaveData)
mux.HandleFunc("POST /savedata/clear", handleSaveData)
mux.HandleFunc("GET /savedata/newclear", handleNewClear)

// daily
mux.HandleFunc("GET /daily/seed", handleDailySeed)
Expand Down
31 changes: 31 additions & 0 deletions api/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,37 @@ func handleSaveData(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
}

func handleNewClear(w http.ResponseWriter, r *http.Request) {
uuid, err := uuidFromRequest(r)
if err != nil {
httpError(w, r, err, http.StatusBadRequest)
return
}

var slot int
if r.URL.Query().Has("slot") {
slot, err = strconv.Atoi(r.URL.Query().Get("slot"))
if err != nil {
httpError(w, r, err, http.StatusBadRequest)
return
}
}

newClear, err := savedata.NewClear(uuid, slot)
if err != nil {
httpError(w, r, fmt.Errorf("failed to read new clear: %s", err), http.StatusInternalServerError)
return
}

err = json.NewEncoder(w).Encode(newClear)
if err != nil {
httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/json")
}

// daily

func handleDailySeed(w http.ResponseWriter, r *http.Request) {
Expand Down
5 changes: 3 additions & 2 deletions api/savedata/clear.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ package savedata

import (
"fmt"
"log"

"github.com/pagefaultgames/rogueserver/db"
"github.com/pagefaultgames/rogueserver/defs"
"log"
)

type ClearResponse struct {
Expand Down Expand Up @@ -56,7 +57,7 @@ func Clear(uuid []byte, slot int, seed string, save defs.SessionSaveData) (Clear
}

if sessionCompleted {
response.Success, err = db.TryAddDailyRunCompletion(uuid, save.Seed, int(save.GameMode))
response.Success, err = db.TryAddSeedCompletion(uuid, save.Seed, int(save.GameMode))
if err != nil {
log.Printf("failed to mark seed as completed: %s", err)
}
Expand Down
44 changes: 44 additions & 0 deletions api/savedata/newclear.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Copyright (C) 2024 Pagefault Games
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package savedata

import (
"fmt"

"github.com/pagefaultgames/rogueserver/db"
"github.com/pagefaultgames/rogueserver/defs"
)

// /savedata/newclear - return whether a session is a new clear for its seed
func NewClear(uuid []byte, slot int) (bool, error) {
if slot < 0 || slot >= defs.SessionSlotCount {
return false, fmt.Errorf("slot id %d out of range", slot)
}

session, err := db.ReadSessionSaveData(uuid, slot)
if err != nil {
return false, err
}

completed, err := db.ReadSeedCompleted(uuid, session.Seed)
if err != nil {
return false, fmt.Errorf("failed to read seed completed: %s", err)
}

return !completed, nil
}
12 changes: 11 additions & 1 deletion db/savedata.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/pagefaultgames/rogueserver/defs"
)

func TryAddDailyRunCompletion(uuid []byte, seed string, mode int) (bool, error) {
func TryAddSeedCompletion(uuid []byte, seed string, mode int) (bool, error) {
var count int
err := handle.QueryRow("SELECT COUNT(*) FROM dailyRunCompletions WHERE uuid = ? AND seed = ?", uuid, seed).Scan(&count)
if err != nil {
Expand All @@ -41,6 +41,16 @@ func TryAddDailyRunCompletion(uuid []byte, seed string, mode int) (bool, error)
return true, nil
}

func ReadSeedCompleted(uuid []byte, seed string) (bool, error) {
var count int
err := handle.QueryRow("SELECT COUNT(*) FROM dailyRunCompletions WHERE uuid = ? AND seed = ?", uuid, seed).Scan(&count)
if err != nil {
return false, err
}

return count > 0, nil
}

func ReadSystemSaveData(uuid []byte) (defs.SystemSaveData, error) {
var system defs.SystemSaveData

Expand Down

0 comments on commit 2704e64

Please sign in to comment.