fix: change HasWon to TeamScore, limit request entries to 1 and merge sql queries

This commit is contained in:
Maple Nebel 2026-07-31 14:43:28 +02:00
parent 3ccda9ed26
commit 81dfc8b113
7 changed files with 72 additions and 95 deletions

View File

@ -0,0 +1,46 @@
package database
import (
"github.com/PretendoNetwork/nex-go/v2/types"
"github.com/PretendoNetwork/splatoon/globals"
)
// might be better to just return a struct here cause its not immediately obbvious from the signature which is which
func GetVoteAndWinCount(splatfestID uint32) (types.List[types.UInt32], types.List[types.UInt32], error) {
row := globals.Postgres.QueryRow(`SELECT
(SELECT
COUNT(user_id)
FROM ranking_splatoon.user_scores WHERE splatfest_id=$1 AND team_id=0) as alpha_team_votes,
(SELECT
COUNT(user_id)
FROM ranking_splatoon.user_scores WHERE splatfest_id=$1 AND team_id=1) as bravo_team_votes,
(SELECT
SUM(team_score)
FROM ranking_splatoon.results
WHERE splatfest_id=$1
AND team_id=1
) as alpha_team_wins,
(SELECT
COUNT(team_score)
FROM ranking_splatoon.results
WHERE splatfest_id=$1
AND team_id=1
) as bravo_team_wins
`, uint64(splatfestID))
if row.Err() != nil {
return types.NewList[types.UInt32](), types.NewList[types.UInt32](), row.Err()
}
var alpha_team_votes uint32
var bravo_team_votes uint32
var alpha_team_wins uint32
var bravo_team_wins uint32
err := row.Scan(&alpha_team_votes, &bravo_team_votes, &alpha_team_wins, &bravo_team_wins)
if err != nil {
return types.NewList[types.UInt32](), types.NewList[types.UInt32](), err
}
return types.List[types.UInt32]{types.NewUInt32(alpha_team_votes), types.NewUInt32(bravo_team_votes)},
types.List[types.UInt32]{types.NewUInt32(alpha_team_wins), types.NewUInt32(bravo_team_wins)}, nil
}

View File

@ -1,23 +0,0 @@
package database
import (
"github.com/PretendoNetwork/splatoon/globals"
)
func GetVoteCount(splatfestID uint32, team_id uint8) (uint32, error) {
row := globals.Postgres.QueryRow(`SELECT
COUNT(user_id)
FROM ranking_splatoon.user_scores WHERE splatfest_id=$1 AND team_id=$2`, uint64(splatfestID), int16(team_id))
if row.Err() != nil {
return 0, row.Err()
}
var count uint32
err := row.Scan(&count)
if err != nil {
return 0, err
}
return count, nil
}

View File

@ -1,28 +0,0 @@
package database
import (
"github.com/PretendoNetwork/splatoon/globals"
)
func GetWinCount(splatfestID uint32, team_id uint8) (uint32, error) {
row := globals.Postgres.QueryRow(`SELECT
COUNT(upload_id)
FROM ranking_splatoon.results
WHERE splatfest_id=$1
AND (
(has_won = false AND team_id NOT EQUAL $2) OR
(has_won = true AND team_id=$2)
)`, uint64(splatfestID), int16(team_id))
if row.Err() != nil {
return 0, row.Err()
}
var count uint32
err := row.Scan(&count)
if err != nil {
return 0, err
}
return count, nil
}

View File

@ -18,8 +18,9 @@ func Setup() {
uploader_pid int8 not null,
splatfest_id int8 not null,
team_id int2 not null,
has_won int2 not null,
team_score int8 not null,
app_data bytea not null,
created_at timestamp not null default now()
)`)
_, err = globals.Postgres.Exec(`CREATE TABLE IF NOT EXISTS ranking_splatoon.user_scores (
uploader_pid int8 not null,
@ -27,7 +28,8 @@ func Setup() {
score int8 not null,
team_id boolean not null,
user_data bytea not null,
last_updated int8 not null
last_updated int8 not null,
created_at timestamp not null default now(),
PRIMARY KEY(uploader_pid, splatfest_id)
)`)
if err != nil {

View File

@ -14,9 +14,9 @@ func StoreUserMatchResult(userPID types.PID, data ranking_splatoon_types.Competi
splatfest_id,
score,
team_id,
has_won
team_score
) VALUES ($1, $2, $3, $4, $5)
`, int64(userPID), int64(data.SplatfestId), int64(data.Score), int16(data.TeamId), bool(data.HasWon))
`, int64(userPID), int64(data.SplatfestId), int64(data.Score), int16(data.TeamId), int64(data.TeamScore))
if err != nil {
return err

View File

@ -16,33 +16,14 @@ func GetSingleCompetitionRankingScore(splatfestID uint32) (ranking_splatoon_type
info.FestId = types.UInt32(splatfestID)
var team_alpha_count uint32
team_alpha_count, err = database.GetVoteCount(splatfestID, 0)
if err != nil {
return info, err
}
info.TeamVotes = append(info.TeamVotes, types.UInt32(team_alpha_count))
team_wins, team_votes, err := database.GetVoteAndWinCount(splatfestID)
var team_bravo_count uint32
team_bravo_count, err = database.GetVoteCount(splatfestID, 1)
if err != nil {
return info, err
}
info.TeamVotes = append(info.TeamVotes, types.UInt32(team_bravo_count))
var team_alpha_wins uint32
team_alpha_wins, err = database.GetWinCount(splatfestID, 0)
if err != nil {
return info, err
}
info.TeamWins = append(info.TeamWins, types.UInt32(team_alpha_wins))
var team_bravo_wins uint32
team_bravo_wins, err = database.GetVoteCount(splatfestID, 1)
if err != nil {
return info, err
}
info.TeamWins = append(info.TeamWins, types.UInt32(team_bravo_wins))
info.TeamWins = types.List[types.UInt32](team_wins)
info.TeamVotes = types.List[types.UInt32](team_votes)
var scoreDataEntries []ranking_splatoon_types.CompetitionRankingScoreData
scoreDataEntries, err = database.GetScoreDataEntries(splatfestID)
@ -70,19 +51,18 @@ func GetCompetitionRankingScore(err error, packet nex.PacketInterface, callID ui
}
retVal := types.NewList[ranking_splatoon_types.CompetitionRankingScoreInfo]()
var festListIndex uint32
for festListIndex = 0; festListIndex < uint32(params.ResultRange.Length); festListIndex++ {
if festListIndex >= uint32(len(params.FestivalIds)) {
return nil, nex.NewError(nex.ResultCodes.Core.InvalidArgument, "out of resultrange is out of bounds")
}
festId := params.FestivalIds[festListIndex+uint32(params.ResultRange.Offset)]
info, err := GetSingleCompetitionRankingScore(uint32(festId))
if err != nil {
return nil, nex.NewError(nex.ResultCodes.Core.Exception, "error retrieving ranking scores")
}
retVal = append(retVal, info)
if uint32(params.ResultRange.Offset) >= uint32(len(params.FestivalIds)) {
return nil, nex.NewError(nex.ResultCodes.Core.InvalidArgument, "out of resultrange is out of bounds")
}
festId := params.FestivalIds[uint32(params.ResultRange.Offset)]
info, err := GetSingleCompetitionRankingScore(uint32(festId))
if err != nil {
return nil, nex.NewError(nex.ResultCodes.Core.Exception, "error retrieving ranking scores")
}
retVal = append(retVal, info)
retVal.WriteTo(rmcResponseStream)
rmcResponse := nex.NewRMCSuccess(globals.SecureEndpoint, rmcResponseStream.Bytes())

View File

@ -16,7 +16,7 @@ type CompetitionRankingUploadScoreParam struct {
Unknown2 types.UInt32
Score types.UInt32
TeamId types.UInt8
HasWon types.Bool
TeamScore types.UInt32
IsFirstUpload types.Bool
AppData types.QBuffer
}
@ -30,7 +30,7 @@ func (crusp CompetitionRankingUploadScoreParam) WriteTo(writable types.Writable)
crusp.Unknown2.WriteTo(contentWritable)
crusp.Score.WriteTo(contentWritable)
crusp.TeamId.WriteTo(contentWritable)
crusp.HasWon.WriteTo(contentWritable)
crusp.TeamScore.WriteTo(contentWritable)
crusp.IsFirstUpload.WriteTo(contentWritable)
crusp.AppData.WriteTo(contentWritable)
@ -62,7 +62,7 @@ func (crusp CompetitionRankingUploadScoreParam) ExtractFrom(readable types.Reada
if err := crusp.TeamId.ExtractFrom(readable); err != nil {
return fmt.Errorf("failed to extract CompetitionRankingUploadScoreParam.TeamId. %s", err.Error())
}
if err := crusp.HasWon.ExtractFrom(readable); err != nil {
if err := crusp.TeamScore.ExtractFrom(readable); err != nil {
return fmt.Errorf("failed to extract CompetitionRankingUploadScoreParam.TeamWin. %s", err.Error())
}
if err := crusp.IsFirstUpload.ExtractFrom(readable); err != nil {
@ -85,7 +85,7 @@ func (crusp CompetitionRankingUploadScoreParam) Copy() types.RVType {
copied.Unknown2 = crusp.Unknown2.Copy().(types.UInt32)
copied.Score = crusp.Score.Copy().(types.UInt32)
copied.TeamId = crusp.TeamId.Copy().(types.UInt8)
copied.HasWon = crusp.HasWon.Copy().(types.Bool)
copied.TeamScore = crusp.TeamScore.Copy().(types.UInt32)
copied.IsFirstUpload = crusp.IsFirstUpload.Copy().(types.Bool)
copied.AppData = crusp.AppData.Copy().(types.QBuffer)
@ -119,7 +119,7 @@ func (crusp CompetitionRankingUploadScoreParam) Equals(o types.RVType) bool {
if !crusp.TeamId.Equals(other.TeamId) {
return false
}
if !crusp.HasWon.Equals(other.HasWon) {
if !crusp.TeamScore.Equals(other.TeamScore) {
return false
}
if !crusp.IsFirstUpload.Equals(other.IsFirstUpload) {
@ -161,7 +161,7 @@ func (crusp CompetitionRankingUploadScoreParam) FormatToString(indentationLevel
fmt.Fprintf(&b, "%sUnknown2: %s,\n", indentationValues, crusp.Unknown2)
fmt.Fprintf(&b, "%sScore: %s,\n", indentationValues, crusp.Score)
fmt.Fprintf(&b, "%sTeamId: %s,\n", indentationValues, crusp.TeamId)
fmt.Fprintf(&b, "%sHasWon: %s,\n", indentationValues, crusp.HasWon)
fmt.Fprintf(&b, "%TeamScore: %s,\n", indentationValues, crusp.TeamScore)
fmt.Fprintf(&b, "%sIsFirstUpload: %s,\n", indentationValues, crusp.IsFirstUpload)
fmt.Fprintf(&b, "%sAppData: %s,\n", indentationValues, crusp.AppData)
fmt.Fprintf(&b, "%s}", indentationEnd)
@ -177,7 +177,7 @@ func NewCompetitionRankingUploadScoreParam() CompetitionRankingUploadScoreParam
Unknown2: types.NewUInt32(0),
Score: types.NewUInt32(0),
TeamId: types.NewUInt8(0),
HasWon: types.NewBool(false),
TeamScore: types.NewUInt32(0),
IsFirstUpload: types.NewBool(false),
AppData: types.NewQBuffer([]byte{}),
}