Add newclear endpoint

better-accounts
Flashfyre 2024-05-10 18:07:14 -04:00
parent e97e5f73d5
commit 2704e64e38
6 changed files with 91 additions and 3 deletions

1
.gitignore vendored
View File

@ -13,3 +13,4 @@ secret.key
*.iml
*.ipr
*.iws
.vscode/launch.json

View File

@ -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)

View File

@ -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) {

View File

@ -19,9 +19,10 @@ package savedata
import (
"fmt"
"log"
"github.com/pagefaultgames/rogueserver/db"
"github.com/pagefaultgames/rogueserver/defs"
"log"
)
type ClearResponse struct {
@ -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)
}

44
api/savedata/newclear.go Normal file
View File

@ -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
}

View File

@ -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 {
@ -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