Styling fixes

styling-fixes
Pancakes 2 months ago
parent ac360ccb1c
commit 00e783ff8a
No known key found for this signature in database
GPG Key ID: 5792877BFA27DC8F

@ -41,6 +41,7 @@ func HandleDiscordCallback(w http.ResponseWriter, r *http.Request) (string, erro
http.Redirect(w, r, GameURL, http.StatusSeeOther) http.Redirect(w, r, GameURL, http.StatusSeeOther)
return "", errors.New("code is empty") return "", errors.New("code is empty")
} }
discordId, err := RetrieveDiscordId(code) discordId, err := RetrieveDiscordId(code)
if err != nil { if err != nil {
http.Redirect(w, r, GameURL, http.StatusSeeOther) http.Redirect(w, r, GameURL, http.StatusSeeOther)

@ -75,5 +75,6 @@ func GenerateTokenForUsername(username string) (string, error) {
if err != nil { if err != nil {
return "", fmt.Errorf("failed to add account session") return "", fmt.Errorf("failed to add account session")
} }
return base64.StdEncoding.EncodeToString(token), nil return base64.StdEncoding.EncodeToString(token), nil
} }

@ -96,11 +96,7 @@ func Init() error {
return nil return nil
} }
_, err = s3scheduler.AddFunc("@hourly", func() { _, err = s3scheduler.AddFunc("@hourly", S3SaveMigration)
time.Sleep(time.Second)
S3SaveMigration()
})
if err != nil { if err != nil {
return err return err
} }
@ -129,32 +125,52 @@ func S3SaveMigration() {
svc := s3.NewFromConfig(cfg, func(o *s3.Options) { svc := s3.NewFromConfig(cfg, func(o *s3.Options) {
o.BaseEndpoint = aws.String(os.Getenv("AWS_ENDPOINT_URL_S3")) o.BaseEndpoint = aws.String(os.Getenv("AWS_ENDPOINT_URL_S3"))
}) })
// retrieve accounts from db // retrieve accounts from db
_, err := svc.CreateBucket(context.Background(), &s3.CreateBucketInput{ _, err := svc.CreateBucket(context.Background(), &s3.CreateBucketInput{
Bucket: aws.String("pokerogue-system"), Bucket: aws.String("pokerogue-system"),
}) })
if err != nil { if err != nil {
log.Printf("error while creating bucket: %s", err) log.Printf("error while creating bucket: %s", err)
} }
accounts := db.RetrieveOldAccounts() accounts, err := db.RetrieveOldAccounts()
if err != nil {
log.Printf("failed to retrieve old accounts")
return
}
for _, user := range accounts { for _, user := range accounts {
data, _ := db.ReadSystemSaveData(user) data, err := db.ReadSystemSaveData(user)
username, _ := db.FetchUsernameFromUUID(user) if err != nil {
json, _ := json.Marshal(data) continue
_, err := svc.PutObject(context.Background(), &s3.PutObjectInput{ }
username, err := db.FetchUsernameFromUUID(user)
if err != nil {
continue
}
json, err := json.Marshal(data)
if err != nil {
continue
}
_, err = svc.PutObject(context.Background(), &s3.PutObjectInput{
Bucket: aws.String("pokerogue-system"), Bucket: aws.String("pokerogue-system"),
Key: aws.String(username), Key: aws.String(username),
Body: bytes.NewReader(json), Body: bytes.NewReader(json),
}) })
if err != nil { if err != nil {
log.Printf("error while saving data in s3 for user %s: %s", username, err) log.Printf("error while saving data in s3 for user %s: %s", username, err)
continue continue
} }
err = db.UpdateLocation(user, username)
if err != nil {
continue
}
fmt.Printf("Saved data in s3 for user %s\n", username) fmt.Printf("Saved data in s3 for user %s\n", username)
db.UpdateLocation(user, username)
} }
} }

@ -1,3 +1,20 @@
/*
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 package savedata
import ( import (

@ -1,3 +1,20 @@
/*
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 package savedata
import ( import (

@ -86,7 +86,6 @@ func AddDiscordIdByUUID(discordId string, uuid []byte) error {
return nil return nil
} }
func FetchUsernameByDiscordId(discordId string) (string, error) { func FetchUsernameByDiscordId(discordId string) (string, error) {
var username string var username string
err := handle.QueryRow("SELECT username FROM accounts WHERE discordId = ?", discordId).Scan(&username) err := handle.QueryRow("SELECT username FROM accounts WHERE discordId = ?", discordId).Scan(&username)

@ -116,5 +116,6 @@ func setupDb(tx *sql.Tx) error {
return fmt.Errorf("failed to execute query: %w, query: %s", err, q) return fmt.Errorf("failed to execute query: %w, query: %s", err, q)
} }
} }
return nil return nil
} }

@ -243,7 +243,10 @@ func RetrieveSystemSaveFromS3(uuid []byte) error {
} }
var session defs.SystemSaveData var session defs.SystemSaveData
json.NewDecoder(resp.Body).Decode(&session) err = json.NewDecoder(resp.Body).Decode(&session)
if err != nil {
return err
}
err = StoreSystemSaveData(uuid, session) err = StoreSystemSaveData(uuid, session)
if err != nil { if err != nil {
@ -262,41 +265,46 @@ func RetrieveSystemSaveFromS3(uuid []byte) error {
Bucket: aws.String("pokerogue-system"), Bucket: aws.String("pokerogue-system"),
Key: aws.String(username), Key: aws.String(username),
}) })
if err != nil { if err != nil {
fmt.Printf("Failed to delete object %s from s3: %s\n", username, err) fmt.Printf("Failed to delete object %s from s3: %s\n", username, err)
} }
return nil return nil
} }
func RetrieveOldAccounts() [][]byte { func RetrieveOldAccounts() ([][]byte, error) {
var users [][]byte var users [][]byte
rows, err := handle.Query("SELECT uuid FROM accounts WHERE isInLocalDb = 1 && lastActivity < DATE_SUB(NOW(), INTERVAL 3 MONTH) LIMIT 3000") rows, err := handle.Query("SELECT uuid FROM accounts WHERE isInLocalDb = 1 && lastActivity < DATE_SUB(NOW(), INTERVAL 3 MONTH) LIMIT 3000")
if err != nil { if err != nil {
return nil return nil, err
} }
defer rows.Close() defer rows.Close()
for rows.Next() { for rows.Next() {
var uuid []byte var uuid []byte
if err := rows.Scan(&uuid); err != nil { err := rows.Scan(&uuid)
return nil if err != nil {
return nil, err
} }
users = append(users, uuid) users = append(users, uuid)
} }
if err := rows.Err(); err != nil {
return nil
}
return users return users, nil
} }
func UpdateLocation(uuid []byte, username string) { func UpdateLocation(uuid []byte, username string) error {
_, err := handle.Exec("UPDATE accounts SET isInLocalDb = 0 WHERE uuid = ?", uuid) _, err := handle.Exec("UPDATE accounts SET isInLocalDb = 0 WHERE uuid = ?", uuid)
if err != nil { if err != nil {
fmt.Printf("Failed to update location for user %s\n", username) fmt.Printf("Failed to update location for user %s\n", username)
return return err
} }
DeleteSystemSaveData(uuid) err = DeleteSystemSaveData(uuid)
if err != nil {
return err
}
return nil
} }

Loading…
Cancel
Save