diff --git a/api/savedata.go b/api/savedata.go index 260a744..2a1cffc 100644 --- a/api/savedata.go +++ b/api/savedata.go @@ -34,6 +34,17 @@ func (s *Server) handleSavedataGet(w http.ResponseWriter, r *http.Request) { return } + compensations, err := db.FetchAndClaimAccountCompensations(uuid) + if err != nil { + httpError(w, r, fmt.Sprintf("failed to fetch compensations: %s", err), http.StatusInternalServerError) + return + } + + for k, v := range compensations { + typeKey := strconv.Itoa(k) + system.VoucherCounts[typeKey] += v + } + saveJson, err := json.Marshal(system) if err != nil { httpError(w, r, fmt.Sprintf("failed to marshal save to json: %s", err), http.StatusInternalServerError) @@ -133,6 +144,8 @@ func (s *Server) handleSavedataUpdate(w http.ResponseWriter, r *http.Request) { httpError(w, r, fmt.Sprintf("failed to write save file: %s", err), http.StatusInternalServerError) return } + + db.DeleteClaimedAccountCompensations(uuid) case "1": // Session slotId, err := strconv.Atoi(r.URL.Query().Get("slot")) if err != nil { diff --git a/db/account.go b/db/account.go index b9d02ec..319a1cf 100644 --- a/db/account.go +++ b/db/account.go @@ -100,6 +100,43 @@ func UpdateAccountStats(uuid []byte, stats defs.GameStats) error { return nil } +func FetchAndClaimAccountCompensations(uuid []byte) (map[int]int, error) { + var compensations = make(map[int]int) + + results, err := handle.Query("SELECT voucherType, count FROM accountCompensations WHERE uuid = ?", uuid) + if err != nil { + return nil, err + } + + defer results.Close() + + for results.Next() { + var voucherType int + var count int + err := results.Scan(&voucherType, &count) + if err != nil { + return compensations, err + } + compensations[voucherType] = count + } + + _, err = handle.Exec("UPDATE accountCompensations SET claimed = 1 WHERE uuid = ?", uuid) + if err != nil { + return compensations, err + } + + return compensations, nil +} + +func DeleteClaimedAccountCompensations(uuid []byte) error { + _, err := handle.Exec("DELETE FROM accountCompensations WHERE uuid = ? AND claimed = 1", uuid) + if err != nil { + return err + } + + return nil +} + func FetchUsernameFromToken(token []byte) (string, error) { var username string err := handle.QueryRow("SELECT a.username FROM accounts a JOIN sessions s ON s.uuid = a.uuid WHERE s.token = ? AND s.expire > UTC_TIMESTAMP()", token).Scan(&username)