diff --git a/database/mario_kart_wii.go b/database/mario_kart_wii.go index 9db7667..32673fd 100644 --- a/database/mario_kart_wii.go +++ b/database/mario_kart_wii.go @@ -4,6 +4,7 @@ import ( "context" "wwfc/common" + "github.com/jackc/pgx/v4" "github.com/jackc/pgx/v4/pgxpool" ) @@ -133,3 +134,50 @@ func InsertMarioKartWiiGhostFile(pool *pgxpool.Pool, ctx context.Context, region return err } + +// Mario Kart Wii friend info functions for API compatibility + +func GetMKWFriendInfo(pool *pgxpool.Pool, ctx context.Context, profileId uint32) string { + records, err := GetSakeRecords(pool, ctx, 1687, []int32{int32(profileId)}, "FriendInfo", nil, []string{"info"}, "") + if err != nil || len(records) == 0 { + return "" + } + + infoField, ok := records[0].Fields["info"] + if !ok { + return "" + } + + return infoField.Value +} + +func UpdateMKWFriendInfo(pool *pgxpool.Pool, ctx context.Context, profileId uint32, info string) { + records, err := GetSakeRecords(pool, ctx, 1687, []int32{int32(profileId)}, "FriendInfo", nil, []string{"info"}, "") + if err == pgx.ErrNoRows || (err == nil && len(records) == 0) { + // No existing record, insert new one + record := SakeRecord{ + GameId: 1687, + TableId: "FriendInfo", + OwnerId: int32(profileId), + Fields: map[string]SakeField{ + "info": { + Type: SakeFieldTypeBinaryData, + Value: info, + }, + }, + } + _, err = InsertSakeRecord(pool, ctx, record) + } else if err == nil { + // Update existing record + records[0].Fields["info"] = SakeField{ + Type: SakeFieldTypeBinaryData, + Value: info, + } + err = UpdateSakeRecord(pool, ctx, records[0], int32(profileId)) + + } + + if err != nil { + panic(err) + } +} diff --git a/database/user.go b/database/user.go index e97956c..8de25b6 100644 --- a/database/user.go +++ b/database/user.go @@ -24,9 +24,6 @@ const ( UpdateUserLastIPAddress = `UPDATE users SET last_ip_address = $2, last_ingamesn = $3 WHERE profile_id = $1` UpdateUserBan = `UPDATE users SET has_ban = true, ban_issued = $2, ban_expires = $3, ban_reason = $4, ban_reason_hidden = $5, ban_moderator = $6, ban_tos = $7 WHERE profile_id = $1` DisableUserBan = `UPDATE users SET has_ban = false WHERE profile_id = $1` - - GetMKWFriendInfoQuery = `SELECT mariokartwii_friend_info FROM users WHERE profile_id = $1` - UpdateMKWFriendInfoQuery = `UPDATE users SET mariokartwii_friend_info = $2 WHERE profile_id = $1` ) type User struct { @@ -163,20 +160,3 @@ func UnbanUser(pool *pgxpool.Pool, ctx context.Context, profileId uint32) bool { _, err := pool.Exec(ctx, DisableUserBan, profileId) return err == nil } - -func GetMKWFriendInfo(pool *pgxpool.Pool, ctx context.Context, profileId uint32) string { - var info string - err := pool.QueryRow(ctx, GetMKWFriendInfoQuery, profileId).Scan(&info) - if err != nil { - return "" - } - - return info -} - -func UpdateMKWFriendInfo(pool *pgxpool.Pool, ctx context.Context, profileId uint32, info string) { - _, err := pool.Exec(ctx, UpdateMKWFriendInfoQuery, profileId, info) - if err != nil { - panic(err) - } -} diff --git a/schema.sql b/schema.sql index b644edf..626d7b7 100644 --- a/schema.sql +++ b/schema.sql @@ -33,8 +33,7 @@ CREATE TABLE IF NOT EXISTS public.users ( email character varying NOT NULL, unique_nick character varying NOT NULL, firstname character varying, - lastname character varying DEFAULT ''::character varying, - mariokartwii_friend_info character varying + lastname character varying DEFAULT ''::character varying );