From 96f8a74396ad8eca354582d7eb0f18ad61dbd1c3 Mon Sep 17 00:00:00 2001 From: maru Date: Mon, 22 Apr 2024 18:32:45 -0400 Subject: [PATCH] Unduplicate more code --- api/savedata/update.go | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/api/savedata/update.go b/api/savedata/update.go index 0ff8f6f..7c75dbc 100644 --- a/api/savedata/update.go +++ b/api/savedata/update.go @@ -29,8 +29,6 @@ func Update(uuid []byte, slot int, save any) error { } var filename string - var buf bytes.Buffer - switch save := save.(type) { case defs.SystemSaveData: // System if save.TrainerId == 0 && save.SecretId == 0 { @@ -45,13 +43,8 @@ func Update(uuid []byte, slot int, save any) error { if err != nil { return fmt.Errorf("failed to update account stats: %s", err) } - - filename = "system" - err = gob.NewEncoder(&buf).Encode(save) - if err != nil { - return fmt.Errorf("failed to serialize save: %s", err) - } + filename = "system" db.DeleteClaimedAccountCompensations(uuid) case defs.SessionSaveData: // Session @@ -63,15 +56,16 @@ func Update(uuid []byte, slot int, save any) error { if slot != 0 { filename += strconv.Itoa(slot) } - - err = gob.NewEncoder(&buf).Encode(save) - if err != nil { - return fmt.Errorf("failed to serialize save: %s", err) - } default: return fmt.Errorf("invalid data type") } + var buf bytes.Buffer + err = gob.NewEncoder(&buf).Encode(save) + if err != nil { + return fmt.Errorf("failed to serialize save: %s", err) + } + if buf.Len() == 0 { return fmt.Errorf("tried to write empty save file") }