diff --git a/competition_ranking_scores.sql b/competition_ranking_scores.sql new file mode 100644 index 0000000..9397774 --- /dev/null +++ b/competition_ranking_scores.sql @@ -0,0 +1,9 @@ +CREATE TABLE IF NOT EXISTS competition_ranking_scores ( + id bigserial PRIMARY KEY, + pid numeric(10) NOT NULL, + festival_id bigint NOT NULL, + score bigint NOT NULL, + team_id smallint NOT NULL, + team_score bigint NOT NULL, + is_first_upload boolean NOT NULL +); diff --git a/globals/upload_competition_ranking_score.go b/globals/upload_competition_ranking_score.go new file mode 100644 index 0000000..2789762 --- /dev/null +++ b/globals/upload_competition_ranking_score.go @@ -0,0 +1,62 @@ +package globals + +import ( + "fmt" + nex "github.com/PretendoNetwork/nex-go/v2" + ranking "github.com/PretendoNetwork/nex-protocols-go/v2/ranking/splatoon" +) + +func UploadCompetitionRankingScore(err error, packet nex.PacketInterface, callID uint32, packetPayload []byte) (*nex.RMCMessage, *nex.Error) { + if err != nil { + return nil, nex.NewError(nex.ResultCodes.Core.Unknown, err.Error()) + } + + request := packet.RMCMessage() + parameters := request.Parameters + endpoint := packet.Sender().Endpoint() + parametersStream := nex.NewByteStreamIn(parameters, endpoint.LibraryVersions(), endpoint.ByteStreamSettings()) + + // Read CompetitionRankingUploadScoreParam data + festivalID, streamErr := parametersStream.ReadUInt32LE() + if streamErr != nil { + return nil, nex.NewError(nex.ResultCodes.Core.Unknown, fmt.Sprintf("Failed to read festival_id: %v", streamErr)) + } + + score, streamErr := parametersStream.ReadUInt32LE() + if streamErr != nil { + return nil, nex.NewError(nex.ResultCodes.Core.Unknown, fmt.Sprintf("Failed to read score: %v", streamErr)) + } + + teamID, streamErr := parametersStream.ReadUInt8() + if streamErr != nil { + return nil, nex.NewError(nex.ResultCodes.Core.Unknown, fmt.Sprintf("Failed to read team_id: %v", streamErr)) + } + + teamScore, streamErr := parametersStream.ReadUInt32LE() + if streamErr != nil { + return nil, nex.NewError(nex.ResultCodes.Core.Unknown, fmt.Sprintf("Failed to read team_score: %v", streamErr)) + } + + isFirstUpload, streamErr := parametersStream.ReadBool() + if streamErr != nil { + return nil, nex.NewError(nex.ResultCodes.Core.Unknown, fmt.Sprintf("Failed to read is_first_upload: %v", streamErr)) + } + + pid := packet.Sender().PID() + + _, dbErr := Postgres.Exec( + "INSERT INTO competition_ranking_scores (pid, festival_id, score, team_id, team_score, is_first_upload) VALUES ($1, $2, $3, $4, $5, $6)", + pid, festivalID, score, teamID, teamScore, isFirstUpload, + ) + if dbErr != nil { + Logger.Error(dbErr.Error()) + return nil, nex.NewError(nex.ResultCodes.Core.Unknown, "Database error") + } + + rmcResponse := nex.NewRMCSuccess(endpoint, nil) + rmcResponse.ProtocolID = ranking.ProtocolID + rmcResponse.MethodID = ranking.MethodUploadCompetitionRankingScore + rmcResponse.CallID = callID + + return rmcResponse, nil +} diff --git a/nex/register_common_secure_server_protocols.go b/nex/register_common_secure_server_protocols.go index 7e6cb85..1e5c1ba 100644 --- a/nex/register_common_secure_server_protocols.go +++ b/nex/register_common_secure_server_protocols.go @@ -56,6 +56,7 @@ func registerCommonSecureServerProtocols() { commonMatchmakeExtensionProtocol.SetManager(globals.MatchmakingManager) rankingProtocol := ranking.NewProtocol(globals.SecureEndpoint) + rankingProtocol.UploadCompetitionRankingScore = globals.UploadCompetitionRankingScore globals.SecureEndpoint.RegisterServiceProtocol(rankingProtocol) commonranking.NewCommonProtocol(rankingProtocol) }